WIP: Move swap starting/stopping to async task.

There is now a private [en|dis]ableSwappingSynchronous() method, to go
along with the public [en|dis]ableSwapping() method. The public method
knows about not spinning up tasks if already enabled, and also about
pushing the invocation to a background task. The private method just
blindly does what it is asked, without checking if it should be done,
and without running on a background task.

The same goes for the restartIfEnabled() method, it is responsible for
creating a single background task which runs in order: disable, and
then enable. This is actually the reason for having the synchronous
methods, rather than having, e.g., the public [en|dis]ableSwapping()
method know about threads. If that was the case, then restarting would
consist of starting, waiting for some form of notification that the
background task has completed, and then scheduling the enable task.
Now, it is a matter of calling both *SwappingSynchronous methods in
succession.
This commit is contained in:
Peter Serwylo 2015-06-03 17:51:16 +10:00
parent 7c9492e6b4
commit 2553b8520c

View File

@ -7,6 +7,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
@ -101,14 +102,26 @@ public class SwapService extends Service {
private boolean enabled = false;
/**
* Ensures that the webserver is running, as are the other services which make swap work.
* Will only do all this if it is not already running, and will run on a background thread.'
* TODO: What about an "enabling" status? Not sure if it will be useful or not.
*/
public void enableSwapping() {
if (!enabled) {
nfcType.start();
webServerType.start();
bonjourType.start();
startForeground(NOTIFICATION, createNotification());
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
enableSwappingSynchronous();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
enabled = true;
}
}.execute();
}
// Regardless of whether it was previously enabled, start the timer again. This ensures that
// if, e.g. a person views the swap activity again, it will attempt to enable swapping if
@ -116,17 +129,49 @@ public class SwapService extends Service {
initTimer();
}
/**
* The guts of this class - responsible for enabling the relevant services for swapping.
* * Doesn't know anything about enabled/disabled.
* * Runs synchronously on the thread it was called.
*/
private void enableSwappingSynchronous() {
nfcType.start();
webServerType.start();
bonjourType.start();
startForeground(NOTIFICATION, createNotification());
}
public void disableSwapping() {
if (enabled) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
disableSwappingSynchronous();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
// 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) {
timer.cancel();
}
enabled = false;
}
}.execute();
}
}
/**
* @see SwapService#enableSwappingSynchronous()
*/
private void disableSwappingSynchronous() {
bonjourType.stop();
webServerType.stop();
nfcType.stop();
stopForeground(true);
if (timer != null) {
timer.cancel();
}
enabled = false;
}
}
public boolean isEnabled() {
@ -135,8 +180,14 @@ public class SwapService extends Service {
public void restartIfEnabled() {
if (enabled) {
disableSwapping();
enableSwapping();
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
disableSwappingSynchronous();
enableSwappingSynchronous();
return null;
}
}.execute();
}
}