@Test public void testUnregisterOnSharedPreferenceChangeListener() throws Exception { final byte[] rawValue = bytesOf("Value"); when(mockValueCrypto.encrypt(rawValue)).thenReturn(CRYPTO.encrypt(rawValue)); final SharedPreferences.OnSharedPreferenceChangeListener firstMockListener = mock(SharedPreferences.OnSharedPreferenceChangeListener.class); final SharedPreferences.OnSharedPreferenceChangeListener secondMockListener = mock(SharedPreferences.OnSharedPreferenceChangeListener.class); cryptoPreferences.registerOnSharedPreferenceChangeListener(firstMockListener); cryptoPreferences.registerOnSharedPreferenceChangeListener(secondMockListener); cryptoPreferences.unregisterOnSharedPreferenceChangeListener(firstMockListener); cryptoPreferences.unregisterOnSharedPreferenceChangeListener(secondMockListener); assertThat(cryptoPreferences.edit().putString(PREF_KEY, "Value").commit(), is(true)); verify(mockKeyCrypto, times(0)).decrypt(bytesOf(PREF_KEY)); verifyZeroInteractions(firstMockListener, secondMockListener); }
@Override public void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener) { synchronized (changeListenersRegistry) { changeListenersRegistry.unregister(listener); if (changeListenersRegistry.isEmpty()) { delegate.unregisterOnSharedPreferenceChangeListener(changeListenersRegistry); changeListenersRegistered.set(false); } } }
CryptoSharedPreferences implements SharedPreferences { @Override public void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener) { synchronized (changeListenersRegistry) { changeListenersRegistry.unregister(listener); if (changeListenersRegistry.isEmpty()) { delegate.unregisterOnSharedPreferenceChangeListener(changeListenersRegistry); changeListenersRegistered.set(false); } } } }
CryptoSharedPreferences implements SharedPreferences { @Override public void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener) { synchronized (changeListenersRegistry) { changeListenersRegistry.unregister(listener); if (changeListenersRegistry.isEmpty()) { delegate.unregisterOnSharedPreferenceChangeListener(changeListenersRegistry); changeListenersRegistered.set(false); } } } @SuppressWarnings("WeakerAccess")  CryptoSharedPreferences(final Builder builder); }
CryptoSharedPreferences implements SharedPreferences { @Override public void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener) { synchronized (changeListenersRegistry) { changeListenersRegistry.unregister(listener); if (changeListenersRegistry.isEmpty()) { delegate.unregisterOnSharedPreferenceChangeListener(changeListenersRegistry); changeListenersRegistered.set(false); } } } @SuppressWarnings("WeakerAccess")  CryptoSharedPreferences(final Builder builder); @Override void registerOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener); @Override void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener); @Override Map<String, ?> getAll(); @Override boolean contains(@NonNull final String key); @Override @Nullable String getString(@NonNull final String key, @Nullable final String defValue); @Override @Nullable Set<String> getStringSet(@NonNull final String key, @Nullable final Set<String> defValues); @Override int getInt(@NonNull final String key, final int defValue); @Override long getLong(@NonNull final String key, final long defValue); @Override float getFloat(@NonNull final String key, final float defValue); @Override boolean getBoolean(@NonNull final String key, final boolean defValue); @SuppressLint("CommitPrefEdits") @Override SharedPreferences.Editor edit(); }
CryptoSharedPreferences implements SharedPreferences { @Override public void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener) { synchronized (changeListenersRegistry) { changeListenersRegistry.unregister(listener); if (changeListenersRegistry.isEmpty()) { delegate.unregisterOnSharedPreferenceChangeListener(changeListenersRegistry); changeListenersRegistered.set(false); } } } @SuppressWarnings("WeakerAccess")  CryptoSharedPreferences(final Builder builder); @Override void registerOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener); @Override void unregisterOnSharedPreferenceChangeListener(@NonNull final OnSharedPreferenceChangeListener listener); @Override Map<String, ?> getAll(); @Override boolean contains(@NonNull final String key); @Override @Nullable String getString(@NonNull final String key, @Nullable final String defValue); @Override @Nullable Set<String> getStringSet(@NonNull final String key, @Nullable final Set<String> defValues); @Override int getInt(@NonNull final String key, final int defValue); @Override long getLong(@NonNull final String key, final long defValue); @Override float getFloat(@NonNull final String key, final float defValue); @Override boolean getBoolean(@NonNull final String key, final boolean defValue); @SuppressLint("CommitPrefEdits") @Override SharedPreferences.Editor edit();  }