/** * 检查当token信息与配置文件是否保持一致,若不一致则对当前的token信息进行初始化 */ private void compareWithConfig() { if (this.context == null) { return; } /** * 对配置的权限信息进行监控,保持多个AccessTokenManager对象之间的,权限信息一致。 */ final SharedPreferences sp = this.context.getSharedPreferences(BAIDU_OAUTH_CONFIG, Context.MODE_PRIVATE); sp.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { String acToken = sp.getString(OauthConfig.PrefenenceKey.SP_ACCESS_TOKEN, null); if (accessToken != null && !accessToken.equals(acToken)) { initToken(); } } }); }
@Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(final SharedPreferences prefs, final String key) { final SubScreenFragment fragment = SubScreenFragment.this; final Context context = fragment.getActivity(); if (context == null || fragment.getPreferenceScreen() == null) { final String tag = fragment.getClass().getSimpleName(); // TODO: Introduce a static function to register this class and ensure that // onCreate must be called before "onSharedPreferenceChanged" is called. Log.w(tag, "onSharedPreferenceChanged called before activity starts."); return; } new BackupManager(context).dataChanged(); fragment.onSharedPreferenceChanged(prefs, key); } }; getSharedPreferences().registerOnSharedPreferenceChangeListener( mSharedPreferenceChangeListener); }
@Inject public PreferencesService(@ApplicationContext Context context) { mPrefs = context.getSharedPreferences(PREFS_FILE_NAME, Context.MODE_PRIVATE); mChangedKeys = Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(final Subscriber<? super String> subscriber) { final OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { subscriber.onNext(key); } }; Subscription subscription = Subscriptions.create(new Action0() { @Override public void call() { mPrefs.unregisterOnSharedPreferenceChangeListener(listener); } }); subscriber.add(subscription); mPrefs.registerOnSharedPreferenceChangeListener(listener); } }).share(); }
public BroadcastEventBridge(Context context, String prefName, CacheCandidateProvider cacheCandidateProvider, CacheProvider cacheProvider, SerializerFactory serializerFactory, TaskExecutor taskExecutor, ValueEncryption valueEncryption, DirectoryProvider directoryProvider, Map<String, List<OnSharedPreferenceChangeListener>> allListeners) { this.context = context; this.prefName = prefName; this.cacheCandidateProvider = cacheCandidateProvider; this.cacheProvider = cacheProvider; this.serializerFactory = serializerFactory; this.taskExecutor = taskExecutor; this.valueEncryption = valueEncryption; this.updateActionName = createUpdateActionName(directoryProvider); this.removeActionName = createRemoveActionName(directoryProvider); this.currentListeners = putIfAbsentListeners(prefName, allListeners); this.updateReceiver = createUpdateReceiver(); this.removeReceiver = createRemoveReceiver(); this.processId = Process.myPid(); }
public boolean commit() { //Log.d(this.getClass().toString(), "commit() changes back to database"); SQLiteDatabase db = hostdb.getWritableDatabase(); db.update(table, update, "_id = ?", new String[] { String.valueOf(id) }); db.close(); // make sure we refresh the parent cached values cacheValues(); // and update any listeners for(OnSharedPreferenceChangeListener listener : listeners) { listener.onSharedPreferenceChanged(CursorPreferenceHack.this, null); } return true; }
public static SharedPreferences.OnSharedPreferenceChangeListener a(Context paramContext, Runnable paramRunnable) { hvu localhvu = (hvu)mbb.a(paramContext, hvu.class); if (hwa.d == localhvu.c()) { if (!efj.j()) { break label68; } paramRunnable.run(); } for (;;) { hvw localhvw = new hvw(paramContext, paramRunnable); a.add(localhvw); localhvu.b.registerOnSharedPreferenceChangeListener(localhvw); localhvu.b(); return localhvw; label68: efj.m().post(paramRunnable); } }
@Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { Set<OnSharedPreferenceChangeListener> listeners = new HashSet<OnSharedPreferenceChangeListener>(); synchronized (this) { if (prefs.contains(key)) { mMap.put(key, sharedPreferences.getAll().get(key)); } else { mMap.remove(key); } listeners.addAll(mListeners.keySet()); } for (OnSharedPreferenceChangeListener listener : listeners) { if (listener != null) { listener.onSharedPreferenceChanged(this, key); } } }
/** * Add an OnSettingChangedListener to the SettingsManager, which will * execute onSettingsChanged when any SharedPreference has been updated. */ public void addListener(final OnSettingChangedListener listener) { if (listener == null) { throw new IllegalArgumentException("OnSettingChangedListener cannot be null."); } if (mListeners.contains(listener)) { return; } mListeners.add(listener); OnSharedPreferenceChangeListener sharedPreferenceListener = getSharedPreferenceListener(listener); mSharedPreferenceListeners.add(sharedPreferenceListener); mDefaultPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceListener); if (mCustomPreferences != null) { mCustomPreferences.registerOnSharedPreferenceChangeListener( sharedPreferenceListener); } Log.v(TAG, "listeners: " + mListeners); }
/** * Remove a specific SettingsListener. This should be done in onPause if a * listener has been set. */ public void removeListener(OnSettingChangedListener listener) { if (listener == null) { throw new IllegalArgumentException(); } if (!mListeners.contains(listener)) { return; } int index = mListeners.indexOf(listener); mListeners.remove(listener); OnSharedPreferenceChangeListener sharedPreferenceListener = mSharedPreferenceListeners.get(index); mSharedPreferenceListeners.remove(index); mDefaultPreferences.unregisterOnSharedPreferenceChangeListener( sharedPreferenceListener); if (mCustomPreferences != null) { mCustomPreferences.unregisterOnSharedPreferenceChangeListener( sharedPreferenceListener); } }
public ComboPreferences(Context context) { mPackageName = context.getPackageName(); mPrefGlobal = context.getSharedPreferences( getGlobalSharedPreferencesName(context), Context.MODE_PRIVATE); mPrefGlobal.registerOnSharedPreferenceChangeListener(this); synchronized (sMap) { sMap.put(context, this); } mListeners = new CopyOnWriteArrayList<OnSharedPreferenceChangeListener>(); // The global preferences was previously stored in the default // shared preferences file. They should be stored in the camera-specific // shared preferences file so we can backup them solely. SharedPreferences oldprefs = PreferenceManager.getDefaultSharedPreferences(context); if (!mPrefGlobal.contains(CameraSettings.KEY_VERSION) && oldprefs.contains(CameraSettings.KEY_VERSION)) { moveGlobalPrefsFrom(oldprefs); } }
@Override public void onCreate() { super.onCreate(); mSharedPreferences = MultiprocessSharedPreferences.getSharedPreferences(this, SP_NAME, Context.MODE_PRIVATE); Log.d(TAG, "onCreate." + SP_KEY + " = " + mSharedPreferences.getString(SP_KEY, null)); mOnSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { String msg = "onSharedPreferenceChanged." + SP_KEY + " = " + sharedPreferences.getString(SP_KEY, null); Log.d(TAG, msg); Toast.makeText(CoreService.this, msg, Toast.LENGTH_SHORT).show(); } }; mSharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener); }
/** * {@inheritDoc} */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplication()); sharedPrefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { BackupManager.dataChanged(getPackageName()); } }); Utils.setStrictMode(true); setContentView(getLayoutResource()); initUI(); }
public PersistentProperties(Future<SharedPreferences> paramFuture1, Future<SharedPreferences> paramFuture2) { this.mLoadReferrerPreferences = paramFuture1; this.mLoadStoredPreferences = paramFuture2; this.mSuperPropertiesCache = null; this.mReferrerPropertiesCache = null; this.mIdentitiesLoaded = false; this.mReferrerChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences paramAnonymousSharedPreferences, String paramAnonymousString) { synchronized (PersistentProperties.sReferrerPrefsLock) { PersistentProperties.this.readReferrerProperties(); PersistentProperties.access$202(false); return; } } }; }
@Override public void onCreate() { super.onCreate(); CancelAllNotif(); Log.d(LOG_TAG, "Service started"); //handle preference UpdatePref(); prefs = PreferenceManager.getDefaultSharedPreferences(this); listener = new SharedPreferences.OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { Log.d(LOG_TAG, "Preference changed"); UpdatePref(); } }; prefs.registerOnSharedPreferenceChangeListener(listener); }
public void restorePreferences(final Parameters param) { // Do synchronization in "reloadPreferences()" OnSharedPreferenceChangeListener l = mSharedPreferenceChangeListener; // Unregister the listener since "upgrade preference" will // change bunch of preferences. We can handle them with one // onSharedPreferencesChanged(); mSharedPrefs.unregisterOnSharedPreferenceChangeListener(l); Context context = getGLRootView().getContext(); Editor editor = mSharedPrefs.edit(); editor.clear(); editor.apply(); CameraSettings.upgradeAllPreferences(mSharedPrefs); CameraSettings.initialCameraPictureSize(context, param); reloadPreferences(); if (mListener != null) { mListener.onSharedPreferencesChanged(); } mSharedPrefs.registerOnSharedPreferenceChangeListener(l); }
@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); setBackground(); setTabButtonListener(); setPlayerControlButtonListeners(); settingsChangeListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals(getString(R.string.KEY_USE_WALLPAPER_BACKGROUND)) || key.equals(getString(R.string.KEY_PICK_BACKGROUND_FROM_GALLERY)) || key.equals(getString(R.string.KEY_GALLERY_BACKGROUND_PATH))) { setBackground(); } } }; settings.addSettingsChangeListener(settingsChangeListener); }
public MyEngine(HexagonService s) { super(); // handle prefs, other initialization renderer = new HexagonRenderer(s); service = s; setRenderer(renderer); setRenderMode(RENDERMODE_CONTINUOUSLY); preferences = HexagonService.this.getSharedPreferences(SHARED_PREFS_NAME, 0); preferences.registerOnSharedPreferenceChangeListener((OnSharedPreferenceChangeListener) this); if (!preferences.contains("colors")) { Editor e = preferences.edit(); e.putString("colors","1"); e.putString("perspective","-1"); e.putString("zoom","-1"); e.putString("pulse","1"); e.putString("rotation","-1"); e.putString("walls","3"); e.putString("cursor","1"); e.commit(); } onSharedPreferenceChanged(preferences, null); }
public boolean commit() { //Log.d(this.getClass().toString(), "commit() changes back to database"); SQLiteDatabase db = m_streamdb.getWritableDatabase(); db.update(table, update, "_id = ?", new String[] { String.valueOf(id) }); db.close(); // make sure we refresh the parent cached values cacheValues(); // and update any listeners for(OnSharedPreferenceChangeListener listener : listeners) { listener.onSharedPreferenceChanged(CursorPreferenceHack.this, null); } return true; }
public BeaconActionsLogsStore(Context context) { preferences = context.getSharedPreferences(SHARED_PREFERENCES_KEY, Context.MODE_PRIVATE); onSharedPreferenceChangeListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { if (listener != null) { listener.onActionAdded(); } } }; preferences.registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener); }
public void registerSettingChangeListener(final SettingChangeListener listener) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); OnSharedPreferenceChangeListener spListener = new OnSharedPreferenceChangeListener() { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { listener.onSettingChanged(Settings.this, key); } }; prefs.registerOnSharedPreferenceChangeListener(spListener); this.listenerMap.put(listener, spListener); }
public void unregisterSettingChangeListener(SettingChangeListener listener) { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); OnSharedPreferenceChangeListener spListener = (OnSharedPreferenceChangeListener) this.listenerMap.get(listener); if (spListener != null) { prefs.unregisterOnSharedPreferenceChangeListener(spListener); } this.listenerMap.remove(listener); }
private List<OnSharedPreferenceChangeListener> putIfAbsentListeners(String prefName, Map<String, List<OnSharedPreferenceChangeListener>> allListeners) { if (allListeners.containsKey(prefName)) { return allListeners.get(prefName); } List<OnSharedPreferenceChangeListener> listeners = new ArrayList<>(); allListeners.put(prefName, listeners); return listeners; }
@Override public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { if (currentListeners.isEmpty()) { subscribeReceivers(); } currentListeners.add(listener); }
@Override public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { currentListeners.remove(listener); if (currentListeners.isEmpty()) { unSubscribeReceivers(); } }
private void notifyListeners(final String key) { handler.post(new Runnable() { @Override public void run() { List<OnSharedPreferenceChangeListener> temp = new ArrayList<>(currentListeners); for (OnSharedPreferenceChangeListener listener : temp) { listener.onSharedPreferenceChanged(null, key); } } }); }
private List<OnSharedPreferenceChangeListener> initListeners(String prefName) { if (allListeners.containsKey(prefName)) { return allListeners.get(prefName); } List<OnSharedPreferenceChangeListener> listeners = new ArrayList<>(); allListeners.put(prefName, listeners); return listeners; }
@Override protected void tearDown() throws Exception { AndroidMock.reset(dataSource); // Expect everything to be unregistered. dataSource.unregisterContentObserver(isA(ContentObserver.class)); AndroidMock.expectLastCall().times(3); dataSource.unregisterOnSharedPreferenceChangeListener( isA(OnSharedPreferenceChangeListener.class)); AndroidMock.replay(dataSource); trackDataHub.stop(); trackDataHub = null; super.tearDown(); }
public OAuthManagerStore(Context ctx, String name, int mode) { // setup credential store this.context = ctx; this.prefs = ctx.getSharedPreferences(name, Context.MODE_PRIVATE); editor = this.prefs.edit(); listener = new SharedPreferences.OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { Log.d(TAG, "Preferences changed: " + key); } }; prefs.registerOnSharedPreferenceChangeListener(listener); }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); options = PreferenceManager.getDefaultSharedPreferences(this); prefListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { //Log.v("TinyLaunch", "pref change detected"); if (key.equals(Options.PREF_DIRTY) && sharedPreferences.getBoolean(Options.PREF_DIRTY, false)) { if (scanner == null || scanner.getStatus() != AsyncTask.Status.RUNNING) { scanner = new GetApps(Apps.this); scanner.execute(false); } } } }; options.registerOnSharedPreferenceChangeListener(prefListener); if (options.getBoolean(Options.PREF_LIGHT, false)) setTheme(android.R.style.Theme_Light); setContentView(R.layout.apps); list = (ListView)findViewById(R.id.apps); res = getResources(); categories = null; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.user_prefs); // Get the username Preference mUserNamePreference = (EditTextPreference) getPreferenceManager() .findPreference(USERNAME); // Attach a listener to update summary when username changes mListener = new OnSharedPreferenceChangeListener() { @Override public void onSharedPreferenceChanged( SharedPreferences sharedPreferences, String key) { if (key.equals(USERNAME)) mUserNamePreference.setSummary(sharedPreferences.getString( USERNAME, "None Set")); } }; // Get SharedPreferences object managed by the PreferenceManager for // this Fragment SharedPreferences prefs = getPreferenceManager() .getSharedPreferences(); // Register a listener on the SharedPreferences object prefs.registerOnSharedPreferenceChangeListener(mListener); // Invoke callback manually to display the current username mListener.onSharedPreferenceChanged(prefs, USERNAME); }
private AsyncSharedPrefs(SharedPreferences prefs) { this.prefs = prefs; this.mMap = new HashMap<String, Object>(); this.mListeners = new WeakHashMap<OnSharedPreferenceChangeListener, Object>(); prefs.registerOnSharedPreferenceChangeListener(this); this.mMap.putAll(prefs.getAll()); }
@Override public void registerOnSharedPreferenceChangeListener( OnSharedPreferenceChangeListener listener) { synchronized (this) { mListeners.put(listener, mContent); } }
@Override public void unregisterOnSharedPreferenceChangeListener( OnSharedPreferenceChangeListener listener) { synchronized (this) { mListeners.remove(listener); } }
public static void registerOnPrefChangeListener(OnSharedPreferenceChangeListener listener) { try { PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()) .registerOnSharedPreferenceChangeListener(listener); } catch (final Exception ignored) { // Seems to be possible to have a // NPE here... Why?? } }
public static void unregisterOnPrefChangeListener(OnSharedPreferenceChangeListener listener) { try { PreferenceManager.getDefaultSharedPreferences(MainApplication.getContext()) .unregisterOnSharedPreferenceChangeListener(listener); } catch (final Exception ignored) { // Seems to be possible to have a // NPE here... Why?? } }
/** * Open a SharedPreferences file by custom scope. * Also registers any known SharedPreferenceListeners on this * SharedPreferences instance. */ protected SharedPreferences openPreferences(String scope) { SharedPreferences preferences = mContext.getSharedPreferences( mPackageName + scope, Context.MODE_PRIVATE); for (OnSharedPreferenceChangeListener listener : mSharedPreferenceListeners) { preferences.registerOnSharedPreferenceChangeListener(listener); } return preferences; }