From 5e8005db4250e991dbd1fe2c990357b5bf1f3459 Mon Sep 17 00:00:00 2001 From: Matthew Fritze Date: Mon, 2 Jul 2018 11:22:59 -0700 Subject: [PATCH] Conditionally block battery percantage from search The PowerUsageSummary fragment doesn't use the regular pattern of a static method to build preference controllers, which can be accessed by both DashboardFragment and BaseSearchIndexProvider, because it depends on the Activity & Fragment in the creation of the preference controllers. The correct & long-term solution here would be to move those dependencies out of the getPreferenceControllers method, such that the we could use a static buildPreferenceControllers method, as seen in DisplaySettings.java. In the mean time, we know that BatteryPercentagePrefController should not show up on devices, so we conditionally add the the preference controller's into getNonIndexableKeys method based on controller Availability, which BasePreferenceController normally does automatically with a getPreferenceController method in the host fragment's SEARCH_INDEX_DATA_PROVIDER. Since this is a short-term solution, it should not be merged into master, and thus I am not marking the bug as fixed. Bug: 110894466 Test: Robotests Change-Id: I06f814571d0b72fbf020dd11a9d23a9eb9907bfd Merged-In: I5993d332dbd218c981ef5432aebb735d0000f67a --- .../settings/fuelgauge/PowerUsageSummary.java | 6 ++++ .../fuelgauge/PowerUsageSummaryTest.java | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/com/android/settings/fuelgauge/PowerUsageSummary.java b/src/com/android/settings/fuelgauge/PowerUsageSummary.java index e4f7a726c17..eec2008021e 100644 --- a/src/com/android/settings/fuelgauge/PowerUsageSummary.java +++ b/src/com/android/settings/fuelgauge/PowerUsageSummary.java @@ -472,6 +472,12 @@ public class PowerUsageSummary extends PowerUsageBase implements OnLongClickList @Override public List getNonIndexableKeys(Context context) { List niks = super.getNonIndexableKeys(context); + + final BatteryPercentagePreferenceController controller = + new BatteryPercentagePreferenceController(context); + if (!controller.isAvailable()) { + niks.add(controller.getPreferenceKey()); + } niks.add(KEY_BATTERY_SAVER_SUMMARY); return niks; } diff --git a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java index 07341a14656..8e7edf310d8 100644 --- a/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java +++ b/tests/robotests/src/com/android/settings/fuelgauge/PowerUsageSummaryTest.java @@ -35,6 +35,7 @@ import static org.mockito.Mockito.when; import android.app.LoaderManager; import android.content.Context; import android.content.Intent; +import android.content.res.Resources; import android.os.Bundle; import android.util.SparseArray; import android.view.Menu; @@ -49,6 +50,7 @@ import com.android.settings.R; import com.android.settings.SettingsActivity; import com.android.settings.applications.LayoutPreference; import com.android.settings.dashboard.SummaryLoader; +import com.android.settings.display.BatteryPercentagePreferenceController; import com.android.settings.fuelgauge.anomaly.Anomaly; import com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController; import com.android.settings.testutils.FakeFeatureFactory; @@ -390,6 +392,34 @@ public class PowerUsageSummaryTest { .isEqualTo("3% - Phone will shut down soon"); } + @Test + public void percentageSettingAvailable_shouldNotBeHiddenInSearch() { + final Resources resources = spy(mRealContext.getResources()); + doReturn(true).when(resources).getBoolean(anyInt()); + doReturn(resources).when(mRealContext).getResources(); + final String prefKey = new BatteryPercentagePreferenceController(mRealContext) + .getPreferenceKey(); + + final List nonIndexableKeys = + PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mRealContext); + + assertThat(nonIndexableKeys).doesNotContain(prefKey); + } + + @Test + public void percentageSettingNotAvailable_shouldBeHiddenInSearch() { + final Resources resources = spy(mRealContext.getResources()); + doReturn(false).when(resources).getBoolean(anyInt()); + doReturn(resources).when(mRealContext).getResources(); + final String prefKey = new BatteryPercentagePreferenceController(mRealContext) + .getPreferenceKey(); + + final List nonIndexableKeys = + PowerUsageSummary.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(mRealContext); + + assertThat(nonIndexableKeys).contains(prefKey); + } + public static class TestFragment extends PowerUsageSummary { private Context mContext;