mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
Update askLocationPermission method (#21)
* enableLocationService return type changed to Future<bool> * typos fixes #15 * fix #15 added to changelog and bug fixes * askLocationPermission return type changed to Future<bool> : fixes #9
This commit is contained in:
parent
e7edb9865b
commit
00289fa7bc
@ -1,3 +1,8 @@
|
|||||||
|
## 2.0.1-dev
|
||||||
|
* askLocationPermission return type changed to Future<bool>
|
||||||
|
* Updated Example accordingly
|
||||||
|
* Readme Updated
|
||||||
|
|
||||||
## 2.0.0-dev
|
## 2.0.0-dev
|
||||||
* enableLocationService return type changed to Future<bool>
|
* enableLocationService return type changed to Future<bool>
|
||||||
* Updated Example accordingly
|
* Updated Example accordingly
|
||||||
|
11
README.md
11
README.md
@ -47,16 +47,17 @@ Since ACCESS_FINE_LOCATION and READ_EXTERNAL_STORAGE is considered to be dangero
|
|||||||
#### As a **convenience** this library provides methods to check and request location and external read/write permissions
|
#### As a **convenience** this library provides methods to check and request location and external read/write permissions
|
||||||
```dart
|
```dart
|
||||||
// returns true/false asynchronously
|
// returns true/false asynchronously
|
||||||
bool a = await Nearby().checkLocationPermissions()
|
bool a = await Nearby().checkLocationPermissions();
|
||||||
// asks for permission only if its not given
|
// asks for permission only if its not given
|
||||||
Nearby().askLocationPermission()
|
// returns true/false if the location permission is granted on/off resp.
|
||||||
|
bool b = Nearby().askLocationPermission();
|
||||||
|
|
||||||
// OPTIONAL: if you need to transfer files and rename it on device
|
// OPTIONAL: if you need to transfer files and rename it on device
|
||||||
bool b = Nearby().checkExternalStoragePermission()
|
bool b = Nearby().checkExternalStoragePermission();
|
||||||
// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given
|
// asks for READ + WRTIE EXTERNAL STORAGE permission only if its not given
|
||||||
Nearby().askExternalStoragePermission()
|
Nearby().askExternalStoragePermission() ;
|
||||||
|
|
||||||
Nearby().askLocationAndExternalStoragePermission() // for all permissions in one go..
|
Nearby().askLocationAndExternalStoragePermission(); // for all permissions in one go..
|
||||||
```
|
```
|
||||||
|
|
||||||
## Work Flow
|
## Work Flow
|
||||||
|
@ -1,11 +1,14 @@
|
|||||||
package com.pkmnapps.nearby_connections;
|
package com.pkmnapps.nearby_connections;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentSender;
|
import android.content.IntentSender;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.app.ActivityCompat;
|
||||||
|
|
||||||
import com.google.android.gms.common.api.ApiException;
|
import com.google.android.gms.common.api.ApiException;
|
||||||
import com.google.android.gms.common.api.ResolvableApiException;
|
import com.google.android.gms.common.api.ResolvableApiException;
|
||||||
@ -20,33 +23,29 @@ import com.google.android.gms.tasks.Task;
|
|||||||
import io.flutter.plugin.common.MethodChannel.Result;
|
import io.flutter.plugin.common.MethodChannel.Result;
|
||||||
import io.flutter.plugin.common.PluginRegistry;
|
import io.flutter.plugin.common.PluginRegistry;
|
||||||
|
|
||||||
class LocationEnabler implements PluginRegistry.ActivityResultListener {
|
class LocationHelper implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener {
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private Activity activity;
|
private Activity activity;
|
||||||
|
|
||||||
private static final int LOCATION_ENABLE_REQUEST = 777;
|
private static final int LOCATION_ENABLE_REQUEST = 777;
|
||||||
|
private static final int REQUEST_LOCATION_PERMISSION = 7777;
|
||||||
|
|
||||||
private LocationSettingsRequest mLocationSettingsRequest;
|
private LocationSettingsRequest mLocationSettingsRequest;
|
||||||
private Result pendingResult;
|
private Result pendingResult;
|
||||||
|
|
||||||
private LocationEnabler(@Nullable Activity activity) {
|
private LocationHelper(@Nullable Activity activity) {
|
||||||
this.activity = activity;
|
this.activity = activity;
|
||||||
}
|
}
|
||||||
|
|
||||||
LocationEnabler(PluginRegistry.Registrar registrar) {
|
LocationHelper(PluginRegistry.Registrar registrar) {
|
||||||
this(registrar.activity());
|
this(registrar.activity());
|
||||||
}
|
}
|
||||||
|
|
||||||
LocationEnabler() {
|
LocationHelper() {
|
||||||
this.activity = null;
|
this.activity = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setActivity(@Nullable Activity activity) {
|
|
||||||
this.activity = activity;
|
|
||||||
initiateLocationServiceRequest();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||||
if (pendingResult == null) {
|
if (pendingResult == null) {
|
||||||
@ -64,6 +63,30 @@ class LocationEnabler implements PluginRegistry.ActivityResultListener {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
if (requestCode == REQUEST_LOCATION_PERMISSION && permissions.length > 0) {
|
||||||
|
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
if (pendingResult != null) {
|
||||||
|
pendingResult.success(true);
|
||||||
|
pendingResult = null;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (pendingResult != null) {
|
||||||
|
pendingResult.success(false);
|
||||||
|
pendingResult = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setActivity(@Nullable Activity activity) {
|
||||||
|
this.activity = activity;
|
||||||
|
initiateLocationServiceRequest();
|
||||||
|
}
|
||||||
|
|
||||||
private void initiateLocationServiceRequest() {
|
private void initiateLocationServiceRequest() {
|
||||||
LocationRequest mLocationRequest = LocationRequest.create();
|
LocationRequest mLocationRequest = LocationRequest.create();
|
||||||
LocationSettingsRequest.Builder builder = new LocationSettingsRequest
|
LocationSettingsRequest.Builder builder = new LocationSettingsRequest
|
||||||
@ -107,4 +130,10 @@ class LocationEnabler implements PluginRegistry.ActivityResultListener {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void requestLocationPermission(Result result) {
|
||||||
|
this.pendingResult = result;
|
||||||
|
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
|
||||||
|
Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -48,8 +48,8 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
private Activity activity;
|
private Activity activity;
|
||||||
private static final String SERVICE_ID = "com.pkmnapps.nearby_connections";
|
private static final String SERVICE_ID = "com.pkmnapps.nearby_connections";
|
||||||
private static MethodChannel channel;
|
private static MethodChannel channel;
|
||||||
private static LocationEnabler locationEnabler;
|
private static LocationHelper locationHelper;
|
||||||
private static ActivityPluginBinding activityBinding;
|
private static ActivityPluginBinding activityPluginBinding;
|
||||||
private static PluginRegistry.Registrar pluginRegistrar;
|
private static PluginRegistry.Registrar pluginRegistrar;
|
||||||
|
|
||||||
private NearbyConnectionsPlugin(Activity activity) {
|
private NearbyConnectionsPlugin(Activity activity) {
|
||||||
@ -63,8 +63,8 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
|
|
||||||
public static void registerWith(Registrar registrar) {
|
public static void registerWith(Registrar registrar) {
|
||||||
pluginRegistrar = registrar;
|
pluginRegistrar = registrar;
|
||||||
locationEnabler = new LocationEnabler(registrar);
|
locationHelper = new LocationHelper(registrar);
|
||||||
locationEnabler.setActivity(registrar.activity());
|
locationHelper.setActivity(registrar.activity());
|
||||||
initiate();
|
initiate();
|
||||||
channel = new MethodChannel(registrar.messenger(), "nearby_connections");
|
channel = new MethodChannel(registrar.messenger(), "nearby_connections");
|
||||||
channel.setMethodCallHandler(new NearbyConnectionsPlugin(registrar.activity()));
|
channel.setMethodCallHandler(new NearbyConnectionsPlugin(registrar.activity()));
|
||||||
@ -85,10 +85,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "askLocationPermission":
|
case "askLocationPermission":
|
||||||
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
|
locationHelper.requestLocationPermission(result);
|
||||||
Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
|
|
||||||
Log.d("nearby_connections", "askLocationPermission");
|
|
||||||
result.success(null);
|
|
||||||
break;
|
break;
|
||||||
case "checkLocationEnabled":
|
case "checkLocationEnabled":
|
||||||
LocationManager lm = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
|
LocationManager lm = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
|
||||||
@ -105,7 +102,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
result.success(gps_enabled || network_enabled);
|
result.success(gps_enabled || network_enabled);
|
||||||
break;
|
break;
|
||||||
case "enableLocationServices":
|
case "enableLocationServices":
|
||||||
locationEnabler.requestLocationEnable(result);
|
locationHelper.requestLocationEnable(result);
|
||||||
break;
|
break;
|
||||||
case "checkExternalStoragePermission":
|
case "checkExternalStoragePermission":
|
||||||
if (ContextCompat.checkSelfPermission(activity,
|
if (ContextCompat.checkSelfPermission(activity,
|
||||||
@ -480,18 +477,18 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
|
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
|
||||||
locationEnabler = new LocationEnabler();
|
locationHelper = new LocationHelper();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
|
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
|
||||||
locationEnabler = null;
|
locationHelper = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void attachToActivity(ActivityPluginBinding binding) {
|
private static void attachToActivity(ActivityPluginBinding binding) {
|
||||||
activityBinding = binding;
|
activityPluginBinding = binding;
|
||||||
try {
|
try {
|
||||||
locationEnabler.setActivity(binding.getActivity());
|
locationHelper.setActivity(binding.getActivity());
|
||||||
initiate();
|
initiate();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -499,8 +496,9 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void detachActivity() {
|
private void detachActivity() {
|
||||||
activityBinding.removeActivityResultListener(locationEnabler);
|
activityPluginBinding.removeRequestPermissionsResultListener(locationHelper);
|
||||||
activityBinding = null;
|
activityPluginBinding.removeActivityResultListener(locationHelper);
|
||||||
|
activityPluginBinding = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -525,9 +523,11 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin
|
|||||||
|
|
||||||
private static void initiate() {
|
private static void initiate() {
|
||||||
if (pluginRegistrar != null) {
|
if (pluginRegistrar != null) {
|
||||||
pluginRegistrar.addActivityResultListener(locationEnabler);
|
pluginRegistrar.addActivityResultListener(locationHelper);
|
||||||
|
pluginRegistrar.addRequestPermissionsResultListener(locationHelper);
|
||||||
} else {
|
} else {
|
||||||
activityBinding.addActivityResultListener(locationEnabler);
|
activityPluginBinding.addActivityResultListener(locationHelper);
|
||||||
|
activityPluginBinding.addRequestPermissionsResultListener(locationHelper);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@ import 'dart:math';
|
|||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:nearby_connections/nearby_connections.dart';
|
|
||||||
import 'package:image_picker/image_picker.dart';
|
import 'package:image_picker/image_picker.dart';
|
||||||
|
import 'package:nearby_connections/nearby_connections.dart';
|
||||||
|
|
||||||
void main() => runApp(MyApp());
|
void main() => runApp(MyApp());
|
||||||
|
|
||||||
@ -68,8 +68,15 @@ class _MyBodyState extends State<Body> {
|
|||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
child: Text("askLocationPermission"),
|
child: Text("askLocationPermission"),
|
||||||
onPressed: () {
|
onPressed: () async {
|
||||||
Nearby().askLocationPermission();
|
if (await Nearby().askLocationPermission()) {
|
||||||
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
|
content: Text("Location Permission granted :)")));
|
||||||
|
} else {
|
||||||
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
|
content:
|
||||||
|
Text("Location permissions not granted :(")));
|
||||||
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
RaisedButton(
|
RaisedButton(
|
||||||
@ -114,11 +121,12 @@ class _MyBodyState extends State<Body> {
|
|||||||
child: Text("enableLocationServices"),
|
child: Text("enableLocationServices"),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
if (await Nearby().enableLocationServices()) {
|
if (await Nearby().enableLocationServices()) {
|
||||||
Scaffold.of(context).showSnackBar(
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
SnackBar(content: Text("Location Service Enabled :)")));
|
content: Text("Location Service Enabled :)")));
|
||||||
} else {
|
} else {
|
||||||
Scaffold.of(context).showSnackBar(
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
SnackBar(content: Text("Enabling Location Service Failed :(")));
|
content:
|
||||||
|
Text("Enabling Location Service Failed :(")));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
@ -108,7 +108,7 @@ packages:
|
|||||||
path: ".."
|
path: ".."
|
||||||
relative: true
|
relative: true
|
||||||
source: path
|
source: path
|
||||||
version: "2.0.0-dev"
|
version: "2.0.1-dev"
|
||||||
path:
|
path:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:typed_data';
|
import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/services.dart';
|
import 'package:flutter/services.dart';
|
||||||
import 'package:nearby_connections/src/defs.dart';
|
|
||||||
import 'package:nearby_connections/src/classes.dart';
|
import 'package:nearby_connections/src/classes.dart';
|
||||||
|
import 'package:nearby_connections/src/defs.dart';
|
||||||
|
|
||||||
/// The NearbyConnection class
|
/// The NearbyConnection class
|
||||||
///
|
///
|
||||||
@ -130,7 +131,8 @@ class Nearby {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//for advertisers
|
//for advertisers
|
||||||
OnConnectionInitiated _advertConnectionInitiated, _discoverConnectionInitiated;
|
OnConnectionInitiated _advertConnectionInitiated,
|
||||||
|
_discoverConnectionInitiated;
|
||||||
OnConnectionResult _advertConnectionResult, _discoverConnectionResult;
|
OnConnectionResult _advertConnectionResult, _discoverConnectionResult;
|
||||||
OnDisconnected _advertDisconnected, _discoverDisconnected;
|
OnDisconnected _advertDisconnected, _discoverDisconnected;
|
||||||
|
|
||||||
@ -156,8 +158,8 @@ class Nearby {
|
|||||||
/// convenience method
|
/// convenience method
|
||||||
///
|
///
|
||||||
/// Asks location permission
|
/// Asks location permission
|
||||||
void askLocationPermission() =>
|
Future<bool> askLocationPermission() async =>
|
||||||
_channel.invokeMethod('askLocationPermission');
|
await _channel.invokeMethod('askLocationPermission');
|
||||||
|
|
||||||
/// convenience method
|
/// convenience method
|
||||||
///
|
///
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
name: nearby_connections
|
name: nearby_connections
|
||||||
description: Plugin for the android NearbyConnections API. Bytes and Files Supported.
|
description: Plugin for the android NearbyConnections API. Bytes and Files Supported.
|
||||||
version: 2.0.0-dev
|
version: 2.0.1-dev
|
||||||
homepage: https://github.com/mannprerak2/nearby_connections
|
homepage: https://github.com/mannprerak2/nearby_connections
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
Loading…
Reference in New Issue
Block a user