diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-03-12 17:11:46 +0100 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-03-12 17:11:46 +0100 |
commit | bed57245b3ce454e714b5d5b22c8167ad7b986c8 (patch) | |
tree | 2328f5d3c9ce5f1cb7f08ebd4bd61211f7cf14bc | |
parent | 7335a079567dd4df884a52c2b1a8722ad1ffeca0 (diff) | |
download | AntennaPod-bed57245b3ce454e714b5d5b22c8167ad7b986c8.zip |
Added single purpose app integration
Antennapod will now import subscriptions from AntennaPodSP (https://github.com/danieloeh/AntennaPodSP) forks when launched
-rw-r--r-- | AndroidManifest.xml | 5 | ||||
-rw-r--r-- | res/values/strings.xml | 4 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/PodcastApp.java | 3 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/receiver/SPAReceiver.java | 55 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/spa/SPAUtil.java | 66 |
5 files changed, 133 insertions, 0 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index cfc8aeca3..772c168e1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -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> diff --git a/res/values/strings.xml b/res/values/strings.xml index 43bedadee..4ea68d980 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -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…</string> </resources> diff --git a/src/de/danoeh/antennapod/PodcastApp.java b/src/de/danoeh/antennapod/PodcastApp.java index 2141f71e8..4c4766327 100644 --- a/src/de/danoeh/antennapod/PodcastApp.java +++ b/src/de/danoeh/antennapod/PodcastApp.java @@ -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 diff --git a/src/de/danoeh/antennapod/receiver/SPAReceiver.java b/src/de/danoeh/antennapod/receiver/SPAReceiver.java new file mode 100644 index 000000000..c378f74e1 --- /dev/null +++ b/src/de/danoeh/antennapod/receiver/SPAReceiver.java @@ -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"); + } + } + } +} diff --git a/src/de/danoeh/antennapod/spa/SPAUtil.java b/src/de/danoeh/antennapod/spa/SPAUtil.java new file mode 100644 index 000000000..7720f7064 --- /dev/null +++ b/src/de/danoeh/antennapod/spa/SPAUtil.java @@ -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(); + } + } +} |