Local Repo webserver turns itself off if it was automatically started

When you visit LocalRepoActivity, the swapping webserver is automatically
turned on, since it is required for any swapping to happen.  When it was
automatically turned on, it will automatically turn itself off after 15
minutes to make sure that it doesn't stay running forever.  If the user
manually turns it off, that cancels the automatic stop.
This commit is contained in:
Hans-Christoph Steiner 2014-05-07 23:51:53 -04:00
parent 16399b760b
commit 107eab5eac
2 changed files with 42 additions and 4 deletions

View File

@ -34,6 +34,10 @@ import java.util.Random;
public class LocalRepoService extends Service {
private static final String TAG = "LocalRepoService";
public static final String STATE = "org.fdroid.fdroid.action.LOCAL_REPO_STATE";
public static final String STARTED = "org.fdroid.fdroid.category.LOCAL_REPO_STARTED";
public static final String STOPPED = "org.fdroid.fdroid.category.LOCAL_REPO_STOPPED";
private NotificationManager notificationManager;
// Unique Identification Number for the Notification.
// We use it on Notification start, and to cancel it.
@ -148,6 +152,9 @@ public class LocalRepoService extends Service {
}
};
new Thread(webServer).start();
Intent intent = new Intent(STATE);
intent.putExtra(STATE, STARTED);
LocalBroadcastManager.getInstance(LocalRepoService.this).sendBroadcast(intent);
}
private void stopWebServer() {
@ -158,5 +165,8 @@ public class LocalRepoService extends Service {
Message msg = webServerThreadHandler.obtainMessage();
msg.obj = webServerThreadHandler.getLooper().getThread().getName() + " says stop";
webServerThreadHandler.sendMessage(msg);
Intent intent = new Intent(STATE);
intent.putExtra(STATE, STOPPED);
LocalBroadcastManager.getInstance(LocalRepoService.this).sendBroadcast(intent);
}
}

View File

@ -22,9 +22,12 @@ import android.view.*;
import android.widget.*;
import org.fdroid.fdroid.*;
import org.fdroid.fdroid.localrepo.LocalRepoService;
import org.fdroid.fdroid.net.WifiStateChangeService;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;
public class LocalRepoActivity extends Activity {
private static final String TAG = "LocalRepoActivity";
@ -34,6 +37,8 @@ public class LocalRepoActivity extends Activity {
private Button enableWifiButton;
private CheckBox repoSwitch;
private Timer stopTimer;
private int SET_IP_ADDRESS = 7345;
private int UPDATE_REPO = 7346;
@ -54,23 +59,34 @@ public class LocalRepoActivity extends Activity {
super.onResume();
resetNetworkInfo();
// start repo by default
setRepoSwitchChecked(true);
FDroidApp.startLocalRepoService(LocalRepoActivity.this);
LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange,
new IntentFilter(WifiStateChangeService.BROADCAST));
LocalBroadcastManager.getInstance(this).registerReceiver(onLocalRepoChange,
new IntentFilter(LocalRepoService.STATE));
// if no local repo exists, create one with only FDroid in it
if (!FDroidApp.localRepo.xmlIndex.exists())
new UpdateAsyncTask(this, new String[] {
getPackageName(),
}).execute();
// start repo by default
FDroidApp.startLocalRepoService(LocalRepoActivity.this);
// automatically turn off after 15 minutes
stopTimer = new Timer();
stopTimer.schedule(new TimerTask() {
@Override
public void run() {
FDroidApp.stopLocalRepoService(LocalRepoActivity.this);
}
}, 900000); // 15 minutes
}
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(onWifiChange);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onLocalRepoChange);
}
private BroadcastReceiver onWifiChange = new BroadcastReceiver() {
@ -80,6 +96,17 @@ public class LocalRepoActivity extends Activity {
}
};
private BroadcastReceiver onLocalRepoChange = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent i) {
String state = i.getStringExtra(LocalRepoService.STATE);
if (state != null && state.equals(LocalRepoService.STARTED))
setRepoSwitchChecked(true);
else
setRepoSwitchChecked(false);
}
};
private void resetNetworkInfo() {
int wifiState = wifiManager.getWifiState();
if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
@ -172,6 +199,7 @@ public class LocalRepoActivity extends Activity {
FDroidApp.startLocalRepoService(LocalRepoActivity.this);
} else {
FDroidApp.stopLocalRepoService(LocalRepoActivity.this);
stopTimer.cancel(); // disable automatic stop
}
}
});