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.Manifest;
20 import android.content.DialogInterface;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.PackageItemInfo;
23 import android.content.pm.PackageManager;
24 import android.net.Uri;
25 import android.os.Bundle;
26 import android.os.PowerManager;
27 import android.os.PowerWhitelistManager;
28 import android.util.Log;
29 
30 import com.android.internal.app.AlertActivity;
31 import com.android.internal.app.AlertController;
32 import com.android.settings.R;
33 
34 public class RequestIgnoreBatteryOptimizations extends AlertActivity implements
35         DialogInterface.OnClickListener {
36     private static final String TAG = "RequestIgnoreBatteryOptimizations";
37     private static final boolean DEBUG = false;
38 
39     private PowerWhitelistManager mPowerWhitelistManager;
40     private String mPackageName;
41 
42     @Override
onCreate(Bundle savedInstanceState)43     public void onCreate(Bundle savedInstanceState) {
44         super.onCreate(savedInstanceState);
45         getWindow().addSystemFlags(android.view.WindowManager.LayoutParams
46                 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
47 
48         mPowerWhitelistManager = getSystemService(PowerWhitelistManager.class);
49 
50         Uri data = getIntent().getData();
51         if (data == null) {
52             debugLog("No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: "
53                     + getIntent());
54             finish();
55             return;
56         }
57         mPackageName = data.getSchemeSpecificPart();
58         if (mPackageName == null) {
59             debugLog("No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: "
60                     + getIntent());
61             finish();
62             return;
63         }
64 
65         PowerManager power = getSystemService(PowerManager.class);
66         if (power.isIgnoringBatteryOptimizations(mPackageName)) {
67             debugLog("Not should prompt, already ignoring optimizations: " + mPackageName);
68             finish();
69             return;
70         }
71 
72         ApplicationInfo ai;
73         try {
74             ai = getPackageManager().getApplicationInfo(mPackageName, 0);
75         } catch (PackageManager.NameNotFoundException e) {
76             debugLog("Requested package doesn't exist: " + mPackageName);
77             finish();
78             return;
79         }
80 
81         if (getPackageManager().checkPermission(
82                 Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, mPackageName)
83                 != PackageManager.PERMISSION_GRANTED) {
84             debugLog("Requested package " + mPackageName + " does not hold permission "
85                     + Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
86             finish();
87             return;
88         }
89 
90         final AlertController.AlertParams p = mAlertParams;
91         final CharSequence appLabel = ai.loadSafeLabel(getPackageManager(),
92                 PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
93                         | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
94         p.mTitle = getText(R.string.high_power_prompt_title);
95         p.mMessage = getString(R.string.high_power_prompt_body, appLabel);
96         p.mPositiveButtonText = getText(R.string.allow);
97         p.mNegativeButtonText = getText(R.string.deny);
98         p.mPositiveButtonListener = this;
99         p.mNegativeButtonListener = this;
100         setupAlert();
101     }
102 
103     @Override
onClick(DialogInterface dialog, int which)104     public void onClick(DialogInterface dialog, int which) {
105         switch (which) {
106             case BUTTON_POSITIVE:
107                 mPowerWhitelistManager.addToWhitelist(mPackageName);
108                 break;
109             case BUTTON_NEGATIVE:
110                 break;
111         }
112     }
113 
debugLog(String debugContent)114     private static void debugLog(String debugContent) {
115         if (DEBUG) Log.w(TAG, debugContent);
116     }
117 }
118