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