WIP: Moved start/stop local repo code from static FDroidApp to SwapState

Didn't change any behaviour, but wanted to start unifying swap state
management in one location.
This commit is contained in:
Peter Serwylo 2015-05-25 08:39:31 +10:00
parent 68c6648da5
commit f325e9d057
6 changed files with 84 additions and 65 deletions

View File

@ -73,9 +73,8 @@ public class FDroidApp extends Application {
// Leaving the fully qualified class name here to help clarify the difference between spongy/bouncy castle. // Leaving the fully qualified class name here to help clarify the difference between spongy/bouncy castle.
private static final org.spongycastle.jce.provider.BouncyCastleProvider spongyCastleProvider; private static final org.spongycastle.jce.provider.BouncyCastleProvider spongyCastleProvider;
private static Messenger localRepoServiceMessenger = null;
private static boolean localRepoServiceIsBound = false;
@SuppressWarnings("unused")
private static final String TAG = "FDroidApp"; private static final String TAG = "FDroidApp";
BluetoothAdapter bluetoothAdapter = null; BluetoothAdapter bluetoothAdapter = null;
@ -301,53 +300,4 @@ public class FDroidApp extends Application {
} }
} }
} }
private static final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
localRepoServiceMessenger = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName className) {
localRepoServiceMessenger = null;
}
};
public static void startLocalRepoService(Context context) {
if (!localRepoServiceIsBound) {
Context app = context.getApplicationContext();
Intent service = new Intent(app, LocalRepoService.class);
localRepoServiceIsBound = app.bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
if (localRepoServiceIsBound)
app.startService(service);
}
}
public static void stopLocalRepoService(Context context) {
Context app = context.getApplicationContext();
if (localRepoServiceIsBound) {
app.unbindService(serviceConnection);
localRepoServiceIsBound = false;
}
app.stopService(new Intent(app, LocalRepoService.class));
}
/**
* Handles checking if the {@link LocalRepoService} is running, and only restarts it if it was running.
*/
public static void restartLocalRepoServiceIfRunning() {
if (localRepoServiceMessenger != null) {
try {
Message msg = Message.obtain(null, LocalRepoService.RESTART, LocalRepoService.RESTART, 0);
localRepoServiceMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public static boolean isLocalRepoServiceRunning() {
return localRepoServiceIsBound;
}
} }

View File

@ -1,7 +1,14 @@
package org.fdroid.fdroid.localrepo; package org.fdroid.fdroid.localrepo;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -31,7 +38,7 @@ public class SwapState {
private int step; private int step;
private SwapState(@NonNull Context context, @SwapStep int step, @NonNull Set<String> appsToSwap) { private SwapState(@NonNull Context context, @SwapStep int step, @NonNull Set<String> appsToSwap) {
this.context = context; this.context = context.getApplicationContext();
this.step = step; this.step = step;
this.appsToSwap = appsToSwap; this.appsToSwap = appsToSwap;
} }
@ -145,4 +152,59 @@ public class SwapState {
@IntDef({STEP_INTRO, STEP_SELECT_APPS, STEP_JOIN_WIFI, STEP_SHOW_NFC, STEP_WIFI_QR}) @IntDef({STEP_INTRO, STEP_SELECT_APPS, STEP_JOIN_WIFI, STEP_SHOW_NFC, STEP_WIFI_QR})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface SwapStep {} public @interface SwapStep {}
// ==========================================
// Local repo stop/start/restart handling
// ==========================================
private Messenger localRepoServiceMessenger = null;
private boolean localRepoServiceIsBound = false;
private final ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
localRepoServiceMessenger = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName className) {
localRepoServiceMessenger = null;
}
};
public void startLocalRepoService() {
if (!localRepoServiceIsBound) {
Intent service = new Intent(context, LocalRepoService.class);
localRepoServiceIsBound = context.bindService(service, serviceConnection, Context.BIND_AUTO_CREATE);
if (localRepoServiceIsBound)
context.startService(service);
}
}
public void stopLocalRepoService() {
if (localRepoServiceIsBound) {
context.unbindService(serviceConnection);
localRepoServiceIsBound = false;
}
context.stopService(new Intent(context, LocalRepoService.class));
}
/**
* Handles checking if the {@link LocalRepoService} is running, and only restarts it if it was running.
*/
public void restartLocalRepoServiceIfRunning() {
if (localRepoServiceMessenger != null) {
try {
Message msg = Message.obtain(null, LocalRepoService.RESTART, LocalRepoService.RESTART, 0);
localRepoServiceMessenger.send(msg);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
public boolean isLocalRepoServiceRunning() {
return localRepoServiceIsBound;
}
} }

View File

@ -17,6 +17,7 @@ import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.localrepo.LocalRepoKeyStore; import org.fdroid.fdroid.localrepo.LocalRepoKeyStore;
import org.fdroid.fdroid.localrepo.LocalRepoManager; import org.fdroid.fdroid.localrepo.LocalRepoManager;
import org.fdroid.fdroid.localrepo.SwapState;
import java.net.Inet6Address; import java.net.Inet6Address;
import java.net.InetAddress; import java.net.InetAddress;
@ -151,7 +152,7 @@ public class WifiStateChangeService extends Service {
Intent intent = new Intent(BROADCAST); Intent intent = new Intent(BROADCAST);
LocalBroadcastManager.getInstance(WifiStateChangeService.this).sendBroadcast(intent); LocalBroadcastManager.getInstance(WifiStateChangeService.this).sendBroadcast(intent);
WifiStateChangeService.this.stopSelf(); WifiStateChangeService.this.stopSelf();
FDroidApp.restartLocalRepoServiceIfRunning(); SwapState.load(WifiStateChangeService.this).restartLocalRepoServiceIfRunning();
} }
} }

