1 /*
2  * Copyright (C) 2022 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.service.games.testing;
18 
19 import android.content.Intent;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 
26 import com.google.common.base.Preconditions;
27 
28 public final class ActivityResult implements Parcelable {
29     private final String mGameSessionPackageName;
30     @Nullable
31     private final Success mSuccess;
32     @Nullable
33     private final Failure mFailure;
34 
35     public static final Creator<ActivityResult> CREATOR = new Creator<ActivityResult>() {
36         @Override
37         public ActivityResult createFromParcel(Parcel in) {
38             String gameSessionPackageName = in.readString();
39             Success success = in.readParcelable(Success.class.getClassLoader(), Success.class);
40             Failure failure = in.readParcelable(Failure.class.getClassLoader(), Failure.class);
41             return new ActivityResult(gameSessionPackageName, success, failure);
42         }
43 
44         @Override
45         public ActivityResult[] newArray(int size) {
46             return new ActivityResult[size];
47         }
48     };
49 
forSuccess(String gameSessionPackageName, int resultCode, @Nullable Intent data)50     public static ActivityResult forSuccess(String gameSessionPackageName, int resultCode,
51             @Nullable Intent data) {
52         return new ActivityResult(gameSessionPackageName, new Success(resultCode, data), null);
53     }
54 
forError(String gameSessionPackageName, Throwable t)55     public static ActivityResult forError(String gameSessionPackageName, Throwable t) {
56         return new ActivityResult(gameSessionPackageName, null,
57                 new Failure(t.getClass(), t.getMessage()));
58     }
59 
ActivityResult(String gameSessionPackageName, @Nullable Success success, @Nullable Failure failure)60     private ActivityResult(String gameSessionPackageName, @Nullable Success success,
61             @Nullable Failure failure) {
62         mGameSessionPackageName = gameSessionPackageName;
63         mSuccess = success;
64         mFailure = failure;
65     }
66 
getGameSessionPackageName()67     public String getGameSessionPackageName() {
68         return mGameSessionPackageName;
69     }
70 
isSuccess()71     public boolean isSuccess() {
72         return mSuccess != null;
73     }
74 
getSuccess()75     public Success getSuccess() {
76         Preconditions.checkState(isSuccess());
77         return mSuccess;
78     }
79 
getFailure()80     public Failure getFailure() {
81         Preconditions.checkState(!isSuccess());
82         return mFailure;
83     }
84 
85     @Override
describeContents()86     public int describeContents() {
87         return 0;
88     }
89 
90     @Override
writeToParcel(@onNull Parcel dest, int flags)91     public void writeToParcel(@NonNull Parcel dest, int flags) {
92         dest.writeString(mGameSessionPackageName);
93         dest.writeParcelable(mSuccess, flags);
94         dest.writeParcelable(mFailure, flags);
95     }
96 
97     public static final class Success implements Parcelable {
98         public static final Creator<Success> CREATOR = new Creator<Success>() {
99             @Override
100             public Success createFromParcel(Parcel source) {
101                 int resultCode = source.readInt();
102                 Intent data = source.readParcelable(Intent.class.getClassLoader(), Intent.class);
103                 return new Success(resultCode, data);
104             }
105 
106             @Override
107             public Success[] newArray(int size) {
108                 return new Success[0];
109             }
110         };
111 
112         private final int mResultCode;
113         @Nullable
114         private final Intent mData;
115 
Success(int resultCode, @Nullable Intent data)116         Success(int resultCode, @Nullable Intent data) {
117             mResultCode = resultCode;
118             mData = data;
119         }
120 
getResultCode()121         public int getResultCode() {
122             return mResultCode;
123         }
124 
125         @Nullable
getData()126         public Intent getData() {
127             return mData;
128         }
129 
130         @Override
describeContents()131         public int describeContents() {
132             return 0;
133         }
134 
135         @Override
writeToParcel(@onNull Parcel dest, int flags)136         public void writeToParcel(@NonNull Parcel dest, int flags) {
137             dest.writeInt(mResultCode);
138             dest.writeParcelable(mData, flags);
139         }
140     }
141 
142     public static final class Failure implements Parcelable {
143         public static final Creator<Failure> CREATOR = new Creator<Failure>() {
144             @Override
145             public Failure createFromParcel(Parcel source) {
146                 Class<?> clazz = source.readSerializable(Class.class.getClassLoader(), Class.class);
147                 String message = source.readString();
148                 return new Failure(clazz, message);
149             }
150 
151             @Override
152             public Failure[] newArray(int size) {
153                 return new Failure[0];
154             }
155         };
156 
157         private final Class<?> mClazz;
158         private final String mMessage;
159 
Failure(Class<?> clazz, String message)160         Failure(Class<?> clazz, String message) {
161             mClazz = clazz;
162             mMessage = message;
163         }
164 
getClazz()165         public Class<?> getClazz() {
166             return mClazz;
167         }
168 
getMessage()169         public String getMessage() {
170             return mMessage;
171         }
172 
173         @Override
describeContents()174         public int describeContents() {
175             return 0;
176         }
177 
178         @Override
writeToParcel(@onNull Parcel dest, int flags)179         public void writeToParcel(@NonNull Parcel dest, int flags) {
180             dest.writeSerializable(mClazz);
181             dest.writeString(mMessage);
182         }
183     }
184 }
185