Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle permissions and gps status #17

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright 2018 Myles McNamara
* Copyright 2015 Parag Garg
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +19,10 @@
import org.apache.cordova.*;
import java.util.List;
import java.util.concurrent.Future;
import java.util.Arrays;
import java.lang.InterruptedException;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

import org.json.JSONArray;
import org.json.JSONException;
Expand All @@ -27,6 +31,7 @@
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
Expand All @@ -37,6 +42,9 @@
import android.content.Context;
import android.util.Log;
import android.os.Build.VERSION;
import android.os.Build;
import android.location.LocationManager;
import android.Manifest;


public class WifiWizard2 extends CordovaPlugin {
Expand Down Expand Up @@ -89,6 +97,16 @@ else if(action.equals(SET_WIFI_ENABLED)) {
return true; // Even though enable wifi failed, we still return true and handle error in callback
}

if(!displayLocationSettingsRequest()){
callbackContext.error("Android 6 and above Gps turned off");
return true;
}

if(!checkCurrentPermissions()){
callbackContext.error("permission not found") ;
return true;
}

// Actions that DO require WiFi to be enabled
if(action.equals(ADD_NETWORK)) {
this.addNetwork(callbackContext, data);
Expand Down Expand Up @@ -195,6 +213,100 @@ private boolean verifyWifiEnabled(){

}

private boolean displayLocationSettingsRequest() {
Context context=this.cordova.getActivity().getApplicationContext();

LocationManager lm = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean gps_enabled = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){

try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

} catch(Exception ex) {

}
}else{
return true;
}
return gps_enabled;
}

public boolean checkCurrentPermissions(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !thasPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)) {
trequestPermissions(this,1,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION});
return false;
}
else return true;
}

public static void trequestPermissions(CordovaPlugin plugin, int requestCode, String[] permissions) {
try {
Method requestPermission = CordovaInterface.class.getDeclaredMethod(
"requestPermissions", CordovaPlugin.class, int.class, String[].class);

// If there is no exception, then this is cordova-android 5.0.0+
requestPermission.invoke(plugin.cordova, plugin, requestCode, permissions);
} catch (NoSuchMethodException noSuchMethodException) {
// cordova-android version is less than 5.0.0, so permission is implicitly granted
LOG.d(TAG, "No need to request permissions " + Arrays.toString(permissions));

// Notify the plugin that all were granted by using more reflection
deliverPermissionResult(plugin, requestCode, permissions);
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(TAG, "IllegalAccessException when requesting permissions " + Arrays.toString(permissions), illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method does not throw any exceptions, so this should never be caught
LOG.e(TAG, "invocationTargetException when requesting permissions " + Arrays.toString(permissions), invocationTargetException);
}
}

public static boolean thasPermission(CordovaPlugin plugin, String permission) {
try {
Method hasPermission = CordovaInterface.class.getDeclaredMethod("hasPermission", String.class);

// If there is no exception, then this is cordova-android 5.0.0+
return (Boolean) hasPermission.invoke(plugin.cordova, permission);
} catch (NoSuchMethodException noSuchMethodException) {
// cordova-android version is less than 5.0.0, so permission is implicitly granted
LOG.d(TAG, "No need to check for permission " + permission);
return true;
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(TAG, "IllegalAccessException when checking permission " + permission, illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method does not throw any exceptions, so this should never be caught
LOG.e(TAG, "invocationTargetException when checking permission " + permission, invocationTargetException);
}
return false;
}

private static void deliverPermissionResult(CordovaPlugin plugin, int requestCode, String[] permissions) {
// Generate the request results
int[] requestResults = new int[permissions.length];
Arrays.fill(requestResults, PackageManager.PERMISSION_GRANTED);

try {
Method onRequestPermissionResult = CordovaPlugin.class.getDeclaredMethod(
"onRequestPermissionResult", int.class, String[].class, int[].class);

onRequestPermissionResult.invoke(plugin, requestCode, permissions, requestResults);
} catch (NoSuchMethodException noSuchMethodException) {
// Should never be caught since the plugin must be written for cordova-android 5.0.0+ if it
// made it to this point
LOG.e(TAG, "NoSuchMethodException when delivering permissions results", noSuchMethodException);
} catch (IllegalAccessException illegalAccessException) {
// Should never be caught; this is a public method
LOG.e(TAG, "IllegalAccessException when delivering permissions results", illegalAccessException);
} catch(InvocationTargetException invocationTargetException) {
// This method may throw a JSONException. We are just duplicating cordova-android's
// exception handling behavior here; all it does is log the exception in CordovaActivity,
// print the stacktrace, and ignore it
LOG.e(TAG, "InvocationTargetException when delivering permissions results", invocationTargetException);
}
}

/**
* WEP has two kinds of password, a hex value that specifies the key or
* a character string used to generate the real hex. This checks what kind of
Expand Down