Merge "Increase number of lines before the fold to make sure Now Playing is visible." into pi-dev
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 2c8f961..4c4b6e9 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -107,7 +107,8 @@
     BatteryHeaderPreferenceController mBatteryHeaderPreferenceController;
     @VisibleForTesting
     boolean mNeedUpdateBatteryTip;
-    private BatteryTipPreferenceController mBatteryTipPreferenceController;
+    @VisibleForTesting
+    BatteryTipPreferenceController mBatteryTipPreferenceController;
     private int mStatsType = BatteryStats.STATS_SINCE_CHARGED;
 
     @VisibleForTesting
@@ -213,8 +214,8 @@
         mAnomalySparseArray = new SparseArray<>();
 
         restartBatteryInfoLoader();
-        mNeedUpdateBatteryTip = icicle == null;
         mBatteryTipPreferenceController.restoreInstanceState(icicle);
+        updateBatteryTipFlag(icicle);
     }
 
     @Override
@@ -382,6 +383,11 @@
         }
     }
 
+    @VisibleForTesting
+    void updateBatteryTipFlag(Bundle icicle) {
+        mNeedUpdateBatteryTip = icicle == null || mBatteryTipPreferenceController.needUpdate();
+    }
+
     @Override
     public boolean onLongClick(View view) {
         showBothEstimates();
diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceController.java b/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceController.java
index 249bf9b..784c54c 100644
--- a/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceController.java
+++ b/src/com/android/settings/fuelgauge/batterytip/BatteryTipPreferenceController.java
@@ -52,6 +52,7 @@
     private Map<String, BatteryTip> mBatteryTipMap;
     private SettingsActivity mSettingsActivity;
     private MetricsFeatureProvider mMetricsFeatureProvider;
+    private boolean mNeedUpdate;
     @VisibleForTesting
     PreferenceGroup mPreferenceGroup;
     @VisibleForTesting
@@ -71,6 +72,7 @@
         mFragment = fragment;
         mSettingsActivity = settingsActivity;
         mMetricsFeatureProvider = FeatureFactory.getFactory(context).getMetricsFeatureProvider();
+        mNeedUpdate = true;
     }
 
     @Override
@@ -111,6 +113,7 @@
                 mBatteryTipMap.put(preference.getKey(), batteryTip);
                 mPreferenceGroup.addPreference(preference);
                 batteryTip.log(mContext, mMetricsFeatureProvider);
+                mNeedUpdate = batteryTip.needUpdate();
                 break;
             }
         }
@@ -153,6 +156,10 @@
         outState.putParcelableList(KEY_BATTERY_TIPS, mBatteryTips);
     }
 
+    public boolean needUpdate() {
+        return mNeedUpdate;
+    }
+
     /**
      * Listener to give the control back to target fragment
      */
diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTip.java
index 3c3a5c0..f02dd72 100644
--- a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTip.java
+++ b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTip.java
@@ -86,17 +86,23 @@
     protected int mType;
     protected int mState;
     protected boolean mShowDialog;
+    /**
+     * Whether we need to update battery tip when configuration change
+     */
+    protected boolean mNeedUpdate;
 
     BatteryTip(Parcel in) {
         mType = in.readInt();
         mState = in.readInt();
         mShowDialog = in.readBoolean();
+        mNeedUpdate = in.readBoolean();
     }
 
     BatteryTip(int type, int state, boolean showDialog) {
         mType = type;
         mState = state;
         mShowDialog = showDialog;
+        mNeedUpdate = true;
     }
 
     @Override
@@ -109,6 +115,7 @@
         dest.writeInt(mType);
         dest.writeInt(mState);
         dest.writeBoolean(mShowDialog);
+        dest.writeBoolean(mNeedUpdate);
     }
 
     public abstract CharSequence getTitle(Context context);
@@ -144,6 +151,10 @@
         return mShowDialog;
     }
 
+    public boolean needUpdate() {
+        return mNeedUpdate;
+    }
+
     public String getKey() {
         return KEY_PREFIX + mType;
     }
diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
index 8b16166..9aa8363 100644
--- a/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
+++ b/src/com/android/settings/fuelgauge/batterytip/tips/RestrictAppTip.java
@@ -42,12 +42,14 @@
     public RestrictAppTip(@StateType int state, List<AppInfo> restrictApps) {
         super(TipType.APP_RESTRICTION, state, state == StateType.NEW /* showDialog */);
         mRestrictAppList = restrictApps;
+        mNeedUpdate = false;
     }
 
     public RestrictAppTip(@StateType int state, AppInfo appInfo) {
         super(TipType.APP_RESTRICTION, state, state == StateType.NEW /* showDialog */);
         mRestrictAppList = new ArrayList<>();
         mRestrictAppList.add(appInfo);
+        mNeedUpdate = false;
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/notification/AppNotificationSettings.java b/src/com/android/settings/notification/AppNotificationSettings.java
index c028298..6776931 100644
--- a/src/com/android/settings/notification/AppNotificationSettings.java
+++ b/src/com/android/settings/notification/AppNotificationSettings.java
@@ -125,7 +125,7 @@
         mControllers.add(new VibrationPreferenceController(context, mBackend));
         mControllers.add(new VisibilityPreferenceController(context, new LockPatternUtils(context),
                 mBackend));
-        mControllers.add(new DndPreferenceController(context, getLifecycle(), mBackend));
+        mControllers.add(new DndPreferenceController(context, mBackend));
         mControllers.add(new AppLinkPreferenceController(context));
         mControllers.add(new DescriptionPreferenceController(context));
         mControllers.add(new NotificationsOffPreferenceController(context));
diff --git a/src/com/android/settings/notification/ChannelNotificationSettings.java b/src/com/android/settings/notification/ChannelNotificationSettings.java
index a138429..bd9771a 100644
--- a/src/com/android/settings/notification/ChannelNotificationSettings.java
+++ b/src/com/android/settings/notification/ChannelNotificationSettings.java
@@ -18,13 +18,16 @@
 
 import android.content.Context;
 import android.content.Intent;
+import android.os.Bundle;
 import android.preference.PreferenceManager;
+import android.support.v7.preference.PreferenceScreen;
 import android.text.TextUtils;
 import android.util.Log;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.internal.widget.LockPatternUtils;
 import com.android.settings.R;
+import com.android.settings.applications.AppInfoBase;
 import com.android.settingslib.core.AbstractPreferenceController;
 
 import java.util.ArrayList;
@@ -39,6 +42,19 @@
     }
 
     @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        final PreferenceScreen screen = getPreferenceScreen();
+        Bundle args = getArguments();
+        // If linking to this screen from an external app, expand settings
+        if (screen != null && args != null) {
+            if (!args.getBoolean(ARG_FROM_SETTINGS, false)) {
+                screen.setInitialExpandedChildrenCount(Integer.MAX_VALUE);
+            }
+        }
+    }
+
+    @Override
     public void onResume() {
         super.onResume();
         if (mUid < 0 || TextUtils.isEmpty(mPkg) || mPkgInfo == null || mChannel == null) {
@@ -92,7 +108,7 @@
                 mBackend));
         mControllers.add(new LightsPreferenceController(context, mBackend));
         mControllers.add(new BadgePreferenceController(context, mBackend));
-        mControllers.add(new DndPreferenceController(context, getLifecycle(), mBackend));
+        mControllers.add(new DndPreferenceController(context, mBackend));
         mControllers.add(new NotificationsOffPreferenceController(context));
         return new ArrayList<>(mControllers);
     }
diff --git a/src/com/android/settings/notification/DndPreferenceController.java b/src/com/android/settings/notification/DndPreferenceController.java
index d4c7a10..899c585 100644
--- a/src/com/android/settings/notification/DndPreferenceController.java
+++ b/src/com/android/settings/notification/DndPreferenceController.java
@@ -28,17 +28,12 @@
 import com.android.settingslib.core.lifecycle.events.OnResume;
 
 public class DndPreferenceController extends NotificationPreferenceController
