unified IP/Wifi state handling in WifiStateChangeService
To handle hotspots, this code will become more complicated. Therefore, first simplify things by putting all of the logic into one place, rather than spread out across FDroidApp, the receiver, and the service
This commit is contained in:
parent
2b59644e02
commit
edb3564e29
@ -33,7 +33,6 @@ import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Configuration;
|
||||
import android.net.Uri;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.os.Message;
|
||||
@ -239,10 +238,7 @@ public class FDroidApp extends Application {
|
||||
|
||||
// initialized the local repo information
|
||||
FDroidApp.initWifiSettings();
|
||||
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||
int wifiState = wifiManager.getWifiState();
|
||||
if (wifiState == WifiManager.WIFI_STATE_ENABLED)
|
||||
startService(new Intent(this, WifiStateChangeService.class));
|
||||
startService(new Intent(this, WifiStateChangeService.class));
|
||||
// if the HTTPS pref changes, then update all affected things
|
||||
Preferences.get().registerLocalRepoHttpsListeners(new ChangeListener() {
|
||||
@Override
|
||||
|
@ -21,26 +21,48 @@ import java.security.cert.Certificate;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WifiStateChangeService extends Service {
|
||||
private static final String TAG = "WifiStateChangeService";
|
||||
|
||||
public static final String BROADCAST = "org.fdroid.fdroid.action.WIFI_CHANGE";
|
||||
|
||||
private WifiManager wifiManager;
|
||||
private static WaitForWifiAsyncTask asyncTask;
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (asyncTask != null)
|
||||
asyncTask.cancel(true);
|
||||
asyncTask = new WaitForWifiAsyncTask();
|
||||
asyncTask.execute();
|
||||
NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||
if (ni == null) {
|
||||
// this app just started up, NetworkInfo is only passed via WifiStateChangeReceiver
|
||||
int wifiState = wifiManager.getWifiState();
|
||||
if (wifiState == WifiManager.WIFI_STATE_ENABLED) {
|
||||
startAsyncTask();
|
||||
}
|
||||
} else if (ni.isConnected()) {
|
||||
Log.i(TAG, "ni.isConnected()");
|
||||
startAsyncTask();
|
||||
} else {
|
||||
Log.i("WifiStateChangeReceiver", "ni != null && !ni.isConnected()");
|
||||
FDroidApp.initWifiSettings();
|
||||
}
|
||||
return START_NOT_STICKY;
|
||||
}
|
||||
|
||||
private void startAsyncTask() {
|
||||
Log.i(TAG, "startAsyncTask");
|
||||
if (asyncTask != null) {
|
||||
Log.i(TAG, "asyncTask.cancel");
|
||||
asyncTask.cancel(true);
|
||||
}
|
||||
asyncTask = new WaitForWifiAsyncTask();
|
||||
asyncTask.execute();
|
||||
}
|
||||
|
||||
public class WaitForWifiAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||
private static final String TAG = "WifiStateChangeService.WaitForWifiAsyncTask";
|
||||
private WifiManager wifiManager;
|
||||
private static final String TAG = "WaitForWifiAsyncTask";
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||
try {
|
||||
while (!wifiManager.isWifiEnabled()) {
|
||||
FDroidApp.initWifiSettings();
|
||||
|
@ -1,23 +1,17 @@
|
||||
package org.fdroid.fdroid.receiver;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import org.fdroid.fdroid.FDroidApp;
|
||||
import org.fdroid.fdroid.net.WifiStateChangeService;
|
||||
|
||||
public class WifiStateChangeReceiver extends BroadcastReceiver {
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||
if (ni.isConnected()) {
|
||||
context.startService(new Intent(context, WifiStateChangeService.class));
|
||||
} else {
|
||||
FDroidApp.initWifiSettings();
|
||||
}
|
||||
intent.setComponent(new ComponentName(context, WifiStateChangeService.class));
|
||||
context.startService(intent);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user