Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 106dc459 authored by jackqdyulei's avatar jackqdyulei
Browse files

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
parent e5fa4cd4
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -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>

+2 −1
Original line number Diff line number Diff line
@@ -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";
+60 −0
Original line number Diff line number Diff line
/*
 * 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());
    }
}
+5 −3
Original line number Diff line number Diff line
@@ -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);

+26 −35
Original line number Diff line number Diff line
@@ -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) {

    }
}
Loading