Files
packages_apps_Settings/src/com/android/settings/fuelgauge/batteryusage/BatteryUsageDataLoader.java
ykhung ef66549e64 Add a mechanism to log battery usage periodic job events
Example history log:
      	Jul 07, 2023, 15:28:51 SCHEDULE_JOB triggerTime=Jul 07, 2023, 16:00:00
      	Jul 07, 2023, 15:32:16 FETCH_USAGE_DATA
      	Jul 07, 2023, 15:32:17 INSERT_USAGE_DATA size=37
      	Jul 07, 2023, 15:43:45 FETCH_USAGE_DATA
      	Jul 07, 2023, 15:43:48 INSERT_USAGE_DATA size=47
      	Jul 07, 2023, 15:43:49 SCHEDULE_JOB triggerTime=Jul 07, 2023, 16:00:00

Bug: 284893240
Test: make test RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.fuelgauge
Change-Id: I45a1ce0ce9b70f095702727e53d7b7ce8824abdb
2023-07-07 17:47:19 +08:00

84 lines
3.2 KiB
Java

/*
* 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 android.content.Context;
import android.os.AsyncTask;
import android.os.BatteryUsageStats;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import com.android.settings.fuelgauge.BatteryUsageHistoricalLogEntry.Action;
import com.android.settings.fuelgauge.batteryusage.bugreport.BatteryUsageLogUtils;
import java.util.List;
import java.util.function.Supplier;
/** Load battery usage data in the background. */
public final class BatteryUsageDataLoader {
private static final String TAG = "BatteryUsageDataLoader";
// For testing only.
@VisibleForTesting
static Supplier<List<BatteryEntry>> sFakeBatteryEntryListSupplier;
private BatteryUsageDataLoader() {
}
static void enqueueWork(final Context context, final boolean isFullChargeStart) {
AsyncTask.execute(() -> {
Log.d(TAG, "loadUsageDataSafely() in the AsyncTask");
loadUsageDataSafely(context.getApplicationContext(), isFullChargeStart);
});
}
@VisibleForTesting
static void loadUsageData(final Context context, final boolean isFullChargeStart) {
BatteryUsageLogUtils.writeLog(context, Action.FETCH_USAGE_DATA, "");
final long start = System.currentTimeMillis();
final BatteryUsageStats batteryUsageStats = DataProcessor.getBatteryUsageStats(context);
final List<BatteryEntry> batteryEntryList =
sFakeBatteryEntryListSupplier != null ? sFakeBatteryEntryListSupplier.get()
: DataProcessor.generateBatteryEntryListFromBatteryUsageStats(context,
batteryUsageStats);
if (batteryEntryList == null || batteryEntryList.isEmpty()) {
Log.w(TAG, "getBatteryEntryList() returns null or empty content");
}
final long elapsedTime = System.currentTimeMillis() - start;
Log.d(TAG, String.format("getBatteryUsageStats() in %d/ms", elapsedTime));
if (isFullChargeStart) {
DatabaseUtils.recordDateTime(
context, DatabaseUtils.KEY_LAST_LOAD_FULL_CHARGE_TIME);
}
// Uploads the BatteryEntry data into database.
DatabaseUtils.sendBatteryEntryData(
context, batteryEntryList, batteryUsageStats, isFullChargeStart);
DataProcessor.closeBatteryUsageStats(batteryUsageStats);
}
private static void loadUsageDataSafely(
final Context context, final boolean isFullChargeStart) {
try {
loadUsageData(context, isFullChargeStart);
} catch (RuntimeException e) {
Log.e(TAG, "loadUsageData:" + e);
}
}
}