diff --git a/F-Droid/src/org/fdroid/fdroid/FDroidApp.java b/F-Droid/src/org/fdroid/fdroid/FDroidApp.java index 6f99b3106..7c0036545 100644 --- a/F-Droid/src/org/fdroid/fdroid/FDroidApp.java +++ b/F-Droid/src/org/fdroid/fdroid/FDroidApp.java @@ -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. 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"; 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; - } } diff --git a/F-Droid/src/org/fdroid/fdroid/localrepo/SwapState.java b/F-Droid/src/org/fdroid/fdroid/localrepo/SwapState.java index 802de5cd9..ad458c57d 100644 --- a/F-Droid/src/org/fdroid/fdroid/localrepo/SwapState.java +++ b/F-Droid/src/org/fdroid/fdroid/localrepo/SwapState.java @@ -1,7 +1,14 @@ package org.fdroid.fdroid.localrepo; +import android.content.ComponentName; import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; 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.NonNull; @@ -31,7 +38,7 @@ public class SwapState { private int step; private SwapState(@NonNull Context context, @SwapStep int step, @NonNull Set appsToSwap) { - this.context = context; + this.context = context.getApplicationContext(); this.step = step; 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}) @Retention(RetentionPolicy.SOURCE) 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; + } } diff --git a/F-Droid/src/org/fdroid/fdroid/net/WifiStateChangeService.java b/F-Droid/src/org/fdroid/fdroid/net/WifiStateChangeService.java index bb90e1af4..9c2330a04 100644 --- a/F-Droid/src/org/fdroid/fdroid/net/WifiStateChangeService.java +++ b/F-Droid/src/org/fdroid/fdroid/net/WifiStateChangeService.java @@ -17,6 +17,7 @@ import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.localrepo.LocalRepoKeyStore; import org.fdroid.fdroid.localrepo.LocalRepoManager; +import org.fdroid.fdroid.localrepo.SwapState; import java.net.Inet6Address; import java.net.InetAddress; @@ -151,7 +152,7 @@ public class WifiStateChangeService extends Service { Intent intent = new Intent(BROADCAST); LocalBroadcastManager.getInstance(WifiStateChangeService.this).sendBroadcast(intent); WifiStateChangeService.this.stopSelf(); - FDroidApp.restartLocalRepoServiceIfRunning(); + SwapState.load(WifiStateChangeService.this).restartLocalRepoServiceIfRunning(); } } diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/ConnectSwapActivity.java b/F-Droid/src/org/fdroid/fdroid/views/swap/ConnectSwapActivity.java index 3eebca77c..6bb1f7efd 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/swap/ConnectSwapActivity.java +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/ConnectSwapActivity.java @@ -27,6 +27,7 @@ import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.data.NewRepoConfig; import org.fdroid.fdroid.data.Repo; import org.fdroid.fdroid.data.RepoProvider; +import org.fdroid.fdroid.localrepo.SwapState; import java.io.IOException; 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. // It is possible to have a swap initiated without first starting a swap, in which // case swapping back is pointless. - if (!newRepoConfig.preventFurtherSwaps() && FDroidApp.isLocalRepoServiceRunning()) { + if (!newRepoConfig.preventFurtherSwaps() && SwapState.load(this).isLocalRepoServiceRunning()) { askServerToSwapWithUs(); } diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapActivity.java b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapActivity.java index c27f0097b..d1fe5d77c 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapActivity.java +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapActivity.java @@ -19,6 +19,7 @@ import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.R; import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.localrepo.LocalRepoManager; +import org.fdroid.fdroid.localrepo.SwapState; import java.util.Set; import java.util.Timer; @@ -100,7 +101,7 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage showFragment(new StartSwapFragment(), STATE_START_SWAP); - if (FDroidApp.isLocalRepoServiceRunning()) { + if (getState().isLocalRepoServiceRunning()) { showSelectApps(); showJoinWifi(); attemptToShowNfc(); @@ -181,12 +182,16 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage } private void ensureLocalRepoRunning() { - if (!FDroidApp.isLocalRepoServiceRunning()) { - FDroidApp.startLocalRepoService(this); + if (!getState().isLocalRepoServiceRunning()) { + getState().startLocalRepoService(); initLocalRepoTimer(900000); // 15 mins } } + private SwapState getState() { + return SwapState.load(this); + } + private void initLocalRepoTimer(long timeoutMilliseconds) { // reset the timer if viewing this Activity again @@ -198,7 +203,7 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage shutdownLocalRepoTimer.schedule(new TimerTask() { @Override public void run() { - FDroidApp.stopLocalRepoService(SwapActivity.this); + getState().stopLocalRepoService(); } }, timeoutMilliseconds); @@ -206,11 +211,11 @@ public class SwapActivity extends ActionBarActivity implements SwapProcessManage @Override public void stopSwapping() { - if (FDroidApp.isLocalRepoServiceRunning()) { + if (getState().isLocalRepoServiceRunning()) { if (shutdownLocalRepoTimer != null) { shutdownLocalRepoTimer.cancel(); } - FDroidApp.stopLocalRepoService(SwapActivity.this); + getState().stopLocalRepoService(); } finish(); } diff --git a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java index 1dab7b974..c954a1c03 100644 --- a/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java +++ b/F-Droid/src/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java @@ -196,8 +196,8 @@ public class SwapWorkflowActivity extends FragmentActivity { } private void ensureLocalRepoRunning() { - if (!FDroidApp.isLocalRepoServiceRunning()) { - FDroidApp.startLocalRepoService(this); + if (!getState().isLocalRepoServiceRunning()) { + getState().startLocalRepoService(); initLocalRepoTimer(900000); // 15 mins } } @@ -213,18 +213,18 @@ public class SwapWorkflowActivity extends FragmentActivity { shutdownLocalRepoTimer.schedule(new TimerTask() { @Override public void run() { - FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); + getState().stopLocalRepoService(); } }, timeoutMilliseconds); } public void stopSwapping() { - if (FDroidApp.isLocalRepoServiceRunning()) { + if (getState().isLocalRepoServiceRunning()) { if (shutdownLocalRepoTimer != null) { shutdownLocalRepoTimer.cancel(); } - FDroidApp.stopLocalRepoService(SwapWorkflowActivity.this); + getState().stopLocalRepoService(); } finish(); }