Skip to content

Commit

Permalink
Merge pull request #1 from jayordway/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
jayordway authored Apr 30, 2020
2 parents a14317c + 6dfea5c commit 06983a4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 116 deletions.
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ dependencies {
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation 'androidx.core:core:1.2.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.android.support:appcompat-v7:28.0.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,86 @@
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;


import androidx.core.app.ActivityCompat;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.ContextCompat;

@NativePlugin()
@NativePlugin(requestCodes = {AppPermissionsPlugin.REQUEST_CAMERA, AppPermissionsPlugin.REQUEST_COARSE_LOCATION,
AppPermissionsPlugin.REQUEST_READ_EXTERNAL_STORAGE, AppPermissionsPlugin.REQUEST_WRITE_EXTERNAL_STORAGE,
AppPermissionsPlugin.REQUEST_NOTIFICATION_POLICY})
public class AppPermissionsPlugin extends Plugin {

static final int REQUEST_CAMERA = 20120;
static final int REQUEST_READ_EXTERNAL_STORAGE = 20130;
static final int REQUEST_WRITE_EXTERNAL_STORAGE = 20140;
static final int REQUEST_COARSE_LOCATION = 20150;
static final int REQUEST_NOTIFICATION_POLICY = 20160;


@RequiresApi(api = Build.VERSION_CODES.M)
@PluginMethod
public void request(PluginCall call){
JSObject ret = new JSObject();
public void request(PluginCall call) {
String name = call.getString("permission");
saveCall(call);
switch (name) {
case "camera":
requestPermissions(Manifest.permission.CAMERA, REQUEST_CAMERA);
break;
case "photos":
requestPermissions(Manifest.permission.READ_EXTERNAL_STORAGE, REQUEST_READ_EXTERNAL_STORAGE);
break;
case "geolocation":
requestPermissions(Manifest.permission.ACCESS_COARSE_LOCATION, REQUEST_COARSE_LOCATION);
break;
case "notifications":
requestPermissions(Manifest.permission.ACCESS_NOTIFICATION_POLICY, REQUEST_NOTIFICATION_POLICY);
break;
case "file-write":
requestPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_WRITE_EXTERNAL_STORAGE);
break;
default:
call.reject("Unknown permission type");
}

}

if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
private void requestPermissions(String permission, int permissionConstant) {
pluginRequestPermission(permission, permissionConstant);
}

@Override
protected void handleRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.handleRequestPermissionsResult(requestCode, permissions, grantResults);
JSObject ret = new JSObject();
PluginCall savedCall = getSavedCall();

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
ret.put("state", "GRANTED");
if (savedCall == null) {
return;
}
else
{
ret.put("state", "ALREADY GRANTED");

for (int result : grantResults) {
if (result == PackageManager.PERMISSION_DENIED) {
ret.put("state", "denied");
savedCall.resolve(ret);
return;
}
}

call.resolve(ret);
if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_COARSE_LOCATION || requestCode == REQUEST_READ_EXTERNAL_STORAGE ||
requestCode == REQUEST_NOTIFICATION_POLICY || requestCode == REQUEST_WRITE_EXTERNAL_STORAGE) {
// We got the permission
ret.put("state", "granted");
savedCall.resolve(ret);
}
}


@PluginMethod
public void query(PluginCall call) {
String name = call.getString("name");
Expand All @@ -51,10 +103,6 @@ public void query(PluginCall call) {
case "notifications":
checkNotifications(call);
break;
case "clipboard-read":
case "clipboard-write":
checkClipboard(call);
break;
case "file-write":
checkFileWrite(call);
break;
Expand All @@ -75,7 +123,7 @@ private void checkPerm(String perm, PluginCall call) {
call.resolve(ret);
}

private void checkFileWrite(PluginCall call){
private void checkFileWrite(PluginCall call) {
checkPerm(Manifest.permission.WRITE_EXTERNAL_STORAGE, call);
}

Expand All @@ -98,9 +146,4 @@ private void checkNotifications(PluginCall call) {
call.resolve(ret);
}

private void checkClipboard(PluginCall call) {
JSObject ret = new JSObject();
ret.put("state", "granted");
call.resolve(ret);
}
}
108 changes: 26 additions & 82 deletions ios/Plugin/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ public class AppPermissionsPlugin: CAPPlugin {
case "LOCATION_ALWAYS":
locationManager.requestAlwaysAuthorization();
break;
case "PHOTO_LIBRARY":
case "photos":
PHPhotoLibrary.requestAuthorization { (PHAuthorizationStatus) in
switch (PHAuthorizationStatus) {
case .authorized:
status = "PHOTO_LIBRARY/AUTHORIZED";
status = "granted";
break;
case.denied, .restricted:
status = "PHOTO_LIBRARY/DENIED";
status = "denied";
break;
case.notDetermined:
status = "PHOTO_LIBRARY/NOT_DETERMINED";
status = "not_determined";
break;
}
call.resolve([
"status": status
])
}
break;
case "CAMERA":
case "camera":
AVCaptureDevice.requestAccess(for: .video) { (granted) in
if granted {
status = "CAMERA/AUTHORIZED";
status = "granted";
}
else {
status = "CAMERA/DENIED";
status = "denied";
}
call.success([
"status": status
])
}
break;
case "NOTIFICATION":
case "notifications":
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
status = "NOTIFICATION/AUTHORIZED";
status = "granted";
}
else {
status = "NOTIFICATION/DENIED";
status = "denied";
}
call.success([
"status": status
Expand All @@ -70,80 +70,24 @@ public class AppPermissionsPlugin: CAPPlugin {
}

}

@objc func query(_ call: CAPPluginCall) {
let permission = call.getString("name") ?? ""
var status = "";
switch (permission) {
case "LOCATION_WHEN_IN_USE", "LOCATION_ALWAYS":
switch (CLLocationManager.authorizationStatus()) {
case.authorizedAlways:
status = "LOCATION/AUTHORIZED_ALWAYS";
break;
case.authorizedWhenInUse:
status = "LOCATION/AUTHORIZED_WHEN_IN_USE";
break;
case.denied, .restricted:
status = "LOCATION/DENIED";
break;
case .notDetermined:
status = "LOCATION/NOT_DETERMINED";
break;
}
call.resolve([
"status": status
])
break;
case "PHOTO_LIBRARY":
switch (PHPhotoLibrary.authorizationStatus()) {
case .authorized:
status = "PHOTO_LIBRARY/AUTHORIZED";
break;
case.denied, .restricted:
status = "PHOTO_LIBRARY/DENIED";
break;
case.notDetermined:
status = "PHOTO_LIBRARY/NOT_DETERMINED";
break;
}
call.resolve([
"status": status
])
break;
case "CAMERA":
switch (AVCaptureDevice.authorizationStatus(for: .video)) {
case .authorized:
status = "CAMERA/AUTHORIZED";
break;
case.denied, .restricted:
status = "CAMERA/DENIED";
break;
case.notDetermined:
status = "CAMERA/NOT_DETERMINED";
break;
}
call.resolve([
"status": status
])
break;
case "NOTIFICATION":
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in
switch settings.authorizationStatus {
case .authorized, .provisional:
status = "NOTIFICATION/AUTHORIZED";
case .denied:
status = "NOTIFICATION/DENIED";
case .notDetermined:
status = "NOTIFICATION/NOT_DETERMINED";
}
call.resolve([
"status": status
])
}
)
break;
guard let name = call.getString("name") else {
call.reject("Must provide a permission to check")
return
}

switch (name) {
case "camera":
return checkCamera(call)
case "geolocation":
return checkGeolocation(call)
case "notifications":
return checkNotifications(call)
case "photos":
return checkPhotos(call)
default:
status = "ERROR";
return call.reject("Unknown permission type")
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capacitor-permissions",
"version": "0.0.21",
"version": "0.0.22",
"description": "A Capacitor plugin for accessing and requesting app permissions",
"main": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down
11 changes: 1 addition & 10 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ export interface PluginListenerHandle {
remove: () => void;
}

export declare enum PermissionType {
Camera = "camera",
Photos = "photos",
FileWrite = "file-write",
Geolocation = "geolocation",
Notifications = "notifications",
ClipboardRead = "clipboard-read",
ClipboardWrite = "clipboard-write"
}

export interface PermissionsRequestResult {
results: any[];
Expand All @@ -32,7 +23,7 @@ export interface PermissionsPlugin extends Plugin {
}

export interface PermissionsOptions {
name: PermissionType;
name: string;
}
export interface PermissionResult {
state: 'granted' | 'denied' | 'prompt';
Expand Down
4 changes: 2 additions & 2 deletions src/web.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WebPlugin } from '@capacitor/core';
import { PermissionsPlugin, PermissionsOptions, PermissionResult, PermissionType } from './definitions';
import { PermissionsPlugin, PermissionsOptions, PermissionResult } from './definitions';


export class AppPermissionsPluginWeb extends WebPlugin implements PermissionsPlugin {
Expand All @@ -22,7 +22,7 @@ export class AppPermissionsPluginWeb extends WebPlugin implements PermissionsPlu
return Promise.reject('This browser does not support the Permissions API');
}

const name = options.name === PermissionType.Photos ? 'camera' : options.name;
const name = options.name;
const ret = await navigator.permissions.query({ name });

return {
Expand Down

0 comments on commit 06983a4

Please sign in to comment.