Skip to content

Commit

Permalink
Update play services version and code
Browse files Browse the repository at this point in the history
  • Loading branch information
Richou committed Jun 15, 2019
1 parent 1d0a3a9 commit 29b0b40
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 73 deletions.
18 changes: 9 additions & 9 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.1.0'
classpath 'com.android.tools.build:gradle:3.4.1'
}
}

apply plugin: 'com.android.library'

android {
compileSdkVersion safeExtGet('compileSdkVersion', 27)
buildToolsVersion safeExtGet('buildToolsVersion', '27.0.3')
compileSdkVersion safeExtGet('compileSdkVersion', 28)
buildToolsVersion safeExtGet('buildToolsVersion', '28.0.3')

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
targetSdkVersion safeExtGet('targetSdkVersion', 27)
versionCode 3
versionName "1.2"
minSdkVersion safeExtGet('minSdkVersion', 19)
targetSdkVersion safeExtGet('targetSdkVersion', 28)
versionCode 4
versionName "1.3"
ndk {
abiFilters "armeabi-v7a", "x86"
abiFilters "armeabi-v7a", "x86", "x86_64"
}
}
lintOptions {
Expand All @@ -38,6 +38,6 @@ android {
dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
api 'com.facebook.react:react-native:+'
api "com.google.android.gms:play-services-location:${safeExtGet('googlePlayServicesVersion', '15.0.1')}"
api "com.google.android.gms:play-services-location:${safeExtGet('googlePlayServicesVersion', '16.0.0')}"
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.facebook.react.bridge.ActivityEventListener;
Expand All @@ -14,16 +12,15 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResult;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import static android.app.Activity.RESULT_OK;

Expand All @@ -34,7 +31,7 @@
* @author Richou
* @since 2016-12
*/
public class RNAndroidLocationEnablerModule extends ReactContextBaseJavaModule implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback<LocationSettingsResult>, ActivityEventListener {
public class RNAndroidLocationEnablerModule extends ReactContextBaseJavaModule implements ActivityEventListener, OnCompleteListener<LocationSettingsResponse> {

private static final String SELF_MODULE_NAME = "RNAndroidLocationEnabler";
private static final String LOCATION_INTERVAL_DURATION_PARAMS_KEY = "interval";
Expand All @@ -47,9 +44,8 @@ public class RNAndroidLocationEnablerModule extends ReactContextBaseJavaModule i
private static final String ERR_USER_DENIED_CODE = "ERR00";
private static final String ERR_SETTINGS_CHANGE_UNAVAILABLE_CODE = "ERR01";
private static final String ERR_FAILED_OPEN_DIALOG_CODE = "ERR02";
private static final String ERR_INTERNAL_ERROR = "ERR03";

private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Promise promise;


Expand All @@ -64,64 +60,20 @@ public void promptForEnableLocationIfNeeded(ReadableMap params, Promise promise)

this.promise = promise;

googleApiClient = new GoogleApiClient.Builder(getCurrentActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
googleApiClient.connect();

locationRequest = LocationRequest.create();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(params.hasKey(LOCATION_INTERVAL_DURATION_PARAMS_KEY) ? params.getInt(LOCATION_INTERVAL_DURATION_PARAMS_KEY) : DEFAULT_INTERVAL_DURATION);
locationRequest.setFastestInterval(params.hasKey(LOCATION_FAST_INTERVAL_DURATION_PARAMS_KEY) ? params.getInt(LOCATION_FAST_INTERVAL_DURATION_PARAMS_KEY) : DEFAULT_FAST_INTERVAL_DURATION);
}

@Override
public String getName() {
return SELF_MODULE_NAME;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(this);
Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(getCurrentActivity()).checkLocationSettings(builder.build());
task.addOnCompleteListener(this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

@Override
public void onResult(@NonNull LocationSettingsResult locationSettingsResult) {
final Status status = locationSettingsResult.getStatus();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
if (promise != null) promise.resolve("already-enabled");
this.promise = null;
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(getCurrentActivity(), REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException exception) {
Log.e(TAG, "Failed to show dialog", exception);
if (promise != null) promise.reject(ERR_FAILED_OPEN_DIALOG_CODE, new RNAndroidLocationEnablerException("Failed to show dialog", exception));
this.promise = null;
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
if (promise != null) promise.reject(ERR_SETTINGS_CHANGE_UNAVAILABLE_CODE, new RNAndroidLocationEnablerException("Settings change unavailable"));
this.promise = null;
break;
}
public String getName() {
return SELF_MODULE_NAME;
}

@Override
Expand All @@ -140,4 +92,33 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
public void onNewIntent(Intent intent) {

}

@Override
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
try {
task.getResult(ApiException.class);
if (promise != null) promise.resolve("already-enabled");
promise = null;
} catch (ApiException exception) {
switch (exception.getStatusCode()) {
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
ResolvableApiException resolvable = (ResolvableApiException) exception;
resolvable.startResolutionForResult(getCurrentActivity(), REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
Log.e(TAG, "Failed to show dialog", sendEx);
if (promise != null) promise.reject(ERR_FAILED_OPEN_DIALOG_CODE, new RNAndroidLocationEnablerException("Failed to show dialog", sendEx));
this.promise = null;
} catch (ClassCastException classCast) {
if (promise != null) promise.reject(ERR_INTERNAL_ERROR, new RNAndroidLocationEnablerException("Internal error", classCast));
this.promise = null;
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
if (promise != null) promise.reject(ERR_SETTINGS_CHANGE_UNAVAILABLE_CODE, new RNAndroidLocationEnablerException("Settings change unavailable"));
this.promise = null;
break;
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-android-location-enabler",
"version": "1.0.9",
"version": "1.1.0",
"description": "Display a GoogleMap like android popup to ask for user to enable location services if disabled ",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion samples/SimpleEnable/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ android {

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
compile project(':react-native-android-location-enabler')
api(project(':react-native-android-location-enabler')) {
exclude group: "com.android.support"
}
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
}
Expand Down
5 changes: 3 additions & 2 deletions samples/SimpleEnable/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
minSdkVersion = 19
compileSdkVersion = 28
targetSdkVersion = 28
supportLibVersion = "28.0.0"
}

repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.0")
classpath('com.android.tools.build:gradle:3.4.1')

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 29b0b40

Please sign in to comment.