Advertisement
Guest User

Untitled

a guest
Aug 1st, 2015
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 KB | None | 0 0
  1. import android.os.Bundle;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.backends.android.AndroidApplication;
  5. import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
  6. import com.buckriderstudio.numbergame.ActionResolver;
  7. import com.buckriderstudio.numbergame.NumberGame;
  8. import com.buckriderstudio.numbergame.android.R;
  9.  
  10. import com.google.android.gms.common.ConnectionResult;
  11. import com.google.android.gms.common.api.GoogleApiClient;
  12. import com.google.android.gms.games.Games;
  13. import com.google.android.gms.games.Player;
  14. import com.google.android.gms.plus.Plus;
  15. import com.google.example.games.basegameutils.BaseGameUtils;
  16.  
  17. public class AndroidLauncher extends AndroidApplication
  18. implements ActionResolver,
  19. GoogleApiClient.ConnectionCallbacks,
  20. GoogleApiClient.OnConnectionFailedListener {
  21.  
  22. // Client used to interact with Google APIs
  23. private GoogleApiClient mGoogleApiClient;
  24.  
  25. // Are we currently resolving a connection failure?
  26. private boolean mResolvingConnectionFailure = false;
  27.  
  28. // Has the user clicked the sign-in button?
  29. private boolean mSignInClicked = false;
  30.  
  31. // Automatically start the sign-in flow when the Activity starts
  32. private boolean mAutoStartSignInFlow = true;
  33.  
  34. // request codes we use when invoking an external activity
  35. private static final int RC_RESOLVE = 5000;
  36. private static final int RC_UNUSED = 5001;
  37. private static final int RC_SIGN_IN = 9001;
  38.  
  39. // tag for debug logging
  40. final boolean ENABLE_DEBUG = true;
  41. final String TAG = "MainActivity";
  42.  
  43. // achievements and scores we're pending to push to the cloud
  44. // (waiting for the user to sign in, for instance)
  45. //AccomplishmentsOutbox mOutbox = new AccomplishmentsOutbox();
  46.  
  47.  
  48. @Override
  49. public void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
  52. initialize(new NumberGame(this), config);
  53.  
  54. mGoogleApiClient = new GoogleApiClient.Builder(this)
  55. .addConnectionCallbacks(this)
  56. .addOnConnectionFailedListener(this)
  57. .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
  58. .addApi(Games.API).addScope(Games.SCOPE_GAMES)
  59. .build();
  60.  
  61.  
  62. }
  63.  
  64. private boolean isSignedIn()
  65. {
  66. return (mGoogleApiClient != null && mGoogleApiClient.isConnected());
  67. }
  68.  
  69. @Override
  70. public void onStart()
  71. {
  72. super.onStart();
  73. Gdx.app.log(TAG, "onStart(): connecting");
  74. mGoogleApiClient.connect();
  75. }
  76.  
  77. @Override
  78. protected void onStop() {
  79. super.onStop();
  80. Gdx.app.log(TAG, "onStop(): disconnecting");
  81. }
  82.  
  83. @Override
  84. public void onConnected(Bundle bundle) {
  85. Gdx.app.log(TAG, "onConnected(): connected to Google APIs");
  86. // Show sign-out button on main menu
  87.  
  88. // Show "you are signed in" message on win screen, with no sign in button.
  89.  
  90. // Set the greeting appropriately on main menu
  91. Player p = Games.Players.getCurrentPlayer(mGoogleApiClient);
  92. String displayName;
  93. if (p == null) {
  94. Gdx.app.log(TAG, "mGamesClient.getCurrentPlayer() is NULL!");
  95. displayName = "???";
  96. } else {
  97. displayName = p.getDisplayName();
  98. }
  99. Gdx.app.log(TAG, "Hello, " + displayName);
  100.  
  101. // if we have accomplishments to push, push them
  102. /*
  103. if (!mOutbox.isEmpty()) {
  104. pushAccomplishments();
  105. Toast.makeText(this, getString(R.string.your_progress_will_be_uploaded),
  106. Toast.LENGTH_LONG).show();
  107. }
  108. */
  109. }
  110.  
  111. @Override
  112. public void onConnectionSuspended(int i) {
  113. Gdx.app.log(TAG, "onConnectionSuspended(): attempting to connect");
  114. mGoogleApiClient.connect();
  115. }
  116.  
  117. @Override
  118. public void onConnectionFailed(ConnectionResult connectionResult) {
  119. Gdx.app.log(TAG, "onConnectionFailed(): attempting to resolve");
  120. if (mResolvingConnectionFailure) {
  121. Gdx.app.log(TAG, "onConnectionFailed(): already resolving");
  122. return;
  123. }
  124.  
  125. if (mSignInClicked || mAutoStartSignInFlow) {
  126. mAutoStartSignInFlow = false;
  127. mSignInClicked = false;
  128. mResolvingConnectionFailure = true;
  129. if (!BaseGameUtils.resolveConnectionFailure(this, mGoogleApiClient, connectionResult,
  130. RC_SIGN_IN, getString(R.string.signin_other_error))) {
  131. mResolvingConnectionFailure = false;
  132. }
  133. }
  134.  
  135. // Sign-in failed, so show sign-in button on main menu
  136. Gdx.app.log(TAG, getString(R.string.signed_out_greeting));
  137. }
  138. //-----------------ActionResolver Methods----------------
  139. @Override
  140. public boolean getSignedInGPGS() {
  141. return isSignedIn();
  142. }
  143. @Override
  144. public void loginGPGS() {
  145.  
  146. }
  147.  
  148. @Override
  149. public void submitScoreGPGS(int score) {
  150.  
  151. }
  152.  
  153. @Override
  154. public void unlockAchievementGPGS(String achievementId) {
  155.  
  156. }
  157.  
  158. @Override
  159. public void getLeaderboardGPGS() {
  160.  
  161. }
  162.  
  163. @Override
  164. public void getAchievementsGPGS() {
  165.  
  166. }
  167.  
  168. 4482-4482/com.buckriderstudio.numbergame.android I/MainActivity﹕ onStop(): disconnecting
  169. 4482-4482/com.buckriderstudio.numbergame.android I/MainActivity﹕ onStart(): connecting
  170. 4482-4482/com.buckriderstudio.numbergame.android I/MainActivity﹕ onConnectionFailed(): attempting to resolve
  171. 4482-4482/com.buckriderstudio.numbergame.android I/MainActivity﹕ onConnectionFailed(): already resolving
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement