-
Notifications
You must be signed in to change notification settings - Fork 15
/
BaseGameUtils.java
183 lines (164 loc) · 7.99 KB
/
BaseGameUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.google.example.games.basegameutils;
import android.app.Dialog;
import android.content.IntentSender;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.GamesActivityResultCodes;
public class BaseGameUtils {
/**
* Show an {@link AlertDialog} with an 'OK' button and a message.
*
* @param activity the Activity in which the Dialog should be displayed.
* @param message the message to display in the Dialog.
*/
public static void showAlert(AppCompatActivity activity, String message) {
(new AlertDialog.Builder(activity)).setMessage(message)
.setNeutralButton(android.R.string.ok, null).create().show();
}
/**
* Resolve a connection failure from
* {@link com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener#onConnectionFailed(com.google.android.gms.common.ConnectionResult)}
*
* @param activity the Activity trying to resolve the connection failure.
* @param client the GoogleAPIClient instance of the Activity.
* @param result the ConnectionResult received by the Activity.
* @param requestCode a request code which the calling Activity can use to identify the result
* of this resolution in onActivityResult.
* @param fallbackErrorMessage a generic error message to display if the failure cannot be resolved.
* @return true if the connection failure is resolved, false otherwise.
*/
public static boolean resolveConnectionFailure(AppCompatActivity activity, GoogleApiClient client,
ConnectionResult result,
int requestCode,
int fallbackErrorMessage) {
if (result.hasResolution()) {
try {
result.startResolutionForResult(activity, requestCode);
return true;
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
client.connect();
return false;
}
} else {
// not resolvable... so show an error message
int errorCode = result.getErrorCode();
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
activity, requestCode);
if (dialog != null) {
dialog.show();
} else {
// no built-in dialog: show the fallback error message
showAlert(activity, activity.getString(fallbackErrorMessage));
}
return false;
}
}
/**
* For use in sample code only. Checks if the sample was set up correctly,
* including changing the package name to a non-Google package name and
* replacing the placeholder IDs. Shows alert dialogs to notify about problems.
* DO NOT call this method from a production app, it's meant only for samples!
*
* @param resIds the resource IDs to check for placeholders
* @return true if sample is set up correctly; false otherwise.
*/
public static boolean verifySampleSetup(AppCompatActivity activity, int... resIds) {
StringBuilder problems = new StringBuilder();
boolean problemFound = false;
problems.append("The following set up problems were found:\n\n");
// Did the developer forget to change the package name?
if (activity.getPackageName().startsWith("com.google.example.games")) {
problemFound = true;
problems.append("- Package name cannot be com.google.*. You need to change the "
+ "sample's package name to your own package.").append("\n");
}
for (int i : resIds) {
if (activity.getString(i).toLowerCase().contains("replaceme")) {
problemFound = true;
problems.append("- You must replace all " +
"placeholder IDs in the ids.xml file by your project's IDs.").append("\n");
break;
}
}
if (problemFound) {
problems.append("\n\nThese problems may prevent the app from working properly.");
showAlert(activity, problems.toString());
return false;
}
return true;
}
/**
* Show a {@link android.app.Dialog} with the correct message for a connection error.
*
* @param activity the Activity in which the Dialog should be displayed.
* @param requestCode the request code from onActivityResult.
* @param actResp the response code from onActivityResult.
* @param errorDescription the resource id of a String for a generic error message.
*/
public static void showActivityResultError(AppCompatActivity activity, int requestCode, int actResp, int errorDescription) {
if (activity == null) {
Log.e("BaseGameUtils", "*** No Activity. Can't show failure dialog!");
return;
}
Dialog errorDialog;
switch (actResp) {
case GamesActivityResultCodes.RESULT_APP_MISCONFIGURED:
errorDialog = makeSimpleDialog(activity,
activity.getString(R.string.app_misconfigured));
break;
case GamesActivityResultCodes.RESULT_SIGN_IN_FAILED:
errorDialog = makeSimpleDialog(activity,
activity.getString(R.string.sign_in_failed));
break;
case GamesActivityResultCodes.RESULT_LICENSE_FAILED:
errorDialog = makeSimpleDialog(activity,
activity.getString(R.string.license_failed));
break;
default:
// No meaningful Activity response code, so generate default Google
// Play services dialog
final int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(activity);
errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode,
activity, requestCode, null);
if (errorDialog == null) {
// get fallback dialog
Log.e("BaseGamesUtils",
"No standard error dialog available. Making fallback dialog.");
errorDialog = makeSimpleDialog(activity, activity.getString(errorDescription));
}
}
errorDialog.show();
}
/**
* Create a simple {@link Dialog} with an 'OK' button and a message.
*
* @param activity the Activity in which the Dialog should be displayed.
* @param text the message to display on the Dialog.
* @return an instance of {@link AlertDialog}
*/
public static Dialog makeSimpleDialog(AppCompatActivity activity, String text) {
return (new AlertDialog.Builder(activity)).setMessage(text)
.setNeutralButton(android.R.string.ok, null).create();
}
/**
* Create a simple {@link Dialog} with an 'OK' button, a title, and a message.
*
* @param activity the Activity in which the Dialog should be displayed.
* @param title the title to display on the dialog.
* @param text the message to display on the Dialog.
* @return an instance of {@link AlertDialog}
*/
public static Dialog makeSimpleDialog(AppCompatActivity activity, String title, String text) {
return (new AlertDialog.Builder(activity))
.setTitle(title)
.setMessage(text)
.setNeutralButton(android.R.string.ok, null)
.create();
}
}