1 /*
2  * Copyright (C) 2020 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.user;
18 
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.os.Parcelable;
22 
23 import com.android.internal.util.DataClass;
24 
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.RetentionPolicy;
27 
28 /**
29  * User switch results.
30  *
31  * @hide
32  */
33 @DataClass(
34         genToString = true,
35         genHiddenConstructor = true,
36         genHiddenConstDefs = true)
37 public final class UserSwitchResult implements Parcelable {
38 
39     /**
40      * When user switch is successful for both HAL and Android.
41      */
42     public static final int STATUS_SUCCESSFUL = CommonResults.STATUS_SUCCESSFUL;
43 
44     /**
45      * When user switch is only successful for Hal but not for Android. Hal user switch rollover
46      * message have been sent.
47      */
48     public static final int STATUS_ANDROID_FAILURE = CommonResults.STATUS_ANDROID_FAILURE;
49 
50     /**
51      * When user switch fails for HAL. User switch for Android is not called.
52      */
53     public static final int STATUS_HAL_FAILURE = CommonResults.STATUS_HAL_FAILURE;
54 
55     /**
56      * When user switch fails for HAL for some internal error. User switch for Android is not
57      * called.
58      */
59     public static final int STATUS_HAL_INTERNAL_FAILURE = CommonResults.STATUS_HAL_INTERNAL_FAILURE;
60 
61     /**
62      * When given parameters or environment states are invalid for switching user. HAL or Android
63      * user switch is not requested.
64      */
65     public static final int STATUS_INVALID_REQUEST = CommonResults.STATUS_INVALID_REQUEST;
66 
67     /**
68      * When target user is same as current user.
69      */
70     public static final int STATUS_OK_USER_ALREADY_IN_FOREGROUND =
71             CommonResults.LAST_COMMON_STATUS + 1;
72 
73     /**
74      * When another user switch request for the same target user is in process.
75      */
76     public static final int STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO =
77             CommonResults.LAST_COMMON_STATUS + 2;
78 
79     /**
80      * When another user switch request for a new different target user is received. Previous
81      * request is abandoned.
82      */
83     public static final int STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST =
84             CommonResults.LAST_COMMON_STATUS + 3;
85 
86     /**
87      * When switching users is currently not allowed for the user this process is running under.
88      */
89     public static final int STATUS_NOT_SWITCHABLE =
90             CommonResults.LAST_COMMON_STATUS + 4;
91 
92     /**
93      * Gets the user switch result status.
94      *
95      * @return either {@link UserSwitchResult#STATUS_SUCCESSFUL},
96      *         {@link UserSwitchResult#STATUS_ANDROID_FAILURE},
97      *         {@link UserSwitchResult#STATUS_HAL_FAILURE},
98      *         {@link UserSwitchResult#STATUS_HAL_INTERNAL_FAILURE},
99      *         {@link UserSwitchResult#STATUS_INVALID_REQUEST},
100      *         {@link UserSwitchResult#STATUS_OK_USER_ALREADY_IN_FOREGROUND},
101      *         {@link UserSwitchResult#STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO},
102      *         {@link UserSwitchResult#STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST}, or
103      *         {@link UserSwitchResult#STATUS_NOT_SWITCHABLE}.
104      */
105     private final @Status int mStatus;
106 
107     /**
108      * Gets the error message, if any.
109      */
110     @Nullable
111     private final String mErrorMessage;
112 
113     /**
114      * Check if {@link UserSwitchResult} is successful.
115      */
isSuccess()116     public boolean isSuccess() {
117         return mStatus == STATUS_SUCCESSFUL || mStatus == STATUS_OK_USER_ALREADY_IN_FOREGROUND;
118     }
119 
120     // TODO(b/158195639): if you change any status constant, you need to manually assign its values
121     // (rather than using CommonResults) before running codegen to regenerate the class
122 
123 
124     // Code below generated by codegen v1.0.15.
125     //
126     // DO NOT MODIFY!
127     // CHECKSTYLE:OFF Generated code
128     //
129     // To regenerate run:
130     // $ codegen $ANDROID_BUILD_TOP/packages/services/Car/car-lib/src/android/car/user/UserSwitchResult.java
131     //
132     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
133     //   Settings > Editor > Code Style > Formatter Control
134     //@formatter:off
135 
136 
137     /** @hide */
138     @IntDef(prefix = "STATUS_", value = {
139         STATUS_SUCCESSFUL,
140         STATUS_ANDROID_FAILURE,
141         STATUS_HAL_FAILURE,
142         STATUS_HAL_INTERNAL_FAILURE,
143         STATUS_INVALID_REQUEST,
144         STATUS_OK_USER_ALREADY_IN_FOREGROUND,
145         STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO,
146         STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST,
147         STATUS_NOT_SWITCHABLE
148     })
149     @Retention(RetentionPolicy.SOURCE)
150     @DataClass.Generated.Member
151     public @interface Status {}
152 
153     /** @hide */
154     @DataClass.Generated.Member
statusToString(@tatus int value)155     public static String statusToString(@Status int value) {
156         switch (value) {
157             case STATUS_SUCCESSFUL:
158                     return "STATUS_SUCCESSFUL";
159             case STATUS_ANDROID_FAILURE:
160                     return "STATUS_ANDROID_FAILURE";
161             case STATUS_HAL_FAILURE:
162                     return "STATUS_HAL_FAILURE";
163             case STATUS_HAL_INTERNAL_FAILURE:
164                     return "STATUS_HAL_INTERNAL_FAILURE";
165             case STATUS_INVALID_REQUEST:
166                     return "STATUS_INVALID_REQUEST";
167             case STATUS_OK_USER_ALREADY_IN_FOREGROUND:
168                     return "STATUS_OK_USER_ALREADY_IN_FOREGROUND";
169             case STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO:
170                     return "STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO";
171             case STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST:
172                     return "STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST";
173             case STATUS_NOT_SWITCHABLE:
174                     return "STATUS_NOT_SWITCHABLE";
175             default: return Integer.toHexString(value);
176         }
177     }
178 
179     /**
180      * Creates a new UserSwitchResult.
181      *
182      * @param status
183      *   Gets the user switch result status.
184      *
185      *   @return either {@link UserSwitchResult#STATUS_SUCCESSFUL},
186      *           {@link UserSwitchResult#STATUS_ANDROID_FAILURE},
187      *           {@link UserSwitchResult#STATUS_HAL_FAILURE},
188      *           {@link UserSwitchResult#STATUS_HAL_INTERNAL_FAILURE},
189      *           {@link UserSwitchResult#STATUS_INVALID_REQUEST},
190      *           {@link UserSwitchResult#STATUS_OK_USER_ALREADY_IN_FOREGROUND},
191      *           {@link UserSwitchResult#STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO},
192      *           {@link UserSwitchResult#STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST}, or
193      *           {@link UserSwitchResult#STATUS_NOT_SWITCHABLE}.
194      * @param errorMessage
195      *   Gets the error message, if any.
196      * @hide
197      */
198     @DataClass.Generated.Member
UserSwitchResult( @tatus int status, @Nullable String errorMessage)199     public UserSwitchResult(
200             @Status int status,
201             @Nullable String errorMessage) {
202         this.mStatus = status;
203 
204         if (!(mStatus == STATUS_SUCCESSFUL)
205                 && !(mStatus == STATUS_ANDROID_FAILURE)
206                 && !(mStatus == STATUS_HAL_FAILURE)
207                 && !(mStatus == STATUS_HAL_INTERNAL_FAILURE)
208                 && !(mStatus == STATUS_INVALID_REQUEST)
209                 && !(mStatus == STATUS_OK_USER_ALREADY_IN_FOREGROUND)
210                 && !(mStatus == STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO)
211                 && !(mStatus == STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST)
212                 && !(mStatus == STATUS_NOT_SWITCHABLE)) {
213             throw new java.lang.IllegalArgumentException(
214                     "status was " + mStatus + " but must be one of: "
215                             + "STATUS_SUCCESSFUL(" + STATUS_SUCCESSFUL + "), "
216                             + "STATUS_ANDROID_FAILURE(" + STATUS_ANDROID_FAILURE + "), "
217                             + "STATUS_HAL_FAILURE(" + STATUS_HAL_FAILURE + "), "
218                             + "STATUS_HAL_INTERNAL_FAILURE(" + STATUS_HAL_INTERNAL_FAILURE + "), "
219                             + "STATUS_INVALID_REQUEST(" + STATUS_INVALID_REQUEST + "), "
220                             + "STATUS_OK_USER_ALREADY_IN_FOREGROUND(" + STATUS_OK_USER_ALREADY_IN_FOREGROUND + "), "
221                             + "STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO(" + STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO + "), "
222                             + "STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST(" + STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST + "), "
223                             + "STATUS_NOT_SWITCHABLE(" + STATUS_NOT_SWITCHABLE + ")");
224         }
225 
226         this.mErrorMessage = errorMessage;
227 
228         // onConstructed(); // You can define this method to get a callback
229     }
230 
231     /**
232      * Gets the user switch result status.
233      *
234      * @return either {@link UserSwitchResult#STATUS_SUCCESSFUL},
235      *         {@link UserSwitchResult#STATUS_ANDROID_FAILURE},
236      *         {@link UserSwitchResult#STATUS_HAL_FAILURE},
237      *         {@link UserSwitchResult#STATUS_HAL_INTERNAL_FAILURE},
238      *         {@link UserSwitchResult#STATUS_INVALID_REQUEST},
239      *         {@link UserSwitchResult#STATUS_OK_USER_ALREADY_IN_FOREGROUND},
240      *         {@link UserSwitchResult#STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO},
241      *         {@link UserSwitchResult#STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST}, or
242      *         {@link UserSwitchResult#STATUS_NOT_SWITCHABLE}.
243      */
244     @DataClass.Generated.Member
getStatus()245     public @Status int getStatus() {
246         return mStatus;
247     }
248 
249     /**
250      * Gets the error message, if any.
251      */
252     @DataClass.Generated.Member
getErrorMessage()253     public @Nullable String getErrorMessage() {
254         return mErrorMessage;
255     }
256 
257     @Override
258     @DataClass.Generated.Member
toString()259     public String toString() {
260         // You can override field toString logic by defining methods like:
261         // String fieldNameToString() { ... }
262 
263         return "UserSwitchResult { " +
264                 "status = " + statusToString(mStatus) + ", " +
265                 "errorMessage = " + mErrorMessage +
266         " }";
267     }
268 
269     @Override
270     @DataClass.Generated.Member
writeToParcel(@ndroid.annotation.NonNull android.os.Parcel dest, int flags)271     public void writeToParcel(@android.annotation.NonNull android.os.Parcel dest, int flags) {
272         // You can override field parcelling by defining methods like:
273         // void parcelFieldName(Parcel dest, int flags) { ... }
274 
275         byte flg = 0;
276         if (mErrorMessage != null) flg |= 0x2;
277         dest.writeByte(flg);
278         dest.writeInt(mStatus);
279         if (mErrorMessage != null) dest.writeString(mErrorMessage);
280     }
281 
282     @Override
283     @DataClass.Generated.Member
describeContents()284     public int describeContents() { return 0; }
285 
286     /** @hide */
287     @SuppressWarnings({"unchecked", "RedundantCast"})
288     @DataClass.Generated.Member
UserSwitchResult(@ndroid.annotation.NonNull android.os.Parcel in)289     /* package-private */ UserSwitchResult(@android.annotation.NonNull android.os.Parcel in) {
290         // You can override field unparcelling by defining methods like:
291         // static FieldType unparcelFieldName(Parcel in) { ... }
292 
293         byte flg = in.readByte();
294         int status = in.readInt();
295         String errorMessage = (flg & 0x2) == 0 ? null : in.readString();
296 
297         this.mStatus = status;
298 
299         if (!(mStatus == STATUS_SUCCESSFUL)
300                 && !(mStatus == STATUS_ANDROID_FAILURE)
301                 && !(mStatus == STATUS_HAL_FAILURE)
302                 && !(mStatus == STATUS_HAL_INTERNAL_FAILURE)
303                 && !(mStatus == STATUS_INVALID_REQUEST)
304                 && !(mStatus == STATUS_OK_USER_ALREADY_IN_FOREGROUND)
305                 && !(mStatus == STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO)
306                 && !(mStatus == STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST)
307                 && !(mStatus == STATUS_NOT_SWITCHABLE)) {
308             throw new java.lang.IllegalArgumentException(
309                     "status was " + mStatus + " but must be one of: "
310                             + "STATUS_SUCCESSFUL(" + STATUS_SUCCESSFUL + "), "
311                             + "STATUS_ANDROID_FAILURE(" + STATUS_ANDROID_FAILURE + "), "
312                             + "STATUS_HAL_FAILURE(" + STATUS_HAL_FAILURE + "), "
313                             + "STATUS_HAL_INTERNAL_FAILURE(" + STATUS_HAL_INTERNAL_FAILURE + "), "
314                             + "STATUS_INVALID_REQUEST(" + STATUS_INVALID_REQUEST + "), "
315                             + "STATUS_OK_USER_ALREADY_IN_FOREGROUND(" + STATUS_OK_USER_ALREADY_IN_FOREGROUND + "), "
316                             + "STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO(" + STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO + "), "
317                             + "STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST(" + STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST + "), "
318                             + "STATUS_NOT_SWITCHABLE(" + STATUS_NOT_SWITCHABLE + ")");
319         }
320 
321         this.mErrorMessage = errorMessage;
322 
323         // onConstructed(); // You can define this method to get a callback
324     }
325 
326     @DataClass.Generated.Member
327     public static final @android.annotation.NonNull Parcelable.Creator<UserSwitchResult> CREATOR
328             = new Parcelable.Creator<UserSwitchResult>() {
329         @Override
330         public UserSwitchResult[] newArray(int size) {
331             return new UserSwitchResult[size];
332         }
333 
334         @Override
335         public UserSwitchResult createFromParcel(@android.annotation.NonNull android.os.Parcel in) {
336             return new UserSwitchResult(in);
337         }
338     };
339 
340     @DataClass.Generated(
341             time = 1592422606349L,
342             codegenVersion = "1.0.15",
343             sourceFile = "packages/services/Car/car-lib/src/android/car/user/UserSwitchResult.java",
344             inputSignatures = "public static final  int STATUS_SUCCESSFUL\npublic static final  int STATUS_ANDROID_FAILURE\npublic static final  int STATUS_HAL_FAILURE\npublic static final  int STATUS_HAL_INTERNAL_FAILURE\npublic static final  int STATUS_INVALID_REQUEST\npublic static final  int STATUS_OK_USER_ALREADY_IN_FOREGROUND\npublic static final  int STATUS_TARGET_USER_ALREADY_BEING_SWITCHED_TO\npublic static final  int STATUS_TARGET_USER_ABANDONED_DUE_TO_A_NEW_REQUEST\npublic static final  int STATUS_NOT_SWITCHABLE\nprivate final @android.car.user.UserSwitchResult.Status int mStatus\nprivate final @android.annotation.Nullable java.lang.String mErrorMessage\npublic  boolean isSuccess()\nclass UserSwitchResult extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genToString=true, genHiddenConstructor=true, genHiddenConstDefs=true)")
345     @Deprecated
__metadata()346     private void __metadata() {}
347 
348 
349     //@formatter:on
350     // End of generated code
351 }
352