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.app;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 
27 /**
28  * State of the game passed to the GameManager.
29  *
30  * This includes a top-level state for the game (indicating if the game can be interrupted without
31  * interfering with content that can't be paused). Since content can be loaded in any state, it
32  * includes an independent boolean flag to indicate loading status.
33  *
34  * Free-form metadata (as a Bundle) and a string description can also be specified by the
35  * application.
36  */
37 public final class GameState implements Parcelable {
38     /**
39      * Default Game mode is unknown.
40      */
41     public static final int MODE_UNKNOWN = 0;
42 
43     /**
44      * No mode means that the game is not in active play, for example the user is using the game
45      * menu.
46      */
47     public static final int MODE_NONE = 1;
48 
49     /**
50      * Indicates if the game is in active, but interruptible, game play.
51      */
52     public static final int MODE_GAMEPLAY_INTERRUPTIBLE = 2;
53 
54     /**
55      * Indicates if the game is in active user play mode, which is real time and cannot be
56      *  interrupted.
57      */
58     public static final int MODE_GAMEPLAY_UNINTERRUPTIBLE = 3;
59 
60     /**
61      * Indicates that the current content shown is not gameplay related. For example it can be an
62      * ad, a web page, a text, or a video.
63      */
64     public static final int MODE_CONTENT = 4;
65 
66     /**
67      * Implement the parcelable interface.
68      */
69     public static final @NonNull Creator<GameState> CREATOR = new Creator<GameState>() {
70         @Override
71         public GameState createFromParcel(Parcel in) {
72             return new GameState(in);
73         }
74 
75         @Override
76         public GameState[] newArray(int size) {
77             return new GameState[size];
78         }
79     };
80 
81     // Indicates if the game is loading assets/resources/compiling/etc. This is independent of game
82     // mode because there could be a loading UI displayed, or there could be loading in the
83     // background.
84     private final boolean mIsLoading;
85 
86     // One of the states listed above.
87     private final @GameStateMode int mMode;
88 
89     // A developer-supplied enum, e.g. to indicate level or scene.
90     private final int mLabel;
91 
92     // The developer-supplied enum, e.g. to indicate the current quality level.
93     private final int mQuality;
94 
95     /**
96      * Create a GameState with the specified loading status.
97      * @param isLoading Whether the game is in the loading state.
98      * @param mode The game state mode of type @GameStateMode.
99      */
GameState(boolean isLoading, @GameStateMode int mode)100     public GameState(boolean isLoading, @GameStateMode int mode) {
101         this(isLoading, mode, -1, -1);
102     }
103 
104     /**
105      * Create a GameState with the given state variables.
106      * @param isLoading Whether the game is in the loading state.
107      * @param mode The game state mode.
108      * @param label An optional developer-supplied enum e.g. for the current level.
109      * @param quality An optional developer-supplied enum, e.g. for the current quality level.
110      */
GameState(boolean isLoading, @GameStateMode int mode, int label, int quality)111     public GameState(boolean isLoading, @GameStateMode int mode, int label, int quality) {
112         mIsLoading = isLoading;
113         mMode = mode;
114         mLabel = label;
115         mQuality = quality;
116     }
117 
GameState(Parcel in)118     private GameState(Parcel in) {
119         mIsLoading = in.readBoolean();
120         mMode = in.readInt();
121         mLabel = in.readInt();
122         mQuality = in.readInt();
123     }
124 
125     /**
126      * @return If the game is loading assets/resources/compiling/etc.
127      */
isLoading()128     public boolean isLoading() {
129         return mIsLoading;
130     }
131 
132     /**
133      * @return The game state mode.
134      */
getMode()135     public @GameStateMode int getMode() {
136         return mMode;
137     }
138 
139     /**
140      * @return The developer-supplied enum, e.g. to indicate level or scene. The default value (if
141      * not supplied) is -1.
142      */
getLabel()143     public int getLabel() {
144         return mLabel;
145     }
146 
147     /**
148      * @return The developer-supplied enum, e.g. to indicate the current quality level. The default
149      * value (if not suplied) is -1.
150      */
getQuality()151     public int getQuality() {
152         return mQuality;
153     }
154 
155     @Override
describeContents()156     public int describeContents() {
157         return 0;
158     }
159 
160     @Override
writeToParcel(@onNull Parcel parcel, int flags)161     public void writeToParcel(@NonNull Parcel parcel, int flags) {
162         parcel.writeBoolean(mIsLoading);
163         parcel.writeInt(mMode);
164         parcel.writeInt(mLabel);
165         parcel.writeInt(mQuality);
166     }
167 
168     /**
169      * @hide
170      */
171     @Retention(RetentionPolicy.SOURCE)
172     @IntDef({MODE_UNKNOWN, MODE_NONE, MODE_GAMEPLAY_INTERRUPTIBLE, MODE_GAMEPLAY_UNINTERRUPTIBLE,
173             MODE_CONTENT
174 
175     })
176     @interface GameStateMode {}
177 }
178