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:
parent
7c9492e6b4
commit
2553b8520c
@ -7,6 +7,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.NotificationCompat;
|
import android.support.v4.app.NotificationCompat;
|
||||||
@ -101,13 +102,25 @@ public class SwapService extends Service {
|
|||||||
|
|
||||||
private boolean enabled = false;
|
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() {
|
public void enableSwapping() {
|
||||||
if (!enabled) {
|
if (!enabled) {
|
||||||
nfcType.start();
|
new AsyncTask<Void, Void, Void>() {
|
||||||
webServerType.start();
|
@Override
|
||||||
bonjourType.start();
|
protected Void doInBackground(Void... params) {
|
||||||
startForeground(NOTIFICATION, createNotification());
|
enableSwappingSynchronous();
|
||||||
enabled = true;
|
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
|
// Regardless of whether it was previously enabled, start the timer again. This ensures that
|
||||||
@ -116,27 +129,65 @@ public class SwapService extends Service {
|
|||||||
initTimer();
|
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() {
|
public void disableSwapping() {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
bonjourType.stop();
|
new AsyncTask<Void, Void, Void>() {
|
||||||
webServerType.stop();
|
@Override
|
||||||
nfcType.stop();
|
protected Void doInBackground(Void... params) {
|
||||||
stopForeground(true);
|
disableSwappingSynchronous();
|
||||||
if (timer != null) {
|
return null;
|
||||||
timer.cancel();
|
}
|
||||||
}
|
|
||||||
enabled = false;
|
@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);
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return enabled;
|
return enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void restartIfEnabled() {
|
public void restartIfEnabled() {
|
||||||
if (enabled) {
|
if (enabled) {
|
||||||
disableSwapping();
|
new AsyncTask<Void, Void, Void>() {
|
||||||
enableSwapping();
|
@Override
|
||||||
|
protected Void doInBackground(Void... params) {
|
||||||
|
disableSwappingSynchronous();
|
||||||
|
enableSwappingSynchronous();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user