• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package com.android.systemui.statusbar.policy;
18 
19 import android.annotation.Nullable;
20 
21 import androidx.annotation.NonNull;
22 
23 import com.android.systemui.Dumpable;
24 import com.android.systemui.animation.Expandable;
25 import com.android.systemui.demomode.DemoMode;
26 import com.android.systemui.statusbar.policy.BatteryController.BatteryStateChangeCallback;
27 
28 import java.io.PrintWriter;
29 import java.lang.ref.WeakReference;
30 
31 /**
32  * Controller for battery related information, including the charge level, power save mode,
33  * and time remaining information
34  */
35 public interface BatteryController extends DemoMode,
36         CallbackController<BatteryStateChangeCallback> {
37     /**
38      * Prints the current state of the {@link BatteryController} to the given {@link PrintWriter}.
39      */
dump(PrintWriter pw, String[] args)40     void dump(PrintWriter pw, String[] args);
41 
42     /**
43      * Sets if the current device is in power save mode.
44      */
setPowerSaveMode(boolean powerSave)45     default void setPowerSaveMode(boolean powerSave) {
46         setPowerSaveMode(powerSave, null);
47     }
48 
49     /**
50      * Sets if the current device is in power save mode.
51      *
52      * Can pass the view that triggered the request.
53      */
setPowerSaveMode(boolean powerSave, @Nullable Expandable expandable)54     void setPowerSaveMode(boolean powerSave, @Nullable Expandable expandable);
55 
56     /**
57      * Gets a reference to the last view used when called {@link #setPowerSaveMode}.
58      */
59     @Nullable
getLastPowerSaverStartExpandable()60     default WeakReference<Expandable> getLastPowerSaverStartExpandable() {
61         return null;
62     }
63 
64     /**
65      * Clears the last view used when called {@link #setPowerSaveMode}.
66      *
67      * Immediately after calling this, a call to {@link #getLastPowerSaverStartExpandable()} should
68      * return {@code null}.
69      */
clearLastPowerSaverStartExpandable()70     default void clearLastPowerSaverStartExpandable() {}
71 
72     /**
73      * Returns {@code true} if the device is currently plugged in.
74      */
isPluggedIn()75     boolean isPluggedIn();
76 
77     /**
78      * Returns {@code true} if the device is currently plugged in via wireless charger.
79      */
isPluggedInWireless()80     default boolean isPluggedInWireless() {
81         return false;
82     }
83 
84     /**
85      * Returns {@code true} if the device is currently in power save mode.
86      */
isPowerSave()87     boolean isPowerSave();
88 
89     /**
90      * Returns {@code true} if AOD was disabled by power saving policies.
91      */
isAodPowerSave()92     boolean isAodPowerSave();
93 
94     /**
95      * Initializes the class.
96      */
init()97     default void init() { }
98 
99     /**
100      * Returns {@code true} if the device is currently in wireless charging mode.
101      */
isWirelessCharging()102     default boolean isWirelessCharging() { return false; }
103 
104     /**
105      * Returns {@code true} if reverse is supported.
106      */
isReverseSupported()107     default boolean isReverseSupported() { return false; }
108 
109     /**
110      * Returns {@code true} if reverse is on.
111      */
isReverseOn()112     default boolean isReverseOn() { return false; }
113 
114     /**
115      * Set reverse state.
116      * @param isReverse true if turn on reverse, false otherwise
117      */
setReverseState(boolean isReverse)118     default void setReverseState(boolean isReverse) {}
119 
120     /**
121      * Returns {@code true} if extreme battery saver is on.
122      */
isExtremeSaverOn()123     default boolean isExtremeSaverOn() {
124         return false;
125     }
126 
127     /**
128      * Returns {@code true} if the charging source is
129      * {@link android.os.BatteryManager#BATTERY_PLUGGED_DOCK}.
130      *
131      * <P>Note that charging from dock is not considered as wireless charging. In other words,
132      * {@link BatteryController#isWirelessCharging()} and this are mutually exclusive.
133      */
isChargingSourceDock()134     default boolean isChargingSourceDock() {
135         return false;
136     }
137 
138     /**
139      * A listener that will be notified whenever a change in battery level or power save mode has
140      * occurred.
141      */
142     interface BatteryStateChangeCallback extends Dumpable {
143 
onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging)144         default void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
145         }
146 
onPowerSaveChanged(boolean isPowerSave)147         default void onPowerSaveChanged(boolean isPowerSave) {
148         }
149 
onBatteryUnknownStateChanged(boolean isUnknown)150         default void onBatteryUnknownStateChanged(boolean isUnknown) {
151         }
152 
onReverseChanged(boolean isReverse, int level, String name)153         default void onReverseChanged(boolean isReverse, int level, String name) {
154         }
155 
onExtremeBatterySaverChanged(boolean isExtreme)156         default void onExtremeBatterySaverChanged(boolean isExtreme) {
157         }
158 
onWirelessChargingChanged(boolean isWirlessCharging)159         default void onWirelessChargingChanged(boolean isWirlessCharging) {
160         }
161 
onIsBatteryDefenderChanged(boolean isBatteryDefender)162         default void onIsBatteryDefenderChanged(boolean isBatteryDefender) {
163         }
164 
onIsIncompatibleChargingChanged(boolean isIncompatibleCharging)165         default void onIsIncompatibleChargingChanged(boolean isIncompatibleCharging) {
166         }
167 
168         @Override
dump(@onNull PrintWriter pw, @NonNull String[] args)169         default void dump(@NonNull PrintWriter pw, @NonNull String[] args) {
170             pw.println(this);
171         }
172     }
173 
174     /**
175      * If available, get the estimated battery time remaining as a string.
176      *
177      * @param completion A lambda that will be called with the result of fetching the estimate. The
178      * first time this method is called may need to be dispatched to a background thread. The
179      * completion is called on the main thread
180      */
getEstimatedTimeRemainingString(EstimateFetchCompletion completion)181     default void getEstimatedTimeRemainingString(EstimateFetchCompletion completion) {}
182 
183     /**
184      * Callback called when the estimated time remaining text is fetched.
185      */
186     public interface EstimateFetchCompletion {
187 
188         /**
189          * The callback
190          * @param estimate the estimate
191          */
onBatteryRemainingEstimateRetrieved(@ullable String estimate)192         void onBatteryRemainingEstimateRetrieved(@Nullable String estimate);
193     }
194 }
195