|
| 1 | +/* |
| 2 | + * Copyright (C) 2013 Google Inc. |
| 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.google.example.games.basegameutils; |
| 18 | + |
| 19 | +import android.content.Intent; |
| 20 | +import android.os.Bundle; |
| 21 | +import android.support.v4.app.FragmentActivity; |
| 22 | +import android.util.Log; |
| 23 | + |
| 24 | +import com.google.android.gms.common.api.GoogleApiClient; |
| 25 | + |
| 26 | +/** |
| 27 | + * Example base class for games. This implementation takes care of setting up |
| 28 | + * the API client object and managing its lifecycle. Subclasses only need to |
| 29 | + * override the @link{#onSignInSucceeded} and @link{#onSignInFailed} abstract |
| 30 | + * methods. To initiate the sign-in flow when the user clicks the sign-in |
| 31 | + * button, subclasses should call @link{#beginUserInitiatedSignIn}. By default, |
| 32 | + * this class only instantiates the GoogleApiClient object. If the PlusClient |
| 33 | + * is also wanted, call the BaseGameActivity(int) |
| 34 | + * constructor and specify the requested clients. For example, to request |
| 35 | + * PlusClient and GamesClient, use BaseGameActivity(CLIENT_GAMES | CLIENT_PLUS). |
| 36 | + * To request all available clients, use BaseGameActivity(CLIENT_ALL). |
| 37 | + * Alternatively, you can also specify the requested clients via |
| 38 | + * @link{#setRequestedClients}, but you must do so before @link{#onCreate} |
| 39 | + * gets called, otherwise the call will have no effect. |
| 40 | + * |
| 41 | + * @author Bruno Oliveira (Google) |
| 42 | + */ |
| 43 | +public abstract class BaseGameActivity extends FragmentActivity implements |
| 44 | + GameHelper.GameHelperListener { |
| 45 | + |
| 46 | + // The game helper object. This class is mainly a wrapper around this object. |
| 47 | + protected GameHelper mHelper; |
| 48 | + |
| 49 | + // We expose these constants here because we don't want users of this class |
| 50 | + // to have to know about GameHelper at all. |
| 51 | + public static final int CLIENT_GAMES = GameHelper.CLIENT_GAMES; |
| 52 | + public static final int CLIENT_PLUS = GameHelper.CLIENT_PLUS; |
| 53 | + public static final int CLIENT_ALL = GameHelper.CLIENT_ALL; |
| 54 | + |
| 55 | + // Requested clients. By default, that's just the games client. |
| 56 | + protected int mRequestedClients = CLIENT_GAMES; |
| 57 | + |
| 58 | + private final static String TAG = "BaseGameActivity"; |
| 59 | + protected boolean mDebugLog = false; |
| 60 | + |
| 61 | + /** Constructs a BaseGameActivity with default client (GamesClient). */ |
| 62 | + protected BaseGameActivity() { |
| 63 | + super(); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Constructs a BaseGameActivity with the requested clients. |
| 68 | + * @param requestedClients The requested clients (a combination of CLIENT_GAMES, |
| 69 | + * CLIENT_PLUS). |
| 70 | + */ |
| 71 | + protected BaseGameActivity(int requestedClients) { |
| 72 | + super(); |
| 73 | + setRequestedClients(requestedClients); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Sets the requested clients. The preferred way to set the requested clients is |
| 78 | + * via the constructor, but this method is available if for some reason your code |
| 79 | + * cannot do this in the constructor. This must be called before onCreate or getGameHelper() |
| 80 | + * in order to have any effect. If called after onCreate()/getGameHelper(), this method |
| 81 | + * is a no-op. |
| 82 | + * |
| 83 | + * @param requestedClients A combination of the flags CLIENT_GAMES, CLIENT_PLUS |
| 84 | + * or CLIENT_ALL to request all available clients. |
| 85 | + */ |
| 86 | + protected void setRequestedClients(int requestedClients) { |
| 87 | + mRequestedClients = requestedClients; |
| 88 | + } |
| 89 | + |
| 90 | + public GameHelper getGameHelper() { |
| 91 | + if (mHelper == null) { |
| 92 | + mHelper = new GameHelper(this, mRequestedClients); |
| 93 | + mHelper.enableDebugLog(mDebugLog); |
| 94 | + } |
| 95 | + return mHelper; |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + protected void onCreate(Bundle b) { |
| 100 | + super.onCreate(b); |
| 101 | + if (mHelper == null) { |
| 102 | + getGameHelper(); |
| 103 | + } |
| 104 | + mHelper.setup(this); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected void onStart() { |
| 109 | + super.onStart(); |
| 110 | + mHelper.onStart(this); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + protected void onStop() { |
| 115 | + super.onStop(); |
| 116 | + mHelper.onStop(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + protected void onActivityResult(int request, int response, Intent data) { |
| 121 | + super.onActivityResult(request, response, data); |
| 122 | + mHelper.onActivityResult(request, response, data); |
| 123 | + } |
| 124 | + |
| 125 | + protected GoogleApiClient getApiClient() { |
| 126 | + return mHelper.getApiClient(); |
| 127 | + } |
| 128 | + |
| 129 | + protected boolean isSignedIn() { |
| 130 | + return mHelper.isSignedIn(); |
| 131 | + } |
| 132 | + |
| 133 | + protected void beginUserInitiatedSignIn() { |
| 134 | + mHelper.beginUserInitiatedSignIn(); |
| 135 | + } |
| 136 | + |
| 137 | + protected void signOut() { |
| 138 | + mHelper.signOut(); |
| 139 | + } |
| 140 | + |
| 141 | + protected void showAlert(String message) { |
| 142 | + mHelper.makeSimpleDialog(message).show(); |
| 143 | + } |
| 144 | + |
| 145 | + protected void showAlert(String title, String message) { |
| 146 | + mHelper.makeSimpleDialog(title, message).show(); |
| 147 | + } |
| 148 | + |
| 149 | + protected void enableDebugLog(boolean enabled) { |
| 150 | + mDebugLog = true; |
| 151 | + if (mHelper != null) { |
| 152 | + mHelper.enableDebugLog(enabled); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + @Deprecated |
| 157 | + protected void enableDebugLog(boolean enabled, String tag) { |
| 158 | + Log.w(TAG, "BaseGameActivity.enabledDebugLog(bool,String) is " + |
| 159 | + "deprecated. Use enableDebugLog(boolean)"); |
| 160 | + enableDebugLog(enabled); |
| 161 | + } |
| 162 | + |
| 163 | + protected String getInvitationId() { |
| 164 | + return mHelper.getInvitationId(); |
| 165 | + } |
| 166 | + |
| 167 | + protected void reconnectClient() { |
| 168 | + mHelper.reconnectClient(); |
| 169 | + } |
| 170 | + |
| 171 | + protected boolean hasSignInError() { |
| 172 | + return mHelper.hasSignInError(); |
| 173 | + } |
| 174 | + |
| 175 | + protected GameHelper.SignInFailureReason getSignInError() { |
| 176 | + return mHelper.getSignInError(); |
| 177 | + } |
| 178 | +} |
0 commit comments