Merge "Fix search non-indexable update"
diff --git a/src/com/android/settings/core/PreferenceControllerListHelper.java b/src/com/android/settings/core/PreferenceControllerListHelper.java
index 220bd88..d955301 100644
--- a/src/com/android/settings/core/PreferenceControllerListHelper.java
+++ b/src/com/android/settings/core/PreferenceControllerListHelper.java
@@ -67,14 +67,20 @@
             try {
                 controller = BasePreferenceController.createInstance(context, controllerName);
             } catch (IllegalStateException e) {
+                Log.d(TAG, "Could not find Context-only controller for pref: " + controllerName);
                 final String key = metadata.getString(METADATA_KEY);
                 if (TextUtils.isEmpty(key)) {
                     Log.w(TAG, "Controller requires key but it's not defined in xml: "
                             + controllerName);
                     continue;
                 }
-                Log.d(TAG, "Could not find Context-only controller for pref: " + key);
-                controller = BasePreferenceController.createInstance(context, controllerName, key);
+                try {
+                    controller = BasePreferenceController.createInstance(context, controllerName,
+                            key);
+                } catch (IllegalStateException e2) {
+                    Log.w(TAG, "Cannot instantiate controller from reflection: " + controllerName);
+                    continue;
+                }
             }
             controllers.add(controller);
         }
diff --git a/src/com/android/settings/fuelgauge/BatterySaverController.java b/src/com/android/settings/fuelgauge/BatterySaverController.java
index 58b7d13..83efefd 100644
--- a/src/com/android/settings/fuelgauge/BatterySaverController.java
+++ b/src/com/android/settings/fuelgauge/BatterySaverController.java
@@ -18,6 +18,7 @@
 import android.content.Context;
 import android.database.ContentObserver;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.PowerManager;
 import android.provider.Settings;
 import android.support.annotation.VisibleForTesting;
@@ -119,7 +120,8 @@
         mBatterySaverPref.setSummary(getSummary());
     }
 
-    private final ContentObserver mObserver = new ContentObserver(new Handler()) {
+    private final ContentObserver mObserver = new ContentObserver(
+            new Handler(Looper.getMainLooper())) {
         @Override
         public void onChange(boolean selfChange) {
             updateSummary();
diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
index 07f4a2c..d3a48a2 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java
@@ -25,7 +25,6 @@
 import android.os.Bundle;
 import android.provider.SearchIndexableResource;
 import android.support.annotation.VisibleForTesting;
-import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceGroup;
 import android.text.format.Formatter;
 import android.util.SparseArray;
@@ -33,17 +32,14 @@
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.View;
-import android.view.View.OnClickListener;
 import android.view.View.OnLongClickListener;
 import android.widget.TextView;
 
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.R;
-import com.android.settings.Settings.HighPowerApplicationsActivity;
 import com.android.settings.SettingsActivity;
 import com.android.settings.Utils;
 import com.android.settings.applications.LayoutPreference;
-import com.android.settings.applications.manageapplications.ManageApplications;
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.display.BatteryPercentagePreferenceController;
 import com.android.settings.fuelgauge.anomaly.Anomaly;
@@ -56,7 +52,6 @@
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
 import com.android.settingslib.core.lifecycle.Lifecycle;
-
 import com.android.settingslib.utils.PowerUtil;
 import com.android.settingslib.utils.StringUtil;
 
@@ -242,11 +237,7 @@
                 KEY_BATTERY_TIP, (SettingsActivity) getActivity(), this /* fragment */, this /*
                 BatteryTipListener */);
         controllers.add(mBatteryTipPreferenceController);
-        BatterySaverController batterySaverController = new BatterySaverController(context);
-        controllers.add(batterySaverController);
         controllers.add(new BatteryPercentagePreferenceController(context));
-
-        lifecycle.addObserver(batterySaverController);
         return controllers;
     }
 
diff --git a/src/com/android/settings/search/BaseSearchIndexProvider.java b/src/com/android/settings/search/BaseSearchIndexProvider.java
index a2953b8..dbd28a3 100644
--- a/src/com/android/settings/search/BaseSearchIndexProvider.java
+++ b/src/com/android/settings/search/BaseSearchIndexProvider.java
@@ -78,8 +78,10 @@
                     ((BasePreferenceController) controller).updateNonIndexableKeys(
                             nonIndexableKeys);
                 } else {
-                    throw new IllegalStateException(controller.getClass().getName()
-                            + " must implement " + PreferenceControllerMixin.class.getName());
+                    Log.e(TAG, controller.getClass().getName()
+                            + " must implement " + PreferenceControllerMixin.class.getName()
+                            + " treating the key non-indexable");
+                    nonIndexableKeys.add(controller.getPreferenceKey());
                 }
             }
             return nonIndexableKeys;
diff --git a/tests/robotests/res/xml-mcc998/location_settings.xml b/tests/robotests/res/xml-mcc998/location_settings.xml
new file mode 100644
index 0000000..993af86
--- /dev/null
+++ b/tests/robotests/res/xml-mcc998/location_settings.xml
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  Copyright (C) 2018 The Android Open Source Project
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  -->
+
+<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
+                  xmlns:settings="http://schemas.android.com/apk/res-auto"
+                  android:key="fake_title_key"
+                  android:title="screen_title"
+                  settings:controller="com.android.settings.slices.FakePreferenceController">
+
+    <Preference
+        android:key="key"
+        android:title="title"
+        android:icon="@drawable/ic_android"
+        android:summary="summary"
+        settings:controller="com.android.settings.core.BadPreferenceController"/>
+
+</PreferenceScreen>
\ No newline at end of file
diff --git a/tests/robotests/src/com/android/settings/core/BadPreferenceController.java b/tests/robotests/src/com/android/settings/core/BadPreferenceController.java
new file mode 100644
index 0000000..e636723
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/core/BadPreferenceController.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.core;
+
+import android.content.Context;
+
+public class BadPreferenceController extends BasePreferenceController {
+
+    public BadPreferenceController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        throw new IllegalArgumentException("error");
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        return AVAILABLE;
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/core/PreferenceControllerListHelperTest.java b/tests/robotests/src/com/android/settings/core/PreferenceControllerListHelperTest.java
index c0bc3ef..4361f78 100644
--- a/tests/robotests/src/com/android/settings/core/PreferenceControllerListHelperTest.java
+++ b/tests/robotests/src/com/android/settings/core/PreferenceControllerListHelperTest.java
@@ -60,6 +60,17 @@
     }
 
     @Test
+    @Config(qualifiers = "mcc998")
+    public void getControllers_partialFailure_shouldReturnTheRest() {
+        final List<BasePreferenceController> controllers =
+                PreferenceControllerListHelper.getPreferenceControllersFromXml(mContext,
+                        R.xml.location_settings);
+
+        assertThat(controllers).hasSize(1);
+        assertThat(controllers.get(0)).isInstanceOf(FakePreferenceController.class);
+    }
+
+    @Test
     public void filterControllers_noFilter_shouldReturnSameList() {
         final List<BasePreferenceController> controllers = new ArrayList<>();
         controllers.add(new BasePreferenceController(mContext, "key") {