Copy BatteryUsageLoaderService and BatteryBroadcastReceiver from

SettingsGoogle to Settings and rename them to BatteryUsageDataLoader and
BatteryUsageBroadcastReceiver.

Bug: 253395332
Test: make RunSettingsRoboTests
Change-Id: Ide7c572a7df826ca576223c297b8ec78c45cc94e
This commit is contained in:
Kuan Wang
2022-10-18 15:03:39 +08:00
parent 772c437de4
commit fb136dd8d5
5 changed files with 419 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/*
* 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 java.util.List;
/** Load battery usage data in the background. */
public final class BatteryUsageDataLoader {
private static final String TAG = "BatteryUsageDataLoader";
@VisibleForTesting
static BatteryAppListPreferenceController sController;
private BatteryUsageDataLoader() {
}
static void enqueueWork(Context context) {
AsyncTask.execute(() -> {
Log.d(TAG, "loadUsageDataSafely() in the AsyncTask");
loadUsageDataSafely(context.getApplicationContext());
});
}
@VisibleForTesting
static void loadUsageData(Context context) {
// Checks whether the battery content provider is available.
if (!DatabaseUtils.isContentProviderEnabled(context)) {
Log.w(TAG, "battery usage content provider is disabled!");
return;
}
final long start = System.currentTimeMillis();
final BatteryUsageStats batteryUsageStats = DataProcessor.getBatteryUsageStats(context);
final List<BatteryEntry> batteryEntryList =
DataProcessor.generateBatteryEntryListFromBatteryUsageStats(
context,
batteryUsageStats,
sController);
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));
// Uploads the BatteryEntry data into SettingsIntelligence.
DatabaseUtils.sendBatteryEntryData(
context, batteryEntryList, batteryUsageStats);
DataProcessor.closeBatteryUsageStats(batteryUsageStats);
}
private static void loadUsageDataSafely(Context context) {
try {
loadUsageData(context);
} catch (RuntimeException e) {
Log.e(TAG, "loadUsageData:" + e);
}
}
}