Skip to content

Commit

Permalink
google play achievments for android
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed May 13, 2017
1 parent 7371847 commit 2638c6f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 2 deletions.
106 changes: 104 additions & 2 deletions android/src/xyz/amulet/AmuletActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@

import com.google.android.gms.ads.*;
import com.android.vending.billing.*;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;

import com.ianmaclarty.jellyjuggle.R; // XXX

public class AmuletActivity extends Activity {
public class AmuletActivity extends Activity
implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
AmuletView view = null;
AdView adView = null;
boolean requestAdVisible = false;
Expand All @@ -41,6 +47,8 @@ public class AmuletActivity extends Activity {
Object adLock = new Object();
RelativeLayout.LayoutParams adLayoutParams = null;
int adHeight;
GoogleApiClient googleApiClient = null;
int googleApiClientConnectionAttempts = 0;

static AmuletActivity singleton;

Expand Down Expand Up @@ -76,6 +84,8 @@ protected void onCreate(Bundle bundle) {
new Intent("com.android.vending.billing.InAppBillingService.BIND");
serviceIntent.setPackage("com.android.vending");
bindService(serviceIntent, iapServiceCon, Context.BIND_AUTO_CREATE);

initGoogleApiClient();
}

@Override
Expand Down Expand Up @@ -431,6 +441,10 @@ void getProducts(String[] productIds) {
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", skuList);
try {
// wait for connect
while (iapService == null) {
Thread.sleep(100);
}
Bundle skuDetails = iapService.getSkuDetails(3,
getPackageName(), "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
Expand All @@ -455,7 +469,6 @@ void getProducts(String[] productIds) {
} else {
Log.i("AMULET", "non-zero getproducts response: " + response);
}

} catch (Exception e) {
Log.i("AMULET", e.getMessage() + ":" + e.getStackTrace());
}
Expand Down Expand Up @@ -608,6 +621,94 @@ public void run() {
}).start();
}

// -- Game services API ----------

void initGoogleApiClient() {
// for leaderboards, achievements
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
googleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
Log.i("AMULET", "Google services connected!");
view.queueEvent(new Runnable() {
public void run() {
Log.i("AMULET", "calling jniSetGameServicesConnected (1)");
jniSetGameServicesConnected(1);
}
});
}

@Override
public void onConnectionSuspended(int i) {
Log.i("AMULET", "Google services suspended!");
view.queueEvent(new Runnable() {
public void run() {
Log.i("AMULET", "calling jniSetGameServicesConnected (0)");
jniSetGameServicesConnected(0);
}
});
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i("AMULET", "Google services connection failed!");
view.queueEvent(new Runnable() {
public void run() {
Log.i("AMULET", "calling jniSetGameServicesConnected (0)");
jniSetGameServicesConnected(0);
}
});
if (googleApiClientConnectionAttempts == 0) {
Log.i("AMULET", "Google services trying reconnect");
// on my device it always fails to connect on first attempt after
// installation and then works after that, so try again once.
googleApiClientConnectionAttempts++;
initGoogleApiClient();
}
}

public static void cppSubmitLeaderboardScore(String leaderboard0, long score0) {
final String leaderboard = leaderboard0;
final long score = score0;
singleton.runOnUiThread(new Runnable() {
public void run() {
if(singleton.googleApiClient != null && singleton.googleApiClient.isConnected()) {
Games.Leaderboards.submitScore(singleton.googleApiClient, leaderboard, score);
}
}
});
}

public static void cppSubmitAchievement(String achievement0) {
final String achievement = achievement0;
singleton.runOnUiThread(new Runnable() {
public void run() {
if(singleton.googleApiClient != null && singleton.googleApiClient.isConnected()) {
//Log.i("AMULET", "unlock " + achievement);
Games.Achievements.unlock(singleton.googleApiClient, achievement);
}
}
});
}

public static void cppShowLeaderboard(String leaderboard0) {
final String leaderboard = leaderboard0;
singleton.runOnUiThread(new Runnable() {
public void run() {
if(singleton.googleApiClient != null && singleton.googleApiClient.isConnected()) {
singleton.startActivityForResult(Games.Leaderboards.getLeaderboardIntent(singleton.googleApiClient,
leaderboard), 1002);
}
}
});
}

// --- JNI ------------

static {
Expand All @@ -627,4 +728,5 @@ public void run() {
public static native void jniIAPProductsRetrieved(int success, String[] productids, String[] prices);
public static native void jniIAPTransactionUpdated(String productId, String status);
public static native void jniIAPRestoreFinished(int success);
public static native void jniSetGameServicesConnected(int flag);
}
13 changes: 13 additions & 0 deletions src/am_backend_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,18 @@ static int submit_google_play_score(lua_State *L) {
return 0;
}

static int submit_google_play_achievement(lua_State *L) {
am_check_nargs(L, 1);
const char *id = lua_tostring(L, 1);
if (id == NULL) return luaL_error(L, "expecting a string in position 1");
jstring jid = jni_env->NewStringUTF(id);
jclass cls = jni_env->FindClass("xyz/amulet/AmuletActivity");
jmethodID mid = jni_env->GetStaticMethodID(cls, "cppSubmitAchievement", "(Ljava/lang/String;)V");
jni_env->CallStaticVoidMethod(cls, mid, jid);
jni_env->DeleteLocalRef(jid);
return 0;
}

static void register_iap_product_mt(lua_State *L) {
lua_newtable(L);

Expand All @@ -722,6 +734,7 @@ void am_open_android_module(lua_State *L) {
{"google_play_available", google_play_is_available},
{"show_google_play_leaderboard", show_google_play_leaderboard},
{"submit_google_play_score", submit_google_play_score},
{"submit_google_play_achievement", submit_google_play_achievement},

{"init_google_banner_ad", init_google_banner_ad},
{"set_google_banner_ad_visible", set_google_banner_ad_visible},
Expand Down

0 comments on commit 2638c6f

Please sign in to comment.