Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0f01c849 authored by Doris Ling's avatar Doris Ling
Browse files

Only register conditions receiver when needed.

1. Update the dnd receiver to listen when dashboard summary running.
- remove the dnd receiver from Android manifest, and create it inside
the dnd condition.
- add lifecycle implementation to condition manager, so that the dnd
condition can know when to register and unregister the receiver.
- remove getReceiverClass() from dnd condition so that its receiver will
not be disabled by the default condition handling when condition is
silenced.

2. Remove all other conditions receiver from Android manifest.
- the broadcast receivers for HotspotCondition, AirplaneModeCondition,
CellularDataCondition from the manifest and create them inside the
condition classes.
- update Condition.onSilenceChanged() to register/unregister the
receivers instead of enable/disable the receiver class.

Change-Id: Iea6288382680df2b02884d1934b8db85daae404c
Fix: 35968517
Test: make RunSettingsRoboTests
parent 0f1d2bb8
Loading
Loading
Loading
Loading
+0 −33
Original line number Diff line number Diff line
@@ -3033,39 +3033,6 @@
            android:exported="true"
            android:permission="android.permission.DUMP" />

        <!-- Conditional receivers, only enabled during silenced state, default off-->
        <receiver
            android:name=".dashboard.conditional.HotspotCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.net.wifi.WIFI_AP_STATE_CHANGED" />
            </intent-filter>
       </receiver>

        <receiver
            android:name=".dashboard.conditional.AirplaneModeCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.intent.action.AIRPLANE_MODE" />
            </intent-filter>
       </receiver>

        <receiver
            android:name=".dashboard.conditional.DndCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL" />
            </intent-filter>
       </receiver>

        <receiver
            android:name=".dashboard.conditional.CellularDataCondition$Receiver"
            android:enabled="false">
            <intent-filter>
                 <action android:name="android.intent.action.ANY_DATA_STATE" />
            </intent-filter>
       </receiver>

        <!-- Quick Settings tiles for Developer Options -->
        <service
            android:name=".qstile.DevelopmentTiles$ShowLayout"
+1 −0
Original line number Diff line number Diff line
@@ -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());
+14 −2
Original line number Diff line number Diff line
@@ -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
+14 −2
Original line number Diff line number Diff line
@@ -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
+20 −25
Original line number Diff line number Diff line
@@ -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();

Loading