Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 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.systemui.qs.tiles; |
| 17 | |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 18 | import android.content.BroadcastReceiver; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.Intent; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 21 | import android.content.IntentFilter; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.os.Handler; |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 24 | import android.text.SpannableStringBuilder; |
| 25 | import android.text.Spanned; |
| 26 | import android.text.style.RelativeSizeSpan; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 29 | import android.view.View.OnAttachStateChangeListener; |
| 30 | import android.view.View.OnClickListener; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 31 | import android.view.ViewGroup; |
| 32 | import android.widget.Checkable; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.TextView; |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 35 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 36 | import com.android.settingslib.BatteryInfo; |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 37 | import com.android.settingslib.graph.UsageView; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 38 | import com.android.systemui.BatteryMeterDrawable; |
| 39 | import com.android.systemui.R; |
| 40 | import com.android.systemui.qs.QSTile; |
| 41 | import com.android.systemui.statusbar.policy.BatteryController; |
| 42 | |
| 43 | import java.text.NumberFormat; |
| 44 | |
| 45 | public class BatteryTile extends QSTile<QSTile.State> implements BatteryController.BatteryStateChangeCallback { |
| 46 | |
| 47 | private final BatteryMeterDrawable mDrawable; |
| 48 | private final BatteryController mBatteryController; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 49 | private final BatteryDetail mBatteryDetail = new BatteryDetail(); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 50 | |
| 51 | private int mLevel; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 52 | private boolean mPowerSave; |
| 53 | private boolean mCharging; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 54 | private boolean mDetailShown; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 55 | |
| 56 | public BatteryTile(Host host) { |
| 57 | super(host); |
| 58 | mBatteryController = host.getBatteryController(); |
| 59 | mDrawable = new BatteryMeterDrawable(host.getContext(), new Handler(), |
| 60 | host.getContext().getColor(R.color.batterymeter_frame_color)); |
| 61 | mDrawable.setBatteryController(mBatteryController); |
| 62 | } |
| 63 | |
| 64 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 65 | public State newTileState() { |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 66 | return new QSTile.State(); |
| 67 | } |
| 68 | |
| 69 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 70 | public DetailAdapter getDetailAdapter() { |
| 71 | return mBatteryDetail; |
| 72 | } |
| 73 | |
| 74 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 75 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 76 | return MetricsEvent.QS_BATTERY_TILE; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void setListening(boolean listening) { |
| 81 | if (listening) { |
| 82 | mDrawable.startListening(); |
| 83 | mBatteryController.addStateChangedCallback(this); |
| 84 | } else { |
| 85 | mDrawable.stopListening(); |
| 86 | mBatteryController.removeStateChangedCallback(this); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 91 | public void setDetailListening(boolean listening) { |
| 92 | super.setDetailListening(listening); |
| 93 | if (!listening) { |
| 94 | mBatteryDetail.mCurrentView = null; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | @Override |
Jason Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 99 | public Intent getLongClickIntent() { |
| 100 | return new Intent(Intent.ACTION_POWER_USAGE_SUMMARY); |
| 101 | } |
| 102 | |
| 103 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 104 | protected void handleClick() { |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 105 | showDetail(true); |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 109 | public CharSequence getTileLabel() { |
| 110 | return mContext.getString(R.string.battery); |
| 111 | } |
| 112 | |
| 113 | @Override |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 114 | protected void handleUpdateState(State state, Object arg) { |
| 115 | int level = (arg != null) ? (Integer) arg : mLevel; |
| 116 | String percentage = NumberFormat.getPercentInstance().format((double) level / 100.0); |
| 117 | |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 118 | state.icon = new Icon() { |
| 119 | @Override |
| 120 | public Drawable getDrawable(Context context) { |
| 121 | return mDrawable; |
| 122 | } |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 123 | |
| 124 | @Override |
| 125 | public int getPadding() { |
| 126 | return mHost.getContext().getResources().getDimensionPixelSize( |
| 127 | R.dimen.qs_battery_padding); |
| 128 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 129 | }; |
| 130 | state.label = percentage; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) { |
| 135 | mLevel = level; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 136 | mCharging = charging; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 137 | refreshState((Integer) level); |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 138 | if (mDetailShown) { |
| 139 | mBatteryDetail.postBindView(); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 140 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 144 | public void onPowerSaveChanged(boolean isPowerSave) { |
| 145 | mPowerSave = isPowerSave; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 146 | if (mDetailShown) { |
| 147 | mBatteryDetail.postBindView(); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 148 | } |
| 149 | } |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 150 | |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 151 | private final class BatteryDetail implements DetailAdapter, OnClickListener, |
| 152 | OnAttachStateChangeListener { |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 153 | private final BatteryMeterDrawable mDrawable = new BatteryMeterDrawable(mHost.getContext(), |
| 154 | new Handler(), mHost.getContext().getColor(R.color.batterymeter_frame_color)); |
| 155 | private View mCurrentView; |
| 156 | |
| 157 | @Override |
| 158 | public CharSequence getTitle() { |
| 159 | return mContext.getString(R.string.battery_panel_title, mLevel); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Boolean getToggleState() { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public View createDetailView(Context context, View convertView, ViewGroup parent) { |
| 169 | if (convertView == null) { |
| 170 | convertView = LayoutInflater.from(mContext).inflate(R.layout.battery_detail, parent, |
| 171 | false); |
| 172 | } |
| 173 | mCurrentView = convertView; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 174 | mCurrentView.addOnAttachStateChangeListener(this); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 175 | bindView(); |
| 176 | return convertView; |
| 177 | } |
| 178 | |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 179 | private void postBindView() { |
Jason Monk | 6cb9012 | 2016-03-02 12:40:43 -0500 | [diff] [blame] | 180 | if (mCurrentView == null) return; |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 181 | mCurrentView.post(new Runnable() { |
| 182 | @Override |
| 183 | public void run() { |
| 184 | bindView(); |
| 185 | } |
| 186 | }); |
| 187 | } |
| 188 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 189 | private void bindView() { |
Jason Monk | 2462a50 | 2016-03-02 16:04:39 -0500 | [diff] [blame] | 190 | if (mCurrentView == null) { |
| 191 | return; |
| 192 | } |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 193 | mDrawable.onBatteryLevelChanged(100, false, false); |
| 194 | mDrawable.onPowerSaveChanged(true); |
Jason Monk | 5c404c8 | 2016-02-05 15:54:10 -0500 | [diff] [blame] | 195 | mDrawable.disableShowPercent(); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 196 | ((ImageView) mCurrentView.findViewById(android.R.id.icon)).setImageDrawable(mDrawable); |
| 197 | Checkable checkbox = (Checkable) mCurrentView.findViewById(android.R.id.toggle); |
| 198 | checkbox.setChecked(mPowerSave); |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 199 | BatteryInfo.getBatteryInfo(mContext, new BatteryInfo.Callback() { |
| 200 | @Override |
| 201 | public void onBatteryInfoLoaded(BatteryInfo info) { |
| 202 | if (mCurrentView != null) { |
| 203 | bindBatteryInfo(info); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 204 | } |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 205 | } |
| 206 | }); |
| 207 | if (mCharging) { |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 208 | ((TextView) mCurrentView.findViewById(android.R.id.title)).setText( |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 209 | R.string.battery_detail_charging_summary); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 210 | mCurrentView.findViewById(android.R.id.icon).setVisibility(View.INVISIBLE); |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 211 | mCurrentView.findViewById(android.R.id.toggle).setVisibility(View.GONE); |
| 212 | mCurrentView.findViewById(R.id.switch_container).setClickable(false); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 213 | } else { |
| 214 | ((TextView) mCurrentView.findViewById(android.R.id.title)).setText( |
| 215 | R.string.battery_detail_switch_title); |
| 216 | ((TextView) mCurrentView.findViewById(android.R.id.summary)).setText( |
| 217 | R.string.battery_detail_switch_summary); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 218 | mCurrentView.findViewById(android.R.id.icon).setVisibility(View.VISIBLE); |
| 219 | mCurrentView.findViewById(android.R.id.toggle).setVisibility(View.VISIBLE); |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 220 | mCurrentView.findViewById(R.id.switch_container).setClickable(true); |
| 221 | mCurrentView.findViewById(R.id.switch_container).setOnClickListener(this); |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Jason Monk | 96ab636 | 2016-02-11 11:33:09 -0500 | [diff] [blame] | 225 | private void bindBatteryInfo(BatteryInfo info) { |
| 226 | SpannableStringBuilder builder = new SpannableStringBuilder(); |
| 227 | builder.append(info.batteryPercentString, new RelativeSizeSpan(2), |
| 228 | Spanned.SPAN_INCLUSIVE_EXCLUSIVE); |
| 229 | if (info.remainingLabel != null) { |
| 230 | if (mContext.getResources().getBoolean(R.bool.quick_settings_wide)) { |
| 231 | builder.append(' '); |
| 232 | } else { |
| 233 | builder.append('\n'); |
| 234 | } |
| 235 | builder.append(info.remainingLabel); |
| 236 | } |
| 237 | ((TextView) mCurrentView.findViewById(R.id.charge_and_estimation)).setText(builder); |
| 238 | |
| 239 | info.bindHistory((UsageView) mCurrentView.findViewById(R.id.battery_usage)); |
| 240 | } |
| 241 | |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 242 | @Override |
| 243 | public void onClick(View v) { |
| 244 | mBatteryController.setPowerSaveMode(!mPowerSave); |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public Intent getSettingsIntent() { |
| 249 | return new Intent(Intent.ACTION_POWER_USAGE_SUMMARY); |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void setToggleState(boolean state) { |
| 254 | // No toggle state. |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | public int getMetricsCategory() { |
Chris Wren | f6e9228b | 2016-01-26 18:04:35 -0500 | [diff] [blame] | 259 | return MetricsEvent.QS_BATTERY_DETAIL; |
Jason Monk | c06fbb1 | 2016-01-08 14:12:18 -0500 | [diff] [blame] | 260 | } |
Jason Monk | f12cfb7 | 2016-02-22 15:46:12 -0500 | [diff] [blame] | 261 | |
| 262 | @Override |
| 263 | public void onViewAttachedToWindow(View v) { |
| 264 | if (!mDetailShown) { |
| 265 | mDetailShown = true; |
| 266 | v.getContext().registerReceiver(mReceiver, |
| 267 | new IntentFilter(Intent.ACTION_TIME_TICK)); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onViewDetachedFromWindow(View v) { |
| 273 | if (mDetailShown) { |
| 274 | mDetailShown = false; |
| 275 | v.getContext().unregisterReceiver(mReceiver); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 280 | @Override |
| 281 | public void onReceive(Context context, Intent intent) { |
| 282 | postBindView(); |
| 283 | } |
| 284 | }; |
Jason Monk | abe1974 | 2015-09-29 09:47:06 -0400 | [diff] [blame] | 285 | } |
| 286 | } |