summaryrefslogtreecommitdiff
path: root/android
diff options
context:
space:
mode:
authorArtur Dryomov <artur.dryomov@gmail.com>2013-06-20 19:03:46 +0300
committerMichael Meeks <michael.meeks@suse.com>2013-07-25 18:01:47 +0100
commitd0f3c74887d88cca9398b0109c911f9f1aeedef4 (patch)
treea4d0b9beee4bd431d4274ead5b89a50c8fdd2a40 /android
parentd170ecde6ce91f6be43fe465889de4fa4498d25f (diff)
Refactor CommunicationService.
Change-Id: I5107d18101a3d37e6df260563814a367c93ec10d
Diffstat (limited to 'android')
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/Preferences.java10
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java8
-rw-r--r--android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java266
3 files changed, 135 insertions, 149 deletions
diff --git a/android/sdremote/src/org/libreoffice/impressremote/Preferences.java b/android/sdremote/src/org/libreoffice/impressremote/Preferences.java
index 35639daa406e..7bc9e4b8252c 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/Preferences.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/Preferences.java
@@ -17,6 +17,7 @@ public final class Preferences {
}
public static final String AUTHORIZED_REMOTES = "sdremote_authorisedremotes";
+ public static final String STORED_SERVERS = "sdremote_storedServers";
}
private Preferences() {
@@ -42,6 +43,15 @@ public final class Preferences {
aPreferencesEditor.commit();
}
+
+ public static void remove(Context aContext, String aLocation, String aKey) {
+ SharedPreferences.Editor aPreferencesEditor = getPreferences(aContext,
+ aLocation).edit();
+
+ aPreferencesEditor.remove(aKey);
+
+ aPreferencesEditor.commit();
+ }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
index 560e5d30a5a6..a3d88dabe6fd 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/SelectorActivity.java
@@ -155,7 +155,7 @@ public class SelectorActivity extends SherlockActivity {
@Override
public void onBackPressed() {
if (mCommunicationService != null)
- mCommunicationService.stopSearching();
+ mCommunicationService.stopSearch();
Intent aIntent = new Intent(this, CommunicationService.class);
stopService(aIntent);
super.onBackPressed();
@@ -175,7 +175,7 @@ public class SelectorActivity extends SherlockActivity {
protected void onPause() {
super.onPause();
if (mCommunicationService != null) {
- mCommunicationService.stopSearching();
+ mCommunicationService.stopSearch();
}
doUnbindService();
if (mProgressDialog != null) {
@@ -202,7 +202,7 @@ public class SelectorActivity extends SherlockActivity {
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
- mCommunicationService.startSearching();
+ mCommunicationService.startSearch();
refreshLists();
}
@@ -345,7 +345,7 @@ public class SelectorActivity extends SherlockActivity {
@Override
public void onClick(View aView) {
- mCommunicationService.stopSearching();
+ mCommunicationService.stopSearch();
Server aDesiredServer = null;
diff --git a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
index 00a903940d55..d87ef6ff96c5 100644
--- a/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
+++ b/android/sdremote/src/org/libreoffice/impressremote/communication/CommunicationService.java
@@ -15,27 +15,70 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import org.libreoffice.impressremote.Globals;
-import org.libreoffice.impressremote.communication.Server.Protocol;
-
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
-import android.content.SharedPreferences.Editor;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
-import android.util.Log;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
-public class CommunicationService extends Service implements Runnable {
+import org.libreoffice.impressremote.Preferences;
+import org.libreoffice.impressremote.communication.Server.Protocol;
+public class CommunicationService extends Service implements Runnable {
public static enum State {
DISCONNECTED, SEARCHING, CONNECTING, CONNECTED
}
+ public static final String MSG_SLIDESHOW_STARTED = "SLIDESHOW_STARTED";
+ public static final String MSG_SLIDE_CHANGED = "SLIDE_CHANGED";
+ public static final String MSG_SLIDE_PREVIEW = "SLIDE_PREVIEW";
+ public static final String MSG_SLIDE_NOTES = "SLIDE_NOTES";
+
+ public static final String MSG_SERVERLIST_CHANGED = "SERVERLIST_CHANGED";
+ public static final String MSG_PAIRING_STARTED = "PAIRING_STARTED";
+ public static final String MSG_PAIRING_SUCCESSFUL = "PAIRING_SUCCESSFUL";
+
+ public static final String STATUS_CONNECTED_SLIDESHOW_RUNNING = "STATUS_CONNECTED_SLIDESHOW_RUNNING";
+ public static final String STATUS_CONNECTED_NOSLIDESHOW = "STATUS_CONNECTED_NOSLIDESHOW";
+
+ public static final String STATUS_PAIRING_PINVALIDATION = "STATUS_PAIRING_PINVALIDATION";
+ public static final String STATUS_CONNECTION_FAILED = "STATUS_CONNECTION_FAILED";
+
+ /**
+ * Used to protect all writes to mState, mStateDesired, and mServerDesired.
+ */
+ private final Object mConnectionVariableMutex = new Object();
+
+ private State mState = State.DISCONNECTED;
+ private State mStateDesired = State.DISCONNECTED;
+
+ private Server mServerDesired = null;
+
+ private boolean mBluetoothPreviouslyEnabled;
+
+ private final IBinder mBinder = new CBinder();
+
+ private Transmitter mTransmitter;
+
+ private Client mClient;
+
+ private final Receiver mReceiver = new Receiver(this);
+
+ private final ServerFinder mNetworkFinder = new ServerFinder(this);
+ private final BluetoothFinder mBluetoothFinder = new BluetoothFinder(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.
@@ -43,41 +86,23 @@ public class CommunicationService extends Service implements Runnable {
* @return The device name.
*/
public static String getDeviceName() {
- BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
- if (aAdapter != null) {
- String aName = aAdapter.getName();
- if (aName != null)
- return aName;
+ if (BluetoothAdapter.getDefaultAdapter() == null) {
+ return Build.MODEL;
}
- return Build.MODEL;
- }
-
- /**
- * Used to protect all writes to mState, mStateDesired, and mServerDesired.
- */
- private Object mConnectionVariableMutex = new Object();
-
- private State mState = State.DISCONNECTED;
- public State getState() {
- return mState;
- }
+ if (BluetoothAdapter.getDefaultAdapter().getName() == null) {
+ return Build.MODEL;
+ }
- public String getPairingPin() {
- return Client.getPin();
+ return BluetoothAdapter.getDefaultAdapter().getName();
}
public String getPairingDeviceName() {
return Client.getName();
}
- private State mStateDesired = State.DISCONNECTED;
-
- private Server mServerDesired = null;
-
@Override
public void run() {
- Log.i(Globals.TAG, "CommunicationService.run()");
synchronized (this) {
while (true) {
// Condition
@@ -87,57 +112,72 @@ public class CommunicationService extends Service implements Runnable {
// We have finished
return;
}
+
// Work
- Log.i(Globals.TAG, "CommunicationService.run: at \"Work\"");
synchronized (mConnectionVariableMutex) {
- if ((mStateDesired == State.CONNECTED && mState == State.CONNECTED)
- || (mStateDesired == State.DISCONNECTED && mState == State.CONNECTED)) {
- mClient.closeConnection();
- mClient = null;
- mState = State.DISCONNECTED;
+ if ((mStateDesired == State.CONNECTED) && (mState == State.CONNECTED)) {
+ closeConnection();
+ }
+
+ if ((mStateDesired == State.DISCONNECTED) && (mState == State.CONNECTED)) {
+ closeConnection();
}
+
if (mStateDesired == State.CONNECTED) {
mState = State.CONNECTING;
- try {
- switch (mServerDesired.getProtocol()) {
- case NETWORK:
- mClient = new NetworkClient(mServerDesired,
- this, mReceiver);
- mClient.validating();
- break;
- case BLUETOOTH:
- mClient = new BluetoothClient(mServerDesired,
- this, mReceiver,
- mBluetoothPreviouslyEnabled);
- mClient.validating();
- break;
- }
- mTransmitter = new Transmitter(mClient);
- mState = State.CONNECTED;
- } catch (IOException e) {
- Log.i(Globals.TAG, "CommunicationService.run: " + e);
- connextionFailed();
- }
+
+ openConnection();
}
}
- Log.i(Globals.TAG, "CommunicationService.finished work");
}
}
}
- private void connextionFailed() {
+ private void closeConnection() {
+ mClient.closeConnection();
+ mClient = null;
+
+ mState = State.DISCONNECTED;
+ }
+
+ private void openConnection() {
+ try {
+ mClient = buildClient();
+ mClient.validating();
+
+ mTransmitter = new Transmitter(mClient);
+
+ mState = State.CONNECTED;
+ } catch (IOException e) {
+ connectionFailed();
+ }
+ }
+
+ private Client buildClient() {
+ switch (mServerDesired.getProtocol()) {
+ case NETWORK:
+ return new NetworkClient(mServerDesired, this, mReceiver);
+
+ case BLUETOOTH:
+ return new BluetoothClient(mServerDesired, this, mReceiver,
+ mBluetoothPreviouslyEnabled);
+
+ default:
+ throw new RuntimeException("Unknown desired protocol.");
+ }
+ }
+
+ private void connectionFailed() {
mClient = null;
mState = State.DISCONNECTED;
Intent aIntent = new Intent(
- CommunicationService.STATUS_CONNECTION_FAILED);
+ CommunicationService.STATUS_CONNECTION_FAILED);
LocalBroadcastManager.getInstance(this).sendBroadcast(aIntent);
}
- private boolean mBluetoothPreviouslyEnabled;
-
- public void startSearching() {
- Log.i(Globals.TAG, "CommunicationService.startSearching()");
- SharedPreferences aPref = PreferenceManager.getDefaultSharedPreferences(this);
+ public void startSearch() {
+ SharedPreferences aPref = PreferenceManager
+ .getDefaultSharedPreferences(this);
boolean bEnableWifi = aPref.getBoolean("option_enablewifi", false);
if (bEnableWifi)
mNetworkFinder.startSearch();
@@ -150,8 +190,7 @@ public class CommunicationService extends Service implements Runnable {
}
}
- public void stopSearching() {
- Log.i(Globals.TAG, "CommunicationService.stopSearching()");
+ public void stopSearch() {
mNetworkFinder.stopSearch();
mBluetoothFinder.stopSearch();
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
@@ -163,7 +202,6 @@ public class CommunicationService extends Service implements Runnable {
}
public void connectTo(Server aServer) {
- Log.i(Globals.TAG, "CommunicationService.connectTo(" + aServer + ")");
synchronized (mConnectionVariableMutex) {
if (mState == State.SEARCHING) {
mNetworkFinder.stopSearch();
@@ -181,7 +219,6 @@ public class CommunicationService extends Service implements Runnable {
}
public void disconnect() {
- Log.d(Globals.TAG, "Service Disconnected");
synchronized (mConnectionVariableMutex) {
mStateDesired = State.DISCONNECTED;
synchronized (this) {
@@ -190,63 +227,19 @@ public class CommunicationService extends Service implements Runnable {
}
}
- /**
- * Return the service to clients.
- */
public class CBinder extends Binder {
public CommunicationService getService() {
return CommunicationService.this;
}
}
- private final IBinder mBinder = new CBinder();
-
- public static final String MSG_SLIDESHOW_STARTED = "SLIDESHOW_STARTED";
- public static final String MSG_SLIDE_CHANGED = "SLIDE_CHANGED";
- public static final String MSG_SLIDE_PREVIEW = "SLIDE_PREVIEW";
- public static final String MSG_SLIDE_NOTES = "SLIDE_NOTES";
-
- public static final String MSG_SERVERLIST_CHANGED = "SERVERLIST_CHANGED";
- public static final String MSG_PAIRING_STARTED = "PAIRING_STARTED";
- public static final String MSG_PAIRING_SUCCESSFUL = "PAIRING_SUCCESSFUL";
-
- /**
- * Notify the UI that the service has connected to a server AND a slideshow
- * is running.
- * In this case the PresentationActivity should be started.
- */
- public static final String STATUS_CONNECTED_SLIDESHOW_RUNNING = "STATUS_CONNECTED_SLIDESHOW_RUNNING";
- /**
- * Notify the UI that the service has connected to a server AND no slideshow
- * is running.
- * In this case the StartPresentationActivity should be started.
- */
- public static final String STATUS_CONNECTED_NOSLIDESHOW = "STATUS_CONNECTED_NOSLIDESHOW";
-
- public static final String STATUS_PAIRING_PINVALIDATION = "STATUS_PAIRING_PINVALIDATION";
-
- public static final String STATUS_CONNECTION_FAILED = "STATUS_CONNECTION_FAILED";
-
- private Transmitter mTransmitter;
-
- private Client mClient;
-
- private Receiver mReceiver = new Receiver(this);
-
- private ServerFinder mNetworkFinder = new ServerFinder(this);
- private BluetoothFinder mBluetoothFinder = new BluetoothFinder(this);
-
@Override
public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
return mBinder;
}
- private Thread mThread = null;
-
@Override
public void onCreate() {
- // TODO Create a notification (if configured).
loadServersFromPreferences();
mThread = new Thread(this);
@@ -255,7 +248,6 @@ public class CommunicationService extends Service implements Runnable {
@Override
public void onDestroy() {
- // TODO Destroy the notification (as necessary).
mManualServers.clear();
mThread.interrupt();
@@ -267,10 +259,12 @@ public class CommunicationService extends Service implements Runnable {
}
public List<Server> getServers() {
- ArrayList<Server> aServers = new ArrayList<Server>();
+ List<Server> aServers = new ArrayList<Server>();
+
aServers.addAll(mNetworkFinder.getServers());
aServers.addAll(mBluetoothFinder.getServers());
aServers.addAll(mManualServers.values());
+
return aServers;
}
@@ -278,35 +272,23 @@ public class CommunicationService extends Service implements Runnable {
return mReceiver.getSlideShow();
}
- public boolean isSlideShowRunning() {
- return mReceiver.isSlideShowRunning();
- }
-
- /**
- * 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 static final String SERVERSTORAGE_KEY = "sdremote_storedServers";
- private HashMap<String, Server> mManualServers = new HashMap<String, Server>();
-
void loadServersFromPreferences() {
- SharedPreferences aPref = getSharedPreferences(SERVERSTORAGE_KEY,
- MODE_PRIVATE);
+ SharedPreferences aPref = getSharedPreferences(
+ Preferences.Locations.STORED_SERVERS,
+ MODE_PRIVATE);
@SuppressWarnings("unchecked")
- Map<String, String> aStoredMap = (Map<String, String>) aPref.getAll();
+ Map<String, String> aStoredMap = (Map<String, String>) aPref.getAll();
for (Entry<String, String> aServerEntry : aStoredMap.entrySet()) {
mManualServers.put(aServerEntry.getKey(), new Server(
- Protocol.NETWORK, aServerEntry.getKey(),
- aServerEntry.getValue(), 0));
+ Protocol.NETWORK, aServerEntry.getKey(),
+ aServerEntry.getValue(), 0));
}
}
/**
* Manually add a new (network) server to the list of servers.
- * @param aAddress
- * @param aRemember
*/
public void addServer(String aAddress, String aName, boolean aRemember) {
for (String aServer : mManualServers.keySet()) {
@@ -314,31 +296,25 @@ public class CommunicationService extends Service implements Runnable {
return;
}
mManualServers.put(aAddress, new Server(Protocol.NETWORK, aAddress,
- aName, 0));
+ aName, 0));
if (aRemember) {
- SharedPreferences aPref = getSharedPreferences(SERVERSTORAGE_KEY,
- MODE_PRIVATE);
- Editor aEditor = aPref.edit();
- aEditor.putString(aAddress, aName);
- aEditor.apply();
+
+ Preferences
+ .set(this, Preferences.Locations.STORED_SERVERS, aAddress,
+ aName);
}
}
public void removeServer(Server aServer) {
mManualServers.remove(aServer.getAddress());
- SharedPreferences aPref = getSharedPreferences(SERVERSTORAGE_KEY,
- MODE_PRIVATE);
- Editor aEditor = aPref.edit();
- aEditor.remove(aServer.getAddress());
- aEditor.apply();
-
+ Preferences.remove(this, Preferences.Locations.STORED_SERVERS,
+ aServer.getAddress());
}
public Client getClient() {
return mClient;
}
-
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */