Update enableLocationService method (#19)
* enableLocationService return type changed to Future<bool> * callback added to enableLocationService * typos fixes #15 * fix #15 added to changelog and bug fixesmultipeer_ios
parent
81519d7571
commit
e7edb9865b
@ -1,3 +1,6 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.pkmnapps.nearby_connections">
|
||||
package="com.pkmnapps.nearby_connections">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
</manifest>
|
||||
|
@ -0,0 +1,110 @@
|
||||
package com.pkmnapps.nearby_connections;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentSender;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
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.LocationSettingsResponse;
|
||||
import com.google.android.gms.location.LocationSettingsStatusCodes;
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
|
||||
import io.flutter.plugin.common.MethodChannel.Result;
|
||||
import io.flutter.plugin.common.PluginRegistry;
|
||||
|
||||
class LocationEnabler implements PluginRegistry.ActivityResultListener {
|
||||
|
||||
@Nullable
|
||||
private Activity activity;
|
||||
|
||||
private static final int LOCATION_ENABLE_REQUEST = 777;
|
||||
|
||||
private LocationSettingsRequest mLocationSettingsRequest;
|
||||
private Result pendingResult;
|
||||
|
||||
private LocationEnabler(@Nullable Activity activity) {
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
LocationEnabler(PluginRegistry.Registrar registrar) {
|
||||
this(registrar.activity());
|
||||
}
|
||||
|
||||
LocationEnabler() {
|
||||
this.activity = null;
|
||||
}
|
||||
|
||||
void setActivity(@Nullable Activity activity) {
|
||||
this.activity = activity;
|
||||
initiateLocationServiceRequest();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (pendingResult == null) {
|
||||
return false;
|
||||
}
|
||||
if (requestCode == LOCATION_ENABLE_REQUEST) {
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
pendingResult.success(true);
|
||||
} else {
|
||||
pendingResult.success(false);
|
||||
}
|
||||
pendingResult = null;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void initiateLocationServiceRequest() {
|
||||
LocationRequest mLocationRequest = LocationRequest.create();
|
||||
LocationSettingsRequest.Builder builder = new LocationSettingsRequest
|
||||
.Builder()
|
||||
.addLocationRequest(mLocationRequest)
|
||||
.setAlwaysShow(true);
|
||||
mLocationSettingsRequest = builder.build();
|
||||
}
|
||||
|
||||
void requestLocationEnable(final Result result) {
|
||||
this.pendingResult = result;
|
||||
Task<LocationSettingsResponse> task = LocationServices.getSettingsClient(activity)
|
||||
.checkLocationSettings(mLocationSettingsRequest);
|
||||
|
||||
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<LocationSettingsResponse> task) {
|
||||
try {
|
||||
task.getResult(ApiException.class);
|
||||
result.success(true);
|
||||
} catch (ApiException ex) {
|
||||
switch (ex.getStatusCode()) {
|
||||
case LocationSettingsStatusCodes.SUCCESS:
|
||||
result.success(true);
|
||||
break;
|
||||
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
|
||||
try {
|
||||
ResolvableApiException resolvableApiException =
|
||||
(ResolvableApiException) ex;
|
||||
resolvableApiException
|
||||
.startResolutionForResult(activity, LOCATION_ENABLE_REQUEST);
|
||||
} catch (IntentSender.SendIntentException e) {
|
||||
result.error("LOCATION_SERVICE_ERROR", e.getMessage(), null);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
result.success(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
<resources></resources>
|
Loading…
Reference in new issue