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

View File

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

View File

@ -65,11 +65,6 @@ public class WebServerType implements SwapType {
}
};
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
@ -78,14 +73,10 @@ public class WebServerType implements SwapType {
Log.i(TAG, "null handler in stopWebServer");
return;
}
Log.d(TAG, "Sending message to swap webserver to stop it.");
Message msg = webServerThreadHandler.obtainMessage();
msg.obj = webServerThreadHandler.getLooper().getThread().getName() + " says stop";
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