Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Commit

Permalink
fix(android): check if Google Play Services are available (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz committed Mar 30, 2023
1 parent b03a592 commit e336da9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ npx cap sync
This plugin will use the following project variables (defined in your app’s `variables.gradle` file):

- `$androidPlayCore` version of `com.google.android.play:core` (default: `1.9.0`)
- `$androidPlayServicesBaseVersion` version of `com.google.android.gms:play-services-base` (default: `18.0.1`)

## Configuration

Expand Down
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ext {
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.3'
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.4.0'
androidPlayCore = project.hasProperty('androidPlayCore') ? rootProject.ext.androidPlayCore : '1.9.0'
androidPlayServicesBaseVersion = project.hasProperty('androidPlayServicesBaseVersion') ? rootProject.ext.androidPlayServicesBaseVersion : '18.0.1'
}

buildscript {
Expand Down Expand Up @@ -53,6 +54,7 @@ dependencies {
implementation project(':capacitor-android')
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
implementation "com.google.android.play:core:$androidPlayCore"
implementation "com.google.android.gms:play-services-base:$androidPlayServicesBaseVersion"
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.play.core.appupdate.AppUpdateInfo;
import com.google.android.play.core.appupdate.AppUpdateManager;
import com.google.android.play.core.appupdate.AppUpdateManagerFactory;
Expand All @@ -41,9 +43,10 @@ public class AppUpdatePlugin extends Plugin {
/** Update result: update info missing. */
public static final int UPDATE_INFO_MISSING = 5;
/** Request code for immediate update */
protected static final int REQUEST_IMMEDIATE_UPDATE = 10;
public static final int REQUEST_IMMEDIATE_UPDATE = 10;
/** Request code for flexible update */
protected static final int REQUEST_FLEXIBLE_UPDATE = 11;
public static final int REQUEST_FLEXIBLE_UPDATE = 11;
public static final String ERROR_GOOGLE_PLAY_SERVICES_UNAVAILABLE = "GooglePlayServices are not available.";
private AppUpdateManager appUpdateManager;
private AppUpdateInfo appUpdateInfo;
private InstallStateUpdatedListener listener;
Expand All @@ -55,6 +58,11 @@ public void load() {

@PluginMethod
public void getAppUpdateInfo(PluginCall call) {
boolean isGooglePlayServicesAvailable = this.isGooglePlayServicesAvailable();
if (!isGooglePlayServicesAvailable) {
call.reject(ERROR_GOOGLE_PLAY_SERVICES_UNAVAILABLE);
return;
}
Task<AppUpdateInfo> appUpdateInfoTask = this.appUpdateManager.getAppUpdateInfo();
appUpdateInfoTask.addOnSuccessListener(
appUpdateInfo -> {
Expand Down Expand Up @@ -180,6 +188,12 @@ protected void handleOnActivityResult(int requestCode, int resultCode, Intent da
savedPluginCall.resolve(ret);
}

public boolean isGooglePlayServicesAvailable() {
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(bridge.getContext());
return resultCode == ConnectionResult.SUCCESS;
}

private PackageInfo getPackageInfo() throws PackageManager.NameNotFoundException {
String packageName = this.getContext().getPackageName();
return this.getContext().getPackageManager().getPackageInfo(packageName, 0);
Expand Down

0 comments on commit e336da9

Please sign in to comment.