From 6ee7aba035901175212e0ecd47a622fcf5d3b513 Mon Sep 17 00:00:00 2001
From: Prerak Mann <mannprerak2@gmail.com>
Date: Fri, 10 May 2019 18:24:38 +0530
Subject: [PATCH] added checkPermission method

---
 .vscode/launch.json                           | 13 ++++++
 .../NearbyConnectionsPlugin.java              | 31 +++++++++++++-
 example/lib/main.dart                         | 41 +++++++++++++++++--
 example/pubspec.yaml                          |  2 +-
 lib/nearby_connections.dart                   | 11 ++++-
 5 files changed, 90 insertions(+), 8 deletions(-)
 create mode 100644 .vscode/launch.json

diff --git a/.vscode/launch.json b/.vscode/launch.json
new file mode 100644
index 0000000..3287bb6
--- /dev/null
+++ b/.vscode/launch.json
@@ -0,0 +1,13 @@
+{
+    // Use IntelliSense to learn about possible attributes.
+    // Hover to view descriptions of existing attributes.
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
+    "version": "0.2.0",
+    "configurations": [
+        {
+            "name": "Flutter",
+            "request": "launch",
+            "type": "dart"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java
index 90b0ea1..6a40035 100644
--- a/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java
+++ b/android/src/main/java/com/pkmnapps/nearby_connections/NearbyConnectionsPlugin.java
@@ -1,5 +1,16 @@
 package com.pkmnapps.nearby_connections;
 
+import android.Manifest;
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.net.Uri;
+import android.provider.Settings;
+import android.support.v4.app.ActivityCompat;
+import android.support.v4.content.ContextCompat;
+import android.util.Log;
+
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
 import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
@@ -10,19 +21,35 @@ import io.flutter.plugin.common.PluginRegistry.Registrar;
  * NearbyConnectionsPlugin
  */
 public class NearbyConnectionsPlugin implements MethodCallHandler {
+    private Activity activity;
+
+    private NearbyConnectionsPlugin(Activity activity) {
+        this.activity = activity;
+    }
+
     /**
      * Plugin registration.
      */
+
     public static void registerWith(Registrar registrar) {
+
         final MethodChannel channel = new MethodChannel(registrar.messenger(), "nearby_connections");
-        channel.setMethodCallHandler(new NearbyConnectionsPlugin());
+        channel.setMethodCallHandler(new NearbyConnectionsPlugin(registrar.activity()));
     }
 
     @Override
     public void onMethodCall(MethodCall call, Result result) {
 
         switch (call.method) {
-
+            case "checkPermissions":
+                if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
+                        != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
+                        != PackageManager.PERMISSION_GRANTED) {
+                    result.success(false);
+                } else {
+                    result.success(true);
+                }
+                break;
             default:
                 result.notImplemented();
         }
diff --git a/example/lib/main.dart b/example/lib/main.dart
index a6570af..5171da1 100644
--- a/example/lib/main.dart
+++ b/example/lib/main.dart
@@ -19,9 +19,44 @@ class _MyAppState extends State<MyApp> {
         appBar: AppBar(
           title: const Text('Plugin example app'),
         ),
-        body: Center(
-          child: Text('Welcome'),
-        ),
+        body: Body(),
+      ),
+    );
+  }
+}
+
+class Body extends StatefulWidget {
+  @override
+  _MyBodyState createState() => _MyBodyState();
+}
+
+class _MyBodyState extends State<Body> {
+  @override
+  Widget build(BuildContext context) {
+    // TODO: implement build
+    return Center(
+      child: Column(
+        children: <Widget>[
+          RaisedButton(
+            child: Text("checkPermission"),
+            onPressed: () async {
+              if (await Nearby.instance.checkPermissions()) {
+                Scaffold.of(context)
+                    .showSnackBar(SnackBar(content: Text("yes")));
+              } else {
+                Scaffold.of(context)
+                    .showSnackBar(SnackBar(content: Text("No")));
+              }
+            },
+          ),
+          RaisedButton(
+            child: Text("askPermission(permission handler)"),
+            onPressed: () async {
+              // await PermissionHandler()
+              //     .requestPermissions([PermissionGroup.location]);
+            },
+          ),
+        ],
       ),
     );
   }
diff --git a/example/pubspec.yaml b/example/pubspec.yaml
index 05f1bd3..046513b 100644
--- a/example/pubspec.yaml
+++ b/example/pubspec.yaml
@@ -8,7 +8,7 @@ environment:
 dependencies:
   flutter:
     sdk: flutter
-
+  
   # The following adds the Cupertino Icons font to your application.
   # Use with the CupertinoIcons class for iOS style icons.
   cupertino_icons: ^0.1.2
diff --git a/lib/nearby_connections.dart b/lib/nearby_connections.dart
index 65bb7b7..31e8319 100644
--- a/lib/nearby_connections.dart
+++ b/lib/nearby_connections.dart
@@ -2,9 +2,16 @@ import 'dart:async';
 
 import 'package:flutter/services.dart';
 
-class NearbyConnections {
+class Nearby {
+  //for maintaining only 1 instance of this class
+  static final Nearby _instance = Nearby._();
+  static Nearby get instance => _instance;
+  Nearby._();
+
   static const MethodChannel _channel =
       const MethodChannel('nearby_connections');
 
-
+  Future<bool> checkPermissions() async => await _channel.invokeMethod(
+        'checkPermissions',
+      );
 }