• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.android.settings.fuelgauge.BatteryOptimizeUtils.MODE_UNRESTRICTED;
20 
21 import android.Manifest;
22 import android.content.DialogInterface;
23 import android.content.pm.ApplicationInfo;
24 import android.content.pm.PackageItemInfo;
25 import android.content.pm.PackageManager;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.PowerManager;
29 import android.text.TextUtils;
30 import android.util.Log;
31 
32 import androidx.annotation.VisibleForTesting;
33 
34 import com.android.internal.app.AlertActivity;
35 import com.android.internal.app.AlertController;
36 import com.android.settings.R;
37 import com.android.settings.fuelgauge.BatteryOptimizeHistoricalLogEntry.Action;
38 
39 public class RequestIgnoreBatteryOptimizations extends AlertActivity
40         implements DialogInterface.OnClickListener {
41     private static final String TAG = "RequestIgnoreBatteryOptimizations";
42     private static final boolean DEBUG = false;
43 
44     @VisibleForTesting
45     static BatteryOptimizeUtils sTestBatteryOptimizeUtils = null;
46 
47     private ApplicationInfo mApplicationInfo;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     public void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         getWindow()
53                 .addSystemFlags(
54                         android.view.WindowManager.LayoutParams
55                                 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
56 
57         Uri data = getIntent().getData();
58         if (data == null) {
59             debugLog(
60                     "No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: " + getIntent());
61             finish();
62             return;
63         }
64         final String packageName = data.getSchemeSpecificPart();
65         if (TextUtils.isEmpty(packageName)) {
66             debugLog(
67                     "No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: " + getIntent());
68             finish();
69             return;
70         }
71 
72         // Package in Unrestricted mode already ignoring the battery optimizations.
73         PowerManager power = getSystemService(PowerManager.class);
74         if (power.isIgnoringBatteryOptimizations(packageName)) {
75             debugLog("Not should prompt, already ignoring optimizations: " + packageName);
76             finish();
77             return;
78         }
79 
80         if (getPackageManager()
81                         .checkPermission(
82                                 Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
83                                 packageName)
84                 != PackageManager.PERMISSION_GRANTED) {
85             debugLog(
86                     "Requested package "
87                             + packageName
88                             + " does not hold permission "
89                             + Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
90             finish();
91             return;
92         }
93 
94         try {
95             mApplicationInfo = getPackageManager().getApplicationInfo(packageName, 0);
96         } catch (PackageManager.NameNotFoundException e) {
97             debugLog("Requested package doesn't exist: " + packageName);
98             finish();
99             return;
100         }
101 
102         final AlertController.AlertParams p = mAlertParams;
103         final CharSequence appLabel =
104                 mApplicationInfo.loadSafeLabel(
105                         getPackageManager(),
106                         PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX,
107                         PackageItemInfo.SAFE_LABEL_FLAG_TRIM
108                                 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
109         p.mTitle = getText(R.string.high_power_prompt_title);
110         p.mMessage = getString(R.string.high_power_prompt_body, appLabel);
111         p.mPositiveButtonText = getText(R.string.allow);
112         p.mNegativeButtonText = getText(R.string.deny);
113         p.mPositiveButtonListener = this;
114         p.mNegativeButtonListener = this;
115         setupAlert();
116     }
117 
118     @Override
onClick(DialogInterface dialog, int which)119     public void onClick(DialogInterface dialog, int which) {
120         switch (which) {
121             case BUTTON_POSITIVE:
122                 BatteryOptimizeUtils batteryOptimizeUtils =
123                         sTestBatteryOptimizeUtils != null
124                                 ? sTestBatteryOptimizeUtils
125                                 : new BatteryOptimizeUtils(
126                                         getApplicationContext(),
127                                         mApplicationInfo.uid,
128                                         mApplicationInfo.packageName);
129                 batteryOptimizeUtils.setAppUsageState(
130                         MODE_UNRESTRICTED, Action.APPLY, /* forceMode= */ true);
131                 break;
132             case BUTTON_NEGATIVE:
133                 break;
134         }
135     }
136 
debugLog(String debugContent)137     private static void debugLog(String debugContent) {
138         if (DEBUG) Log.w(TAG, debugContent);
139     }
140 }
141