2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.systemui.qs.tiles;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.graphics.drawable.Drawable;
21 import android.service.quicksettings.Tile;
22 import android.widget.Switch;
23 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
24 import com.android.settingslib.graph.BatteryMeterDrawableBase;
25 import com.android.systemui.Dependency;
26 import com.android.systemui.R;
27 import com.android.systemui.plugins.qs.QSTile.BooleanState;
28 import com.android.systemui.qs.QSHost;
29 import com.android.systemui.qs.tileimpl.QSTileImpl;
30 import com.android.systemui.statusbar.policy.BatteryController;
32 public class BatterySaverTile extends QSTileImpl<BooleanState> implements
33 BatteryController.BatteryStateChangeCallback {
35 private final BatteryController mBatteryController;
38 private boolean mPowerSave;
39 private boolean mCharging;
40 private boolean mPluggedIn;
42 public BatterySaverTile(QSHost host) {
44 mBatteryController = Dependency.get(BatteryController.class);
48 public BooleanState newTileState() {
49 return new BooleanState();
53 public int getMetricsCategory() {
54 return MetricsEvent.QS_BATTERY_TILE;
58 public void handleSetListening(boolean listening) {
60 mBatteryController.addCallback(this);
62 mBatteryController.removeCallback(this);
67 public Intent getLongClickIntent() {
68 return new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
72 protected void handleClick() {
73 mBatteryController.setPowerSaveMode(!mPowerSave);
77 public CharSequence getTileLabel() {
78 return mContext.getString(R.string.battery_detail_switch_title);
82 protected void handleUpdateState(BooleanState state, Object arg) {
83 state.state = mPluggedIn ? Tile.STATE_UNAVAILABLE
84 : mPowerSave ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE;
85 BatterySaverIcon bsi = new BatterySaverIcon();
86 bsi.mState = state.state;
88 state.label = mContext.getString(R.string.battery_detail_switch_title);
89 state.contentDescription = state.label;
90 state.value = mPowerSave;
91 state.expandedAccessibilityClassName = Switch.class.getName();
95 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
97 mPluggedIn = pluggedIn;
103 public void onPowerSaveChanged(boolean isPowerSave) {
104 mPowerSave = isPowerSave;
108 public static class BatterySaverIcon extends Icon {
112 public Drawable getDrawable(Context context) {
113 BatterySaverDrawable b =
114 new BatterySaverDrawable(context, QSTileImpl.getColorForState(context, mState));
116 final int pad = context.getResources()
117 .getDimensionPixelSize(R.dimen.qs_tile_divider_height);
118 b.setPadding(pad, pad, pad, pad);
123 private static class BatterySaverDrawable extends BatteryMeterDrawableBase {
125 private static final int MAX_BATTERY = 100;
127 BatterySaverDrawable(Context context, int frameColor) {
128 super(context, frameColor);
129 // Show as full so it's always uniform color
130 super.setBatteryLevel(MAX_BATTERY);
133 setPowerSaveAsColorError(false);
134 mPowerSaveAsColorError = true;
135 mFramePaint.setColor(0);
136 mPowersavePaint.setColor(frameColor);
137 mFramePaint.setStrokeWidth(mPowersavePaint.getStrokeWidth());
138 mPlusPaint.setColor(frameColor);
142 protected int batteryColorForLevel(int level) {
147 public void setBatteryLevel(int val) {
148 // Don't change the actual level, otherwise this won't draw correctly