Added single purpose app integration

Antennapod will now import subscriptions from AntennaPodSP (https://github.com/danieloeh/AntennaPodSP) forks when launched
This commit is contained in:
daniel oeh 2014-03-12 17:11:46 +01:00
parent 7335a07956
commit bed57245b3
5 changed files with 133 additions and 0 deletions

View File

@ -465,6 +465,11 @@
android:scheme="package"/>
</intent-filter>
</receiver>
<receiver android:name=".receiver.SPAReceiver">
<intent-filter>
<action android:name="de.danoeh.antennapdsp.intent.SP_APPS_QUERY_FEEDS_RESPONSE"/>
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -350,4 +350,8 @@
<string name="in_queue_label">Episode is in the queue</string>
<string name="new_episodes_count_label">Number of new episodes</string>
<string name="in_progress_episodes_count_label">Number of episodes you have started listening to</string>
<!-- AntennaPodSP -->
<string name="sp_apps_importing_feeds_msg">Importing subscriptions from single-purpose apps&#8230;</string>
</resources>

View File

@ -7,6 +7,7 @@ import de.danoeh.antennapod.asynctask.ImageLoader;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.preferences.PlaybackPreferences;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.spa.SPAUtil;
/** Main application class. */
public class PodcastApp extends Application {
@ -31,6 +32,8 @@ public class PodcastApp extends Application {
UserPreferences.createInstance(this);
PlaybackPreferences.createInstance(this);
EventDistributor.getInstance();
SPAUtil.sendSPAppsQueryFeedsIntent(this);
}
@Override

View File

@ -0,0 +1,55 @@
package de.danoeh.antennapod.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.storage.DownloadRequester;
import org.apache.commons.lang3.StringUtils;
import java.util.Arrays;
import java.util.Date;
/**
* Receives intents from AntennaPod Single Purpose apps
*/
public class SPAReceiver extends BroadcastReceiver{
private static final String TAG = "SPAReceiver";
public static final String ACTION_SP_APPS_QUERY_FEEDS = "de.danoeh.antennapdsp.intent.SP_APPS_QUERY_FEEDS";
public static final String ACTION_SP_APPS_QUERY_FEEDS_REPSONSE = "de.danoeh.antennapdsp.intent.SP_APPS_QUERY_FEEDS_RESPONSE";
public static final String ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA = "feeds";
@Override
public void onReceive(Context context, Intent intent) {
if (StringUtils.equals(intent.getAction(), ACTION_SP_APPS_QUERY_FEEDS_REPSONSE)) {
if (AppConfig.DEBUG) Log.d(TAG, "Received SP_APPS_QUERY_RESPONSE");
if (intent.hasExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA)) {
String[] feedUrls = intent.getStringArrayExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA);
if (feedUrls != null) {
if (AppConfig.DEBUG) Log.d(TAG, "Received feeds list: " + Arrays.toString(feedUrls));
for (String url : feedUrls) {
Feed f = new Feed(url, new Date());
try {
DownloadRequester.getInstance().downloadFeed(context, f);
} catch (DownloadRequestException e) {
Log.e(TAG, "Error while trying to add feed " + url);
e.printStackTrace();
}
}
Toast.makeText(context, R.string.sp_apps_importing_feeds_msg, Toast.LENGTH_LONG).show();
} else {
Log.e(TAG, "Received invalid SP_APPS_QUERY_REPSONSE: extra was null");
}
} else {
Log.e(TAG, "Received invalid SP_APPS_QUERY_RESPONSE: Contains no extra");
}
}
}
}

View File

@ -0,0 +1,66 @@
package de.danoeh.antennapod.spa;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.receiver.SPAReceiver;
/**
* Provides methods related to AntennaPodSP (https://github.com/danieloeh/AntennaPodSP)
*/
public class SPAUtil {
private static final String TAG = "SPAUtil";
private static final String PREF_HAS_QUERIED_SP_APPS = "prefSPAUtil.hasQueriedSPApps";
private SPAUtil() {
}
/**
* Sends an ACTION_SP_APPS_QUERY_FEEDS intent to all AntennaPod Single Purpose apps.
* The receiving single purpose apps will then send their feeds back to AntennaPod via an
* ACTION_SP_APPS_QUERY_FEEDS_RESPONSE intent.
* This intent will only be sent once.
*
* @return True if an intent was sent, false otherwise (for example if the intent has already been
* sent before.
*/
public static synchronized boolean sendSPAppsQueryFeedsIntent(Context context) {
if (context == null) throw new IllegalArgumentException("context = null");
final Context appContext = context.getApplicationContext();
if (appContext == null) {
Log.wtf(TAG, "Unable to get application context");
return false;
}
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext);
if (!prefs.getBoolean(PREF_HAS_QUERIED_SP_APPS, false)) {
appContext.sendBroadcast(new Intent(SPAReceiver.ACTION_SP_APPS_QUERY_FEEDS));
if (AppConfig.DEBUG) Log.d(TAG, "Sending SP_APPS_QUERY_FEEDS intent");
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(PREF_HAS_QUERIED_SP_APPS, true);
editor.commit();
return true;
} else {
return false;
}
}
/**
* Resets all preferences created by this class. Should only be used for debug purposes.
*/
public static void resetSPAPreferences(Context c) {
if (AppConfig.DEBUG) {
if (c == null) throw new IllegalArgumentException("c = null");
SharedPreferences.Editor editor = PreferenceManager
.getDefaultSharedPreferences(c.getApplicationContext()).edit();
editor.putBoolean(PREF_HAS_QUERIED_SP_APPS, false);
editor.commit();
}
}
}