remember Hotspot state when using swap, restore on return

This commit is contained in:
Hans-Christoph Steiner 2018-04-13 00:27:44 +02:00
parent ca81518f74
commit abd61208d4
2 changed files with 56 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import android.support.v4.app.NotificationCompat;
import android.support.v4.content.LocalBroadcastManager; import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import cc.mvdan.accesspoint.WifiApControl;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.R; 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_APPS_TO_SWAP = "appsToSwap";
private static final String KEY_BLUETOOTH_ENABLED = "bluetoothEnabled"; private static final String KEY_BLUETOOTH_ENABLED = "bluetoothEnabled";
private static final String KEY_WIFI_ENABLED = "wifiEnabled"; 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_ENABLED_BEFORE_SWAP = "bluetoothEnabledBeforeSwap";
private static final String KEY_BLUETOOTH_NAME_BEFORE_SWAP = "bluetoothNameBeforeSwap"; 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_WIFI_ENABLED_BEFORE_SWAP = "wifiEnabledBeforeSwap";
private static final String KEY_HOTSPOT_ACTIVATED_BEFORE_SWAP = "hotspotEnabledBeforeSwap";
@NonNull @NonNull
private final Set<String> appsToSwap = new HashSet<>(); private final Set<String> appsToSwap = new HashSet<>();
@ -294,6 +297,14 @@ public class SwapService extends Service {
swapPreferences.edit().putBoolean(SwapService.KEY_WIFI_ENABLED, visible).apply(); 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() { public static boolean wasBluetoothEnabledBeforeSwap() {
return swapPreferences.getBoolean(SwapService.KEY_BLUETOOTH_ENABLED_BEFORE_SWAP, false); 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(); 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 static final int NOTIFICATION = 1;
private final Binder binder = new Binder(); 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(bonjourPeerRemoved, new IntentFilter(BonjourManager.ACTION_REMOVED));
localBroadcastManager.registerReceiver(localRepoStatus, new IntentFilter(LocalRepoService.ACTION_STATUS)); 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.start(this);
BonjourManager.setVisible(this, getWifiVisibleUserPreference()); BonjourManager.setVisible(this, getWifiVisibleUserPreference() || getHotspotActivatedUserPreference());
} }
/** /**
@ -413,6 +443,15 @@ public class SwapService extends Service {
wifiManager.setWifiEnabled(false); wifiManager.setWifiEnabled(false);
} }
WifiApControl ap = WifiApControl.getInstance(this);
if (ap != null) {
if (wasHotspotEnabledBeforeSwap()) {
ap.enable();
} else {
ap.disable();
}
}
stopPollingConnectedSwapRepo(); stopPollingConnectedSwapRepo();
if (timer != null) { if (timer != null) {
@ -504,7 +543,7 @@ public class SwapService extends Service {
if (hasIp) { if (hasIp) {
LocalHTTPDManager.restart(this); LocalHTTPDManager.restart(this);
BonjourManager.restart(this); BonjourManager.restart(this);
BonjourManager.setVisible(this, getWifiVisibleUserPreference()); BonjourManager.setVisible(this, getWifiVisibleUserPreference() || getHotspotActivatedUserPreference());
} else { } else {
BonjourManager.stop(this); BonjourManager.stop(this);
LocalHTTPDManager.stop(this); LocalHTTPDManager.stop(this);

View File

@ -442,12 +442,20 @@ public class SwapWorkflowActivity extends AppCompatActivity {
} }
private void setupWifiAP() { 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); wifiManager.setWifiEnabled(false);
if (ap.enable()) { if (wifiApControl.enable()) {
Toast.makeText(this, R.string.swap_toast_hotspot_enabled, Toast.LENGTH_SHORT).show(); Toast.makeText(this, R.string.swap_toast_hotspot_enabled, Toast.LENGTH_SHORT).show();
SwapService.putHotspotActivatedUserPreference(true);
} else { } else {
Toast.makeText(this, R.string.swap_toast_could_not_enable_hotspot, Toast.LENGTH_LONG).show(); 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."); Log.e(TAG, "Could not enable WiFi AP.");
} }
} }
@ -986,7 +994,11 @@ public class SwapWorkflowActivity extends AppCompatActivity {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Context context = getApplicationContext(); Context context = getApplicationContext();
if (isChecked) { if (isChecked) {
wifiManager.setWifiEnabled(true); if (wifiApControl != null && wifiApControl.isEnabled()) {
setupWifiAP();
} else {
wifiManager.setWifiEnabled(true);
}
BonjourManager.start(context); BonjourManager.start(context);
} }
BonjourManager.setVisible(context, isChecked); BonjourManager.setVisible(context, isChecked);