+ * When an application is hosting preferences which are served by a different process, + * the former needs to stay updated with changes in order to display the correct + * summary when the user returns to the latter. + *
+ * This class implements a simple ordered broadcast mechanism where the application + * running the RemotePreference sends an explicit broadcast to the host, who + * fills out the extras in the result bundle and returns it to the caller. + *
+ * A minimal implementation will override getSummary and return a summary
+ * for the given key. Alternatively, fillResultExtras can be overridden
+ * if additional data should be added to the result.
+ */
+public class RemotePreferenceUpdater extends BroadcastReceiver {
+
+ private static final String TAG = RemotePreferenceUpdater.class.getSimpleName();
+
+ private static final boolean DEBUG = Log.isLoggable(
+ RemotePreference.class.getSimpleName(), Log.VERBOSE);
+
+ private static Intent getTargetIntent(Context context, String key) {
+ final Intent i = new Intent(RemotePreference.ACTION_REFRESH_PREFERENCE);
+ i.putExtra(RemotePreference.EXTRA_KEY, key);
+ return i;
+ }
+
+ /**
+ * Fetch the updated summary for the given key
+ *
+ * @param key
+ * @return the summary for the given key
+ */
+ protected String getSummary(Context context, String key) {
+ return null;
+ }
+
+
+ /**
+ * Fill the bundle with the summary and any other data needed to update
+ * the client.
+ *
+ * @param context
+ * @param key
+ * @param extras
+ * @return true if successful
+ */
+ protected boolean fillResultExtras(Context context, String key, Bundle extras) {
+ extras.putString(RemotePreference.EXTRA_KEY, key);
+
+ final String summary = getSummary(context, key);
+ if (summary == null) {
+ return false;
+ }
+
+ extras.putString(RemotePreference.EXTRA_SUMMARY, summary);
+ return true;
+ }
+
+
+ /**
+ * @hide
+ */
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (isOrderedBroadcast() &&
+ RemotePreference.ACTION_UPDATE_PREFERENCE.equals(intent.getAction())) {
+ final String key = intent.getStringExtra(RemotePreference.EXTRA_KEY);
+ if (DEBUG) Log.d(TAG, "onReceive key=" +key +
+ " intent=" + Objects.toString(intent) +
+ " extras=" + Objects.toString(intent.getExtras()));
+
+ if (key != null) {
+ if (fillResultExtras(context, key, getResultExtras(true))) {
+ setResultCode(Activity.RESULT_OK);
+
+ if (DEBUG) Log.d(TAG, "onReceive result=" +
+ Objects.toString(getResultExtras(true)));
+ return;
+ }
+ }
+ abortBroadcast();
+ }
+ }
+
+ /**
+ * Tell the RemotePreference that updated state is available. Call from
+ * the fragment when necessary.
+ *
+ * @param context
+ * @param key
+ */
+ public static void notifyChanged(Context context, String key) {
+ if (DEBUG) Log.d(TAG, "notifyChanged: key=" + key +
+ " target=" + Objects.toString(getTargetIntent(context, key)));
+ context.sendBroadcastAsUser(getTargetIntent(context, key),
+ UserHandle.CURRENT, Manifest.permission.MANAGE_REMOTE_PREFERENCES);
+ }
+}
diff --git a/sdk/src/java/org/cyanogenmod/internal/cmparts/PartsList.java b/sdk/src/java/org/cyanogenmod/internal/cmparts/PartsList.java
index 0097efc3..7334dca6 100644
--- a/sdk/src/java/org/cyanogenmod/internal/cmparts/PartsList.java
+++ b/sdk/src/java/org/cyanogenmod/internal/cmparts/PartsList.java
@@ -237,7 +237,7 @@ public class PartsList {
if (mRemotes.size() == 0) {
final IntentFilter filter = new IntentFilter(ACTION_PART_CHANGED);
mContext.registerReceiver(mPartChangedReceiver, filter,
- Manifest.permission.MANAGE_PARTS, null);
+ Manifest.permission.MANAGE_REMOTE_PREFERENCES, null);
}
Set