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 com.android.server.devicestate;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.hardware.devicestate.DeviceState;
22 import android.hardware.devicestate.DeviceStateRequest;
23 import android.os.IBinder;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * A request to override the state managed by {@link DeviceStateManagerService}.
30  *
31  * @see OverrideRequestController
32  */
33 final class OverrideRequest {
34     private final IBinder mToken;
35     private final int mPid;
36     private final int mUid;
37     @NonNull
38     private final DeviceState mRequestedState;
39     @DeviceStateRequest.RequestFlags
40     private final int mFlags;
41     @OverrideRequestType
42     private final int mRequestType;
43 
44     /**
45      * Denotes that the request is meant to override the emulated state of the device. This will
46      * not change the base (physical) state of the device.
47      *
48      * This request type should be used if you are looking to emulate a device state for a feature
49      * but want the system to be aware of the physical state of the device.
50      */
51     public static final int OVERRIDE_REQUEST_TYPE_EMULATED_STATE = 0;
52 
53     /**
54      * Denotes that the request is meant to override the base (physical) state of the device.
55      * Overriding the base state may not change the emulated state of the device if there is also an
56      * override request active for that property.
57      *
58      * This request type should only be used for testing, where you want to simulate the physical
59      * state of the device changing.
60      */
61     public static final int OVERRIDE_REQUEST_TYPE_BASE_STATE = 1;
62 
63     /**
64      * Flags for signifying the type of {@link OverrideRequest}.
65      *
66      * @hide
67      */
68     @IntDef(flag = true, prefix = { "REQUEST_TYPE_" }, value = {
69             OVERRIDE_REQUEST_TYPE_BASE_STATE,
70             OVERRIDE_REQUEST_TYPE_EMULATED_STATE
71     })
72     @Retention(RetentionPolicy.SOURCE)
73     public @interface OverrideRequestType {}
74 
OverrideRequest(IBinder token, int pid, int uid, @NonNull DeviceState requestedState, @DeviceStateRequest.RequestFlags int flags, @OverrideRequestType int requestType)75     OverrideRequest(IBinder token, int pid, int uid,
76             @NonNull DeviceState requestedState, @DeviceStateRequest.RequestFlags int flags,
77             @OverrideRequestType int requestType) {
78         mToken = token;
79         mPid = pid;
80         mUid = uid;
81         mRequestedState = requestedState;
82         mFlags = flags;
83         mRequestType = requestType;
84     }
85 
getToken()86     IBinder getToken() {
87         return mToken;
88     }
89 
getPid()90     int getPid() {
91         return mPid;
92     }
93 
getUid()94     int getUid() {
95         return mUid;
96     }
97 
98     @NonNull
getRequestedDeviceState()99     DeviceState getRequestedDeviceState() {
100         return mRequestedState;
101     }
102 
getRequestedStateIdentifier()103     int getRequestedStateIdentifier() {
104         return mRequestedState.getIdentifier();
105     }
106 
107     @DeviceStateRequest.RequestFlags
getFlags()108     int getFlags() {
109         return mFlags;
110     }
111 
112     @OverrideRequestType
getRequestType()113     int getRequestType() {
114         return mRequestType;
115     }
116 }
117