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