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

Commit 1ce97c8e authored by Kuan Wang's avatar Kuan Wang
Browse files

Generate app and device screen-on time data and return back to UI for

rendering.

Test: make RunSettingsRoboTests + manually
Bug: 260964903
Change-Id: I2fd69b4686cc2e1413ad0eb43c07b6164e411411
parent a3df41ca
Loading
Loading
Loading
Loading
+54 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.batteryusage;

import androidx.annotation.Nullable;

import java.util.Locale;
import java.util.Map;

/** Wraps the battery usage data and device screen-on time data used for battery usage page. */
public class BatteryCallbackData {

    // The usage app data used for rendering app list.
    private final Map<Integer, Map<Integer, BatteryDiffData>> mBatteryUsageMap;
    // The device screen-on time data.
    private final Map<Integer, Map<Integer, Long>> mDeviceScreenOnTime;

    public BatteryCallbackData(
            @Nullable Map<Integer, Map<Integer, BatteryDiffData>> batteryUsageMap,
            @Nullable Map<Integer, Map<Integer, Long>> deviceScreenOnTime) {
        mBatteryUsageMap = batteryUsageMap;
        mDeviceScreenOnTime = deviceScreenOnTime;
    }

    public Map<Integer, Map<Integer, BatteryDiffData>> getBatteryUsageMap() {
        return mBatteryUsageMap;
    }

    public Map<Integer, Map<Integer, Long>> getDeviceScreenOnTime() {
        return mDeviceScreenOnTime;
    }

