WIP: Added logging, removed dead code, fixed minor bugs.

Logging to help understand when the service is started, stopped,
restarted, and when individual components (e.g. web server, bonjour)
are started/stopped.

Fixed bug where service was moved to foreground during "restartIfEnabled"
because that is unneccesary if it is already in the foreground.
This commit is contained in:
Peter Serwylo 2015-06-03 23:41:51 +10:00
parent 2553b8520c
commit d9f91da9a6
3 changed files with 25 additions and 15 deletions

View File

@ -69,8 +69,8 @@ public class SwapService extends Service {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
Log.d(TAG, "Creating service, will register appropriate listeners.");
Preferences.get().unregisterLocalRepoBonjourListeners(bonjourEnabledListener); Preferences.get().registerLocalRepoBonjourListeners(bonjourEnabledListener);
Preferences.get().registerLocalRepoHttpsListeners(httpsEnabledListener); Preferences.get().registerLocalRepoHttpsListeners(httpsEnabledListener);
LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange, LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange,
@ -85,7 +85,11 @@ public class SwapService extends Service {
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
Log.d(TAG, "Destroying service, will disable swapping if required, and unregister listeners.");
disableSwapping(); disableSwapping();
Preferences.get().unregisterLocalRepoBonjourListeners(bonjourEnabledListener);
Preferences.get().unregisterLocalRepoHttpsListeners(httpsEnabledListener);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onWifiChange);
} }
private Notification createNotification() { private Notification createNotification() {
@ -112,12 +116,15 @@ public class SwapService extends Service {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
Log.d(TAG, "Started background task to enable swapping.");
enableSwappingSynchronous(); enableSwappingSynchronous();
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
Log.d(TAG, "Moving SwapService to foreground so that it hangs around even when F-Droid is closed.");
startForeground(NOTIFICATION, createNotification());
enabled = true; enabled = true;
} }
}.execute(); }.execute();
@ -138,7 +145,6 @@ public class SwapService extends Service {
nfcType.start(); nfcType.start();
webServerType.start(); webServerType.start();
bonjourType.start(); bonjourType.start();
startForeground(NOTIFICATION, createNotification());
} }
public void disableSwapping() { public void disableSwapping() {
@ -146,12 +152,15 @@ public class SwapService extends Service {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
Log.d(TAG, "Started background task to disable swapping.");
disableSwappingSynchronous(); disableSwappingSynchronous();
return null; return null;
} }
@Override @Override
protected void onPostExecute(Void aVoid) { protected void onPostExecute(Void aVoid) {
Log.d(TAG, "Finished background task to disable swapping.");
// TODO: Does this need to be run before the background task, so that the timer // TODO: Does this need to be run before the background task, so that the timer
// can't kick in while we are shutting down everything? // can't kick in while we are shutting down everything?
if (timer != null) { if (timer != null) {
@ -159,6 +168,9 @@ public class SwapService extends Service {
} }
enabled = false; enabled = false;
Log.d(TAG, "Moving SwapService to background so that it can be GC'ed if required.");
stopForeground(true);
} }
}.execute(); }.execute();
} }
@ -168,10 +180,10 @@ public class SwapService extends Service {
* @see SwapService#enableSwappingSynchronous() * @see SwapService#enableSwappingSynchronous()
*/ */
private void disableSwappingSynchronous() { private void disableSwappingSynchronous() {
Log.d(TAG, "Disabling SwapService (bonjour, webserver, etc)");
bonjourType.stop(); bonjourType.stop();
webServerType.stop(); webServerType.stop();
nfcType.stop(); nfcType.stop();
stopForeground(true);
} }
public boolean isEnabled() { public boolean isEnabled() {
@ -183,6 +195,7 @@ public class SwapService extends Service {
new AsyncTask<Void, Void, Void>() { new AsyncTask<Void, Void, Void>() {
@Override @Override
protected Void doInBackground(Void... params) { protected Void doInBackground(Void... params) {
Log.d(TAG, "Restarting swap services.");
disableSwappingSynchronous(); disableSwappingSynchronous();
enableSwappingSynchronous(); enableSwappingSynchronous();
return null; return null;
@ -192,14 +205,18 @@ public class SwapService extends Service {
} }
private void initTimer() { private void initTimer() {
if (timer != null) if (timer != null) {
Log.d(TAG, "Cancelling existing timer");
timer.cancel(); timer.cancel();
}
// automatically turn off after 15 minutes // automatically turn off after 15 minutes
Log.d(TAG, "Initializing timer to 15 minutes");
timer = new Timer(); timer = new Timer();
timer.schedule(new TimerTask() { timer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
Log.d(TAG, "Disabling swap because " + TIMEOUT + "ms passed.");
disableSwapping(); disableSwapping();
} }
}, TIMEOUT); }, TIMEOUT);

View File

@ -52,9 +52,11 @@ public class BonjourType implements SwapType {
type = "_http._tcp.local."; type = "_http._tcp.local.";
} }
try { try {
Log.d(TAG, "Starting bonjour service...");
pairService = ServiceInfo.create(type, repoName, FDroidApp.port, 0, 0, values); pairService = ServiceInfo.create(type, repoName, FDroidApp.port, 0, 0, values);
jmdns = JmDNS.create(); jmdns = JmDNS.create();
jmdns.registerService(pairService); jmdns.registerService(pairService);
Log.d(TAG, "... Bounjour service started.");
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Error while registering jmdns service: " + e); Log.e(TAG, "Error while registering jmdns service: " + e);
Log.e(TAG, Log.getStackTraceString(e)); Log.e(TAG, Log.getStackTraceString(e));

View File

@ -65,11 +65,6 @@ public class WebServerType implements SwapType {
} }
}; };
new Thread(webServer).start(); new Thread(webServer).start();
// TODO: Don't think these were ever being received by anyone...
/*Intent intent = new Intent(STATE);
intent.putExtra(STATE, STARTED);
LocalBroadcastManager.getInstance(LocalRepoService.this).sendBroadcast(intent);*/
} }
@Override @Override
@ -78,14 +73,10 @@ public class WebServerType implements SwapType {
Log.i(TAG, "null handler in stopWebServer"); Log.i(TAG, "null handler in stopWebServer");
return; return;
} }
Log.d(TAG, "Sending message to swap webserver to stop it.");
Message msg = webServerThreadHandler.obtainMessage(); Message msg = webServerThreadHandler.obtainMessage();
msg.obj = webServerThreadHandler.getLooper().getThread().getName() + " says stop"; msg.obj = webServerThreadHandler.getLooper().getThread().getName() + " says stop";
webServerThreadHandler.sendMessage(msg); webServerThreadHandler.sendMessage(msg);
// TODO: Don't think these were ever being received by anyone...
/*Intent intent = new Intent(STATE);
intent.putExtra(STATE, STOPPED);
LocalBroadcastManager.getInstance(LocalRepoService.this).sendBroadcast(intent);*/
} }
@Override @Override