View File

@ -27,6 +27,7 @@ import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.NewRepoConfig; import org.fdroid.fdroid.data.NewRepoConfig;
import org.fdroid.fdroid.data.Repo; import org.fdroid.fdroid.data.Repo;
import org.fdroid.fdroid.data.RepoProvider; import org.fdroid.fdroid.data.RepoProvider;
import org.fdroid.fdroid.localrepo.SwapState;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -154,7 +155,7 @@ public class ConnectSwapActivity extends ActionBarActivity implements ProgressLi
// Only ask server to swap with us, if we are actually running a local repo service. // Only ask server to swap with us, if we are actually running a local repo service.
// It is possible to have a swap initiated without first starting a swap, in which // It is possible to have a swap initiated without first starting a swap, in which
// case swapping back is pointless. // case swapping back is pointless.
if (!newRepoConfig.preventFurtherSwaps() && FDroidApp.isLocalRepoServiceRunning()) { if (!newRepoConfig.preventFurtherSwaps() && SwapState.load(this).isLocalRepoServiceRunning()) {
askServerToSwapWithUs(); askServerToSwapWithUs();
} }

View File

@ -19,6 +19,7 @@ import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.localrepo.LocalRepoManager; import org.fdroid.fdroid.localrepo.LocalRepoManager;
import org.fdroid.fdroid.localrepo.SwapState;
import java.util.Set; import java.util.Set;
import java.util.Timer; import java.util.Timer;
@ -100,7 +101,7 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
showFragment(new StartSwapFragment(), STATE_START_SWAP); showFragment(new StartSwapFragment(), STATE_START_SWAP);
if (FDroidApp.isLocalRepoServiceRunning()) { if (getState().isLocalRepoServiceRunning()) {
showSelectApps(); showSelectApps();
showJoinWifi(); showJoinWifi();
attemptToShowNfc(); attemptToShowNfc();
@ -181,12 +182,16 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
} }
private void ensureLocalRepoRunning() { private void ensureLocalRepoRunning() {
if (!FDroidApp.isLocalRepoServiceRunning()) { if (!getState().isLocalRepoServiceRunning()) {
FDroidApp.startLocalRepoService(this); getState().startLocalRepoService();
initLocalRepoTimer(900000); // 15 mins initLocalRepoTimer(900000); // 15 mins
} }
} }
private SwapState getState() {
return SwapState.load(this);
}
private void initLocalRepoTimer(long timeoutMilliseconds) { private void initLocalRepoTimer(long timeoutMilliseconds) {
// reset the timer if viewing this Activity again // reset the timer if viewing this Activity again
@ -198,7 +203,7 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
shutdownLocalRepoTimer.schedule(new TimerTask() { shutdownLocalRepoTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
FDroidApp.stopLocalRepoService(SwapActivity.this); getState().stopLocalRepoService();
} }
}, timeoutMilliseconds); }, timeoutMilliseconds);
@ -206,11 +211,11 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage
@Override @Override
public void stopSwapping() { public void stopSwapping() {
if (FDroidApp.isLocalRepoServiceRunning()) { if (getState().isLocalRepoServiceRunning()) {
if (shutdownLocalRepoTimer != null) { if (shutdownLocalRepoTimer != null) {
shutdownLocalRepoTimer.cancel(); shutdownLocalRepoTimer.cancel();
} }
FDroidApp.stopLocalRepoService(SwapActivity.this); getState().stopLocalRepoService();
} }
finish(); finish();
} }

View File

@ -196,8 +196,8 @@ public class SwapWorkflowActivity extends FragmentActivity {
} }
private void ensureLocalRepoRunning() { private void ensureLocalRepoRunning() {
if (!FDroidApp.isLocalRepoServiceRunning()) { if (!getState().isLocalRepoServiceRunning()) {
FDroidApp.startLocalRepoService(this); getState().startLocalRepoService();
initLocalRepoTimer(900000); // 15 mins initLocalRepoTimer(900000); // 15 mins
} }
} }
@ -213,18 +213,18 @@ public class SwapWorkflowActivity extends FragmentActivity {
shutdownLocalRepoTimer.schedule(new TimerTask() { shutdownLocalRepoTimer.schedule(new TimerTask() {
@Override @Override
public void run() { public void run() {
FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); getState().stopLocalRepoService();
} }
}, timeoutMilliseconds); }, timeoutMilliseconds);
} }
public void stopSwapping() { public void stopSwapping() {
if (FDroidApp.isLocalRepoServiceRunning()) { if (getState().isLocalRepoServiceRunning()) {
if (shutdownLocalRepoTimer != null) { if (shutdownLocalRepoTimer != null) {
shutdownLocalRepoTimer.cancel(); shutdownLocalRepoTimer.cancel();
} }
FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); getState().stopLocalRepoService();
} }
finish(); finish();
} }