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.Dialog;
     21 import android.app.DialogFragment;
     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 
     41 public class HighPowerDetail extends InstrumentedDialogFragment implements OnClickListener,
     42         View.OnClickListener {
     43 
     44     private static final String ARG_DEFAULT_ON = "default_on";
     45 
     46     private final PowerWhitelistBackend mBackend = PowerWhitelistBackend.getInstance();
     47 
     48     private String mPackageName;
     49     private CharSequence mLabel;
     50     private boolean mDefaultOn;
     51     private boolean mIsEnabled;
     52     private Checkable mOptionOn;
     53     private Checkable mOptionOff;
     54 
     55     @Override
     56     public int getMetricsCategory() {
     57         return MetricsProto.MetricsEvent.DIALOG_HIGH_POWER_DETAILS;
     58     }
     59 
     60     @Override
     61     public void onCreate(Bundle savedInstanceState) {
     62         super.onCreate(savedInstanceState);
     63 
     64         mPackageName = getArguments().getString(AppInfoBase.ARG_PACKAGE_NAME);
     65         PackageManager pm = getContext().getPackageManager();
     66         try {
     67             mLabel = pm.getApplicationInfo(mPackageName, 0).loadLabel(pm);
     68         } catch (NameNotFoundException e) {
     69             mLabel = mPackageName;
     70         }
     71         mDefaultOn = getArguments().getBoolean(ARG_DEFAULT_ON);
     72         mIsEnabled = mDefaultOn || mBackend.isWhitelisted(mPackageName);
     73     }
     74 
     75     public Checkable setup(View view, boolean on) {
     76         ((TextView) view.findViewById(android.R.id.title)).setText(on
     77                 ? R.string.ignore_optimizations_on : R.string.ignore_optimizations_off);
     78         ((TextView) view.findViewById(android.R.id.summary)).setText(on
     79                 ? R.string.ignore_optimizations_on_desc : R.string.ignore_optimizations_off_desc);
     80         view.setClickable(true);
     81         view.setOnClickListener(this);
     82         if (!on && mBackend.isSysWhitelisted(mPackageName)) {
     83             view.setEnabled(false);
     84         }
     85         return (Checkable) view;
     86     }
     87 
     88     @Override
     89     public Dialog onCreateDialog(Bundle savedInstanceState) {
     90         AlertDialog.Builder b = new AlertDialog.Builder(getContext())
     91                 .setTitle(mLabel)
     92                 .setNegativeButton(R.string.cancel, null)
     93                 .setView(R.layout.ignore_optimizations_content);
     94         if (!mBackend.isSysWhitelisted(mPackageName)) {
     95             b.setPositiveButton(R.string.done, this);
     96         }
     97         return b.create();
     98     }
     99 
    100     @Override
    101     public void onStart() {
    102         super.onStart();
    103         mOptionOn = setup(getDialog().findViewById(R.id.ignore_on), true);
    104         mOptionOff = setup(getDialog().findViewById(R.id.ignore_off), false);
    105         updateViews();
    106     }
    107 
    108     private void updateViews() {
    109         mOptionOn.setChecked(mIsEnabled);
    110         mOptionOff.setChecked(!mIsEnabled);
    111     }
    112 
    113     @Override
    114     public void onClick(View v) {
    115         if (v == mOptionOn) {
    116             mIsEnabled = true;
    117             updateViews();
    118         } else if (v == mOptionOff) {
    119             mIsEnabled = false;
    120             updateViews();
    121         }
    122     }
    123 
    124     @Override
    125     public void onClick(DialogInterface dialog, int which) {
    126         if (which == DialogInterface.BUTTON_POSITIVE) {
    127             boolean newValue = mIsEnabled;
    128             boolean oldValue = mBackend.isWhitelisted(mPackageName);
    129             if (newValue != oldValue) {
    130                 logSpecialPermissionChange(newValue, mPackageName, getContext());
    131                 if (newValue) {
    132                     mBackend.addApp(mPackageName);
    133                 } else {
    134                     mBackend.removeApp(mPackageName);
    135                 }
    136             }
    137         }
    138     }
    139 
    140     @VisibleForTesting
    141     static void logSpecialPermissionChange(boolean whitelist, String packageName, Context context) {
    142         int logCategory = whitelist ? MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_DENY
    143                 : MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_BATTERY_ALLOW;
    144         FeatureFactory.getFactory(context).getMetricsFeatureProvider().action(context, logCategory,
    145                 packageName);
    146     }
    147 
    148     @Override
    149     public void onDismiss(DialogInterface dialog) {
    150         super.onDismiss(dialog);
    151         Fragment target = getTargetFragment();
    152         if (target != null && target.getActivity() != null) {
    153             target.onActivityResult(getTargetRequestCode(), 0, null);
    154         }
    155     }
    156 
    157     public static CharSequence getSummary(Context context, AppEntry entry) {
    158         return getSummary(context, entry.info.packageName);
    159     }
    160 
    161     public static CharSequence getSummary(Context context, String pkg) {
    162         PowerWhitelistBackend powerWhitelist = PowerWhitelistBackend.getInstance();
    163         return context.getString(powerWhitelist.isSysWhitelisted(pkg) ? R.string.high_power_system
    164                 : powerWhitelist.isWhitelisted(pkg) ? R.string.high_power_on
    165                 : R.string.high_power_off);
    166     }
    167 
    168     public static void show(Fragment caller, String packageName, int requestCode,
    169             boolean defaultToOn) {
    170         HighPowerDetail fragment = new HighPowerDetail();
    171         Bundle args = new Bundle();
    172         args.putString(AppInfoBase.ARG_PACKAGE_NAME, packageName);
    173         args.putBoolean(ARG_DEFAULT_ON, defaultToOn);
    174         fragment.setArguments(args);
    175         fragment.setTargetFragment(caller, requestCode);
    176         fragment.show(caller.getFragmentManager(), HighPowerDetail.class.getSimpleName());
    177     }
    178 }
    179