GoogleGamesSocial.java 3.4 KB

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