Skip to content

Instantly share code, notes, and snippets.

@aron-bordin
Created April 17, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aron-bordin/915215be9963f6467696 to your computer and use it in GitHub Desktop.
Save aron-bordin/915215be9963f6467696 to your computer and use it in GitHub Desktop.
AppActivity.java for Cocos2d-js and Google Play Services
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import org.cocos2dx.plugin.PluginWrapper;
import android.content.Context;
import org.json.JSONObject;
import android.util.Log;
import org.json.JSONException;
import com.google.android.gms.games.Games;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.games.Player;
import com.google.android.gms.games.Players.LoadPlayersResult;
import com.google.android.gms.games.PlayerBuffer;
import java.util.List;
public class AppActivity extends BaseGameActivity {
static Context mContext;
static final String LEADERBOARD_ID = "LEADERBOARD_ID";
static final int REQUEST_LEADERBOARD = 1;
static final int REQUEST_ACHIEVEMENTS = 2;
static final int FRIENDS_PER_PAGE = 10;
static List<Player> mFriends;
@Override
public Cocos2dxGLSurfaceView onCreateView() {
mContext = AppActivity.this;
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
PluginWrapper.init(this);
PluginWrapper.setGLSurfaceView(glSurfaceView);
return glSurfaceView;
}
public static void gameServicesSignIn(JSONObject parameters) {
((AppActivity)mContext).runOnUiThread(new Runnable() {
public void run() {
((AppActivity)mContext).beginUserInitiatedSignIn();
}
});
}
public static void updateTopScoreLeaderboard(JSONObject parameters) {
int score = 0;
try {
score = parameters.getInt("score");
String msg = "score = " + score;
Log.w("LeaderboardsActivity", msg);
} catch (JSONException e) {
e.printStackTrace();
}
Games.Leaderboards.submitScore(((AppActivity)mContext).getApiClient(), LEADERBOARD_ID, score);
}
public static void updateAchievement(String id, int percentage) {
Games.Achievements.increment(((AppActivity)mContext).getApiClient(), id,percentage);
}
public static void unlockAchievement(String id) {
Games.Achievements.unlock(((AppActivity)mContext).getApiClient(), id);
}
public static void showLeaderboards() {
((AppActivity)mContext).runOnUiThread(new Runnable() {
public void run() {
((AppActivity)mContext).startActivityForResult(Games.Leaderboards.getLeaderboardIntent(((AppActivity)mContext).getApiClient(),
LEADERBOARD_ID), REQUEST_LEADERBOARD);
}
});
}
public static void showAchievements() {
((AppActivity)mContext).runOnUiThread(new Runnable() {
public void run() {
((AppActivity)mContext).startActivityForResult(Games.Achievements.getAchievementsIntent(((AppActivity)mContext).getApiClient()), REQUEST_ACHIEVEMENTS);
}
});
}
public static void loadFriends(JSONObject parameters) {
if (mFriends.size() > 0) {
mFriends.clear();
}
((AppActivity)mContext).runOnUiThread(new Runnable() {
public void run() {
Games.Players.loadInvitablePlayers(((AppActivity)mContext).getApiClient(), FRIENDS_PER_PAGE, false).setResultCallback(new ResultCallback<LoadPlayersResult>(){
@Override
public void onResult(LoadPlayersResult result) {
Log.w("GOOGLE SERVICES ****** ", "onResult");
PlayerBuffer playerBuffer = result.getPlayers();
for (Player player : playerBuffer) {
mFriends.add(player);
Log.i("GOOGLE SERVICES ---- ", String.format("Found player with id [%s] and display name [%s]", player.getPlayerId(), player.getDisplayName()));
}
if (playerBuffer.getCount() == FRIENDS_PER_PAGE) {
Log.w("GOOGLE SERVICES +++=== ", "loadMoreInvitablePlayers");
Games.Players.loadMoreInvitablePlayers(((AppActivity)mContext).getApiClient(), FRIENDS_PER_PAGE);
} else {
// call out and return all the friends
Log.w("GOOGLE SERVICES======== ", "call out and return all the friends");
for (Player friend : mFriends) {
Log.i("GOOGLE SERVICES", String.format("Found player with id [%s] and display name [%s]", friend.getPlayerId(), friend.getDisplayName()));
}
}
} // onResult
}); // loadInvitablePlayers
} // run
}); // runOnUiThread
}
@Override
public void onSignInFailed() {
Log.w("LeaderboardsActivity", "LeaderboardsActivity :: onSignInFailed");
}
@Override
public void onSignInSucceeded() {
Log.w("LeaderboardsActivity", "LeaderboardsActivity :: onSignInSucceeded");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment