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:
Hans-Christoph Steiner 2018-01-29 22:46:59 +01:00
parent 6d011c3895
commit 8a8ca2e6f7
4 changed files with 19 additions and 16 deletions

View File

@ -386,12 +386,12 @@ public class FDroidApp extends Application {
ImageLoader.getInstance().init(config); ImageLoader.getInstance().init(config);
FDroidApp.initWifiSettings(); FDroidApp.initWifiSettings();
startService(new Intent(this, WifiStateChangeService.class)); WifiStateChangeService.start(this, null);
// 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
public void onPreferenceChange() { public void onPreferenceChange() {
startService(new Intent(FDroidApp.this, WifiStateChangeService.class)); WifiStateChangeService.start(getApplicationContext(), null);
} }
}); });

View File

@ -2,23 +2,16 @@ package org.fdroid.fdroid.localrepo.type;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.util.Log; import android.util.Log;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.localrepo.SwapService; import org.fdroid.fdroid.localrepo.SwapService;
import org.fdroid.fdroid.net.LocalHTTPD; import org.fdroid.fdroid.net.LocalHTTPD;
import org.fdroid.fdroid.net.WifiStateChangeService; import org.fdroid.fdroid.net.WifiStateChangeService;
import java.io.IOException;
import java.net.BindException;
import java.util.Random;
import rx.Single; import rx.Single;
import rx.SingleSubscriber; import rx.SingleSubscriber;
import rx.android.schedulers.AndroidSchedulers; import rx.android.schedulers.AndroidSchedulers;
@ -26,6 +19,10 @@ import rx.functions.Action1;
import rx.functions.Func2; import rx.functions.Func2;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
import java.io.IOException;
import java.net.BindException;
import java.util.Random;
@SuppressWarnings("LineLength") @SuppressWarnings("LineLength")
public class WifiSwap extends SwapType { public class WifiSwap extends SwapType {
@ -143,7 +140,7 @@ public class WifiSwap extends SwapType {
} catch (BindException e) { } catch (BindException e) {
int prev = FDroidApp.port; int prev = FDroidApp.port;
FDroidApp.port = FDroidApp.port + new Random().nextInt(1111); 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 + "!")); singleSubscriber.onError(new Exception("port " + prev + " occupied, trying on " + FDroidApp.port + "!"));
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Could not start local repo HTTP server", e); Log.e(TAG, "Could not start local repo HTTP server", e);

View File

@ -1,16 +1,17 @@
package org.fdroid.fdroid.net; package org.fdroid.fdroid.net;
import android.app.IntentService; import android.app.IntentService;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.DhcpInfo; import android.net.DhcpInfo;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.support.annotation.Nullable;
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 org.apache.commons.net.util.SubnetUtils; import org.apache.commons.net.util.SubnetUtils;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences; 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 * which is how it can be triggered by code, or it came in from the system
* via {@link org.fdroid.fdroid.receiver.WifiStateChangeReceiver}, in * via {@link org.fdroid.fdroid.receiver.WifiStateChangeReceiver}, in
* which case an instance of {@link NetworkInfo} is included. * which case an instance of {@link NetworkInfo} is included.
* * <p>
* The work is done in a {@link Thread} so that new incoming {@code Intents} * 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 * are not blocked by processing. A new {@code Intent} immediately nullifies
* the current state because it means that something about the wifi has * the current state because it means that something about the wifi has
@ -54,6 +55,14 @@ public class WifiStateChangeService extends IntentService {
super("WifiStateChangeService"); 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 @Override
protected void onHandleIntent(Intent intent) { protected void onHandleIntent(Intent intent) {
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST); android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);

View File

@ -1,11 +1,9 @@
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.wifi.WifiManager; import android.net.wifi.WifiManager;
import org.fdroid.fdroid.Utils; import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.net.WifiStateChangeService; import org.fdroid.fdroid.net.WifiStateChangeService;
@ -15,8 +13,7 @@ public class WifiStateChangeReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) { if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
intent.setComponent(new ComponentName(context, WifiStateChangeService.class)); WifiStateChangeService.start(context, intent);
context.startService(intent);
} else { } else {
Utils.debugLog(TAG, "received unsupported Intent: " + intent); Utils.debugLog(TAG, "received unsupported Intent: " + intent);
} }