blob: 33f8b069b7515611448aeb5032e43d6004e71473 [file] [log] [blame]
Adrian Roos56021892017-02-27 20:25:09 +01001/*
2 * Copyright (C) 2017 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
17package com.android.systemui;
18
19import android.annotation.Nullable;
20import android.content.Context;
21import android.content.res.TypedArray;
Adrian Roosaf1700a2017-08-17 18:35:43 +020022import android.os.UserHandle;
Adrian Roos56021892017-02-27 20:25:09 +010023import android.util.AttributeSet;
24import android.widget.ImageView;
25
Adrian Roosaf1700a2017-08-17 18:35:43 +020026import com.android.internal.hardware.AmbientDisplayConfiguration;
Adrian Roos56021892017-02-27 20:25:09 +010027import com.android.systemui.statusbar.policy.BatteryController;
28import com.android.systemui.statusbar.policy.ConfigurationController;
29
30/**
31 * A view that only shows its drawable while the phone is charging.
32 *
33 * Also reloads its drawable upon density changes.
34 */
35public class ChargingView extends ImageView implements
36 BatteryController.BatteryStateChangeCallback,
37 ConfigurationController.ConfigurationListener {
38
Adrian Roosaf1700a2017-08-17 18:35:43 +020039 private static final long CHARGING_INDICATION_DELAY_MS = 1000;
40
41 private final AmbientDisplayConfiguration mConfig;
42 private final Runnable mClearSuppressCharging = this::clearSuppressCharging;
Adrian Roos56021892017-02-27 20:25:09 +010043 private BatteryController mBatteryController;
44 private int mImageResource;
45 private boolean mCharging;
46 private boolean mDark;
Adrian Roosaf1700a2017-08-17 18:35:43 +020047 private boolean mSuppressCharging;
48
49
50 private void clearSuppressCharging() {
51 mSuppressCharging = false;
52 removeCallbacks(mClearSuppressCharging);
53 updateVisibility();
54 }
Adrian Roos56021892017-02-27 20:25:09 +010055
56 public ChargingView(Context context, @Nullable AttributeSet attrs) {
57 super(context, attrs);
58
Adrian Roosaf1700a2017-08-17 18:35:43 +020059 mConfig = new AmbientDisplayConfiguration(context);
60
Adrian Roos56021892017-02-27 20:25:09 +010061 TypedArray a = context.obtainStyledAttributes(attrs, new int[]{android.R.attr.src});
62 int srcResId = a.getResourceId(0, 0);
63
64 if (srcResId != 0) {
65 mImageResource = srcResId;
66 }
67
68 a.recycle();
69
70 updateVisibility();
71 }
72
73 @Override
74 public void onAttachedToWindow() {
75 super.onAttachedToWindow();
76 mBatteryController = Dependency.get(BatteryController.class);
77 mBatteryController.addCallback(this);
78 Dependency.get(ConfigurationController.class).addCallback(this);
79 }
80
81 @Override
82 public void onDetachedFromWindow() {
83 super.onDetachedFromWindow();
84 mBatteryController.removeCallback(this);
85 Dependency.get(ConfigurationController.class).removeCallback(this);
Adrian Roosaf1700a2017-08-17 18:35:43 +020086 removeCallbacks(mClearSuppressCharging);
Adrian Roos56021892017-02-27 20:25:09 +010087 }
88
89 @Override
90 public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
Adrian Roosaf1700a2017-08-17 18:35:43 +020091 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
94 // hidden again.
95 clearSuppressCharging();
96 mSuppressCharging = true;
97 postDelayed(mClearSuppressCharging, CHARGING_INDICATION_DELAY_MS);
98 }
Adrian Roos56021892017-02-27 20:25:09 +010099 mCharging = charging;
100 updateVisibility();
101 }
102
Adrian Roosaf1700a2017-08-17 18:35:43 +0200103 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;
108 }
109
Adrian Roos56021892017-02-27 20:25:09 +0100110 @Override
111 public void onDensityOrFontScaleChanged() {
112 setImageResource(mImageResource);
113 }
114
115 public void setDark(boolean dark) {
116 mDark = dark;
Adrian Roosaf1700a2017-08-17 18:35:43 +0200117 if (!dark) {
118 clearSuppressCharging();
119 }
Adrian Roos56021892017-02-27 20:25:09 +0100120 updateVisibility();
121 }
122
123 private void updateVisibility() {
Adrian Roosaf1700a2017-08-17 18:35:43 +0200124 setVisibility(mCharging && !mSuppressCharging && mDark ? VISIBLE : INVISIBLE);
Adrian Roos56021892017-02-27 20:25:09 +0100125 }
126}