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 35 implements 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() 46 .addSystemFlags( 47 android.view.WindowManager.LayoutParams 48 .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS); 49 50 mPowerWhitelistManager = getSystemService(PowerWhitelistManager.class); 51 52 Uri data = getIntent().getData(); 53 if (data == null) { 54 debugLog( 55 "No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: " + getIntent()); 56 finish(); 57 return; 58 } 59 mPackageName = data.getSchemeSpecificPart(); 60 if (mPackageName == null) { 61 debugLog( 62 "No data supplied for IGNORE_BATTERY_OPTIMIZATION_SETTINGS in: " + getIntent()); 63 finish(); 64 return; 65 } 66 67 PowerManager power = getSystemService(PowerManager.class); 68 if (power.isIgnoringBatteryOptimizations(mPackageName)) { 69 debugLog("Not should prompt, already ignoring optimizations: " + mPackageName); 70 finish(); 71 return; 72 } 73 74 if (getPackageManager() 75 .checkPermission( 76 Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, 77 mPackageName) 78 != PackageManager.PERMISSION_GRANTED) { 79 debugLog( 80 "Requested package " 81 + mPackageName 82 + " does not hold permission " 83 + Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); 84 finish(); 85 return; 86 } 87 88 ApplicationInfo ai; 89 try { 90 ai = getPackageManager().getApplicationInfo(mPackageName, 0); 91 } catch (PackageManager.NameNotFoundException e) { 92 debugLog("Requested package doesn't exist: " + mPackageName); 93 finish(); 94 return; 95 } 96 97 final AlertController.AlertParams p = mAlertParams; 98 final CharSequence appLabel = 99 ai.loadSafeLabel( 100 getPackageManager(), 101 PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX, 102 PackageItemInfo.SAFE_LABEL_FLAG_TRIM 103 | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE); 104 p.mTitle = getText(R.string.high_power_prompt_title); 105 p.mMessage = getString(R.string.high_power_prompt_body, appLabel); 106 p.mPositiveButtonText = getText(R.string.allow); 107 p.mNegativeButtonText = getText(R.string.deny); 108 p.mPositiveButtonListener = this; 109 p.mNegativeButtonListener = this; 110 setupAlert(); 111 } 112 113 @Override onClick(DialogInterface dialog, int which)114 public void onClick(DialogInterface dialog, int which) { 115 switch (which) { 116 case BUTTON_POSITIVE: 117 mPowerWhitelistManager.addToWhitelist(mPackageName); 118 break; 119 case BUTTON_NEGATIVE: 120 break; 121 } 122 } 123 debugLog(String debugContent)124 private static void debugLog(String debugContent) { 125 if (DEBUG) Log.w(TAG, debugContent); 126 } 127 } 128