зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1129591 - Extract EditorBranch and PrefsBranch. r=nalexander
This commit is contained in:
Родитель
e22847194c
Коммит
cc5713a1fa
|
@ -770,6 +770,7 @@ sync_java_files = [
|
|||
'background/bagheera/BoundedByteArrayEntity.java',
|
||||
'background/bagheera/DeflateHelper.java',
|
||||
'background/common/DateUtils.java',
|
||||
'background/common/EditorBranch.java',
|
||||
'background/common/GlobalConstants.java',
|
||||
'background/common/log/Logger.java',
|
||||
'background/common/log/writers/AndroidLevelCachingLogWriter.java',
|
||||
|
@ -781,6 +782,7 @@ sync_java_files = [
|
|||
'background/common/log/writers/StringLogWriter.java',
|
||||
'background/common/log/writers/TagLogWriter.java',
|
||||
'background/common/log/writers/ThreadLocalTagLogWriter.java',
|
||||
'background/common/PrefsBranch.java',
|
||||
'background/common/telemetry/TelemetryWrapper.java',
|
||||
'background/datareporting/TelemetryRecorder.java',
|
||||
'background/db/CursorDumper.java',
|
||||
|
|
|
@ -0,0 +1,82 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.background.common;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.Editor;
|
||||
|
||||
public class EditorBranch implements Editor {
|
||||
|
||||
private final String prefix;
|
||||
private Editor editor;
|
||||
|
||||
public EditorBranch(final SharedPreferences prefs, final String prefix) {
|
||||
if (!prefix.endsWith(".")) {
|
||||
throw new IllegalArgumentException("No trailing period in prefix.");
|
||||
}
|
||||
this.prefix = prefix;
|
||||
this.editor = prefs.edit();
|
||||
}
|
||||
|
||||
public void apply() {
|
||||
// Android <=r8 SharedPreferences.Editor does not contain apply() for overriding.
|
||||
this.editor.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor clear() {
|
||||
this.editor = this.editor.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commit() {
|
||||
return this.editor.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putBoolean(String key, boolean value) {
|
||||
this.editor = this.editor.putBoolean(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putFloat(String key, float value) {
|
||||
this.editor = this.editor.putFloat(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putInt(String key, int value) {
|
||||
this.editor = this.editor.putInt(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putLong(String key, long value) {
|
||||
this.editor = this.editor.putLong(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putString(String key, String value) {
|
||||
this.editor = this.editor.putString(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Not marking as Override, because Android <= 10 doesn't have
|
||||
// putStringSet. Neither can we implement it.
|
||||
public Editor putStringSet(String key, Set<String> value) {
|
||||
throw new RuntimeException("putStringSet not available.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor remove(String key) {
|
||||
this.editor = this.editor.remove(prefix + key);
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.gecko.background.common;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
/**
|
||||
* A wrapper around a portion of the SharedPreferences space.
|
||||
*/
|
||||
public class PrefsBranch implements SharedPreferences {
|
||||
private final SharedPreferences prefs;
|
||||
private final String prefix; // Including trailing period.
|
||||
|
||||
public PrefsBranch(SharedPreferences prefs, String prefix) {
|
||||
if (!prefix.endsWith(".")) {
|
||||
throw new IllegalArgumentException("No trailing period in prefix.");
|
||||
}
|
||||
this.prefs = prefs;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String key) {
|
||||
return prefs.contains(prefix + key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor edit() {
|
||||
return new EditorBranch(prefs, prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ?> getAll() {
|
||||
// Not implemented. TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defValue) {
|
||||
return prefs.getBoolean(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(String key, float defValue) {
|
||||
return prefs.getFloat(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String key, int defValue) {
|
||||
return prefs.getInt(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key, long defValue) {
|
||||
return prefs.getLong(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defValue) {
|
||||
return prefs.getString(prefix + key, defValue);
|
||||
}
|
||||
|
||||
// Not marking as Override, because Android <= 10 doesn't have
|
||||
// getStringSet. Neither can we implement it.
|
||||
public Set<String> getStringSet(String key, Set<String> defValue) {
|
||||
throw new RuntimeException("getStringSet not available.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
|
||||
prefs.registerOnSharedPreferenceChangeListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
|
||||
prefs.unregisterOnSharedPreferenceChangeListener(listener);
|
||||
}
|
||||
}
|
|
@ -13,6 +13,7 @@ import java.util.Map;
|
|||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
||||
import org.mozilla.gecko.background.common.PrefsBranch;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.sync.crypto.KeyBundle;
|
||||
import org.mozilla.gecko.sync.crypto.PersistedCrypto5Keys;
|
||||
|
@ -23,158 +24,6 @@ import android.content.SharedPreferences;
|
|||
import android.content.SharedPreferences.Editor;
|
||||
|
||||
public class SyncConfiguration {
|
||||
|
||||
public class EditorBranch implements Editor {
|
||||
|
||||
private final String prefix;
|
||||
private Editor editor;
|
||||
|
||||
public EditorBranch(SyncConfiguration config, String prefix) {
|
||||
if (!prefix.endsWith(".")) {
|
||||
throw new IllegalArgumentException("No trailing period in prefix.");
|
||||
}
|
||||
this.prefix = prefix;
|
||||
this.editor = config.getEditor();
|
||||
}
|
||||
|
||||
public void apply() {
|
||||
// Android <=r8 SharedPreferences.Editor does not contain apply() for overriding.
|
||||
this.editor.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor clear() {
|
||||
this.editor = this.editor.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commit() {
|
||||
return this.editor.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putBoolean(String key, boolean value) {
|
||||
this.editor = this.editor.putBoolean(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putFloat(String key, float value) {
|
||||
this.editor = this.editor.putFloat(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putInt(String key, int value) {
|
||||
this.editor = this.editor.putInt(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putLong(String key, long value) {
|
||||
this.editor = this.editor.putLong(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor putString(String key, String value) {
|
||||
this.editor = this.editor.putString(prefix + key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Not marking as Override, because Android <= 10 doesn't have
|
||||
// putStringSet. Neither can we implement it.
|
||||
public Editor putStringSet(String key, Set<String> value) {
|
||||
throw new RuntimeException("putStringSet not available.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor remove(String key) {
|
||||
this.editor = this.editor.remove(prefix + key);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A wrapper around a portion of the SharedPreferences space.
|
||||
*
|
||||
* @author rnewman
|
||||
*
|
||||
*/
|
||||
public class ConfigurationBranch implements SharedPreferences {
|
||||
|
||||
private final SyncConfiguration config;
|
||||
private final String prefix; // Including trailing period.
|
||||
|
||||
public ConfigurationBranch(SyncConfiguration syncConfiguration,
|
||||
String prefix) {
|
||||
if (!prefix.endsWith(".")) {
|
||||
throw new IllegalArgumentException("No trailing period in prefix.");
|
||||
}
|
||||
this.config = syncConfiguration;
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean contains(String key) {
|
||||
return config.getPrefs().contains(prefix + key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Editor edit() {
|
||||
return new EditorBranch(config, prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, ?> getAll() {
|
||||
// Not implemented. TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String key, boolean defValue) {
|
||||
return config.getPrefs().getBoolean(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(String key, float defValue) {
|
||||
return config.getPrefs().getFloat(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String key, int defValue) {
|
||||
return config.getPrefs().getInt(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key, long defValue) {
|
||||
return config.getPrefs().getLong(prefix + key, defValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key, String defValue) {
|
||||
return config.getPrefs().getString(prefix + key, defValue);
|
||||
}
|
||||
|
||||
// Not marking as Override, because Android <= 10 doesn't have
|
||||
// getStringSet. Neither can we implement it.
|
||||
public Set<String> getStringSet(String key, Set<String> defValue) {
|
||||
throw new RuntimeException("getStringSet not available.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
|
||||
config.getPrefs().registerOnSharedPreferenceChangeListener(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) {
|
||||
config.getPrefs().unregisterOnSharedPreferenceChangeListener(listener);
|
||||
}
|
||||
}
|
||||
|
||||
private static final String LOG_TAG = "SyncConfiguration";
|
||||
|
||||
// These must be set in GlobalSession's constructor.
|
||||
|
@ -299,11 +148,11 @@ public class SyncConfiguration {
|
|||
/**
|
||||
* Return a convenient accessor for part of prefs.
|
||||
* @return
|
||||
* A ConfigurationBranch object representing this
|
||||
* A PrefsBranch object representing this
|
||||
* section of the preferences space.
|
||||
*/
|
||||
public ConfigurationBranch getBranch(String prefix) {
|
||||
return new ConfigurationBranch(this, prefix);
|
||||
public PrefsBranch getBranch(String prefix) {
|
||||
return new PrefsBranch(this.getPrefs(), prefix);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,8 +7,8 @@ package org.mozilla.gecko.sync;
|
|||
import java.io.IOException;
|
||||
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.mozilla.gecko.background.common.PrefsBranch;
|
||||
import org.mozilla.gecko.background.common.log.Logger;
|
||||
import org.mozilla.gecko.sync.SyncConfiguration.ConfigurationBranch;
|
||||
import org.mozilla.gecko.sync.repositories.RepositorySessionBundle;
|
||||
|
||||
import android.content.SharedPreferences.Editor;
|
||||
|
@ -20,7 +20,7 @@ public class SynchronizerConfiguration {
|
|||
public RepositorySessionBundle remoteBundle;
|
||||
public RepositorySessionBundle localBundle;
|
||||
|
||||
public SynchronizerConfiguration(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
|
||||
public SynchronizerConfiguration(PrefsBranch config) throws NonObjectJSONException, IOException, ParseException {
|
||||
this.load(config);
|
||||
}
|
||||
|
||||
|
@ -31,7 +31,7 @@ public class SynchronizerConfiguration {
|
|||
}
|
||||
|
||||
// This should get partly shuffled back into SyncConfiguration, I think.
|
||||
public void load(ConfigurationBranch config) throws NonObjectJSONException, IOException, ParseException {
|
||||
public void load(PrefsBranch config) throws NonObjectJSONException, IOException, ParseException {
|
||||
if (config == null) {
|
||||
throw new IllegalArgumentException("config cannot be null.");
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ public class SynchronizerConfiguration {
|
|||
Logger.debug(LOG_TAG, "Loaded SynchronizerConfiguration. syncID: " + syncID + ", remoteBundle: " + remoteBundle + ", localBundle: " + localBundle);
|
||||
}
|
||||
|
||||
public void persist(ConfigurationBranch config) {
|
||||
public void persist(PrefsBranch config) {
|
||||
if (config == null) {
|
||||
throw new IllegalArgumentException("config cannot be null.");
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче