WifiStateChangeService: use Intent static start method pattern
This is the standard pattern for starting IntentServices. It also makes it really easy to trace what is starting this Service.
This commit is contained in:
parent
6d011c3895
commit
8a8ca2e6f7
@ -386,12 +386,12 @@ public class FDroidApp extends Application {
|
||||
ImageLoader.getInstance().init(config);
|
||||
|
||||
FDroidApp.initWifiSettings();
|
||||
startService(new Intent(this, WifiStateChangeService.class));
|
||||
WifiStateChangeService.start(this, null);
|
||||
// if the HTTPS pref changes, then update all affected things
|
||||
Preferences.get().registerLocalRepoHttpsListeners(new ChangeListener() {
|
||||
@Override
|
||||
public void onPreferenceChange() {
|
||||
startService(new Intent(FDroidApp.this, WifiStateChangeService.class));
|
||||
WifiStateChangeService.start(getApplicationContext(), null);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -2,23 +2,16 @@ package org.fdroid.fdroid.localrepo.type;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.FDroidApp;
|
||||
import org.fdroid.fdroid.Preferences;
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.localrepo.SwapService;
|
||||
import org.fdroid.fdroid.net.LocalHTTPD;
|
||||
import org.fdroid.fdroid.net.WifiStateChangeService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.util.Random;
|
||||
|
||||
import rx.Single;
|
||||
import rx.SingleSubscriber;
|
||||
import rx.android.schedulers.AndroidSchedulers;
|
||||
@ -26,6 +19,10 @@ import rx.functions.Action1;
|
||||
import rx.functions.Func2;
|
||||
import rx.schedulers.Schedulers;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.BindException;
|
||||
import java.util.Random;
|
||||
|
||||
@SuppressWarnings("LineLength")
|
||||
public class WifiSwap extends SwapType {
|
||||
|
||||
@ -143,7 +140,7 @@ public class WifiSwap extends SwapType {
|
||||
} catch (BindException e) {
|
||||
int prev = FDroidApp.port;
|
||||
FDroidApp.port = FDroidApp.port + new Random().nextInt(1111);
|
||||
context.startService(new Intent(context, WifiStateChangeService.class));
|
||||
WifiStateChangeService.start(context, null);
|
||||
singleSubscriber.onError(new Exception("port " + prev + " occupied, trying on " + FDroidApp.port + "!"));
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Could not start local repo HTTP server", e);
|
||||
|
@ -1,16 +1,17 @@
|
||||
package org.fdroid.fdroid.net;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.DhcpInfo;
|
||||
import android.net.NetworkInfo;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.LocalBroadcastManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import org.apache.commons.net.util.SubnetUtils;
|
||||
import org.fdroid.fdroid.FDroidApp;
|
||||
import org.fdroid.fdroid.Preferences;
|
||||
@ -34,7 +35,7 @@ import java.util.Locale;
|
||||
* which is how it can be triggered by code, or it came in from the system
|
||||
* via {@link org.fdroid.fdroid.receiver.WifiStateChangeReceiver}, in
|
||||
* which case an instance of {@link NetworkInfo} is included.
|
||||
*
|
||||
* <p>
|
||||
* The work is done in a {@link Thread} so that new incoming {@code Intents}
|
||||
* are not blocked by processing. A new {@code Intent} immediately nullifies
|
||||
* the current state because it means that something about the wifi has
|
||||
@ -54,6 +55,14 @@ public class WifiStateChangeService extends IntentService {
|
||||
super("WifiStateChangeService");
|
||||
}
|
||||
|
||||
public static void start(Context context, @Nullable Intent intent) {
|
||||
if (intent == null) {
|
||||
intent = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||
}
|
||||
intent.setComponent(new ComponentName(context, WifiStateChangeService.class));
|
||||
context.startService(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
|
||||
|
@ -1,11 +1,9 @@
|
||||
package org.fdroid.fdroid.receiver;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.wifi.WifiManager;
|
||||
|
||||
import org.fdroid.fdroid.Utils;
|
||||
import org.fdroid.fdroid.net.WifiStateChangeService;
|
||||
|
||||
@ -15,8 +13,7 @@ public class WifiStateChangeReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
|
||||
intent.setComponent(new ComponentName(context, WifiStateChangeService.class));
|
||||
context.startService(intent);
|
||||
WifiStateChangeService.start(context, intent);
|
||||
} else {
|
||||
Utils.debugLog(TAG, "received unsupported Intent: " + intent);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user