1 /*
2  * Copyright (C) 2021 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 android.car.builtin.power;
18 
19 import android.annotation.SystemApi;
20 import android.content.Context;
21 import android.os.PowerManager;
22 import android.os.PowerManager.WakeLock;
23 
24 /**
25  * Helper for PowerManager related operations.
26  *
27  * @hide
28  */
29 @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES)
30 public final class PowerManagerHelper {
31 
32     /** See {@code PowerManager.BRIGHTNESS_ON} */
33     public static final int BRIGHTNESS_ON = PowerManager.BRIGHTNESS_ON;
34 
35     /** See {@code PowerManager.BRIGHTNESS_OFF} */
36     public static final int BRIGHTNESS_OFF = PowerManager.BRIGHTNESS_OFF;
37 
38     /** See {@code PowerManager.BRIGHTNESS_DEFAULT} */
39     public static final int BRIGHTNESS_DEFAULT = PowerManager.BRIGHTNESS_DEFAULT;
40 
41     /** See {@code PowerManager.BRIGHTNESS_INVALID} */
42     public static final int BRIGHTNESS_INVALID = PowerManager.BRIGHTNESS_INVALID;
43 
44     /** See {@code PowerManager.BRIGHTNESS_MAX} */
45     public static final float BRIGHTNESS_MAX = PowerManager.BRIGHTNESS_MAX;
46 
47     /** See {@code PowerManager.BRIGHTNESS_MIN} */
48     public static final float BRIGHTNESS_MIN = PowerManager.BRIGHTNESS_MIN;
49 
50     /** See {@code PowerManager.BRIGHTNESS_OFF_FLOAT} */
51     public static final float BRIGHTNESS_OFF_FLOAT = PowerManager.BRIGHTNESS_OFF_FLOAT;
52 
53     /** See {@code PowerManager.BRIGHTNESS_INVALID_FLOAT} */
54     public static final float BRIGHTNESS_INVALID_FLOAT = PowerManager.BRIGHTNESS_INVALID_FLOAT;
55 
PowerManagerHelper()56     private PowerManagerHelper() {
57         throw new UnsupportedOperationException("contains only static members");
58     }
59 
60     /**
61      * Gets the maximum supported screen brightness setting.
62      * This wraps {@link PowerManager.getMaximumScreenBrightnessSetting}.
63      *
64      * @param context Context to use.
65      * @return The maximum value that can be set by the user.
66      */
getMaximumScreenBrightnessSetting(Context context)67     public static int getMaximumScreenBrightnessSetting(Context context) {
68         return context.getSystemService(PowerManager.class).getMaximumScreenBrightnessSetting();
69     }
70 
71     /**
72      * Gets the minimum supported screen brightness setting.
73      * This wraps {@link PowerManager.getMinimumScreenBrightnessSetting}.
74      *
75      * @param context Context to use.
76      * @return The minimum value that can be set by the user.
77      */
getMinimumScreenBrightnessSetting(Context context)78     public static int getMinimumScreenBrightnessSetting(Context context) {
79         return context.getSystemService(PowerManager.class).getMinimumScreenBrightnessSetting();
80     }
81 
82     /**
83      * Forces the {@link com.android.server.display.DisplayGroup#DEFAULT default display group}
84      * to turn on or off.
85      *
86      * @param context Context to use.
87      * @param on Whether to turn the display on or off.
88      * @param upTime The time when the request was issued, in the {@link SystemClock#uptimeMillis}
89      *               time base.
90      */
setDisplayState(Context context, boolean on, long upTime)91     public static void setDisplayState(Context context, boolean on, long upTime) {
92         PowerManager powerManager = context.getSystemService(PowerManager.class);
93         if (on) {
94             powerManager.wakeUp(upTime, PowerManager.WAKE_REASON_UNKNOWN, "wake up by CarService");
95         } else {
96             powerManager.goToSleep(upTime,
97                     PowerManager.GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF, /* flags= */ 0);
98         }
99     }
100 
101     /**
102      * Turns off the display of {@code displayId}.
103      *
104      * @param context Context to use.
105      * @param displayId The display ID to turn off. If {@code displayId} is
106      *                  {@link Display#INVALID_DISPLAY}, then all displays are turned off.
107      * @param upTime The time when the request was issued, in the {@link SystemClock#uptimeMillis}
108      *               time base.
109      */
goToSleep(Context context, int displayId, long upTime)110     public static void goToSleep(Context context, int displayId, long upTime) {
111         context.getSystemService(PowerManager.class).goToSleep(displayId, upTime,
112                 PowerManager.GO_TO_SLEEP_REASON_DISPLAY_GROUPS_TURNED_OFF, /* flags= */ 0);
113     }
114 
115     /**
116      * Turns off the device.
117      *
118      * @param context Context to use.
119      * @param confirm If {@code true}, shows a shutdown confirmation dialog.
120      * @param reason Code to pass to android_reboot() (e.g. "userrequested"), or {@code null}.
121      * @param wait If {@code true}, this call waits for the shutdown to complete and does not
122      *             return.
123      */
shutdown(Context context, boolean confirm, String reason, boolean wait)124     public static void shutdown(Context context, boolean confirm, String reason, boolean wait) {
125         context.getSystemService(PowerManager.class).shutdown(confirm, reason, wait);
126     }
127 
128     /**
129      * Acquires a wake lock for the givien display.
130      *
131      * <p>This wraps {@link PowerManager#newWakeLock(int, String, int)}.
132      *
133      * @param context Context to use.
134      * @param levelAndFlags Combination of wake lock level and flag values defining the requested
135      *                      behavior of the WakeLock.
136      * @param tag Your class name (or other tag) for debugging purposes.
137      * @param displayId The display id to which this wake lock is tied.
138      *
139      * @see PowerManager#PARTIAL_WAKE_LOCK
140      * @see PowerManager#FULL_WAKE_LOCK
141      * @see PowerManager#SCREEN_DIM_WAKE_LOCK
142      * @see PowerManager#SCREEN_BRIGHT_WAKE_LOCK
143      * @see PowerManager#PROXIMITY_SCREEN_OFF_WAKE_LOCK
144      * @see PowerManager#ACQUIRE_CAUSES_WAKEUP
145      * @see PowerManager#ON_AFTER_RELEASE
146      */
newWakeLock(Context context, int levelAndFlags, String tag, int displayId)147     public static WakeLock newWakeLock(Context context, int levelAndFlags, String tag,
148             int displayId) {
149         PowerManager powerManager = context.getSystemService(PowerManager.class);
150         return powerManager.newWakeLock(levelAndFlags, tag, displayId);
151     }
152 }
153