1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.hardware.devicestate;
import android.Manifest;
import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.SystemService;
import android.annotation.TestApi;
import android.content.Context;
import com.android.internal.util.ArrayUtils;
import java.util.concurrent.Executor;
import java.util.function.Consumer;
/**
* Manages the state of the system for devices with user-configurable hardware like a foldable
* phone.
*
* @hide
*/
@TestApi
@SystemService(Context.DEVICE_STATE_SERVICE)
public final class DeviceStateManager {
/**
* Invalid device state.
*
* @hide
*/
public static final int INVALID_DEVICE_STATE = -1;
/** The minimum allowed device state identifier. */
public static final int MINIMUM_DEVICE_STATE = 0;
/** The maximum allowed device state identifier. */
public static final int MAXIMUM_DEVICE_STATE = 255;
/**
* Intent needed to launch the rear display overlay activity from SysUI
*
* @hide
*/
public static final String ACTION_SHOW_REAR_DISPLAY_OVERLAY =
"com.android.intent.action.SHOW_REAR_DISPLAY_OVERLAY";
/**
* Intent extra sent to the rear display overlay activity of the current base state
*
* @hide
*/
public static final String EXTRA_ORIGINAL_DEVICE_BASE_STATE =
"original_device_base_state";
private final DeviceStateManagerGlobal mGlobal;
/** @hide */
public DeviceStateManager() {
DeviceStateManagerGlobal global = DeviceStateManagerGlobal.getInstance();
if (global == null) {
throw new IllegalStateException(
"Failed to get instance of global device state manager.");
}
mGlobal = global;
}
/**
* Returns the list of device states that are supported and can be requested with
* {@link #requestState(DeviceStateRequest, Executor, DeviceStateRequest.Callback)}.
*/
@NonNull
public int[] getSupportedStates() {
return mGlobal.getSupportedStates();
}
/**
* Submits a {@link DeviceStateRequest request} to modify the device state.
* <p>
* By default, the request is kept active until one of the following occurs:
* <ul>
* <li>The system deems the request can no longer be honored, for example if the requested
* state becomes unsupported.
* <li>A call to {@link #cancelStateRequest}.
* <li>Another processes submits a request succeeding this request in which case the request
* will be canceled.
* </ul>
* However, this behavior can be changed by setting flags on the {@link DeviceStateRequest}.
*
* @throws IllegalArgumentException if the requested state is unsupported.
* @throws SecurityException if the caller is neither the current top-focused activity nor if
* the {@link android.Manifest.permission#CONTROL_DEVICE_STATE} permission is held.
*
* @see DeviceStateRequest
*/
@RequiresPermission(value = android.Manifest.permission.CONTROL_DEVICE_STATE,
conditional = true)
public void requestState(@NonNull DeviceStateRequest request,
@Nullable @CallbackExecutor Executor executor,
@Nullable DeviceStateRequest.Callback callback) {
mGlobal.requestState(request, executor, callback);
}
/**
* Cancels the active {@link DeviceStateRequest} previously submitted with a call to
* {@link #requestState(DeviceStateRequest, Executor, DeviceStateRequest.Callback)}.
* <p>
* This method is noop if there is no request currently active.
*
* @throws SecurityException if the caller is neither the current top-focused activity nor if
* the {@link android.Manifest.permission#CONTROL_DEVICE_STATE} permission is held.
*/
@RequiresPermission(value = android.Manifest.permission.CONTROL_DEVICE_STATE,
conditional = true)
public void cancelStateRequest() {
mGlobal.cancelStateRequest();
}
/**
* Submits a {@link DeviceStateRequest request} to override the base state of the device. This
* should only be used for testing, where you want to simulate the physical change to the
* device state.
* <p>
* By default, the request is kept active until one of the following occurs:
* <ul>
* <li>The physical state of the device changes</li>
* <li>The system deems the request can no longer be honored, for example if the requested
* state becomes unsupported.
* <li>A call to {@link #cancelBaseStateOverride}.
* <li>Another processes submits a request succeeding this request in which case the request
* will be canceled.
* </ul>
*
* Submitting a base state override request may not cause any change in the presentation
* of the system if there is an emulated request made through {@link #requestState}, as the
* emulated override requests take priority.
*
* @throws IllegalArgumentException if the requested state is unsupported.
* @throws SecurityException if the caller does not hold the
* {@link android.Manifest.permission#CONTROL_DEVICE_STATE} permission.
*
* @see DeviceStateRequest
*/
@RequiresPermission(android.Manifest.permission.CONTROL_DEVICE_STATE)
public void requestBaseStateOverride(@NonNull DeviceStateRequest request,
@Nullable @CallbackExecutor Executor executor,
@Nullable DeviceStateRequest.Callback callback) {
mGlobal.requestBaseStateOverride(request, executor, callback);
}
/**
* Cancels the active {@link DeviceStateRequest} previously submitted with a call to
* {@link #requestBaseStateOverride(DeviceStateRequest, Executor, DeviceStateRequest.Callback)}.
* <p>
* This method is noop if there is no base state request currently active.
*
* @throws SecurityException if the caller does not hold the
* {@link android.Manifest.permission#CONTROL_DEVICE_STATE} permission.
*/
@RequiresPermission(Manifest.permission.CONTROL_DEVICE_STATE)
public void cancelBaseStateOverride() {
mGlobal.cancelBaseStateOverride();
}
/**
* Registers a callback to receive notifications about changes in device state.
*
* @param executor the executor to process notifications.
* @param callback the callback to register.
*
* @see DeviceStateCallback
*/
public void registerCallback(@NonNull @CallbackExecutor Executor executor,
@NonNull DeviceStateCallback callback) {
mGlobal.registerDeviceStateCallback(callback, executor);
}
/**
* Unregisters a callback previously registered with
* {@link #registerCallback(Executor, DeviceStateCallback)}.
*/
public void unregisterCallback(@NonNull DeviceStateCallback callback) {
mGlobal.unregisterDeviceStateCallback(callback);
}
/** Callback to receive notifications about changes in device state. */
public interface DeviceStateCallback {
/**
* Called in response to a change in the states supported by the device.
* <p>
* Guaranteed to be called once on registration of the callback with the initial value and
* then on every subsequent change in the supported states.
*
* @param supportedStates the new supported states.
*
* @see DeviceStateManager#getSupportedStates()
*/
default void onSupportedStatesChanged(@NonNull int[] supportedStates) {}
/**
* Called in response to a change in the base device state.
* <p>
* The base state is the state of the device without considering any requests made through
* calls to {@link #requestState(DeviceStateRequest, Executor, DeviceStateRequest.Callback)}
* from any client process. The base state is guaranteed to match the state provided with a
* call to {@link #onStateChanged(int)} when there are no active requests from any process.
* <p>
* Guaranteed to be called once on registration of the callback with the initial value and
* then on every subsequent change in the non-override state.
*
* @param state the new base device state.
*/
default void onBaseStateChanged(int state) {}
/**
* Called in response to device state changes.
* <p>
* Guaranteed to be called once on registration of the callback with the initial value and
* then on every subsequent change in device state.
*
* @param state the new device state.
*/
void onStateChanged(int state);
}
/**
* Listens to changes in device state and reports the state as folded if the device state
* matches the value in the {@link com.android.internal.R.integer.config_foldedDeviceState}
* resource.
* @hide
*/
public static class FoldStateListener implements DeviceStateCallback {
private final int[] mFoldedDeviceStates;
private final Consumer<Boolean> mDelegate;
@Nullable
private Boolean lastResult;
public FoldStateListener(Context context, Consumer<Boolean> listener) {
mFoldedDeviceStates = context.getResources().getIntArray(
com.android.internal.R.array.config_foldedDeviceStates);
mDelegate = listener;
}
@Override
public final void onStateChanged(int state) {
final boolean folded = ArrayUtils.contains(mFoldedDeviceStates, state);
if (lastResult == null || !lastResult.equals(folded)) {
lastResult = folded;
mDelegate.accept(folded);
}
}
}
}
|