Move battery stats loading to AsyncLoader.
It takes 3-4 seconds to load the battery settings page in Ryu. Main reason is that it takes too long to init BatteryStatsHelper. This cl moves loading code to AsyncLoader, and we will refresh ui once the loading part is finished. Following cl will add animation for the battery header to make it not so janky Bug: 37196170 Test: RunSettingsRoboTest Change-Id: I40dfdde4a072e28a56c8fdf0ec6d671b5109fc6d
This commit is contained in:
@@ -46,8 +46,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="12dp"
|
||||
android:textAppearance="@android:style/TextAppearance.Material.Small"
|
||||
android:text="@string/estimated_time_left"/>
|
||||
android:textAppearance="@android:style/TextAppearance.Material.Small"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
@@ -41,6 +41,7 @@ import com.android.settings.Utils;
|
||||
import com.android.settings.applications.AppHeaderController;
|
||||
import com.android.settings.applications.LayoutPreference;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
|
||||
import com.android.settings.enterprise.DevicePolicyManagerWrapperImpl;
|
||||
import com.android.settings.overlay.FeatureFactory;
|
||||
@@ -56,7 +57,7 @@ import java.util.List;
|
||||
* 2. Battery related controls for app(i.e uninstall, force stop)
|
||||
*
|
||||
*/
|
||||
public class AdvancedPowerUsageDetail extends PowerUsageBase implements
|
||||
public class AdvancedPowerUsageDetail extends DashboardFragment implements
|
||||
ButtonActionDialogFragment.AppButtonsDialogListener {
|
||||
|
||||
public static final String TAG = "AdvancedPowerUsageDetail";
|
||||
|
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* 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 android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.utils.AsyncLoader;
|
||||
|
||||
/**
|
||||
* Loader to get new {@link BatteryStatsHelper} in the background
|
||||
*/
|
||||
public class BatteryStatsHelperLoader extends AsyncLoader<BatteryStatsHelper> {
|
||||
@VisibleForTesting
|
||||
UserManager mUserManager;
|
||||
private Bundle mBundle;
|
||||
|
||||
public BatteryStatsHelperLoader(Context context, Bundle bundle) {
|
||||
super(context);
|
||||
mBundle = bundle;
|
||||
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BatteryStatsHelper loadInBackground() {
|
||||
final BatteryStatsHelper statsHelper = new BatteryStatsHelper(getContext(), true);
|
||||
|
||||
initBatteryStatsHelper(statsHelper);
|
||||
return statsHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDiscardResult(BatteryStatsHelper result) {
|
||||
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void initBatteryStatsHelper(BatteryStatsHelper statsHelper) {
|
||||
statsHelper.create(mBundle);
|
||||
statsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, mUserManager.getUserProfiles());
|
||||
}
|
||||
}
|
@@ -126,7 +126,6 @@ public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
refreshStats();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -165,8 +164,11 @@ public class PowerUsageAdvanced extends PowerUsageBase {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshStats() {
|
||||
super.refreshStats();
|
||||
protected void refreshUi() {
|
||||
final Context context = getContext();
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
updatePreference(mHistPref);
|
||||
|
||||
|
@@ -16,28 +16,23 @@
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.app.LoaderManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.BatteryStats;
|
||||
import android.content.Loader;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.UserManager;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuInflater;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.utils.AsyncLoader;
|
||||
|
||||
/**
|
||||
* Common base class for things that need to show the battery usage graph.
|
||||
*/
|
||||
public abstract class PowerUsageBase extends DashboardFragment {
|
||||
public abstract class PowerUsageBase extends DashboardFragment
|
||||
implements LoaderManager.LoaderCallbacks<BatteryStatsHelper> {
|
||||
|
||||
// +1 to allow ordering for PowerUsageSummary.
|
||||
@VisibleForTesting
|
||||
@@ -62,27 +57,23 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
|
||||
mBatteryBroadcastReceiver = new BatteryBroadcastReceiver(getContext());
|
||||
mBatteryBroadcastReceiver.setBatteryChangedListener(() -> {
|
||||
if (!mHandler.hasMessages(MSG_REFRESH_STATS)) {
|
||||
mHandler.sendEmptyMessageDelayed(MSG_REFRESH_STATS, 500);
|
||||
}
|
||||
getLoaderManager().restartLoader(0, null, this);
|
||||
});
|
||||
|
||||
getLoaderManager().initLoader(0, icicle, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
mStatsHelper.clearStats();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
|
||||
BatteryStatsHelper.dropFile(getActivity(), BatteryHistoryDetail.BATTERY_HISTORY_FILE);
|
||||
mBatteryBroadcastReceiver.register();
|
||||
if (mHandler.hasMessages(MSG_REFRESH_STATS)) {
|
||||
mHandler.removeMessages(MSG_REFRESH_STATS);
|
||||
mStatsHelper.clearStats();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,7 +85,6 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
@Override
|
||||
public void onStop() {
|
||||
super.onStop();
|
||||
mHandler.removeMessages(MSG_REFRESH_STATS);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,26 +95,27 @@ public abstract class PowerUsageBase extends DashboardFragment {
|
||||
}
|
||||
}
|
||||
|
||||
protected void refreshStats() {
|
||||
mStatsHelper.refreshStats(BatteryStats.STATS_SINCE_CHARGED, mUm.getUserProfiles());
|
||||
}
|
||||
protected abstract void refreshUi();
|
||||
|
||||
protected void updatePreference(BatteryHistoryPreference historyPref) {
|
||||
historyPref.setStats(mStatsHelper);
|
||||
}
|
||||
|
||||
static final int MSG_REFRESH_STATS = 100;
|
||||
|
||||
private final Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
switch (msg.what) {
|
||||
case MSG_REFRESH_STATS:
|
||||
mStatsHelper.clearStats();
|
||||
refreshStats();
|
||||
break;
|
||||
public Loader<BatteryStatsHelper> onCreateLoader(int id,
|
||||
Bundle args) {
|
||||
return new BatteryStatsHelperLoader(getContext(), args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoadFinished(Loader<BatteryStatsHelper> loader,
|
||||
BatteryStatsHelper statsHelper) {
|
||||
mStatsHelper = statsHelper;
|
||||
refreshUi();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onLoaderReset(Loader<BatteryStatsHelper> loader) {
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -18,6 +18,8 @@ package com.android.settings.fuelgauge;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Build;
|
||||
@@ -142,7 +144,6 @@ public class PowerUsageSummary extends PowerUsageBase {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
refreshStats();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -236,7 +237,7 @@ public class PowerUsageSummary extends PowerUsageBase {
|
||||
} else {
|
||||
mStatsType = BatteryStats.STATS_SINCE_CHARGED;
|
||||
}
|
||||
refreshStats();
|
||||
refreshUi();
|
||||
return true;
|
||||
case MENU_HIGH_POWER_APPS:
|
||||
Bundle args = new Bundle();
|
||||
@@ -259,7 +260,7 @@ public class PowerUsageSummary extends PowerUsageBase {
|
||||
item.setTitle(mShowAllApps ? R.string.hide_extra_apps : R.string.show_all_apps);
|
||||
metricsFeatureProvider.action(context,
|
||||
MetricsEvent.ACTION_SETTINGS_MENU_BATTERY_APPS_TOGGLE, mShowAllApps);
|
||||
refreshStats();
|
||||
refreshUi();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
@@ -396,15 +397,11 @@ public class PowerUsageSummary extends PowerUsageBase {
|
||||
return results;
|
||||
}
|
||||
|
||||
protected void refreshStats() {
|
||||
super.refreshStats();
|
||||
|
||||
BatteryInfo.getBatteryInfo(getContext(), new BatteryInfo.Callback() {
|
||||
@Override
|
||||
public void onBatteryInfoLoaded(BatteryInfo info) {
|
||||
updateHeaderPreference(info);
|
||||
protected void refreshUi() {
|
||||
final Context context = getContext();
|
||||
if (context == null) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
cacheRemoveAllPrefs(mAppListGroup);
|
||||
mAppListGroup.setOrderingAsAdded(false);
|
||||
@@ -413,7 +410,13 @@ public class PowerUsageSummary extends PowerUsageBase {
|
||||
final PowerProfile powerProfile = mStatsHelper.getPowerProfile();
|
||||
final BatteryStats stats = mStatsHelper.getStats();
|
||||
final double averagePower = powerProfile.getAveragePower(PowerProfile.POWER_SCREEN_FULL);
|
||||
final Context context = getContext();
|
||||
|
||||
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
|
||||
Intent batteryBroadcast = context.registerReceiver(null,
|
||||
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
|
||||
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
|
||||
mStatsHelper.getStats(), elapsedRealtimeUs, false);
|
||||
updateHeaderPreference(batteryInfo);
|
||||
|
||||
final TypedValue value = new TypedValue();
|
||||
context.getTheme().resolveAttribute(android.R.attr.colorControlNormal, value, true);
|
||||
|
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* 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.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.BatteryStats;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserManager;
|
||||
|
||||
import com.android.internal.os.BatteryStatsHelper;
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class BatteryStatsHelperLoaderTest {
|
||||
@Mock
|
||||
private BatteryStatsHelper mBatteryStatsHelper;
|
||||
@Mock
|
||||
private Bundle mBundle;
|
||||
@Mock
|
||||
private Context mContext;
|
||||
@Mock
|
||||
private UserManager mUserManager;
|
||||
private BatteryStatsHelperLoader mLoader;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mLoader = new BatteryStatsHelperLoader(mContext, mBundle);
|
||||
mLoader.mUserManager = mUserManager;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitBatteryStatsHelper_init() {
|
||||
mLoader.initBatteryStatsHelper(mBatteryStatsHelper);
|
||||
|
||||
verify(mBatteryStatsHelper).create(mBundle);
|
||||
verify(mBatteryStatsHelper).refreshStats(BatteryStats.STATS_SINCE_CHARGED,
|
||||
mUserManager.getUserProfiles());
|
||||
}
|
||||
}
|
@@ -486,7 +486,7 @@ public class PowerUsageSummaryTest {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void refreshStats() {
|
||||
protected void refreshUi() {
|
||||
// Leave it empty for toggle apps menu test
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user