diff options
author | Artur Dryomov <artur.dryomov@gmail.com> | 2013-07-12 19:24:20 +0300 |
---|---|---|
committer | Michael Meeks <michael.meeks@suse.com> | 2013-07-25 18:01:53 +0100 |
commit | 94255fc98ecaf5a41d4e37956c9d9e67145b7e23 (patch) | |
tree | ecc51d9903c05ecdd702a4f61653bb15826fa2d5 /android/sdremote/src | |
parent | 1990f88aa6394d26d4fde08ff47055e9b1d9ce59 (diff) |
Move servers management from the service to a separate class.
Change-Id: I7739bbf961c2783b3699e2bac21fc79eedcf5b26
Diffstat (limited to 'android/sdremote/src')
4 files changed, 130 insertions, 79 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/Preferences.java b/android/sdremote/src/org/libreoffice/impressremote/Preferences.java index 7bc9e4b8252c..937d706e935f 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/Preferences.java +++ b/android/sdremote/src/org/libreoffice/impressremote/Preferences.java @@ -8,6 +8,8 @@ */ package org.libreoffice.impressremote; +import java.util.Map; + import android.content.Context; import android.content.SharedPreferences; @@ -31,6 +33,10 @@ public final class Preferences { return aContext.getSharedPreferences(aLocation, Context.MODE_PRIVATE); } + public static Map<String, ?> getAll(Context aContext, String aLocation) { + return getPreferences(aContext, aLocation).getAll(); + } + public static String getString(Context aContext, String aLocation, String aKey) { return getPreferences(aContext, aLocation).getString(aKey, null); } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java index 2c3466e849be..4309cc52bca6 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java @@ -8,20 +8,14 @@ */ package org.libreoffice.impressremote.communication; -import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import android.app.Service; import android.bluetooth.BluetoothAdapter; import android.content.Intent; -import android.content.SharedPreferences; import android.os.Binder; import android.os.Build; import android.os.IBinder; -import android.preference.PreferenceManager; import android.support.v4.content.LocalBroadcastManager; import org.libreoffice.impressremote.Preferences; @@ -56,23 +50,13 @@ public class CommunicationService extends Service implements Runnable, MessagesL private Server mServerDesired = null; - private boolean mBluetoothPreviouslyEnabled; - private final IBinder mBinder = new CBinder(); - private final ServersFinder mTcpServersFinder = new TcpServersFinder(this); - private final ServersFinder mBluetoothServersFinder = new BluetoothServersFinder( - this); + private final ServersManager mServersManager = new ServersManager(this); private Thread mThread = null; /** - * Key to use with getSharedPreferences to obtain a Map of stored servers. - * The keys are the ip/hostnames, the values are the friendly names. - */ - private final Map<String, Server> mManualServers = new HashMap<String, Server>(); - - /** * Get the publicly visible device name -- generally the bluetooth name, * however for bluetoothless devices the device model name is used. * @@ -210,36 +194,25 @@ public class CommunicationService extends Service implements Runnable, MessagesL public void startSearch() { mState = State.SEARCHING; - mTcpServersFinder.startSearch(); - - BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); - if (aAdapter != null) { - mBluetoothPreviouslyEnabled = aAdapter.isEnabled(); - - if (!mBluetoothPreviouslyEnabled) { - aAdapter.enable(); - } - - mBluetoothServersFinder.startSearch(); + if (BluetoothAdapter.getDefaultAdapter() != null) { + BluetoothAdapter.getDefaultAdapter().enable(); } + + mServersManager.startServersSearch(); } public void stopSearch() { - mTcpServersFinder.stopSearch(); - mBluetoothServersFinder.stopSearch(); - BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter(); - if (aAdapter != null) { - if (!mBluetoothPreviouslyEnabled) { - aAdapter.disable(); - } + mServersManager.stopServersSearch(); + + if (BluetoothAdapter.getDefaultAdapter() != null) { + BluetoothAdapter.getDefaultAdapter().disable(); } } public void connectTo(Server aServer) { synchronized (mConnectionVariableMutex) { if (mState == State.SEARCHING) { - mTcpServersFinder.stopSearch(); - mBluetoothServersFinder.stopSearch(); + mServersManager.stopServersSearch(); mState = State.DISCONNECTED; } mServerDesired = aServer; @@ -274,15 +247,13 @@ public class CommunicationService extends Service implements Runnable, MessagesL @Override public void onCreate() { - loadServersFromPreferences(); - mThread = new Thread(this); mThread.start(); } @Override public void onDestroy() { - mManualServers.clear(); + stopSearch(); mThread.interrupt(); mThread = null; @@ -293,58 +264,22 @@ public class CommunicationService extends Service implements Runnable, MessagesL } public List<Server> getServers() { - List<Server> aServers = new ArrayList<Server>(); - - aServers.addAll(mTcpServersFinder.getServers()); - aServers.addAll(mBluetoothServersFinder.getServers()); - aServers.addAll(mManualServers.values()); - - return aServers; + return mServersManager.getServers(); } public SlideShow getSlideShow() { return mSlideShow; } - void loadServersFromPreferences() { - SharedPreferences aPref = getSharedPreferences( - Preferences.Locations.STORED_SERVERS, - MODE_PRIVATE); - - @SuppressWarnings("unchecked") - Map<String, String> aStoredMap = (Map<String, String>) aPref.getAll(); - - for (Entry<String, String> aServerEntry : aStoredMap.entrySet()) { - mManualServers.put(aServerEntry.getKey(), new Server( - Server.Protocol.TCP, aServerEntry.getKey(), - aServerEntry.getValue(), 0)); - } - } - /** * Manually add a new (network) server to the list of servers. */ public void addServer(String aAddress, String aName, boolean aRemember) { - for (String aServer : mManualServers.keySet()) { - if (aServer.equals(aAddress)) - return; - } - mManualServers - .put(aAddress, new Server(Server.Protocol.TCP, aAddress, - aName, 0)); - if (aRemember) { - - Preferences - .set(this, Preferences.Locations.STORED_SERVERS, aAddress, - aName); - } + mServersManager.addTcpServer(aAddress, aName); } public void removeServer(Server aServer) { - mManualServers.remove(aServer.getAddress()); - - Preferences.remove(this, Preferences.Locations.STORED_SERVERS, - aServer.getAddress()); + mServersManager.removeServer(aServer); } @Override diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java index f51e3525b28f..a96ee4231bc7 100644 --- a/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/Server.java @@ -43,6 +43,10 @@ public class Server implements Parcelable { this.mTimeDiscovered = System.currentTimeMillis(); } + public static Server newTcpInstance(String aAddress, String aName) { + return new Server(Protocol.TCP, aAddress, aName); + } + public Protocol getProtocol() { return mProtocol; } diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java b/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java new file mode 100644 index 000000000000..22148f78c341 --- /dev/null +++ b/android/sdremote/src/org/libreoffice/impressremote/communication/ServersManager.java @@ -0,0 +1,106 @@ +package org.libreoffice.impressremote.communication; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import android.content.Context; + +import org.libreoffice.impressremote.Preferences; + +public class ServersManager { + private final Context mContext; + + private final ServersFinder mBluetoothServersFinder; + private final ServersFinder mTcpServersFinder; + + private final Set<Server> mBlacklistedServers; + + public ServersManager(Context aContext) { + mContext = aContext; + + mBluetoothServersFinder = new BluetoothServersFinder(mContext); + mTcpServersFinder = new TcpServersFinder(mContext); + + mBlacklistedServers = new HashSet<Server>(); + } + + public void startServersSearch() { + mBluetoothServersFinder.startSearch(); + mTcpServersFinder.startSearch(); + } + + public void stopServersSearch() { + mBluetoothServersFinder.stopSearch(); + mTcpServersFinder.stopSearch(); + } + + public List<Server> getServers() { + List<Server> aServers = new ArrayList<Server>(); + + aServers.addAll(mBluetoothServersFinder.getServers()); + aServers.addAll(mTcpServersFinder.getServers()); + aServers.addAll(getManualAddedTcpServers()); + + return filterBlacklistedServers(aServers); + } + + private List<Server> getManualAddedTcpServers() { + Map<String, ?> aServersEntries = Preferences + .getAll(mContext, Preferences.Locations.STORED_SERVERS); + + return buildTcpServers(aServersEntries); + } + + private List<Server> buildTcpServers(Map<String, ?> aServersEntries) { + List<Server> aServers = new ArrayList<Server>(); + + for (String aServerAddress : aServersEntries.keySet()) { + String aServerName = (String) aServersEntries.get(aServerAddress); + + aServers.add(Server.newTcpInstance(aServerAddress, aServerName)); + } + + return aServers; + } + + private List<Server> filterBlacklistedServers(List<Server> aServers) { + List<Server> aFilteredServers = new ArrayList<Server>(); + + for (Server aServer : aServers) { + if (mBlacklistedServers.contains(aServer)) { + continue; + } + + aFilteredServers.add(aServer); + } + + return aFilteredServers; + } + + public void addTcpServer(String aAddress, String aName) { + Preferences.set(mContext, Preferences.Locations.STORED_SERVERS, + aAddress, aName); + } + + public void removeServer(Server aServer) { + if (getManualAddedTcpServers().contains(aServer)) { + removeManualAddedServer(aServer); + + return; + } + + blacklistServer(aServer); + } + + private void removeManualAddedServer(Server aServer) { + Preferences.remove(mContext, Preferences.Locations.STORED_SERVERS, + aServer.getAddress()); + } + + private void blacklistServer(Server aServer) { + mBlacklistedServers.add(aServer); + } +} |