Change the Preferences class.

* Remove context dependency.
* Modify the interface for easy usage.

Change-Id: I9dfabbea1ec9ec9224dc8238a1884fdf695fc8db
This commit is contained in:
Artur Dryomov
2013-08-03 16:39:25 +03:00
parent ef5342c677
commit 38a3eba78d
3 changed files with 34 additions and 44 deletions

View File

@@ -15,10 +15,10 @@ import android.os.Build;
import org.libreoffice.impressremote.util.Preferences;
final class PairingProvider {
private final Context mContext;
private final Preferences mAuthorizedServersPreferences;
private PairingProvider(Context aContext) {
mContext = aContext;
mAuthorizedServersPreferences = Preferences.getAuthorizedServersInstance(aContext);
}
public static boolean isPairingNecessary(Server aServer) {
@@ -46,17 +46,11 @@ final class PairingProvider {
}
private String getSavedPin(Server aServer) {
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
String aServerAddress = aServer.getAddress();
return Preferences.getString(mContext, aLocation, aServerAddress);
return mAuthorizedServersPreferences.get(aServer.getAddress());
}
private void savePin(Server aServer, String aPin) {
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
String aServerAddress = aServer.getAddress();
Preferences.set(mContext, aLocation, aServerAddress, aPin);
mAuthorizedServersPreferences.set(aServer.getAddress(), aPin);
}
public static String getPairingDeviceName(Context aContext) {

View File

@@ -22,20 +22,18 @@ import android.content.Context;
import org.libreoffice.impressremote.util.Preferences;
class ServersManager implements Comparator<Server> {
private final Context mContext;
private final ServersFinder mBluetoothServersFinder;
private final ServersFinder mTcpServersFinder;
private final Set<Server> mBlacklistedServers;
private final Preferences mSavedServersPreferences;
public ServersManager(Context aContext) {
mContext = aContext;
mBluetoothServersFinder = new BluetoothServersFinder(mContext);
mTcpServersFinder = new TcpServersFinder(mContext);
mBluetoothServersFinder = new BluetoothServersFinder(aContext);
mTcpServersFinder = new TcpServersFinder(aContext);
mBlacklistedServers = new HashSet<Server>();
mSavedServersPreferences = Preferences.getSavedServersInstance(aContext);
}
public void startServersSearch() {
@@ -63,8 +61,7 @@ class ServersManager implements Comparator<Server> {
}
private List<Server> getManualAddedTcpServers() {
Map<String, ?> aServersEntries = Preferences
.getAll(mContext, Preferences.Locations.STORED_SERVERS);
Map<String, ?> aServersEntries = mSavedServersPreferences.getAll();
return buildTcpServers(aServersEntries);
}
@@ -104,8 +101,7 @@ class ServersManager implements Comparator<Server> {
}
public void addTcpServer(String aAddress, String aName) {
Preferences.set(mContext, Preferences.Locations.STORED_SERVERS,
aAddress, aName);
mSavedServersPreferences.set(aAddress, aName);
}
public void removeServer(Server aServer) {
@@ -129,8 +125,7 @@ class ServersManager implements Comparator<Server> {
}
private void removeManualAddedServer(Server aServer) {
Preferences.remove(mContext, Preferences.Locations.STORED_SERVERS,
aServer.getAddress());
mSavedServersPreferences.remove(aServer.getAddress());
}
private void blacklistServer(Server aServer) {

View File

@@ -14,45 +14,46 @@ import android.content.Context;
import android.content.SharedPreferences;
public final class Preferences {
public static final class Locations {
private static final class Locations {
private Locations() {
}
public static final String AUTHORIZED_REMOTES = "sdremote_authorisedremotes";
public static final String STORED_SERVERS = "sdremote_storedServers";
public static final String AUTHORIZED_SERVERS = "authorized_servers";
public static final String SAVED_SERVERS = "saved_servers";
}
private Preferences() {
private final SharedPreferences mPreferences;
public static Preferences getAuthorizedServersInstance(Context aContext) {
return new Preferences(aContext, Locations.AUTHORIZED_SERVERS);
}
private static SharedPreferences getPreferences(Context aContext, String aLocation) {
private Preferences(Context aContext, String aLocation) {
mPreferences = getPreferences(aContext, aLocation);
}
private SharedPreferences getPreferences(Context aContext, String aLocation) {
return aContext.getSharedPreferences(aLocation, Context.MODE_PRIVATE);
}
public static Map<String, ?> getAll(Context aContext, String aLocation) {
return getPreferences(aContext, aLocation).getAll();
public static Preferences getSavedServersInstance(Context aContext) {
return new Preferences(aContext, Locations.SAVED_SERVERS);
}
public static String getString(Context aContext, String aLocation, String aKey) {
return getPreferences(aContext, aLocation).getString(aKey, null);
public Map<String, ?> getAll() {
return mPreferences.getAll();
}
public static void set(Context aContext, String aLocation, String aKey, String aValue) {
SharedPreferences.Editor aPreferencesEditor = getPreferences(aContext,
aLocation).edit();
aPreferencesEditor.putString(aKey, aValue);
aPreferencesEditor.commit();
public String get(String aKey) {
return mPreferences.getString(aKey, null);
}
public static void remove(Context aContext, String aLocation, String aKey) {
SharedPreferences.Editor aPreferencesEditor = getPreferences(aContext,
aLocation).edit();
public void set(String aKey, String aValue) {
mPreferences.edit().putString(aKey, aValue).commit();
}
aPreferencesEditor.remove(aKey);
aPreferencesEditor.commit();
public void remove(String aKey) {
mPreferences.edit().remove(aKey).commit();
}
}