2 * Copyright (C) 2017 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
17 package com.android.systemui;
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.os.UserHandle;
23 import android.util.AttributeSet;
24 import android.widget.ImageView;
26 import com.android.internal.hardware.AmbientDisplayConfiguration;
27 import com.android.systemui.statusbar.policy.BatteryController;
28 import com.android.systemui.statusbar.policy.ConfigurationController;
31 * A view that only shows its drawable while the phone is charging.
33 * Also reloads its drawable upon density changes.
35 public class ChargingView extends ImageView implements
36 BatteryController.BatteryStateChangeCallback,
37 ConfigurationController.ConfigurationListener {
39 private static final long CHARGING_INDICATION_DELAY_MS = 1000;
41 private final AmbientDisplayConfiguration mConfig;
42 private final Runnable mClearSuppressCharging = this::clearSuppressCharging;
43 private BatteryController mBatteryController;
44 private int mImageResource;
45 private boolean mCharging;
46 private boolean mDark;
47 private boolean mSuppressCharging;
50 private void clearSuppressCharging() {
51 mSuppressCharging = false;
52 removeCallbacks(mClearSuppressCharging);
56 public ChargingView(Context context, @Nullable AttributeSet attrs) {
57 super(context, attrs);
59 mConfig = new AmbientDisplayConfiguration(context);
61 TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.src});
62 int srcResId = a.getResourceId(0, 0);
65 mImageResource = srcResId;
74 public void onAttachedToWindow() {
75 super.onAttachedToWindow();
76 mBatteryController = Dependency.get(BatteryController.class);
77 mBatteryController.addCallback(this);
78 Dependency.get(ConfigurationController.class).addCallback(this);
82 public void onDetachedFromWindow() {
83 super.onDetachedFromWindow();
84 mBatteryController.removeCallback(this);
85 Dependency.get(ConfigurationController.class).removeCallback(this);
86 removeCallbacks(mClearSuppressCharging);
90 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
91 boolean startCharging = charging && !mCharging;
92 if (startCharging && deviceWillWakeUpWhenPluggedIn() && mDark) {
93 // We're about to wake up, and thus don't want to show the indicator just for it to be
95 clearSuppressCharging();
96 mSuppressCharging = true;
97 postDelayed(mClearSuppressCharging, CHARGING_INDICATION_DELAY_MS);
103 private boolean deviceWillWakeUpWhenPluggedIn() {
104 boolean plugTurnsOnScreen = getResources().getBoolean(
105 com.android.internal.R.bool.config_unplugTurnsOnScreen);
106 boolean aod = mConfig.alwaysOnEnabled(UserHandle.USER_CURRENT);
107 return !aod && plugTurnsOnScreen;
111 public void onDensityOrFontScaleChanged() {
112 setImageResource(mImageResource);
115 public void setDark(boolean dark) {
118 clearSuppressCharging();
123 private void updateVisibility() {
124 setVisibility(mCharging && !mSuppressCharging && mDark ? VISIBLE : INVISIBLE);