-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathIntentBridgedPreferenceSender.java
More file actions
53 lines (46 loc) · 1.81 KB
/
IntentBridgedPreferenceSender.java
File metadata and controls
53 lines (46 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.crossbowffs.remotepreferences;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
/**
* <p>
* Propagates changes in the given {@link SharedPreferences} as intents
* to any other app on the device.
* </p>
*
* <p>
* You must extend this class and declare a 0-argument constructor which
* calls the super constructor with the appropriate action and preferences
* parameters.
* </p>
*
* <p>
* Remember to hold onto the created instance explicitely, e.g. through
* a member of an activity. This registers a listener on the given
* {@link SharedPreferences} and these are only weakly referenced.
* </p>
*/
public class IntentBridgedPreferenceSender {
private final String mActionName;
private final Context mContext;
private final SharedPreferences.OnSharedPreferenceChangeListener mListener;
/**
* Initializes this sender with the specified action and the given
* {@link SharedPreferences} as sources for changes and preferences.
*
* @param context The {@link Context} of this app.
* @param actionName The actionName of the action.
* @param sourcePreferences The {@link SharedPreferences} used as source.
*/
public IntentBridgedPreferenceSender(Context context, String actionName, SharedPreferences sourcePreferences) {
mContext = context;
mActionName = actionName;
mListener = this::onPreferenceChanged;
sourcePreferences.registerOnSharedPreferenceChangeListener(mListener);
}
private void onPreferenceChanged(SharedPreferences sharedPreferences, String key) {
Intent intent = new Intent(mActionName + ".PREFERENCES");
IntentBridgedUtils.setAsIntentExtra(intent, key, sharedPreferences.getAll().get(key));
mContext.sendBroadcast(intent);
}
}