GooglePlaySocial.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package org.gameplay3d;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.support.v4.app.Fragment;
  5. import android.util.Log;
  6. import android.widget.Toast;
  7. import com.google.android.gms.games.Player;
  8. import com.google.example.games.basegameutils.BaseGameActivity;
  9. public class GooglePlaySocial extends BaseGameActivity
  10. {
  11. private static List<Player> mFriends = new ArrayList<Player>();
  12. private static final int FRIENDS_PER_PAGE = 10;
  13. public static void gameServicesSignIn()
  14. {
  15. ((appname)mContext).runOnUiThread(new Runnable()
  16. {
  17. public void run() {
  18. ((appname)mContext).beginUserInitiatedSignIn();
  19. }
  20. });
  21. }
  22. public static void updateTopScoreLeaderboard(int score)
  23. {
  24. if (mContext.isSignedIn())
  25. mContext.getGamesClient().submitScore("leaderboardid", score);
  26. }
  27. public static void submitAchievement(String achievementId)
  28. {
  29. if (mContext.isSignedIn())
  30. {
  31. mContext.getGamesClient().unlockAchievement(achievementId);
  32. }
  33. }
  34. public static void incrementAchievement(String achievementId, int percentage)
  35. {
  36. if (mContext.isSignedIn())
  37. {
  38. mContext.getGamesClient().incrementAchievement(achievementId, percentage);
  39. }
  40. }
  41. public static void displayLeaderboards()
  42. {
  43. if (mContext.isSignedIn())
  44. {
  45. ((appname)mContext).runOnUiThread(new Runnable()
  46. {
  47. public void run() {
  48. ((appname)mContext).startActivityForResult(((appname)mContext).getGamesClient().getLeaderboardIntent("leaderboardidfromgoogleplay"), 5001);
  49. }
  50. });
  51. }
  52. else
  53. {
  54. gameServicesSignIn();
  55. }
  56. }
  57. public static void displayAchievements()
  58. {
  59. if (mContext.isSignedIn())
  60. {
  61. ((appname)mContext).runOnUiThread(new Runnable()
  62. {
  63. public void run() {
  64. ((appname)mContext).startActivityForResult(((appname)mContext).getGamesClient().getAchievementsIntent(), 5001);
  65. }
  66. });
  67. }
  68. else
  69. {
  70. gameServicesSignIn();
  71. }
  72. }
  73. public static void loadFriends()
  74. {
  75. if (mFriends.size() > 0) {
  76. mFriends.clear();
  77. }
  78. ((appname)mContext).runOnUiThread(new Runnable()
  79. {
  80. public void run()
  81. {
  82. ((appname)mContext).getGamesClient().loadInvitablePlayers(new OnPlayersLoadedListener() {
  83. @Override
  84. public void onPlayersLoaded(int statusCode, PlayerBuffer playerBuffer) {
  85. if (statusCode == GamesClient.STATUS_OK) {
  86. for (Player player : playerBuffer) {
  87. mFriends.add(player);
  88. }
  89. if (playerBuffer.getCount() == FRIENDS_PER_PAGE) {
  90. ((appname)mContext).getGamesClient().loadMoreInvitablePlayers(this, FRIENDS_PER_PAGE);
  91. } else {
  92. // call out and return all the friends <more code to call into C++>
  93. for (Player friend : mFriends) {
  94. Log.i(TAG, String.format("Found player with id [%s] and display name [%s]", friend.getPlayerId(), friend.getDisplayName()));
  95. }
  96. }
  97. }
  98. }
  99. }, FRIENDS_PER_PAGE, false);
  100. });
  101. }
  102. }