Skip to content

Commit

Permalink
Merge pull request #39 from jquick-axway/TIMOB-27896
Browse files Browse the repository at this point in the history
feat: update to 17.5.0 and backgrounding support
  • Loading branch information
jquick-axway authored Nov 4, 2020
2 parents b9c28b0 + c162e08 commit 4a030a9
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 116 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
*.orig

node_modules/

Expand Down
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.0.0.GA'
npmPublish = isMaster
}
65 changes: 48 additions & 17 deletions android/Resources/ti.playservices/ti.playservices.bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
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 @@ -34,4 +49,20 @@ 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)) {
if (Ti.UI.hasSession !== undefined) {
// Titanium supports backgrounding and showing multiple UI sessions with a single JS runtime instance.
// Show bootstrap's UI every time a new UI session has been created.
exports.showUI = showUI;
} else {
// Older Titanium versions will only run this bootstrap once per JS runtime instance.
exports.execute = showUI;
}
}
90 changes: 0 additions & 90 deletions android/Resources/ti.playservices/ti.playservices.js

This file was deleted.

4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

dependencies {
// Needed to check for Google Play Services availability on device.
implementation 'com.google.android.gms:play-services-base:17.1.0'
implementation 'com.google.android.gms:play-services-base:17.5.0'

// App devs expect adding "ti.playservices" will enable "Fused Location" support to "Ti.Gelocation" APIs.
// So, we must add this library to support it, even though this module doesn't use this library at all.
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-location:17.1.0'
}
3 changes: 1 addition & 2 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.5.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: Titanium Google Play Services module.
Expand All @@ -15,4 +15,3 @@ moduleid: ti.playservices
guid: 32184149-411f-436b-92a8-c6ddb98a5fb6
platform: android
minsdk: 9.0.0
commonjs: true
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
1 change: 0 additions & 1 deletion android/timodule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<ti:module xmlns:ti="http://ti.appcelerator.org" xmlns:android="http://schemas.android.com/apk/res/android">
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
Expand Down
30 changes: 29 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@titanium-sdk/ti.playservices",
"version": "17.1.1",
"version": "17.5.0",
"description": "Titanium Google Play Services module.",
"scripts": {
"ci": "node updater/index.js ci",
Expand Down

0 comments on commit 4a030a9

Please # to comment.