Java tutorial
package com.bluewall.picturegame; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.FragmentTransaction; import android.content.Context; import android.content.Intent; import android.content.IntentSender; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.FragmentActivity; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.ProgressBar; import com.bluewall.picturegame.fragments.QuestionFragment; import com.bluewall.picturegame.fragments.SignInFragment; 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.Games; import com.google.android.gms.games.GamesActivityResultCodes; import com.google.android.gms.plus.Plus; import com.parse.Parse; import com.parse.ParseException; import com.parse.ParseInstallation; import com.parse.ParsePush; import com.parse.SaveCallback; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { final static String TAG = "Image Hunt"; // Request code used to invoke sign in user interactions. private static final int RC_SIGN_IN = 9001; // Client used to interact with Google APIs. private static GoogleApiClient mGoogleApiClient; // Are we currently resolving a connection failure? private boolean mResolvingConnectionFailure = false; // Has the user clicked the sign-in button? private static boolean mSignInClicked = false; // Set to true to automatically start the sign in flow when the Activity starts. // Set to false to require the user to click the button in order to sign in. private boolean mAutoStartSignInFlow = false; private View v; public static Context context; public static ProgressBar mProgress; private int mProgressStatus = 0; public static boolean hasQ = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.con_frag); context = getApplicationContext(); ParsePush.subscribeInBackground("", new SaveCallback() { @Override public void done(ParseException e) { if (e == null) { Log.i("com.parse.push", "successfully subscribed to the channel."); } else { Log.i("com.parse.push", "failed to subscribe for push", e); } } }); if (savedInstanceState == null) { getFragmentManager().beginTransaction().add(R.id.container, new SignInFragment()).commit(); } // Create the Google Api Client with access to Plus and Games mGoogleApiClient = new GoogleApiClient.Builder(context).addConnectionCallbacks(this) .addOnConnectionFailedListener(this).addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN) .addApi(Games.API).addScope(Games.SCOPE_GAMES).build(); } public static void onSignInClick() { Log.i(TAG, "Sign-in button clicked"); mSignInClicked = true; mGoogleApiClient.connect(); } @Override public void onActivityResult(int requestCode, int responseCode, Intent intent) { super.onActivityResult(requestCode, responseCode, intent); switch (requestCode) { case RC_SIGN_IN: Log.d(TAG, "onActivityResult with requestCode == RC_SIGN_IN, responseCode=" + responseCode + ", intent=" + intent); mSignInClicked = false; mResolvingConnectionFailure = false; if (responseCode == RESULT_OK) { mGoogleApiClient.connect(); } else { showActivityResultError(this, requestCode, responseCode, R.string.signin_other_error); } break; } super.onActivityResult(requestCode, responseCode, intent); } @Override public void onStart() { if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) { Log.w(TAG, "GameHelper: client was already connected on onStart()"); } else { Log.d(TAG, "Connecting client."); //TODO mGoogleApiClient.connect(); } super.onStart(); } @Override public void onConnected(Bundle connectionHint) { Log.d(TAG, "onConnected() called. Sign in successful!"); Log.d(TAG, "Sign-in succeeded."); getFragmentManager().beginTransaction().replace(R.id.container, new QuestionFragment()).commit(); } public static String getPlayerId() { return Games.Players.getCurrentPlayerId(mGoogleApiClient); } @Override public void onConnectionSuspended(int i) { Log.d(TAG, "onConnectionSuspended() called. Trying to reconnect."); mGoogleApiClient.connect(); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Log.d(TAG, "onConnectionFailed() called, result: " + connectionResult); if (mResolvingConnectionFailure) { Log.d(TAG, "onConnectionFailed() ignoring connection failure; already resolving."); return; } if (mSignInClicked || mAutoStartSignInFlow) { mAutoStartSignInFlow = false; mSignInClicked = false; mResolvingConnectionFailure = resolveConnectionFailure(this, mGoogleApiClient, connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error)); } } public static Dialog makeSimpleDialog(Activity activity, String text) { return (new AlertDialog.Builder(activity)).setMessage(text).setNeutralButton(android.R.string.ok, null) .create(); } public static boolean resolveConnectionFailure(Activity activity, GoogleApiClient client, ConnectionResult result, int requestCode, String 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, fallbackErrorMessage); } return false; } } public static void showAlert(Activity activity, String message) { (new AlertDialog.Builder(activity)).setMessage(message).setNeutralButton(android.R.string.ok, null).create() .show(); } public static void showActivityResultError(Activity 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(); } // Sets the flag to keep this screen on. It's recommended to do that during // the // handshake when setting up a game, because if the screen turns off, the // game will be // cancelled. void keepScreenOn() { getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } // Clears the flag that keeps the screen on. void stopKeepingScreenOn() { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } public static Context getAppContext() { return context; } public static GoogleApiClient getGoogleShiz() { return mGoogleApiClient; } }