Never store battery stats for cache
In PowerUsageBase, it will store battery stats if it has a configuration change, which will make BatteryStatsLoaderHelper never get the correct battery stats because it uses Bundle.EMPTY as the bundle message. This cl: 1. Remove the store action even though it is configuration change. 2. Always use null to get battery stats 3. Always start a battery status check in register() Bug: 63658232 Test: RunSettingsRoboTests Change-Id: Ifbf970c63378ed66dddcdae4d952b7d1fd84216a
This commit is contained in:
@@ -209,7 +209,7 @@ public class InstalledAppDetails extends AppInfoBase
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Loader<BatteryStatsHelper> onCreateLoader(int id, Bundle args) {
|
public Loader<BatteryStatsHelper> onCreateLoader(int id, Bundle args) {
|
||||||
return new BatteryStatsHelperLoader(getContext(), args);
|
return new BatteryStatsHelperLoader(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -50,11 +50,7 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
updateBatteryStatus(intent);
|
||||||
if (mBatteryListener != null && Intent.ACTION_BATTERY_CHANGED.equals(action)
|
|
||||||
&& updateBatteryStatus(intent)) {
|
|
||||||
mBatteryListener.onBatteryChanged();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setBatteryChangedListener(OnBatteryChangedListener lsn) {
|
public void setBatteryChangedListener(OnBatteryChangedListener lsn) {
|
||||||
@@ -62,26 +58,27 @@ public class BatteryBroadcastReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void register() {
|
public void register() {
|
||||||
mContext.registerReceiver(this,
|
final Intent intent = mContext.registerReceiver(this,
|
||||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||||
|
updateBatteryStatus(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void unRegister() {
|
public void unRegister() {
|
||||||
mContext.unregisterReceiver(this);
|
mContext.unregisterReceiver(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean updateBatteryStatus(Intent intent) {
|
private void updateBatteryStatus(Intent intent) {
|
||||||
if (intent != null) {
|
if (intent != null && mBatteryListener != null && Intent.ACTION_BATTERY_CHANGED.equals(
|
||||||
|
intent.getAction())) {
|
||||||
String batteryLevel = Utils.getBatteryPercentage(intent);
|
String batteryLevel = Utils.getBatteryPercentage(intent);
|
||||||
String batteryStatus = Utils.getBatteryStatus(
|
String batteryStatus = Utils.getBatteryStatus(
|
||||||
mContext.getResources(), intent);
|
mContext.getResources(), intent);
|
||||||
if (!batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(mBatteryStatus)) {
|
if (!batteryLevel.equals(mBatteryLevel) || !batteryStatus.equals(mBatteryStatus)) {
|
||||||
mBatteryLevel = batteryLevel;
|
mBatteryLevel = batteryLevel;
|
||||||
mBatteryStatus = batteryStatus;
|
mBatteryStatus = batteryStatus;
|
||||||
return true;
|
mBatteryListener.onBatteryChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -31,12 +31,13 @@ import com.android.settings.utils.AsyncLoader;
|
|||||||
public class BatteryStatsHelperLoader extends AsyncLoader<BatteryStatsHelper> {
|
public class BatteryStatsHelperLoader extends AsyncLoader<BatteryStatsHelper> {
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
UserManager mUserManager;
|
UserManager mUserManager;
|
||||||
private Bundle mBundle;
|
@VisibleForTesting
|
||||||
|
BatteryUtils mBatteryUtils;
|
||||||
|
|
||||||
public BatteryStatsHelperLoader(Context context, Bundle bundle) {
|
public BatteryStatsHelperLoader(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
mBundle = bundle;
|
|
||||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||||
|
mBatteryUtils = BatteryUtils.getInstance(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -44,9 +45,8 @@ public class BatteryStatsHelperLoader extends AsyncLoader<BatteryStatsHelper> {
|
|||||||
Context context = getContext();
|
Context context = getContext();
|
||||||
final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
|
final BatteryStatsHelper statsHelper = new BatteryStatsHelper(context,
|
||||||
true /* collectBatteryBroadcast */);
|
true /* collectBatteryBroadcast */);
|
||||||
|
mBatteryUtils.initBatteryStatsHelper(statsHelper, null /* bundle */, mUserManager);
|
||||||
|
|
||||||
BatteryUtils.getInstance(context).initBatteryStatsHelper(statsHelper, mBundle,
|
|
||||||
mUserManager);
|
|
||||||
return statsHelper;
|
return statsHelper;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,5 +55,4 @@ public class BatteryStatsHelperLoader extends AsyncLoader<BatteryStatsHelper> {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -370,6 +370,7 @@ public class BatteryUtils {
|
|||||||
public void initBatteryStatsHelper(BatteryStatsHelper statsHelper, Bundle bundle,
|
public void initBatteryStatsHelper(BatteryStatsHelper statsHelper, Bundle bundle,
|
||||||
UserManager userManager) {
|
UserManager userManager) {
|
||||||
statsHelper.create(bundle);
|
statsHelper.create(bundle);
|
||||||
|
statsHelper.clearStats();
|
||||||
statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, userManager.getUserProfiles());
|
statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, userManager.getUserProfiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -82,19 +82,6 @@ public abstract class PowerUsageBase extends DashboardFragment
|
|||||||
mBatteryBroadcastReceiver.unRegister();
|
mBatteryBroadcastReceiver.unRegister();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onStop() {
|
|
||||||
super.onStop();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onDestroy() {
|
|
||||||
super.onDestroy();
|
|
||||||
if (getActivity().isChangingConfigurations()) {
|
|
||||||
mStatsHelper.storeState();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void restartBatteryStatsLoader() {
|
protected void restartBatteryStatsLoader() {
|
||||||
getLoaderManager().restartLoader(0, Bundle.EMPTY, this);
|
getLoaderManager().restartLoader(0, Bundle.EMPTY, this);
|
||||||
}
|
}
|
||||||
@@ -108,7 +95,7 @@ public abstract class PowerUsageBase extends DashboardFragment
|
|||||||
@Override
|
@Override
|
||||||
public Loader<BatteryStatsHelper> onCreateLoader(int id,
|
public Loader<BatteryStatsHelper> onCreateLoader(int id,
|
||||||
Bundle args) {
|
Bundle args) {
|
||||||
return new BatteryStatsHelperLoader(getContext(), args);
|
return new BatteryStatsHelperLoader(getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -32,7 +32,11 @@ import org.robolectric.RuntimeEnvironment;
|
|||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@@ -52,7 +56,7 @@ public class BatteryBroadcastReceiverTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
|
||||||
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
|
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(mContext);
|
||||||
mBatteryBroadcastReceiver.mBatteryLevel = BATTERY_INIT_LEVEL;
|
mBatteryBroadcastReceiver.mBatteryLevel = BATTERY_INIT_LEVEL;
|
||||||
@@ -92,4 +96,17 @@ public class BatteryBroadcastReceiverTest {
|
|||||||
verify(mBatteryListener, never()).onBatteryChanged();
|
verify(mBatteryListener, never()).onBatteryChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister_updateBatteryStatus() {
|
||||||
|
doReturn(mChargingIntent).when(mContext).registerReceiver(any(), any());
|
||||||
|
|
||||||
|
mBatteryBroadcastReceiver.register();
|
||||||
|
|
||||||
|
assertThat(mBatteryBroadcastReceiver.mBatteryLevel).isEqualTo(
|
||||||
|
Utils.getBatteryPercentage(mChargingIntent));
|
||||||
|
assertThat(mBatteryBroadcastReceiver.mBatteryStatus).isEqualTo(
|
||||||
|
Utils.getBatteryStatus(mContext.getResources(), mChargingIntent));
|
||||||
|
verify(mBatteryListener).onBatteryChanged();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* 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.fuelgauge;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class BatteryStatsHelperLoaderTest {
|
||||||
|
@Mock
|
||||||
|
private BatteryUtils mBatteryUtils;
|
||||||
|
@Mock
|
||||||
|
private ConnectivityManager mConnectivityManager;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private BatteryStatsHelperLoader mBatteryStatsHelperLoader;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
|
doReturn(mConnectivityManager).when(mContext).getSystemService(
|
||||||
|
Context.CONNECTIVITY_SERVICE);
|
||||||
|
|
||||||
|
mBatteryStatsHelperLoader = spy(new BatteryStatsHelperLoader(mContext));
|
||||||
|
mBatteryStatsHelperLoader.mBatteryUtils = mBatteryUtils;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadInBackground_loadWithoutBundle() {
|
||||||
|
doReturn(mContext).when(mBatteryStatsHelperLoader).getContext();
|
||||||
|
mBatteryStatsHelperLoader.loadInBackground();
|
||||||
|
|
||||||
|
verify(mBatteryUtils).initBatteryStatsHelper(any(), eq(null), any());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user