    @Override
    public String toString() {
        return String.format(Locale.ENGLISH,
                "batteryUsageMap: %s; deviceScreenOnTime: %s",
                mBatteryUsageMap,
                mDeviceScreenOnTime);
    }
}
+2 −2
Original line number Diff line number Diff line
@@ -210,8 +210,8 @@ public class BatteryChartPreferenceController extends AbstractPreferenceControll
        animateBatteryChartViewGroup();
        final BatteryLevelData batteryLevelData =
                DataProcessManager.getBatteryLevelData(mContext, mHandler, batteryHistoryMap,
                        batteryUsageMap -> {
                            mBatteryUsageMap = batteryUsageMap;
                        batteryCallbackData -> {
                            mBatteryUsageMap = batteryCallbackData.getBatteryUsageMap();
                            refreshUi();
                        });
        Log.d(TAG, "getBatteryLevelData: " + batteryLevelData);
+7 −1
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ public class BatteryDiffEntry {

    public long mForegroundUsageTimeInMs;
    public long mBackgroundUsageTimeInMs;
    public long mScreenOnTimeInMs;
    public double mConsumePower;
    public double mForegroundUsageConsumePower;
    public double mForegroundServiceUsageConsumePower;
@@ -83,6 +84,7 @@ public class BatteryDiffEntry {
            Context context,
            long foregroundUsageTimeInMs,
            long backgroundUsageTimeInMs,
            long screenOnTimeInMs,
            double consumePower,
            double foregroundUsageConsumePower,
            double foregroundServiceUsageConsumePower,
@@ -97,6 +99,7 @@ public class BatteryDiffEntry {
        mCachedUsageConsumePower = cachedUsageConsumePower;
        mForegroundUsageTimeInMs = foregroundUsageTimeInMs;
        mBackgroundUsageTimeInMs = backgroundUsageTimeInMs;
        mScreenOnTimeInMs = screenOnTimeInMs;
        mBatteryHistEntry = batteryHistEntry;
        mUserManager = context.getSystemService(UserManager.class);
    }
@@ -119,6 +122,7 @@ public class BatteryDiffEntry {
                this.mContext,
                this.mForegroundUsageTimeInMs,
                this.mBackgroundUsageTimeInMs,
                this.mScreenOnTimeInMs,
                this.mConsumePower,
                this.mForegroundUsageConsumePower,
                this.mForegroundServiceUsageConsumePower,
@@ -367,10 +371,12 @@ public class BatteryDiffEntry {
                        mForegroundUsageConsumePower, mForegroundServiceUsageConsumePower))
                .append(String.format("\n\tconsume power= background:%f cached:%f",
                        mBackgroundUsageConsumePower, mCachedUsageConsumePower))
                .append(String.format("\n\ttime= foreground:%s background:%s",
                .append(String.format("\n\ttime= foreground:%s background:%s screen-on:%s",
                        StringUtil.formatElapsedTime(mContext, (double) mForegroundUsageTimeInMs,
                                /*withSeconds=*/ true, /*collapseTimeUnit=*/ false),
                        StringUtil.formatElapsedTime(mContext, (double) mBackgroundUsageTimeInMs,
                                /*withSeconds=*/ true, /*collapseTimeUnit=*/ false),
                        StringUtil.formatElapsedTime(mContext, (double) mScreenOnTimeInMs,
                                /*withSeconds=*/ true, /*collapseTimeUnit=*/ false)))
                .append(String.format("\n\tpackage:%s|%s uid:%d userId:%d",
                        mBatteryHistEntry.mPackageName, getPackageName(),
+37 −15
Original line number Diff line number Diff line
@@ -330,9 +330,9 @@ public class DataProcessManager {
    }

    private void loadAndApplyBatteryMapFromServiceOnly() {
        new AsyncTask<Void, Void, Map<Integer, Map<Integer, BatteryDiffData>>>() {
        new AsyncTask<Void, Void, BatteryCallbackData>() {
            @Override
            protected Map<Integer, Map<Integer, BatteryDiffData>> doInBackground(Void... voids) {
            protected BatteryCallbackData doInBackground(Void... voids) {
                final long startTime = System.currentTimeMillis();
                final Map<Integer, Map<Integer, BatteryDiffData>> batteryUsageMap =
                        DataProcessor.getBatteryUsageMapFromStatsService(mContext);
@@ -340,18 +340,18 @@ public class DataProcessManager {
                Log.d(TAG, String.format(
                        "execute loadAndApplyBatteryMapFromServiceOnly size=%d in %d/ms",
                        batteryUsageMap.size(), (System.currentTimeMillis() - startTime)));
                return batteryUsageMap;
                return new BatteryCallbackData(batteryUsageMap, /*deviceScreenOnTime=*/ null);
            }

            @Override
            protected void onPostExecute(
                    final Map<Integer, Map<Integer, BatteryDiffData>> batteryUsageMap) {
                    final BatteryCallbackData batteryCallbackData) {
                // Set the unused variables to null.
                mContext = null;
                // Post results back to main thread to refresh UI.
                if (mHandler != null && mCallbackFunction != null) {
                    mHandler.post(() -> {
                        mCallbackFunction.onBatteryUsageMapLoaded(batteryUsageMap);
                        mCallbackFunction.onBatteryCallbackDataLoaded(batteryCallbackData);
                    });
                }
            }
@@ -391,12 +391,36 @@ public class DataProcessManager {
    }

    private void generateFinalDataAndApplyCallback() {
        // TODO: generate the final data including battery usage map and device screen-on time and
        // then apply the callback function.
        new AsyncTask<Void, Void, BatteryCallbackData>() {
            @Override
            protected BatteryCallbackData doInBackground(Void... voids) {
                final long startTime = System.currentTimeMillis();
                final Map<Integer, Map<Integer, BatteryDiffData>> batteryUsageMap =
                        DataProcessor.getBatteryUsageMap(
                                mContext, mHourlyBatteryLevelsPerDay, mBatteryHistoryMap,
                                mAppUsagePeriodMap);
                final Map<Integer, Map<Integer, Long>> deviceScreenOnTime =
                        DataProcessor.getDeviceScreenOnTime(mAppUsagePeriodMap);
                DataProcessor.loadLabelAndIcon(batteryUsageMap);
                Log.d(TAG, String.format("execute generateFinalDataAndApplyCallback in %d/ms",
                        (System.currentTimeMillis() - startTime)));
                return new BatteryCallbackData(batteryUsageMap, deviceScreenOnTime);
            }

            @Override
            protected void onPostExecute(final BatteryCallbackData batteryCallbackData) {
                // Set the unused variables to null.
                mContext = null;
                mHourlyBatteryLevelsPerDay = null;
                mBatteryHistoryMap = null;
                // Post results back to main thread to refresh UI.
                if (mHandler != null && mCallbackFunction != null) {
                    mHandler.post(() -> {
                        mCallbackFunction.onBatteryCallbackDataLoaded(batteryCallbackData);
                    });
                }
            }
        }.execute();
    }

    // Whether we should load app usage data from service or database.
@@ -465,15 +489,13 @@ public class DataProcessManager {
            return null;
        }

        // TODO: replace the task below with new DataProcessManager(...).start() after
        // DataProcessManager is completed;
        // Start the async task to compute diff usage data and load labels and icons.
        new DataProcessor.ComputeUsageMapAndLoadItemsTask(
        new DataProcessManager(
                context,
                handler,
                asyncResponseDelegate,
                batteryLevelData.getHourlyBatteryLevelsPerDay(),
                processedBatteryHistoryMap).execute();
                processedBatteryHistoryMap).start();

        return batteryLevelData;
    }
+189 −77

File changed.

Preview size limit exceeded, changes collapsed.

Loading