mirror of
				https://github.com/hackku21/nearby_connections.git
				synced 2025-06-13 12:53:50 +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 | ||||
| * enableLocationService return type changed to Future<bool> | ||||
| * 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 | ||||
| ```dart | ||||
| // returns true/false asynchronously  | ||||
| bool a = await Nearby().checkLocationPermissions() | ||||
| bool a = await Nearby().checkLocationPermissions(); | ||||
| // 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 | ||||
| bool b = Nearby().checkExternalStoragePermission() | ||||
| bool b = Nearby().checkExternalStoragePermission(); | ||||
| // 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 | ||||
|  | ||||
| @ -1,11 +1,14 @@ | ||||
| package com.pkmnapps.nearby_connections; | ||||
| 
 | ||||
| import android.Manifest; | ||||
| import android.app.Activity; | ||||
| import android.content.Intent; | ||||
| import android.content.IntentSender; | ||||
| import android.content.pm.PackageManager; | ||||
| 
 | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| import androidx.core.app.ActivityCompat; | ||||
| 
 | ||||
| import com.google.android.gms.common.api.ApiException; | ||||
| 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.PluginRegistry; | ||||
| 
 | ||||
| class LocationEnabler implements PluginRegistry.ActivityResultListener { | ||||
| class LocationHelper implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener { | ||||
| 
 | ||||
|     @Nullable | ||||
|     private Activity activity; | ||||
| 
 | ||||
|     private static final int LOCATION_ENABLE_REQUEST = 777; | ||||
|     private static final int REQUEST_LOCATION_PERMISSION = 7777; | ||||
| 
 | ||||
|     private LocationSettingsRequest mLocationSettingsRequest; | ||||
|     private Result pendingResult; | ||||
| 
 | ||||
|     private LocationEnabler(@Nullable Activity activity) { | ||||
|     private LocationHelper(@Nullable Activity activity) { | ||||
|         this.activity = activity; | ||||
|     } | ||||
| 
 | ||||
|     LocationEnabler(PluginRegistry.Registrar registrar) { | ||||
|     LocationHelper(PluginRegistry.Registrar registrar) { | ||||
|         this(registrar.activity()); | ||||
|     } | ||||
| 
 | ||||
|     LocationEnabler() { | ||||
|     LocationHelper() { | ||||
|         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) { | ||||
| @ -64,6 +63,30 @@ class LocationEnabler implements PluginRegistry.ActivityResultListener { | ||||
|         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() { | ||||
|         LocationRequest mLocationRequest = LocationRequest.create(); | ||||
|         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 static final String SERVICE_ID = "com.pkmnapps.nearby_connections"; | ||||
|     private static MethodChannel channel; | ||||
|     private static LocationEnabler locationEnabler; | ||||
|     private static ActivityPluginBinding activityBinding; | ||||
|     private static LocationHelper locationHelper; | ||||
|     private static ActivityPluginBinding activityPluginBinding; | ||||
|     private static PluginRegistry.Registrar pluginRegistrar; | ||||
| 
 | ||||
|     private NearbyConnectionsPlugin(Activity activity) { | ||||
| @ -63,8 +63,8 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
| 
 | ||||
|     public static void registerWith(Registrar registrar) { | ||||
|         pluginRegistrar = registrar; | ||||
|         locationEnabler = new LocationEnabler(registrar); | ||||
|         locationEnabler.setActivity(registrar.activity()); | ||||
|         locationHelper = new LocationHelper(registrar); | ||||
|         locationHelper.setActivity(registrar.activity()); | ||||
|         initiate(); | ||||
|         channel = new MethodChannel(registrar.messenger(), "nearby_connections"); | ||||
|         channel.setMethodCallHandler(new NearbyConnectionsPlugin(registrar.activity())); | ||||
| @ -85,10 +85,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
|                 } | ||||
|                 break; | ||||
|             case "askLocationPermission": | ||||
|                 ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, | ||||
|                         Manifest.permission.ACCESS_COARSE_LOCATION}, 0); | ||||
|                 Log.d("nearby_connections", "askLocationPermission"); | ||||
|                 result.success(null); | ||||
|                 locationHelper.requestLocationPermission(result); | ||||
|                 break; | ||||
|             case "checkLocationEnabled": | ||||
|                 LocationManager lm = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE); | ||||
| @ -105,7 +102,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
|                 result.success(gps_enabled || network_enabled); | ||||
|                 break; | ||||
|             case "enableLocationServices": | ||||
|                 locationEnabler.requestLocationEnable(result); | ||||
|                 locationHelper.requestLocationEnable(result); | ||||
|                 break; | ||||
|             case "checkExternalStoragePermission": | ||||
|                 if (ContextCompat.checkSelfPermission(activity, | ||||
| @ -480,18 +477,18 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
| 
 | ||||
|     @Override | ||||
|     public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { | ||||
|         locationEnabler = new LocationEnabler(); | ||||
|         locationHelper = new LocationHelper(); | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
|     public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||||
|         locationEnabler = null; | ||||
|         locationHelper = null; | ||||
|     } | ||||
| 
 | ||||
|     private static void attachToActivity(ActivityPluginBinding binding) { | ||||
|         activityBinding = binding; | ||||
|         activityPluginBinding = binding; | ||||
|         try { | ||||
|             locationEnabler.setActivity(binding.getActivity()); | ||||
|             locationHelper.setActivity(binding.getActivity()); | ||||
|             initiate(); | ||||
|         } catch (Exception e) { | ||||
|             e.printStackTrace(); | ||||
| @ -499,8 +496,9 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
|     } | ||||
| 
 | ||||
|     private void detachActivity() { | ||||
|         activityBinding.removeActivityResultListener(locationEnabler); | ||||
|         activityBinding = null; | ||||
|         activityPluginBinding.removeRequestPermissionsResultListener(locationHelper); | ||||
|         activityPluginBinding.removeActivityResultListener(locationHelper); | ||||
|         activityPluginBinding = null; | ||||
|     } | ||||
| 
 | ||||
|     @Override | ||||
| @ -525,9 +523,11 @@ public class NearbyConnectionsPlugin implements MethodCallHandler, FlutterPlugin | ||||
| 
 | ||||
|     private static void initiate() { | ||||
|         if (pluginRegistrar != null) { | ||||
|             pluginRegistrar.addActivityResultListener(locationEnabler); | ||||
|             pluginRegistrar.addActivityResultListener(locationHelper); | ||||
|             pluginRegistrar.addRequestPermissionsResultListener(locationHelper); | ||||
|         } else { | ||||
|             activityBinding.addActivityResultListener(locationEnabler); | ||||
|             activityPluginBinding.addActivityResultListener(locationHelper); | ||||
|             activityPluginBinding.addRequestPermissionsResultListener(locationHelper); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|  | ||||
| @ -3,8 +3,8 @@ import 'dart:math'; | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:flutter/material.dart'; | ||||
| import 'package:nearby_connections/nearby_connections.dart'; | ||||
| import 'package:image_picker/image_picker.dart'; | ||||
| import 'package:nearby_connections/nearby_connections.dart'; | ||||
| 
 | ||||
| void main() => runApp(MyApp()); | ||||
| 
 | ||||
| @ -68,8 +68,15 @@ class _MyBodyState extends State<Body> { | ||||
|                 ), | ||||
|                 RaisedButton( | ||||
|                   child: Text("askLocationPermission"), | ||||
|                   onPressed: () { | ||||
|                     Nearby().askLocationPermission(); | ||||
|                   onPressed: () async { | ||||
|                     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( | ||||
| @ -114,11 +121,12 @@ class _MyBodyState extends State<Body> { | ||||
|                   child: Text("enableLocationServices"), | ||||
|                   onPressed: () async { | ||||
|                     if (await Nearby().enableLocationServices()) { | ||||
|                       Scaffold.of(context).showSnackBar( | ||||
|                           SnackBar(content: Text("Location Service Enabled :)"))); | ||||
|                       Scaffold.of(context).showSnackBar(SnackBar( | ||||
|                           content: Text("Location Service Enabled :)"))); | ||||
|                     } else { | ||||
|                       Scaffold.of(context).showSnackBar( | ||||
|                           SnackBar(content: Text("Enabling Location Service Failed :("))); | ||||
|                       Scaffold.of(context).showSnackBar(SnackBar( | ||||
|                           content: | ||||
|                               Text("Enabling Location Service Failed :("))); | ||||
|                     } | ||||
|                   }, | ||||
|                 ), | ||||
|  | ||||
| @ -108,7 +108,7 @@ packages: | ||||
|       path: ".." | ||||
|       relative: true | ||||
|     source: path | ||||
|     version: "2.0.0-dev" | ||||
|     version: "2.0.1-dev" | ||||
|   path: | ||||
|     dependency: transitive | ||||
|     description: | ||||
|  | ||||
| @ -1,9 +1,10 @@ | ||||
| import 'dart:async'; | ||||
| import 'dart:typed_data'; | ||||
| 
 | ||||
| import 'package:flutter/foundation.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/defs.dart'; | ||||
| 
 | ||||
| /// The NearbyConnection class | ||||
| /// | ||||
| @ -130,7 +131,8 @@ class Nearby { | ||||
|   } | ||||
| 
 | ||||
|   //for advertisers | ||||
|   OnConnectionInitiated _advertConnectionInitiated, _discoverConnectionInitiated; | ||||
|   OnConnectionInitiated _advertConnectionInitiated, | ||||
|       _discoverConnectionInitiated; | ||||
|   OnConnectionResult _advertConnectionResult, _discoverConnectionResult; | ||||
|   OnDisconnected _advertDisconnected, _discoverDisconnected; | ||||
| 
 | ||||
| @ -156,8 +158,8 @@ class Nearby { | ||||
|   /// convenience method | ||||
|   /// | ||||
|   /// Asks location permission | ||||
|   void askLocationPermission() => | ||||
|       _channel.invokeMethod('askLocationPermission'); | ||||
|   Future<bool> askLocationPermission() async => | ||||
|       await _channel.invokeMethod('askLocationPermission'); | ||||
| 
 | ||||
|   /// convenience method | ||||
|   /// | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| name: nearby_connections | ||||
| 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 | ||||
| 
 | ||||
| environment: | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user