Change the Preferences class.
* Remove context dependency. * Modify the interface for easy usage. Change-Id: I9dfabbea1ec9ec9224dc8238a1884fdf695fc8db
This commit is contained in:
@@ -15,10 +15,10 @@ import android.os.Build;
|
|||||||
import org.libreoffice.impressremote.util.Preferences;
|
import org.libreoffice.impressremote.util.Preferences;
|
||||||
|
|
||||||
final class PairingProvider {
|
final class PairingProvider {
|
||||||
private final Context mContext;
|
private final Preferences mAuthorizedServersPreferences;
|
||||||
|
|
||||||
private PairingProvider(Context aContext) {
|
private PairingProvider(Context aContext) {
|
||||||
mContext = aContext;
|
mAuthorizedServersPreferences = Preferences.getAuthorizedServersInstance(aContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isPairingNecessary(Server aServer) {
|
public static boolean isPairingNecessary(Server aServer) {
|
||||||
@@ -46,17 +46,11 @@ final class PairingProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getSavedPin(Server aServer) {
|
private String getSavedPin(Server aServer) {
|
||||||
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
|
return mAuthorizedServersPreferences.get(aServer.getAddress());
|
||||||
String aServerAddress = aServer.getAddress();
|
|
||||||
|
|
||||||
return Preferences.getString(mContext, aLocation, aServerAddress);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void savePin(Server aServer, String aPin) {
|
private void savePin(Server aServer, String aPin) {
|
||||||
String aLocation = Preferences.Locations.AUTHORIZED_REMOTES;
|
mAuthorizedServersPreferences.set(aServer.getAddress(), aPin);
|
||||||
String aServerAddress = aServer.getAddress();
|
|
||||||
|
|
||||||
Preferences.set(mContext, aLocation, aServerAddress, aPin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getPairingDeviceName(Context aContext) {
|
public static String getPairingDeviceName(Context aContext) {
|
||||||
|
@@ -22,20 +22,18 @@ import android.content.Context;
|
|||||||
import org.libreoffice.impressremote.util.Preferences;
|
import org.libreoffice.impressremote.util.Preferences;
|
||||||
|
|
||||||
class ServersManager implements Comparator<Server> {
|
class ServersManager implements Comparator<Server> {
|
||||||
private final Context mContext;
|
|
||||||
|
|
||||||
private final ServersFinder mBluetoothServersFinder;
|
private final ServersFinder mBluetoothServersFinder;
|
||||||
private final ServersFinder mTcpServersFinder;
|
private final ServersFinder mTcpServersFinder;
|
||||||
|
|
||||||
private final Set<Server> mBlacklistedServers;
|
private final Set<Server> mBlacklistedServers;
|
||||||
|
private final Preferences mSavedServersPreferences;
|
||||||
|
|
||||||
public ServersManager(Context aContext) {
|
public ServersManager(Context aContext) {
|
||||||
mContext = aContext;
|
mBluetoothServersFinder = new BluetoothServersFinder(aContext);
|
||||||
|
mTcpServersFinder = new TcpServersFinder(aContext);
|
||||||
mBluetoothServersFinder = new BluetoothServersFinder(mContext);
|
|
||||||
mTcpServersFinder = new TcpServersFinder(mContext);
|
|
||||||
|
|
||||||
mBlacklistedServers = new HashSet<Server>();
|
mBlacklistedServers = new HashSet<Server>();
|
||||||
|
mSavedServersPreferences = Preferences.getSavedServersInstance(aContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startServersSearch() {
|
public void startServersSearch() {
|
||||||
@@ -63,8 +61,7 @@ class ServersManager implements Comparator<Server> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<Server> getManualAddedTcpServers() {
|
private List<Server> getManualAddedTcpServers() {
|
||||||
Map<String, ?> aServersEntries = Preferences
|
Map<String, ?> aServersEntries = mSavedServersPreferences.getAll();
|
||||||
.getAll(mContext, Preferences.Locations.STORED_SERVERS);
|
|
||||||
|
|
||||||
return buildTcpServers(aServersEntries);
|
return buildTcpServers(aServersEntries);
|
||||||
}
|
}
|
||||||
@@ -104,8 +101,7 @@ class ServersManager implements Comparator<Server> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addTcpServer(String aAddress, String aName) {
|
public void addTcpServer(String aAddress, String aName) {
|
||||||
Preferences.set(mContext, Preferences.Locations.STORED_SERVERS,
|
mSavedServersPreferences.set(aAddress, aName);
|
||||||
aAddress, aName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeServer(Server aServer) {
|
public void removeServer(Server aServer) {
|
||||||
@@ -129,8 +125,7 @@ class ServersManager implements Comparator<Server> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void removeManualAddedServer(Server aServer) {
|
private void removeManualAddedServer(Server aServer) {
|
||||||
Preferences.remove(mContext, Preferences.Locations.STORED_SERVERS,
|
mSavedServersPreferences.remove(aServer.getAddress());
|
||||||
aServer.getAddress());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void blacklistServer(Server aServer) {
|
private void blacklistServer(Server aServer) {
|
||||||
|
@@ -14,45 +14,46 @@ import android.content.Context;
|
|||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
public final class Preferences {
|
public final class Preferences {
|
||||||
public static final class Locations {
|
private static final class Locations {
|
||||||
private Locations() {
|
private Locations() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final String AUTHORIZED_REMOTES = "sdremote_authorisedremotes";
|
public static final String AUTHORIZED_SERVERS = "authorized_servers";
|
||||||
public static final String STORED_SERVERS = "sdremote_storedServers";
|
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);
|
return aContext.getSharedPreferences(aLocation, Context.MODE_PRIVATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, ?> getAll(Context aContext, String aLocation) {
|
public static Preferences getSavedServersInstance(Context aContext) {
|
||||||
return getPreferences(aContext, aLocation).getAll();
|
return new Preferences(aContext, Locations.SAVED_SERVERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getString(Context aContext, String aLocation, String aKey) {
|
public Map<String, ?> getAll() {
|
||||||
return getPreferences(aContext, aLocation).getString(aKey, null);
|
return mPreferences.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void set(Context aContext, String aLocation, String aKey, String aValue) {
|
public String get(String aKey) {
|
||||||
SharedPreferences.Editor aPreferencesEditor = getPreferences(aContext,
|
return mPreferences.getString(aKey, null);
|
||||||
aLocation).edit();
|
|
||||||
|
|
||||||
aPreferencesEditor.putString(aKey, aValue);
|
|
||||||
|
|
||||||
aPreferencesEditor.commit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void remove(Context aContext, String aLocation, String aKey) {
|
public void set(String aKey, String aValue) {
|
||||||
SharedPreferences.Editor aPreferencesEditor = getPreferences(aContext,
|
mPreferences.edit().putString(aKey, aValue).commit();
|
||||||
aLocation).edit();
|
}
|
||||||
|
|
||||||
aPreferencesEditor.remove(aKey);
|
public void remove(String aKey) {
|
||||||
|
mPreferences.edit().remove(aKey).commit();
|
||||||
aPreferencesEditor.commit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user