Skip to content

Commit

Permalink
only init google play services if requested
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmaclarty committed May 18, 2017
1 parent a440475 commit dc7a022
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
30 changes: 17 additions & 13 deletions android/src/xyz/amulet/AmuletActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
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
implements
GoogleApiClient.ConnectionCallbacks,
Expand Down Expand Up @@ -84,8 +82,6 @@ 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 @@ -119,7 +115,11 @@ public AmuletView(Context context) {
setEGLConfigChooser(new ConfigChooser());
setRenderer(new AMRenderer());
Context appContext = context;
jniInit(context.getAssets(), appContext.getFilesDir().getPath() + "/", context.getResources().getString(R.string.lang));
Configuration config = appContext.getResources().getConfiguration();
String lang = config.locale.getLanguage().toLowerCase();
String country = config.locale.getCountry();
if (country != null && country.length() > 0) lang += "-" + country.toUpperCase();
jniInit(context.getAssets(), appContext.getFilesDir().getPath() + "/", lang);
}

private static class ContextFactory implements GLSurfaceView.EGLContextFactory {
Expand Down Expand Up @@ -623,14 +623,18 @@ public void run() {

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

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

@Override
Expand Down Expand Up @@ -669,7 +673,7 @@ public void run() {
// on my device it always fails to connect on first attempt after
// installation and then works after that, so try again once.
googleApiClientConnectionAttempts++;
initGoogleApiClient();
cppInitGoogleApiClient();
}
}

Expand Down
6 changes: 3 additions & 3 deletions doc/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ The platform Amulet is running on. It will be one of the strings

### am.language() {#am.language .func-def}

Returns the user's preferred language code (e.g. `"en"`),
possibly with a coutry suffix (e.g. `"pt-PT"`).
Returns the user's preferred ISO 639-1 language code in lower case(e.g. `"en"`),
possibly followed by a dash and an ISO 3166-1 coutry code in upper case (e.g. `"fr-CA"`).
The returned value will be one of the languages listed in
the `conf.lua` file (see [Exporting](#exporting)).
This currently only returns a meaningful value on iOS
and Android (on all other platforms it returns `"en"`).
and Android (on all other platforms it always returns `"en"`).

# Game Center (iOS only)

Expand Down
8 changes: 8 additions & 0 deletions src/am_backend_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ static int google_play_is_available(lua_State *L) {
return 1;
}

static int init_google_play_services(lua_State *L) {
jclass cls = jni_env->FindClass("xyz/amulet/AmuletActivity");
jmethodID mid = jni_env->GetStaticMethodID(cls, "cppInitGoogleApiClient", "()V");
jni_env->CallStaticVoidMethod(cls, mid);
return 0;
}

static int show_google_play_leaderboard(lua_State *L) {
am_check_nargs(L, 1);
const char *leaderboard = lua_tostring(L, 1);
Expand Down Expand Up @@ -731,6 +738,7 @@ static void register_iap_product_mt(lua_State *L) {

void am_open_android_module(lua_State *L) {
luaL_Reg funcs[] = {
{"init_google_play_services", init_google_play_services},
{"google_play_available", google_play_is_available},
{"show_google_play_leaderboard", show_google_play_leaderboard},
{"submit_google_play_score", submit_google_play_score},
Expand Down
21 changes: 10 additions & 11 deletions src/am_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,19 @@ void *am_read_file(const char *filename, size_t *len) {
fprintf(stderr, "Error: unable to open file %s\n", filename);
return NULL;
}
size_t l = 0;
int c;
do {
c = fgetc(f);
if (c == EOF) break;
else l++;
} while (1);
fseek(f, 0, SEEK_END);
size_t l = ftell(f);
rewind(f);
if (len != NULL) *len = l;
unsigned char *buf = (unsigned char*)malloc(l + 1);
fseek(f, 0, SEEK_SET);
for (size_t i = 0; i < l; i++) {
buf[i] = fgetc(f);
}
size_t r = fread(buf, 1, l, f);
fclose(f);
buf[l] = 0; // null terminate so text file data can be cast to string
if (r != l) {
free(buf);
fprintf(stderr, "Error: unable to read file %s\n", filename);
return NULL;
}
return buf;
}

Expand Down

0 comments on commit dc7a022

Please sign in to comment.