Merge "Only register conditions receiver when needed." into oc-dev
This commit is contained in:
@@ -92,6 +92,7 @@ public class DashboardSummary extends InstrumentedFragment
|
||||
mSummaryLoader = new SummaryLoader(activity, CategoryKey.CATEGORY_HOMEPAGE);
|
||||
|
||||
mConditionManager = ConditionManager.get(activity, false);
|
||||
getLifecycle().addObserver(mConditionManager);
|
||||
mSuggestionParser = new SuggestionParser(activity,
|
||||
activity.getSharedPreferences(SUGGESTIONS, 0), R.xml.suggestion_ordering);
|
||||
mSuggestionsChecks = new SuggestionsChecks(getContext());
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.android.settings.dashboard.conditional;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.util.Log;
|
||||
@@ -29,8 +30,14 @@ import com.android.settingslib.WirelessUtils;
|
||||
public class AirplaneModeCondition extends Condition {
|
||||
public static String TAG = "APM_Condition";
|
||||
|
||||
private final Receiver mReceiver;
|
||||
|
||||
private static final IntentFilter AIRPLANE_MODE_FILTER =
|
||||
new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
|
||||
|
||||
public AirplaneModeCondition(ConditionManager conditionManager) {
|
||||
super(conditionManager);
|
||||
mReceiver = new Receiver();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -40,8 +47,13 @@ public class AirplaneModeCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
protected BroadcastReceiver getReceiver() {
|
||||
return mReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IntentFilter getIntentFilter() {
|
||||
return AIRPLANE_MODE_FILTER;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -13,6 +13,7 @@ package com.android.settings.dashboard.conditional;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.telephony.TelephonyManager;
|
||||
@@ -23,8 +24,14 @@ import com.android.settings.Settings;
|
||||
|
||||
public class CellularDataCondition extends Condition {
|
||||
|
||||
private final Receiver mReceiver;
|
||||
|
||||
private static final IntentFilter DATA_CONNECTION_FILTER =
|
||||
new IntentFilter(TelephonyIntents.ACTION_ANY_DATA_CONNECTION_STATE_CHANGED);
|
||||
|
||||
public CellularDataCondition(ConditionManager manager) {
|
||||
super(manager);
|
||||
mReceiver = new Receiver();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -41,8 +48,13 @@ public class CellularDataCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
protected BroadcastReceiver getReceiver() {
|
||||
return mReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IntentFilter getIntentFilter() {
|
||||
return DATA_CONNECTION_FILTER;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -16,18 +16,17 @@
|
||||
|
||||
package com.android.settings.dashboard.conditional;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.PersistableBundle;
|
||||
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
|
||||
import static android.content.pm.PackageManager.DONT_KILL_APP;
|
||||
|
||||
public abstract class Condition {
|
||||
|
||||
private static final String KEY_SILENCE = "silence";
|
||||
@@ -49,12 +48,6 @@ public abstract class Condition {
|
||||
Condition(ConditionManager manager, MetricsFeatureProvider metricsFeatureProvider) {
|
||||
mManager = manager;
|
||||
mMetricsFeatureProvider = metricsFeatureProvider;
|
||||
Class<?> receiverClass = getReceiverClass();
|
||||
if (receiverClass != null && shouldAlwaysListenToBroadcast()) {
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), receiverClass),
|
||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED, DONT_KILL_APP);
|
||||
}
|
||||
}
|
||||
|
||||
void restoreState(PersistableBundle bundle) {
|
||||
@@ -110,29 +103,25 @@ public abstract class Condition {
|
||||
}
|
||||
}
|
||||
|
||||
private void onSilenceChanged(boolean silenced) {
|
||||
if (shouldAlwaysListenToBroadcast()) {
|
||||
// Don't try to disable BroadcastReceiver if we want it always on.
|
||||
@VisibleForTesting
|
||||
void onSilenceChanged(boolean silenced) {
|
||||
final BroadcastReceiver receiver = getReceiver();
|
||||
if (receiver == null) {
|
||||
return;
|
||||
}
|
||||
Class<?> clz = getReceiverClass();
|
||||
if (clz == null) {
|
||||
return;
|
||||
if (silenced) {
|
||||
mManager.getContext().registerReceiver(receiver, getIntentFilter());
|
||||
} else {
|
||||
mManager.getContext().unregisterReceiver(receiver);
|
||||
}
|
||||
// Only need to listen for changes when its been silenced.
|
||||
PackageManager pm = mManager.getContext().getPackageManager();
|
||||
pm.setComponentEnabledSetting(new ComponentName(mManager.getContext(), clz),
|
||||
silenced ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
|
||||
: PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||
DONT_KILL_APP);
|
||||
}
|
||||
|
||||
protected Class<?> getReceiverClass() {
|
||||
protected BroadcastReceiver getReceiver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected boolean shouldAlwaysListenToBroadcast() {
|
||||
return false;
|
||||
protected IntentFilter getIntentFilter() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean shouldShow() {
|
||||
@@ -143,6 +132,12 @@ public abstract class Condition {
|
||||
return mLastStateChange;
|
||||
}
|
||||
|
||||
public void onResume() {
|
||||
}
|
||||
|
||||
public void onPause() {
|
||||
}
|
||||
|
||||
// State.
|
||||
public abstract void refreshState();
|
||||
|
||||
|
||||
@@ -21,6 +21,9 @@ import android.os.PersistableBundle;
|
||||
import android.util.Log;
|
||||
import android.util.Xml;
|
||||
|
||||
import com.android.settings.core.lifecycle.LifecycleObserver;
|
||||
import com.android.settings.core.lifecycle.events.OnPause;
|
||||
import com.android.settings.core.lifecycle.events.OnResume;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
@@ -34,7 +37,7 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class ConditionManager {
|
||||
public class ConditionManager implements LifecycleObserver, OnResume, OnPause {
|
||||
|
||||
private static final String TAG = "ConditionManager";
|
||||
|
||||
@@ -228,6 +231,20 @@ public class ConditionManager {
|
||||
mListeners.remove(listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
for (int i = 0, size = mConditions.size(); i < size; i++) {
|
||||
mConditions.get(i).onResume();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
for (int i = 0, size = mConditions.size(); i < size; i++) {
|
||||
mConditions.get(i).onPause();
|
||||
}
|
||||
}
|
||||
|
||||
private class ConditionLoader extends AsyncTask<Void, Void, ArrayList<Condition>> {
|
||||
@Override
|
||||
protected ArrayList<Condition> doInBackground(Void... params) {
|
||||
|
||||
@@ -21,11 +21,13 @@ import android.app.StatusBarManager;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.PersistableBundle;
|
||||
import android.provider.Settings;
|
||||
import android.provider.Settings.Global;
|
||||
import android.service.notification.ZenModeConfig;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||
import com.android.settings.R;
|
||||
@@ -35,11 +37,21 @@ public class DndCondition extends Condition {
|
||||
private static final String TAG = "DndCondition";
|
||||
private static final String KEY_STATE = "state";
|
||||
|
||||
private boolean mRegistered;
|
||||
|
||||
@VisibleForTesting
|
||||
static final IntentFilter DND_FILTER =
|
||||
new IntentFilter(NotificationManager.ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL);
|
||||
|
||||
private int mZen;
|
||||
private ZenModeConfig mConfig;
|
||||
private final Receiver mReceiver;
|
||||
|
||||
public DndCondition(ConditionManager manager) {
|
||||
super(manager);
|
||||
mReceiver = new Receiver();
|
||||
mManager.getContext().registerReceiver(mReceiver, DND_FILTER);
|
||||
mRegistered = true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -68,11 +80,6 @@ public class DndCondition extends Condition {
|
||||
mZen = bundle.getInt(KEY_STATE, Global.ZEN_MODE_OFF);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
}
|
||||
|
||||
private CharSequence getZenState() {
|
||||
switch (mZen) {
|
||||
case Settings.Global.ZEN_MODE_ALARMS:
|
||||
@@ -149,7 +156,16 @@ public class DndCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldAlwaysListenToBroadcast() {
|
||||
return true;
|
||||
public void onResume() {
|
||||
if (!mRegistered) {
|
||||
mManager.getContext().registerReceiver(mReceiver, DND_FILTER);
|
||||
mRegistered = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
mManager.getContext().unregisterReceiver(mReceiver);
|
||||
mRegistered = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.android.settings.dashboard.conditional;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.wifi.WifiConfiguration;
|
||||
@@ -31,15 +32,19 @@ import com.android.settings.TetherSettings;
|
||||
import com.android.settings.Utils;
|
||||
import com.android.settingslib.RestrictedLockUtils;
|
||||
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
|
||||
import com.android.settingslib.TetherUtil;
|
||||
|
||||
public class HotspotCondition extends Condition {
|
||||
|
||||
private final WifiManager mWifiManager;
|
||||
private final Receiver mReceiver;
|
||||
|
||||
private static final IntentFilter WIFI_AP_STATE_FILTER =
|
||||
new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
|
||||
|
||||
public HotspotCondition(ConditionManager manager) {
|
||||
super(manager);
|
||||
mWifiManager = mManager.getContext().getSystemService(WifiManager.class);
|
||||
mReceiver = new Receiver();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -49,8 +54,13 @@ public class HotspotCondition extends Condition {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> getReceiverClass() {
|
||||
return Receiver.class;
|
||||
protected BroadcastReceiver getReceiver() {
|
||||
return mReceiver;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IntentFilter getIntentFilter() {
|
||||
return WIFI_AP_STATE_FILTER;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user