Fix enhanced estimate discharge text showing while charging

In the PowerUsageAdvanced screen the text showing whether your time
remaining information is enhanced or not should not show when the
device is plugged in. This CL fixes a bug in that logic that would
show the text when it wasn't supposed to.

Test: robotests still pass, manual verification
Bug: 63176050
Change-Id: I33fb31671cd9c29aed20483301e51ae1ded1f1b6
This commit is contained in:
Salvador Martinez
2017-07-13 14:20:28 -07:00
parent a7f5953a02
commit 8ccb5a4698
7 changed files with 79 additions and 23 deletions

View File

@@ -35,8 +35,9 @@ public class BatteryHistoryPreference extends Preference {
private CharSequence mSummary;
private TextView mSummaryView;
private boolean hideSummary;
@VisibleForTesting
boolean hideSummary;
@VisibleForTesting
BatteryInfo mBatteryInfo;
@@ -56,8 +57,10 @@ public class BatteryHistoryPreference extends Preference {
public void setBottomSummary(CharSequence text) {
mSummary = text;
if (mSummaryView != null) {
mSummaryView.setVisibility(View.VISIBLE);
mSummaryView.setText(mSummary);
}
hideSummary = false;
}
public void hideBottomSummary() {

View File

@@ -15,7 +15,10 @@ package com.android.settings.fuelgauge;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.BatteryManager;
import android.os.BatteryStats;
import android.os.Bundle;
import android.os.Handler;
@@ -171,7 +174,11 @@ public class PowerUsageAdvanced extends PowerUsageBase {
updatePreference(mHistPref);
refreshPowerUsageDataList(mStatsHelper, mUsageListGroup);
if (mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context)) {
Intent batteryIntent =
context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
final boolean plugged = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1) != 0;
if (mPowerUsageFeatureProvider.isEnhancedBatteryPredictionEnabled(context) && !plugged) {
mHistPref.setBottomSummary(
mPowerUsageFeatureProvider.getAdvancedUsageScreenInfoString());
} else {

View File

@@ -97,6 +97,7 @@ public class BatteryHistoryPreferenceTest {
TextView view = (TextView) mViewHolder.findViewById(R.id.bottom_summary);
assertThat(view.getVisibility()).isEqualTo(View.VISIBLE);
assertThat(view.getText()).isEqualTo(TEST_STRING);
assertThat(mBatteryHistoryPreference.hideSummary).isFalse();
}
@Test
@@ -107,5 +108,6 @@ public class BatteryHistoryPreferenceTest {
TextView view = (TextView) mViewHolder.findViewById(R.id.bottom_summary);
assertThat(view.getVisibility()).isEqualTo(View.GONE);
assertThat(view.getText()).isEqualTo("");
assertThat(mBatteryHistoryPreference.hideSummary).isTrue();
}
}

View File

@@ -30,6 +30,7 @@ import android.os.BatteryManager;
import android.os.BatteryStats;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.TestConfig;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
@@ -64,12 +65,7 @@ public class BatteryInfoLoaderTest {
.setupForTest(mContext)
.getPowerUsageFeatureProvider(mContext);
mDisChargingBatteryBroadcast = new Intent();
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 0);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_FULL);
mDisChargingBatteryBroadcast = BatteryTestUtils.getDischargingIntent();
doReturn(mContext).when(mContext).getApplicationContext();
when(mStats.computeBatteryTimeRemaining(anyLong())).thenReturn(TEST_TIME_REMAINING);

View File

@@ -39,6 +39,7 @@ import android.util.SparseIntArray;
import com.android.settings.TestConfig;
import com.android.settings.graph.UsageView;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -63,6 +64,7 @@ public class BatteryInfoTest {
private static final String STATUS_FULL = "Full";
private static final String STATUS_CHARGING_NO_TIME = "50% - charging";
private static final String STATUS_CHARGING_TIME = "50% - 0m until fully charged";
private static final String STATUS_NOT_CHARGING = "Not charging";
private static final int PLUGGED_IN = 1;
private static final long REMAINING_TIME_NULL = -1;
private static final long REMAINING_TIME = 2;
@@ -87,20 +89,9 @@ public class BatteryInfoTest {
mContext = spy(RuntimeEnvironment.application);
mFeatureFactory = FakeFeatureFactory.setupForTest(mContext);
mDisChargingBatteryBroadcast = new Intent();
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED, 0);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 0);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
mDisChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_FULL);
mDisChargingBatteryBroadcast = BatteryTestUtils.getDischargingIntent();
mChargingBatteryBroadcast = new Intent();
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_PLUGGED,
BatteryManager.BATTERY_PLUGGED_AC);
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_LEVEL, 50);
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_SCALE, 100);
mChargingBatteryBroadcast.putExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
mChargingBatteryBroadcast = BatteryTestUtils.getChargingIntent();
}
@Test
@@ -110,7 +101,7 @@ public class BatteryInfoTest {
mDisChargingBatteryBroadcast, mBatteryStats, SystemClock.elapsedRealtime() * 1000,
true /* shortString */);
assertThat(info.statusLabel).isEqualTo(STATUS_FULL);
assertThat(info.statusLabel).isEqualTo(STATUS_NOT_CHARGING);
}
@Test

View File

@@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
@@ -28,16 +29,19 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.UserManager;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup;
import android.support.v7.preference.PreferenceManager;
import android.view.View;
import com.android.internal.os.BatterySipper;
import com.android.internal.os.BatterySipper.DrainType;
import com.android.internal.os.BatteryStatsHelper;
import com.android.settings.R;
import com.android.settings.testutils.BatteryTestUtils;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.Utils;
@@ -90,11 +94,12 @@ public class PowerUsageAdvancedTest {
private PowerUsageAdvanced mPowerUsageAdvanced;
private PowerUsageData mPowerUsageData;
private Context mShadowContext;
private Intent mDischargingBatteryIntent;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mShadowContext = RuntimeEnvironment.application;
mShadowContext = spy(RuntimeEnvironment.application);
mPowerUsageAdvanced = spy(new PowerUsageAdvanced());
List<BatterySipper> batterySippers = new ArrayList<>();
@@ -107,6 +112,8 @@ public class PowerUsageAdvancedTest {
batterySippers.add(new BatterySipper(DrainType.WIFI, new FakeUid(FAKE_UID_1),
TYPE_WIFI_USAGE));
mDischargingBatteryIntent = BatteryTestUtils.getDischargingIntent();
doReturn(mDischargingBatteryIntent).when(mShadowContext).registerReceiver(any(), any());
when(mBatteryStatsHelper.getStats().getDischargeAmount(anyInt())).thenReturn(
DISCHARGE_AMOUNT);
when(mBatteryStatsHelper.getUsageList()).thenReturn(batterySippers);

View File

@@ -0,0 +1,50 @@
/*
* Copyright (C) 2017 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.testutils;
import android.content.Intent;
import android.os.BatteryManager;
public class BatteryTestUtils {
public static Intent getChargingIntent() {
return getCustomBatteryIntent(
BatteryManager.BATTERY_PLUGGED_AC,
50 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_CHARGING);
}
public static Intent getDischargingIntent() {
return getCustomBatteryIntent(
0 /* plugged */,
10 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_DISCHARGING);
}
public static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
Intent intent = new Intent();
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
intent.putExtra(BatteryManager.EXTRA_LEVEL, level);
intent.putExtra(BatteryManager.EXTRA_SCALE, scale);
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
return intent;
}
}