1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.fuelgauge; 18 19 import android.app.AppOpsManager; 20 import android.app.Dialog; 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.DialogInterface.OnClickListener; 25 import android.content.pm.PackageManager; 26 import android.content.pm.PackageManager.NameNotFoundException; 27 import android.os.Bundle; 28 import android.view.View; 29 import android.widget.Checkable; 30 import android.widget.TextView; 31 32 import androidx.annotation.VisibleForTesting; 33 import androidx.appcompat.app.AlertDialog; 34 import androidx.fragment.app.Fragment; 35 36 import com.android.settings.R; 37 import com.android.settings.applications.AppInfoBase; 38 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 39 import com.android.settings.overlay.FeatureFactory; 40 import com.android.settingslib.applications.ApplicationsState.AppEntry; 41 import com.android.settingslib.fuelgauge.PowerAllowlistBackend; 42 43 public class HighPowerDetail extends InstrumentedDialogFragment 44 implements OnClickListener, View.OnClickListener { 45 46 private static final String ARG_DEFAULT_ON = "default_on"; 47 48 @VisibleForTesting PowerAllowlistBackend mBackend; 49 @VisibleForTesting BatteryUtils mBatteryUtils; 50 @VisibleForTesting String mPackageName; 51 @VisibleForTesting int mPackageUid; 52 private CharSequence mLabel; 53 private boolean mDefaultOn; 54 @VisibleForTesting boolean mIsEnabled; 55 private Checkable mOptionOn; 56 private Checkable mOptionOff; 57 58 @Override getMetricsCategory()59 public int getMetricsCategory() { 60 return SettingsEnums.DIALOG_HIGH_POWER_DETAILS; 61 } 62 63 @Override onCreate(Bundle savedInstanceState)64 public void onCreate(Bundle savedInstanceState) { 65 super.onCreate(savedInstanceState); 66 final Context context = getContext(); 67 mBatteryUtils = BatteryUtils.getInstance(context); 68 mBackend = PowerAllowlistBackend.getInstance(context); 69 70 mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME); 71 mPackageUid = getArguments().getInt(AppInfoBase.ARG_PACKAGE_UID); 72 final PackageManager pm = context.getPackageManager(); 73 try { 74 mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm); 75 } catch (NameNotFoundException e) { 76 mLabel = mPackageName; 77 } 78 mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON); 79 mIsEnabled = mDefaultOn || mBackend.isAllowlisted(mPackageName, mPackageUid); 80 } 81 setup(View view, boolean on)82 public Checkable setup(View view, boolean on) { 83 ((TextView) view.findViewById(android.R.id.title)) 84 .setText(on ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off); 85 ((TextView) view.findViewById(android.R.id.summary)) 86 .setText( 87 on 88 ? R.string.ignore_optimizations_on_desc 89 : R.string.ignore_optimizations_off_desc); 90 view.setClickable(true); 91 view.setOnClickListener(this); 92 if (!on && mBackend.isSysAllowlisted(mPackageName)) { 93 view.setEnabled(false); 94 } 95 return (Checkable) view; 96 } 97 98 @Override onCreateDialog(Bundle savedInstanceState)99 public Dialog onCreateDialog(Bundle savedInstanceState) { 100 AlertDialog.Builder b = 101 new AlertDialog.Builder(getContext()) 102 .setTitle(mLabel) 103 .setNegativeButton(R.string.cancel, null) 104 .setView(R.layout.ignore_optimizations_content); 105 if (!mBackend.isSysAllowlisted(mPackageName)) { 106 b.setPositiveButton(R.string.done, this); 107 } 108 return b.create(); 109 } 110 111 @Override onStart()112 public void onStart() { 113 super.onStart(); 114 mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true); 115 mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false); 116 updateViews(); 117 } 118 updateViews()119 private void updateViews() { 120 mOptionOn.setChecked(mIsEnabled); 121 mOptionOff.setChecked(!mIsEnabled); 122 } 123 124 @Override onClick(View v)125 public void onClick(View v) { 126 if (v == mOptionOn) { 127 mIsEnabled = true; 128 updateViews(); 129 } else if (v == mOptionOff) { 130 mIsEnabled = false; 131 updateViews(); 132 } 133 } 134 135 @Override onClick(DialogInterface dialog, int which)136 public void onClick(DialogInterface dialog, int which) { 137 if (which == DialogInterface.BUTTON_POSITIVE) { 138 boolean newValue = mIsEnabled; 139 boolean oldValue = mBackend.isAllowlisted(mPackageName, mPackageUid); 140 if (newValue != oldValue) { 141 logSpecialPermissionChange(newValue, mPackageName, getContext()); 142 if (newValue) { 143 mBatteryUtils.setForceAppStandby( 144 mPackageUid, mPackageName, AppOpsManager.MODE_ALLOWED); 145 mBackend.addApp(mPackageName, mPackageUid); 146 } else { 147 mBackend.removeApp(mPackageName, mPackageUid); 148 } 149 } 150 } 151 } 152 153 @VisibleForTesting logSpecialPermissionChange(boolean allowlist, String packageName, Context context)154 static void logSpecialPermissionChange(boolean allowlist, String packageName, Context context) { 155 int logCategory = 156 allowlist 157 ? SettingsEnums.APP_SPECIAL_PERMISSION_BATTERY_DENY 158 : SettingsEnums.APP_SPECIAL_PERMISSION_BATTERY_ALLOW; 159 FeatureFactory.getFeatureFactory() 160 .getMetricsFeatureProvider() 161 .action(context, logCategory, packageName); 162 } 163 164 @Override onDismiss(DialogInterface dialog)165 public void onDismiss(DialogInterface dialog) { 166 super.onDismiss(dialog); 167 Fragment target = getTargetFragment(); 168 if (target != null && target.getActivity() != null) { 169 target.onActivityResult(getTargetRequestCode(), 0, null); 170 } 171 } 172 getSummary(Context context, AppEntry entry)173 public static CharSequence getSummary(Context context, AppEntry entry) { 174 return getSummary(context, entry.info.packageName, entry.info.uid); 175 } 176 getSummary(Context context, String pkg, int uid)177 public static CharSequence getSummary(Context context, String pkg, int uid) { 178 return getSummary(context, PowerAllowlistBackend.getInstance(context), pkg, uid); 179 } 180 181 @VisibleForTesting getSummary( Context context, PowerAllowlistBackend powerAllowlist, String pkg, int uid)182 static CharSequence getSummary( 183 Context context, PowerAllowlistBackend powerAllowlist, String pkg, int uid) { 184 return context.getString( 185 powerAllowlist.isSysAllowlisted(pkg) || powerAllowlist.isDefaultActiveApp(pkg, uid) 186 ? R.string.high_power_system 187 : powerAllowlist.isAllowlisted(pkg, uid) 188 ? R.string.high_power_on 189 : R.string.high_power_off); 190 } 191 show(Fragment caller, int uid, String packageName, int requestCode)192 public static void show(Fragment caller, int uid, String packageName, int requestCode) { 193 HighPowerDetail fragment = new HighPowerDetail(); 194 Bundle args = new Bundle(); 195 args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName); 196 args.putInt(AppInfoBase.ARG_PACKAGE_UID, uid); 197 fragment.setArguments(args); 198 fragment.setTargetFragment(caller, requestCode); 199 fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName()); 200 } 201 } 202