-        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener,
-        LifecycleObserver {
+        implements PreferenceControllerMixin, Preference.OnPreferenceChangeListener {
 
     private static final String KEY_BYPASS_DND = "bypass_dnd";
 
-    public DndPreferenceController(Context context, Lifecycle lifecycle,
-            NotificationBackend backend) {
+    public DndPreferenceController(Context context, NotificationBackend backend) {
         super(context, backend);
-        if (lifecycle != null) {
-            lifecycle.addObserver(this);
-        }
     }
 
     @Override
diff --git a/src/com/android/settings/notification/NotificationSettingsBase.java b/src/com/android/settings/notification/NotificationSettingsBase.java
index 6436de6..7eb0ba4 100644
--- a/src/com/android/settings/notification/NotificationSettingsBase.java
+++ b/src/com/android/settings/notification/NotificationSettingsBase.java
@@ -58,6 +58,7 @@
 abstract public class NotificationSettingsBase extends DashboardFragment {
     private static final String TAG = "NotifiSettingsBase";
     private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
+    protected static final String ARG_FROM_SETTINGS = "fromSettings";
 
     protected PackageManager mPm;
     protected NotificationBackend mBackend = new NotificationBackend();
@@ -249,6 +250,7 @@
         channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mUid);
         channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mPkg);
         channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId());
+        channelArgs.putBoolean(ARG_FROM_SETTINGS, true);
         channelPref.setIntent(new SubSettingLauncher(getActivity())
                 .setDestination(ChannelNotificationSettings.class.getName())
                 .setArguments(channelArgs)
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
index 989c033..b20cf16 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java
@@ -49,6 +49,7 @@
 import com.android.settings.SettingsActivity;
 import com.android.settings.applications.LayoutPreference;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
+import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.SettingsRobolectricTestRunner;
 import com.android.settings.testutils.XmlTestUtils;
@@ -187,7 +188,7 @@
     }
 
     @Test
-    public void testUpdateLastFullChargePreference_noAverageTime_showLastFullChargeSummary() {
+    public void updateLastFullChargePreference_noAverageTime_showLastFullChargeSummary() {
         mFragment.mBatteryInfo = null;
         when(mFragment.getContext()).thenReturn(mRealContext);
         doReturn(TIME_SINCE_LAST_FULL_CHARGE_MS).when(
@@ -200,7 +201,7 @@
     }
 
     @Test
-    public void testUpdateLastFullChargePreference_hasAverageTime_showFullChargeLastSummary() {
+    public void updateLastFullChargePreference_hasAverageTime_showFullChargeLastSummary() {
         mFragment.mBatteryInfo = mBatteryInfo;
         mBatteryInfo.averageTimeToDischarge = TIME_SINCE_LAST_FULL_CHARGE_MS;
         when(mFragment.getContext()).thenReturn(mRealContext);
@@ -212,7 +213,7 @@
     }
 
     @Test
-    public void testNonIndexableKeys_MatchPreferenceKeys() {
+    public void nonIndexableKeys_MatchPreferenceKeys() {
         final Context context = RuntimeEnvironment.application;
         final List<String> niks =
             PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(context);
@@ -224,7 +225,7 @@
     }
 
     @Test
-    public void testPreferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
+    public void preferenceControllers_getPreferenceKeys_existInPreferenceScreen() {
         final Context context = RuntimeEnvironment.application;
         final PowerUsageSummary fragment = new PowerUsageSummary();
         final List<String> preferenceScreenKeys =
@@ -239,7 +240,7 @@
     }
 
     @Test
-    public void testUpdateAnomalySparseArray() {
+    public void updateAnomalySparseArray() {
         mFragment.mAnomalySparseArray = new SparseArray<>();
         final List<Anomaly> anomalies = new ArrayList<>();
         final Anomaly anomaly1 = new Anomaly.Builder().setUid(UID).build();
@@ -256,7 +257,7 @@
     }
 
     @Test
-    public void testRestartBatteryTipLoader() {
+    public void restartBatteryTipLoader() {
         //TODO: add policy logic here when BatteryTipPolicy is implemented
         doReturn(mLoaderManager).when(mFragment).getLoaderManager();
 
@@ -267,7 +268,7 @@
     }
 
     @Test
-    public void testShowBothEstimates_summariesAreBothModified() {
+    public void showBothEstimates_summariesAreBothModified() {
         when(mFeatureFactory.powerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(any()))
             .thenReturn(true);
         doAnswer(new Answer() {
@@ -296,7 +297,7 @@
     }
 
     @Test
-    public void testDebugMode() {
+    public void debugMode() {
         doReturn(true).when(mFeatureFactory.powerUsageFeatureProvider).isEstimateDebugEnabled();
         doReturn(new TextView(mRealContext)).when(mBatteryLayoutPref).findViewById(R.id.summary2);
 
@@ -315,7 +316,7 @@
     }
 
     @Test
-    public void testRestartBatteryStatsLoader_notClearHeader_quickUpdateNotInvoked() {
+    public void restartBatteryStatsLoader_notClearHeader_quickUpdateNotInvoked() {
         mFragment.mBatteryHeaderPreferenceController = mBatteryHeaderPreferenceController;
 
         mFragment.restartBatteryStatsLoader(false /* clearHeader */);
@@ -324,7 +325,7 @@
     }
 
     @Test
-    public void testOptionsMenu_advancedPageEnabled() {
+    public void optionsMenu_advancedPageEnabled() {
         when(mFeatureFactory.powerUsageFeatureProvider.isPowerAccountingToggleEnabled())
                 .thenReturn(true);
 
@@ -335,7 +336,7 @@
     }
 
     @Test
-    public void testOptionsMenu_clickAdvancedPage_fireIntent() {
+    public void optionsMenu_clickAdvancedPage_fireIntent() {
         final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
         doAnswer(invocation -> {
             // Get the intent in which it has the app info bundle
@@ -353,13 +354,27 @@
     }
 
     @Test
-    public void testRefreshUi_deviceRotate_doNotUpdateBatteryTip() {
-        mFragment.mNeedUpdateBatteryTip = false;
+    public void refreshUi_deviceRotate_doNotUpdateBatteryTip() {
+        mFragment.mBatteryTipPreferenceController = mock(BatteryTipPreferenceController.class);
+        when(mFragment.mBatteryTipPreferenceController.needUpdate()).thenReturn(false);
+        mFragment.updateBatteryTipFlag(new Bundle());
+
         mFragment.refreshUi();
 
         verify(mFragment, never()).restartBatteryTipLoader();
     }
 
+    @Test
+    public void refreshUi_tipNeedUpdate_updateBatteryTip() {
+        mFragment.mBatteryTipPreferenceController = mock(BatteryTipPreferenceController.class);
+        when(mFragment.mBatteryTipPreferenceController.needUpdate()).thenReturn(true);
+        mFragment.updateBatteryTipFlag(new Bundle());
+
+        mFragment.refreshUi();
+
+        verify(mFragment).restartBatteryTipLoader();
+    }
+
     public static class TestFragment extends PowerUsageSummary {
         private Context mContext;
 
diff --git a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
index cee647e..405e761 100644
--- a/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
+++ b/tests/robotests/src/com/android/settings/fuelgauge/batterytip/tips/BatteryTipTest.java
@@ -74,6 +74,7 @@
         assertThat(parcelTip.getTitle(mContext)).isEqualTo(TITLE);
         assertThat(parcelTip.getSummary(mContext)).isEqualTo(SUMMARY);
         assertThat(parcelTip.getIconId()).isEqualTo(ICON_ID);
+        assertThat(parcelTip.needUpdate()).isTrue();
     }
 
     @Test
diff --git a/tests/robotests/src/com/android/settings/notification/DndPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/notification/DndPreferenceControllerTest.java
index 54dda6a..ba612ac 100644
--- a/tests/robotests/src/com/android/settings/notification/DndPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/notification/DndPreferenceControllerTest.java
@@ -64,8 +64,6 @@
     private UserManager mUm;
     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private PreferenceScreen mScreen;
-    @Mock
-    private Lifecycle mLifecycle;
 
     private DndPreferenceController mController;
 
@@ -76,7 +74,7 @@
         shadowApplication.setSystemService(Context.NOTIFICATION_SERVICE, mNm);
         shadowApplication.setSystemService(Context.USER_SERVICE, mUm);
         mContext = RuntimeEnvironment.application;
-        mController = spy(new DndPreferenceController(mContext, mLifecycle, mBackend));
+        mController = spy(new DndPreferenceController(mContext, mBackend));
     }
 
     @Test