Add Apps > Battery optimization page implementation.
Moving the old restricted page to the new optimization page will happen
in a follow-up CL.
Test: Unit, manual
Bug: 238026672
Change-Id: I5fee9ebe03284a013da6bfca9ada8b166c6af91c
(cherry picked from commit 5ecb1a1d69
)
Merged-In: I5fee9ebe03284a013da6bfca9ada8b166c6af91c
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* 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.applications;
|
||||
|
||||
import android.annotation.IntDef;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.settingslib.applications.ApplicationsState;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppEntry;
|
||||
import com.android.settingslib.applications.ApplicationsState.AppFilter;
|
||||
import com.android.settingslib.fuelgauge.PowerAllowlistBackend;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* Class for bridging the Battery optimization information to ApplicationState.
|
||||
*/
|
||||
public class AppStateBatteryOptimizationBridge extends AppStateBaseBridge {
|
||||
private static final String TAG = AppStateBatteryOptimizationBridge.class.getSimpleName();
|
||||
static final boolean DEBUG = Build.IS_DEBUGGABLE;
|
||||
|
||||
private final Context mContext;
|
||||
private final AppOpsManager mAppOpsManager;
|
||||
private final PowerAllowlistBackend mPowerAllowlistBackend;
|
||||
|
||||
private static final int MODE_UNKNOWN = 0;
|
||||
private static final int MODE_UNRESTRICTED = 1;
|
||||
private static final int MODE_OPTIMIZED = 2;
|
||||
private static final int MODE_RESTRICTED = 3;
|
||||
|
||||
@IntDef(
|
||||
prefix = {"MODE_"},
|
||||
value = {
|
||||
MODE_UNKNOWN,
|
||||
MODE_RESTRICTED,
|
||||
MODE_UNRESTRICTED,
|
||||
MODE_OPTIMIZED,
|
||||
})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
@interface OptimizationMode {
|
||||
}
|
||||
|
||||
public AppStateBatteryOptimizationBridge(
|
||||
Context context, ApplicationsState appState, Callback callback) {
|
||||
super(appState, callback);
|
||||
mContext = context;
|
||||
mAppOpsManager = context.getSystemService(AppOpsManager.class);
|
||||
mPowerAllowlistBackend = PowerAllowlistBackend.getInstance(mContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void updateExtraInfo(AppEntry app, String pkg, int uid) {
|
||||
app.extraInfo = getAppBatteryOptimizationState(pkg, uid);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void loadAllExtraInfo() {
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Start loadAllExtraInfo()");
|
||||
}
|
||||
mAppSession.getAllApps().stream().forEach(appEntry ->
|
||||
updateExtraInfo(appEntry, appEntry.info.packageName, appEntry.info.uid));
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "End loadAllExtraInfo()");
|
||||
}
|
||||
}
|
||||
|
||||
protected Object getAppBatteryOptimizationState(String pkg, int uid) {
|
||||
// Restricted = AppOpsManager.MODE_IGNORED + !allowListed
|
||||
// Unrestricted = AppOpsManager.MODE_ALLOWED + allowListed
|
||||
// Optimized = AppOpsManager.MODE_ALLOWED + !allowListed
|
||||
|
||||
boolean allowListed = mPowerAllowlistBackend.isAllowlisted(pkg);
|
||||
int aomMode =
|
||||
mAppOpsManager.checkOpNoThrow(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, uid, pkg);
|
||||
@OptimizationMode int mode = MODE_UNKNOWN;
|
||||
String modeName = "";
|
||||
if (aomMode == AppOpsManager.MODE_IGNORED && !allowListed) {
|
||||
mode = MODE_RESTRICTED;
|
||||
if (DEBUG) {
|
||||
modeName = "RESTRICTED";
|
||||
}
|
||||
} else if (aomMode == AppOpsManager.MODE_ALLOWED) {
|
||||
mode = allowListed ? MODE_UNRESTRICTED : MODE_OPTIMIZED;
|
||||
if (DEBUG) {
|
||||
modeName = allowListed ? "UNRESTRICTED" : "OPTIMIZED";
|
||||
}
|
||||
}
|
||||
if (DEBUG) {
|
||||
Log.d(TAG, "Pkg: " + pkg + ", mode: " + modeName);
|
||||
}
|
||||
return new BatteryOptimizationDetails(mode);
|
||||
}
|
||||
|
||||
@OptimizationMode
|
||||
private static int getBatteryOptimizationDetailsMode(AppEntry entry) {
|
||||
if (entry == null || entry.extraInfo == null) {
|
||||
return MODE_UNKNOWN;
|
||||
}
|
||||
|
||||
return entry.extraInfo instanceof BatteryOptimizationDetails
|
||||
? ((BatteryOptimizationDetails) entry.extraInfo).mMode
|
||||
: MODE_UNKNOWN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by {@link com.android.settings.applications.manageapplications.AppFilterRegistry} to
|
||||
* determine which apps are unrestricted.
|
||||
*/
|
||||
public static final AppFilter FILTER_BATTERY_UNRESTRICTED_APPS =
|
||||
new AppFilter() {
|
||||
@Override
|
||||
public void init() {}
|
||||
|
||||
@Override
|
||||
public boolean filterApp(AppEntry info) {
|
||||
return getBatteryOptimizationDetailsMode(info) == MODE_UNRESTRICTED;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Used by {@link com.android.settings.applications.manageapplications.AppFilterRegistry} to
|
||||
* determine which apps are optimized.
|
||||
*/
|
||||
public static final AppFilter FILTER_BATTERY_OPTIMIZED_APPS =
|
||||
new AppFilter() {
|
||||
@Override
|
||||
public void init() {}
|
||||
|
||||
@Override
|
||||
public boolean filterApp(AppEntry info) {
|
||||
return getBatteryOptimizationDetailsMode(info) == MODE_OPTIMIZED;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Used by {@link com.android.settings.applications.manageapplications.AppFilterRegistry} to
|
||||
* determine which apps are restricted.
|
||||
*/
|
||||
public static final AppFilter FILTER_BATTERY_RESTRICTED_APPS =
|
||||
new AppFilter() {
|
||||
@Override
|
||||
public void init() {}
|
||||
|
||||
@Override
|
||||
public boolean filterApp(AppEntry info) {
|
||||
return getBatteryOptimizationDetailsMode(info) == MODE_RESTRICTED;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Extra details for battery optimization app data.
|
||||
*/
|
||||
static final class BatteryOptimizationDetails {
|
||||
@OptimizationMode
|
||||
int mMode;
|
||||
|
||||
BatteryOptimizationDetails(@OptimizationMode int mode) {
|
||||
mMode = mode;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user