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 package com.android.settings.dashboard.conditional; 17 18 import android.graphics.drawable.Icon; 19 import android.os.PowerManager; 20 import com.android.internal.logging.nano.MetricsProto.MetricsEvent; 21 import com.android.settings.R; 22 import com.android.settings.Utils; 23 import com.android.settings.fuelgauge.BatterySaverSettings; 24 25 public class BatterySaverCondition extends Condition { 26 public BatterySaverCondition(ConditionManager manager) { 27 super(manager); 28 } 29 30 @Override 31 public void refreshState() { 32 PowerManager powerManager = mManager.getContext().getSystemService(PowerManager.class); 33 setActive(powerManager.isPowerSaveMode()); 34 } 35 36 @Override 37 public Icon getIcon() { 38 return Icon.createWithResource(mManager.getContext(), R.drawable.ic_settings_battery); 39 } 40 41 @Override 42 public CharSequence getTitle() { 43 return mManager.getContext().getString(R.string.condition_battery_title); 44 } 45 46 @Override 47 public CharSequence getSummary() { 48 return mManager.getContext().getString(R.string.condition_battery_summary); 49 } 50 51 @Override 52 public CharSequence[] getActions() { 53 return new CharSequence[] { mManager.getContext().getString(R.string.condition_turn_off) }; 54 } 55 56 @Override 57 public void onPrimaryClick() { 58 Utils.startWithFragment(mManager.getContext(), BatterySaverSettings.class.getName(), null, 59 null, 0, R.string.battery_saver, null, MetricsEvent.DASHBOARD_SUMMARY); 60 } 61 62 @Override 63 public void onActionClick(int index) { 64 if (index == 0) { 65 mManager.getContext().getSystemService(PowerManager.class).setPowerSaveMode(false); 66 refreshState(); 67 } else { 68 throw new IllegalArgumentException("Unexpected index " + index); 69 } 70 } 71 72 @Override 73 public int getMetricsConstant() { 74 return MetricsEvent.SETTINGS_CONDITION_BATTERY_SAVER; 75 } 76 } 77