Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to 17.5.0 and backgrounding support #39

Merged
merged 9 commits into from
Nov 4, 2020
Merged
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ library 'pipeline-library'
def isMaster = env.BRANCH_NAME.equals('master')

buildModule {
sdkVersion = '9.0.0.v20200205134519' // TODO: Change to 9.0.0.GA once released
sdkVersion = '9.1.0.v20200603062635' // TODO: Change to 9.1.0.GA once released
npmPublish = isMaster
}
58 changes: 41 additions & 17 deletions android/Resources/ti.playservices/ti.playservices.bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@

exports.execute = (finished) => {
// Check if Google Play Services validation is enabled on startup. (Enabled by default.)
// Can be disabled via "tiapp.xml" entry:
// <ti:app>
// <property name="ti.playservices.validate.on.startup" type="bool">false</property>
// </ti:app>
const isEnabled = Ti.App.Properties.getBool('ti.playservices.validate.on.startup', true);
if (!isEnabled) {
finished();
return;
}

// Check if Google Play Services is available (ie: installed/updated) on devices that support it.
// If not available, will display Google's standard dialog asking end-user to install/update it.
/**
* Checks if Google Play Services is available (ie: installed/updated) on devices that support it.
* If not available, will display Google's standard dialog asking end-user to install/update it.
* @param {Function} finished Callback to be invoked when this check has finished.
*/
function showUI(finished) {
try {
const PlayServices = require('ti.playservices');

// Check if Play Services needs to be installed, updated, or enabled.
let isUpdateNeeded = false;
const resultCode = PlayServices.isGooglePlayServicesAvailable();
if (resultCode === PlayServices.RESULT_SUCCESS) {
// Play Services is installed/updated.
const versionString = PlayServices.GOOGLE_PLAY_SERVICES_VERSION_CODE;
Ti.API.info(`ti.playservices: Google Play Services is available. (version: ${versionString})`);
isUpdateNeeded = false;
} else {
// Play Services needs to be installed, updated, or enabled.
// If invalid, then device does not support Google Play (such as Amazon devices) or it's a hacked version.
const errorString = PlayServices.getErrorString(resultCode);
Ti.API.info(`ti.playservices: Google Play Services is unavailable. (${errorString})`);
isUpdateNeeded = (resultCode !== PlayServices.RESULT_SERVICE_INVALID);
}

// Stop here if update is not required.
if (!isUpdateNeeded) {
finished();
return;
}

// Display Google's update dialog.
PlayServices.makeGooglePlayServicesAvailable((e) => {
if (e.success) {
// Play Services is installed/updated. Proceed to load "app.js".
// Play Services is installed/updated.
finished();
} else if (e.code === PlayServices.RESULT_SERVICE_INVALID) {
// Device does not support Google Play (such as an Amazon device) or it's a hacked version.
// Proceed to load "app.js". (It's impossible to install on device anyways.)
finished();
} else {
// Exit the app, because the end-user refused to install/update Play Services when prompted.
Expand All @@ -35,4 +50,13 @@ exports.execute = (finished) => {
Ti.API.error(err);
finished();
}
};
}

// Only inject this bootstrap's UI on startup if Google Play Services validation is enabled.
// Can be disabled via "tiapp.xml" entry:
// <ti:app>
// <property name="ti.playservices.validate.on.startup" type="bool">false</property>
// </ti:app>
if (Ti.App.Properties.getBool('ti.playservices.validate.on.startup', true)) {
exports.showUI = showUI;
}
90 changes: 0 additions & 90 deletions android/Resources/ti.playservices/ti.playservices.js

This file was deleted.

5 changes: 2 additions & 3 deletions android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 17.1.1
version: 17.3.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Titanium Google Play Services module.
Expand All @@ -14,5 +14,4 @@ name: playservices
moduleid: ti.playservices
guid: 32184149-411f-436b-92a8-c6ddb98a5fb6
platform: android
minsdk: 9.0.0
commonjs: true
minsdk: 9.1.0
57 changes: 56 additions & 1 deletion android/src/ti/playservices/PlayServicesModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
*/
package ti.playservices;

import android.app.Activity;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.Task;
import java.util.concurrent.CancellationException;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollFunction;
import org.appcelerator.kroll.KrollModule;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;

@Kroll.module(name = "PlayServices", id = "ti.playservices")
public class PlayServicesModule extends KrollModule
Expand Down Expand Up @@ -47,7 +53,7 @@ public PlayServicesModule()
@Kroll.method
public int isGooglePlayServicesAvailable()
{
return this.api.isGooglePlayServicesAvailable(TiApplication.getAppRootOrCurrentActivity());
return this.api.isGooglePlayServicesAvailable(TiApplication.getInstance());
}

@Kroll.method
Expand All @@ -56,6 +62,55 @@ public boolean isUserResolvableError(int code)
return this.api.isUserResolvableError(code);
}

@Kroll.method
public void makeGooglePlayServicesAvailable(final KrollFunction callback)
{
// Validate argument.
if (callback == null) {
throw new IllegalArgumentException(
"makeGooglePlayServicesAvailable() method must be given a 'callback' argument.");
}

// Do not continue if Play Services is installed and up-to-date. (This is an optimization.)
int resultCode = isGooglePlayServicesAvailable();
if (resultCode == ConnectionResult.SUCCESS) {
callback.callAsync(getKrollObject(), createEventForMakeAvailable(resultCode));
return;
}

// Fetch the top-most activity.
Activity activity = TiApplication.getAppCurrentActivity();
if (activity == null) {
activity = TiApplication.getInstance().getRootActivity();
if (activity == null) {
callback.callAsync(getKrollObject(), createEventForMakeAvailable(resultCode));
return;
}
}

// Display Google's standard dialog asking end-user to update Google Play Services.
Task<Void> task = this.api.makeGooglePlayServicesAvailable(activity);
task.addOnCompleteListener((Task<Void> theTask) -> {
int newResultCode = theTask.isSuccessful() ? ConnectionResult.SUCCESS : resultCode;
callback.callAsync(getKrollObject(), createEventForMakeAvailable(newResultCode));
});
}

private KrollDict createEventForMakeAvailable(int resultCode)
{
String message;
if (resultCode == ConnectionResult.SUCCESS) {
message = "Google Play Services is available. (version: " + GOOGLE_PLAY_SERVICES_VERSION_CODE + ")";
} else {
message = "Google Play Services is unavailable. (" + getErrorString(resultCode) + ")";
}

KrollDict event = new KrollDict();
event.putCodeAndMessage(resultCode, message);
event.put(TiC.PROPERTY_MESSAGE, message);
return event;
}

@Kroll.method
public String getErrorString(int code)
{
Expand Down