Home | History | Annotate | Download | only in fuelgauge
      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.AlertDialog;
     20 import android.app.AppOpsManager;
     21 import android.app.Dialog;
     22 import android.app.Fragment;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.content.DialogInterface.OnClickListener;
     26 import android.content.pm.PackageManager;
     27 import android.content.pm.PackageManager.NameNotFoundException;
     28 import android.os.Bundle;
     29 import android.view.View;
     30 import android.widget.Checkable;
     31 import android.widget.TextView;
     32 
     33 import com.android.internal.annotations.VisibleForTesting;
     34 import com.android.internal.logging.nano.MetricsProto;
     35 import com.android.settings.R;
     36 import com.android.settings.applications.AppInfoBase;
     37 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
     38 import com.android.settings.overlay.FeatureFactory;
     39 import com.android.settingslib.applications.ApplicationsState.AppEntry;
     40 import com.android.settingslib.fuelgauge.PowerWhitelistBackend;
     41 
     42 public class HighPowerDetail extends InstrumentedDialogFragment implements OnClickListener,
     43         View.OnClickListener {
     44 
     45     private static final String ARG_DEFAULT_ON = "default_on";
     46 
     47     @VisibleForTesting
     48     PowerWhitelistBackend mBackend;
     49     @VisibleForTesting
     50     BatteryUtils mBatteryUtils;
     51     @VisibleForTesting
     52     String mPackageName;
     53     @VisibleForTesting
     54     int mPackageUid;
     55     private CharSequence mLabel;
     56     private boolean mDefaultOn;
     57     @VisibleForTesting
     58     boolean mIsEnabled;
     59     private Checkable mOptionOn;
     60     private Checkable mOptionOff;
     61 
     62     @Override
     63     public int getMetricsCategory() {
     64         return MetricsProto.MetricsEvent.DIALOG_HIGH_POWER_DETAILS;
     65     }
     66 
     67     @Override
     68     public void onCreate(Bundle savedInstanceState) {
     69         super.onCreate(savedInstanceState);
     70         final Context context = getContext();
     71         mBatteryUtils = BatteryUtils.getInstance(context);
     72         mBackend = PowerWhitelistBackend.getInstance(context);
     73 
     74         mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
     75         mPackageUid = getArguments().getInt(AppInfoBase.ARG_PACKAGE_UID);
     76         final PackageManager pm = context.getPackageManager();
     77         try {
     78             mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
     79         } catch (NameNotFoundException e) {
     80             mLabel = mPackageName;
     81         }
     82         mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
     83         mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
     84     }
     85 
     86     public Checkable setup(View view, boolean on) {
     87         ((TextView) view.findViewById(android.R.id.title)).setText(on
     88                 ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off);
     89         ((TextView) view.findViewById(android.R.id.summary)).setText(on
     90                 ? R.string.ignore_optimizations_on_desc : R.string.ignore_optimizations_off_desc);
     91         view.setClickable(true);
     92         view.setOnClickListener(this);
     93         if (!on && mBackend.isSysWhitelisted(mPackageName)) {
     94             view.setEnabled(false);
     95         }
     96         return (Checkable) view;
     97     }
     98 
     99     @Override
    100     public Dialog onCreateDialog(Bundle savedInstanceState) {
    101         AlertDialog.Builder b = new AlertDialog.Builder(getContext())
    102                 .setTitle(mLabel)
    103                 .setNegativeButton(R.string.cancel, null)
    104                 .setView(R.layout.ignore_optimizations_content);
    105         if (!mBackend.isSysWhitelisted(mPackageName)) {
    106             b.setPositiveButton(R.string.done, this);
    107         }
    108         return b.create();
    109     }
    110 
    111     @Override
    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 
    119     private void updateViews() {
    120         mOptionOn.setChecked(mIsEnabled);
    121         mOptionOff.setChecked(!mIsEnabled);
    122     }
    123 
    124     @Override
    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
    136     public void onClick(DialogInterface dialog, int which) {
    137         if (which == DialogInterface.BUTTON_POSITIVE) {
    138             boolean newValue = mIsEnabled;
    139             boolean oldValue = mBackend.isWhitelisted(mPackageName);
    140             if (newValue != oldValue) {
    141                 logSpecialPermissionChange(newValue, mPackageName, getContext());
    142                 if (newValue) {
    143                     mBatteryUtils.setForceAppStandby(mPackageUid, mPackageName,
    144                             AppOpsManager.MODE_ALLOWED);
    145                     mBackend.addApp(mPackageName);
    146                 } else {
    147                     mBackend.removeApp(mPackageName);
    148                 }
    149             }
    150         }
    151     }
    152 
    153     @VisibleForTesting
    154     static void logSpecialPermissionChange(boolean whitelist, String packageName, Context context) {
    155         int logCategory = whitelist ? MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_DENY
    156                 : MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW;
    157         FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(context, logCategory,
    158                 packageName);
    159     }
    160 
    161     @Override
    162     public void onDismiss(DialogInterface dialog) {
    163         super.onDismiss(dialog);
    164         Fragment target = getTargetFragment();
    165         if (target != null && target.getActivity() != null) {
    166             target.onActivityResult(getTargetRequestCode(), 0, null);
    167         }
    168     }
    169 
    170     public static CharSequence getSummary(Context context, AppEntry entry) {
    171         return getSummary(context, entry.info.packageName);
    172     }
    173 
    174     public static CharSequence getSummary(Context context, String pkg) {
    175         PowerWhitelistBackend powerWhitelist = PowerWhitelistBackend.getInstance(context);
    176         return context.getString(powerWhitelist.isSysWhitelisted(pkg) ? R.string.high_power_system
    177                 : powerWhitelist.isWhitelisted(pkg) ? R.string.high_power_on
    178                         : R.string.high_power_off);
    179     }
    180 
    181     public static void show(Fragment caller, int uid, String packageName, int requestCode) {
    182         HighPowerDetail fragment = new HighPowerDetail();
    183         Bundle args = new Bundle();
    184         args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
    185         args.putInt(AppInfoBase.ARG_PACKAGE_UID, uid);
    186         fragment.setArguments(args);
    187         fragment.setTargetFragment(caller, requestCode);
    188         fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());
    189     }
    190 }
    191