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.pm.ResolveInfo;
|
||||||
import android.content.res.Configuration;
|
import android.content.res.Configuration;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.net.wifi.WifiManager;
|
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
@ -239,10 +238,7 @@ public class FDroidApp extends Application {
|
|||||||
|
|
||||||
// initialized the local repo information
|
// initialized the local repo information
|
||||||
FDroidApp.initWifiSettings();
|
FDroidApp.initWifiSettings();
|
||||||
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
startService(new Intent(this, WifiStateChangeService.class));
|
||||||
int wifiState = wifiManager.getWifiState();
|
|
||||||
if (wifiState == WifiManager.WIFI_STATE_ENABLED)
|
|
||||||
startService(new Intent(this, WifiStateChangeService.class));
|
|
||||||
// if the HTTPS pref changes, then update all affected things
|
// if the HTTPS pref changes, then update all affected things
|
||||||
Preferences.get().registerLocalRepoHttpsListeners(new ChangeListener() {
|
Preferences.get().registerLocalRepoHttpsListeners(new ChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -21,26 +21,48 @@ import java.security.cert.Certificate;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
public class WifiStateChangeService extends Service {
|
public class WifiStateChangeService extends Service {
|
||||||
|
private static final String TAG = "WifiStateChangeService";
|
||||||
|
|
||||||
public static final String BROADCAST = "org.fdroid.fdroid.action.WIFI_CHANGE";
|
public static final String BROADCAST = "org.fdroid.fdroid.action.WIFI_CHANGE";
|
||||||
|
|
||||||
|
private WifiManager wifiManager;
|
||||||
private static WaitForWifiAsyncTask asyncTask;
|
private static WaitForWifiAsyncTask asyncTask;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
if (asyncTask != null)
|
NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||||
asyncTask.cancel(true);
|
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
||||||
asyncTask = new WaitForWifiAsyncTask();
|
if (ni == null) {
|
||||||
asyncTask.execute();
|
// 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;
|
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> {
|
public class WaitForWifiAsyncTask extends AsyncTask<Void, Void, Void> {
|
||||||
private static final String TAG = "WifiStateChangeService.WaitForWifiAsyncTask";
|
private static final String TAG = "WaitForWifiAsyncTask";
|
||||||
private WifiManager wifiManager;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Void doInBackground(Void... params) {
|
protected Void doInBackground(Void... params) {
|
||||||
wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
|
|
||||||
try {
|
try {
|
||||||
while (!wifiManager.isWifiEnabled()) {
|
while (!wifiManager.isWifiEnabled()) {
|
||||||
FDroidApp.initWifiSettings();
|
FDroidApp.initWifiSettings();
|
||||||
|
@ -1,23 +1,17 @@
|
|||||||
package org.fdroid.fdroid.receiver;
|
package org.fdroid.fdroid.receiver;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.net.wifi.WifiManager;
|
|
||||||
|
|
||||||
import org.fdroid.fdroid.FDroidApp;
|
|
||||||
import org.fdroid.fdroid.net.WifiStateChangeService;
|
import org.fdroid.fdroid.net.WifiStateChangeService;
|
||||||
|
|
||||||
public class WifiStateChangeReceiver extends BroadcastReceiver {
|
public class WifiStateChangeReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
intent.setComponent(new ComponentName(context, WifiStateChangeService.class));
|
||||||
if (ni.isConnected()) {
|
context.startService(intent);
|
||||||
context.startService(new Intent(context, WifiStateChangeService.class));
|
|
||||||
} else {
|
|
||||||
FDroidApp.initWifiSettings();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user