Skip to content

Commit

Permalink
feat: improve permissions
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Imhoff <[email protected]>
Co-authored-by: jcesarmobile <[email protected]>
  • Loading branch information
3 people authored Oct 30, 2020
1 parent f3022a3 commit eec61a6
Show file tree
Hide file tree
Showing 10 changed files with 439 additions and 157 deletions.
50 changes: 38 additions & 12 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.plugin.SplashScreen;
import com.getcapacitor.util.HostMask;
import java.io.File;
Expand Down Expand Up @@ -403,18 +404,27 @@ public void registerPlugins(Class<? extends Plugin>[] pluginClasses) {
* @param pluginClass a class inheriting from Plugin
*/
public void registerPlugin(Class<? extends Plugin> pluginClass) {
NativePlugin pluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);
String pluginName;

CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
Logger.error("NativePlugin doesn't have the @NativePlugin annotation. Please add it");
return;
NativePlugin legacyPluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);

if (legacyPluginAnnotation == null) {
Logger.error("Plugin doesn't have the @CapacitorPlugin annotation. Please add it");
return;
}

pluginName = legacyPluginAnnotation.name();
} else {
pluginName = pluginAnnotation.name();
}

String pluginId = pluginClass.getSimpleName();

// Use the supplied name as the id if available
if (!pluginAnnotation.name().equals("")) {
pluginId = pluginAnnotation.name();
if (!pluginName.equals("")) {
pluginId = pluginName;
}

Logger.debug("Registering plugin: " + pluginId);
Expand All @@ -425,7 +435,7 @@ public void registerPlugin(Class<? extends Plugin> pluginClass) {
Logger.error(
"NativePlugin " +
pluginClass.getName() +
" is invalid. Ensure the @NativePlugin annotation exists on the plugin class and" +
" is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" +
" the class extends Plugin"
);
} catch (PluginLoadException ex) {
Expand All @@ -445,19 +455,31 @@ public PluginHandle getPlugin(String pluginId) {
*/
public PluginHandle getPluginWithRequestCode(int requestCode) {
for (PluginHandle handle : this.plugins.values()) {
NativePlugin pluginAnnotation = handle.getPluginAnnotation();
int[] requestCodes;
int permissionRequestCode;

CapacitorPlugin pluginAnnotation = handle.getPluginAnnotation();
if (pluginAnnotation == null) {
continue;
// Check for legacy plugin annotation, @NativePlugin
NativePlugin legacyPluginAnnotation = handle.getLegacyPluginAnnotation();
if (legacyPluginAnnotation == null) {
continue;
}

requestCodes = legacyPluginAnnotation.requestCodes();
permissionRequestCode = legacyPluginAnnotation.permissionRequestCode();
} else {
requestCodes = pluginAnnotation.requestCodes();
permissionRequestCode = pluginAnnotation.permissionRequestCode();
}

int[] requestCodes = pluginAnnotation.requestCodes();
for (int rc : requestCodes) {
if (rc == requestCode) {
return handle;
}
}

if (pluginAnnotation.permissionRequestCode() == requestCode) {
if (permissionRequestCode == requestCode) {
return handle;
}
}
Expand Down Expand Up @@ -677,7 +699,6 @@ public void startActivityForPluginWithResult(PluginCall call, Intent intent, int
* @param permissions the permissions requested
* @param grantResults the set of granted/denied permissions
*/

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
PluginHandle plugin = getPluginWithRequestCode(requestCode);

Expand All @@ -691,7 +712,12 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
return;
}

plugin.getInstance().handleRequestPermissionsResult(requestCode, permissions, grantResults);
if (plugin.getPluginAnnotation() != null) {
plugin.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
} else {
// Call deprecated method if using deprecated NativePlugin annotation
plugin.getInstance().handleRequestPermissionsResult(requestCode, permissions, grantResults);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
Expand All @@ -21,8 +20,8 @@
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import androidx.core.app.ActivityCompat;
import androidx.core.content.FileProvider;
import com.getcapacitor.util.PermissionHelper;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
Expand All @@ -45,7 +44,6 @@ public class BridgeWebChromeClient extends WebChromeClient {
static final int FILE_CHOOSER_CAMERA_PERMISSION = PluginRequestCodes.FILE_CHOOSER_CAMERA_PERMISSION;
static final int GET_USER_MEDIA_PERMISSIONS = PluginRequestCodes.GET_USER_MEDIA_PERMISSIONS;
static final int GEOLOCATION_REQUEST_PERMISSIONS = PluginRequestCodes.GEOLOCATION_REQUEST_PERMISSIONS;
static final String[] geoPermissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION };

private Bridge bridge;

Expand Down Expand Up @@ -262,7 +260,9 @@ public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermiss
super.onGeolocationPermissionsShowPrompt(origin, callback);
Logger.debug("onGeolocationPermissionsShowPrompt: DOING IT HERE FOR ORIGIN: " + origin);

if (!hasPermissions(geoPermissions)) {
final String[] geoPermissions = { Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION };

if (!PermissionHelper.hasPermissions(bridge.getContext(), geoPermissions)) {
this.bridge.cordovaInterface.requestPermissions(
new CordovaPlugin() {
@Override
Expand Down Expand Up @@ -333,7 +333,10 @@ public void onRequestPermissionResult(int requestCode, String[] permissions, int

private boolean isMediaCaptureSupported() {
String[] permissions = { Manifest.permission.CAMERA };
return hasPermissions(permissions) || !hasDefinedPermission(Manifest.permission.CAMERA);
return (
PermissionHelper.hasPermissions(bridge.getContext(), permissions) ||
!PermissionHelper.hasDefinedPermission(bridge.getContext(), Manifest.permission.CAMERA)
);
}

private void showMediaCaptureOrFilePicker(ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams, boolean isVideo) {
Expand Down Expand Up @@ -513,39 +516,4 @@ private File createImageFile(Activity activity) throws IOException {

return image;
}

private boolean hasPermissions(String[] permissions) {
for (String perm : permissions) {
if (ActivityCompat.checkSelfPermission(this.bridge.getActivity(), perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}

private boolean hasDefinedPermission(String permission) {
boolean hasPermission = false;
String[] requestedPermissions = getManifestPermissions();
if (requestedPermissions != null && requestedPermissions.length > 0) {
List<String> requestedPermissionsList = Arrays.asList(requestedPermissions);
ArrayList<String> requestedPermissionsArrayList = new ArrayList<>(requestedPermissionsList);
if (requestedPermissionsArrayList.contains(permission)) {
hasPermission = true;
}
}
return hasPermission;
}

private String[] getManifestPermissions() {
String[] requestedPermissions = null;
try {
PackageManager pm = bridge.getContext().getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(bridge.getContext().getPackageName(), PackageManager.GET_PERMISSIONS);

if (packageInfo != null) {
requestedPermissions = packageInfo.requestedPermissions;
}
} catch (Exception ex) {}
return requestedPermissions;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.getcapacitor;

import com.getcapacitor.annotation.CapacitorPlugin;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
* Base annotation for all Plugins
* @deprecated
* <p> Use {@link CapacitorPlugin} instead
*/
@Retention(RetentionPolicy.RUNTIME)
@Deprecated
public @interface NativePlugin {
/**
* Request codes this plugin uses and responds to, in order to tie
Expand Down
Loading

0 comments on commit eec61a6

Please sign in to comment.