diff --git a/app/src/full/java/org/fdroid/fdroid/localrepo/SwapService.java b/app/src/full/java/org/fdroid/fdroid/localrepo/SwapService.java index 1055ffd1c..b761a4a0c 100644 --- a/app/src/full/java/org/fdroid/fdroid/localrepo/SwapService.java +++ b/app/src/full/java/org/fdroid/fdroid/localrepo/SwapService.java @@ -22,6 +22,7 @@ import android.support.v4.app.NotificationCompat; import android.support.v4.content.LocalBroadcastManager; import android.text.TextUtils; import android.util.Log; +import cc.mvdan.accesspoint.WifiApControl; import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.R; @@ -57,9 +58,11 @@ public class SwapService extends Service { private static final String KEY_APPS_TO_SWAP = "appsToSwap"; private static final String KEY_BLUETOOTH_ENABLED = "bluetoothEnabled"; private static final String KEY_WIFI_ENABLED = "wifiEnabled"; + private static final String KEY_HOTSPOT_ACTIVATED = "hotspotEnabled"; private static final String KEY_BLUETOOTH_ENABLED_BEFORE_SWAP = "bluetoothEnabledBeforeSwap"; private static final String KEY_BLUETOOTH_NAME_BEFORE_SWAP = "bluetoothNameBeforeSwap"; private static final String KEY_WIFI_ENABLED_BEFORE_SWAP = "wifiEnabledBeforeSwap"; + private static final String KEY_HOTSPOT_ACTIVATED_BEFORE_SWAP = "hotspotEnabledBeforeSwap"; @NonNull private final Set appsToSwap = new HashSet<>(); @@ -294,6 +297,14 @@ public class SwapService extends Service { swapPreferences.edit().putBoolean(SwapService.KEY_WIFI_ENABLED, visible).apply(); } + public static boolean getHotspotActivatedUserPreference() { + return swapPreferences.getBoolean(SwapService.KEY_HOTSPOT_ACTIVATED, false); + } + + public static void putHotspotActivatedUserPreference(boolean visible) { + swapPreferences.edit().putBoolean(SwapService.KEY_HOTSPOT_ACTIVATED, visible).apply(); + } + public static boolean wasBluetoothEnabledBeforeSwap() { return swapPreferences.getBoolean(SwapService.KEY_BLUETOOTH_ENABLED_BEFORE_SWAP, false); } @@ -318,6 +329,14 @@ public class SwapService extends Service { swapPreferences.edit().putBoolean(SwapService.KEY_WIFI_ENABLED_BEFORE_SWAP, visible).apply(); } + public static boolean wasHotspotEnabledBeforeSwap() { + return swapPreferences.getBoolean(SwapService.KEY_HOTSPOT_ACTIVATED_BEFORE_SWAP, false); + } + + public static void putHotspotEnabledBeforeSwap(boolean visible) { + swapPreferences.edit().putBoolean(SwapService.KEY_HOTSPOT_ACTIVATED_BEFORE_SWAP, visible).apply(); + } + private static final int NOTIFICATION = 1; private final Binder binder = new Binder(); @@ -370,8 +389,19 @@ public class SwapService extends Service { localBroadcastManager.registerReceiver(bonjourPeerRemoved, new IntentFilter(BonjourManager.ACTION_REMOVED)); localBroadcastManager.registerReceiver(localRepoStatus, new IntentFilter(LocalRepoService.ACTION_STATUS)); + if (getHotspotActivatedUserPreference()) { + WifiApControl wifiApControl = WifiApControl.getInstance(this); + if (wifiApControl != null) { + wifiApControl.enable(); + } + } else if (getWifiVisibleUserPreference()) { + if (wifiManager != null) { + wifiManager.setWifiEnabled(true); + } + } + BonjourManager.start(this); - BonjourManager.setVisible(this, getWifiVisibleUserPreference()); + BonjourManager.setVisible(this, getWifiVisibleUserPreference() || getHotspotActivatedUserPreference()); } /** @@ -413,6 +443,15 @@ public class SwapService extends Service { wifiManager.setWifiEnabled(false); } + WifiApControl ap = WifiApControl.getInstance(this); + if (ap != null) { + if (wasHotspotEnabledBeforeSwap()) { + ap.enable(); + } else { + ap.disable(); + } + } + stopPollingConnectedSwapRepo(); if (timer != null) { @@ -504,7 +543,7 @@ public class SwapService extends Service { if (hasIp) { LocalHTTPDManager.restart(this); BonjourManager.restart(this); - BonjourManager.setVisible(this, getWifiVisibleUserPreference()); + BonjourManager.setVisible(this, getWifiVisibleUserPreference() || getHotspotActivatedUserPreference()); } else { BonjourManager.stop(this); LocalHTTPDManager.stop(this); diff --git a/app/src/full/java/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java b/app/src/full/java/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java index c4a5ed457..3958428ca 100644 --- a/app/src/full/java/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java +++ b/app/src/full/java/org/fdroid/fdroid/views/swap/SwapWorkflowActivity.java @@ -442,12 +442,20 @@ public class SwapWorkflowActivity extends AppCompatActivity { } private void setupWifiAP() { - WifiApControl ap = WifiApControl.getInstance(this); + WifiApControl wifiApControl = WifiApControl.getInstance(this); + if (wifiApControl == null) { + Log.e(TAG, "WiFi AP is null"); + Toast.makeText(this, R.string.swap_toast_could_not_enable_hotspot, Toast.LENGTH_LONG).show(); + return; + } + SwapService.putHotspotEnabledBeforeSwap(wifiApControl.isEnabled()); wifiManager.setWifiEnabled(false); - if (ap.enable()) { + if (wifiApControl.enable()) { Toast.makeText(this, R.string.swap_toast_hotspot_enabled, Toast.LENGTH_SHORT).show(); + SwapService.putHotspotActivatedUserPreference(true); } else { Toast.makeText(this, R.string.swap_toast_could_not_enable_hotspot, Toast.LENGTH_LONG).show(); + SwapService.putHotspotActivatedUserPreference(false); Log.e(TAG, "Could not enable WiFi AP."); } } @@ -986,7 +994,11 @@ public class SwapWorkflowActivity extends AppCompatActivity { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { Context context = getApplicationContext(); if (isChecked) { - wifiManager.setWifiEnabled(true); + if (wifiApControl != null && wifiApControl.isEnabled()) { + setupWifiAP(); + } else { + wifiManager.setWifiEnabled(true); + } BonjourManager.start(context); } BonjourManager.setVisible(context, isChecked);