Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 0 additions & 6 deletions android/capacitor/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,3 @@
}

-keep public class * extends com.getcapacitor.Plugin { *; }

# Rules for Capacitor v2 plugins and annotations
# These are deprecated but can still be used with Capacitor for now
-keep @com.getcapacitor.NativePlugin public class * {
@com.getcapacitor.PluginMethod public <methods>;
}
62 changes: 10 additions & 52 deletions android/capacitor/src/main/java/com/getcapacitor/Bridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,17 +618,6 @@ public void registerPluginInstances(Plugin[] pluginInstances) {
}
}

@SuppressWarnings("deprecation")
private String getLegacyPluginName(Class<? extends Plugin> pluginClass) {
NativePlugin legacyPluginAnnotation = pluginClass.getAnnotation(NativePlugin.class);
if (legacyPluginAnnotation == null) {
Logger.error("Plugin doesn't have the @CapacitorPlugin annotation. Please add it");
return null;
}

return legacyPluginAnnotation.name();
}

/**
* Register a plugin class
* @param pluginClass a class inheriting from Plugin
Expand Down Expand Up @@ -671,28 +660,25 @@ private String pluginId(Class<? extends Plugin> clazz) {
}

private String pluginName(Class<? extends Plugin> clazz) {
String pluginName;
CapacitorPlugin pluginAnnotation = clazz.getAnnotation(CapacitorPlugin.class);
if (pluginAnnotation == null) {
pluginName = this.getLegacyPluginName(clazz);
} else {
pluginName = pluginAnnotation.name();
Logger.error("Plugin doesn't have the @CapacitorPlugin annotation. Please add it");
return null;
}

return pluginName;
return pluginAnnotation.name();
}

private void logInvalidPluginException(Class<? extends Plugin> clazz) {
Logger.error(
"NativePlugin " +
"Plugin " +
clazz.getName() +
" is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" +
" the class extends Plugin"
);
}

private void logPluginLoadException(Class<? extends Plugin> clazz, Exception ex) {
Logger.error("NativePlugin " + clazz.getName() + " failed to load", ex);
Logger.error("Plugin " + clazz.getName() + " failed to load", ex);
}

public PluginHandle getPlugin(String pluginId) {
Expand All @@ -706,38 +692,16 @@ public PluginHandle getPlugin(String pluginId) {
* @return
*/
@Deprecated
@SuppressWarnings("deprecation")
public PluginHandle getPluginWithRequestCode(int requestCode) {
for (PluginHandle handle : this.plugins.values()) {
int[] requestCodes;

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

if (legacyPluginAnnotation.permissionRequestCode() == requestCode) {
continue;
}
for (int rc : pluginAnnotation.requestCodes()) {
if (rc == requestCode) {
return handle;
}

requestCodes = legacyPluginAnnotation.requestCodes();

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

for (int rc : requestCodes) {
if (rc == requestCode) {
return handle;
}
}
}
}
return null;
Expand Down Expand Up @@ -1053,7 +1017,7 @@ public void startActivityForPluginWithResult(PluginCall call, Intent intent, int
}

/**
* Check for legacy Capacitor or Cordova plugins that may have registered to handle a permission
* Check for Cordova plugins that may have registered to handle a permission
* request, and handle them if so. If not handled, false is returned.
*
* @param requestCode the code that was requested
Expand All @@ -1079,12 +1043,6 @@ boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[]
return permissionHandled;
}

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

return false;
}

Expand Down
37 changes: 0 additions & 37 deletions android/capacitor/src/main/java/com/getcapacitor/NativePlugin.java

This file was deleted.

152 changes: 54 additions & 98 deletions android/capacitor/src/main/java/com/getcapacitor/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,15 @@ public boolean hasDefinedPermissions(String[] permissions) {
public boolean hasDefinedRequiredPermissions() {
CapacitorPlugin annotation = handle.getPluginAnnotation();
if (annotation == null) {
// Check for legacy plugin annotation, @NativePlugin
NativePlugin legacyAnnotation = handle.getLegacyPluginAnnotation();
return hasDefinedPermissions(legacyAnnotation.permissions());
} else {
for (Permission perm : annotation.permissions()) {
for (String permString : perm.strings()) {
if (!PermissionHelper.hasDefinedPermission(getContext(), permString)) {
return false;
}
return true;
}
for (Permission perm : annotation.permissions()) {
for (String permString : perm.strings()) {
if (!PermissionHelper.hasDefinedPermission(getContext(), permString)) {
return false;
}
}
}

return true;
}

Expand Down Expand Up @@ -390,25 +386,15 @@ public boolean hasPermission(String permission) {
public boolean hasRequiredPermissions() {
CapacitorPlugin annotation = handle.getPluginAnnotation();
if (annotation == null) {
// Check for legacy plugin annotation, @NativePlugin
NativePlugin legacyAnnotation = handle.getLegacyPluginAnnotation();
for (String perm : legacyAnnotation.permissions()) {
if (ActivityCompat.checkSelfPermission(this.getContext(), perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}

return true;
}

for (Permission perm : annotation.permissions()) {
for (String permString : perm.strings()) {
if (ActivityCompat.checkSelfPermission(this.getContext(), permString) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}

return true;
}

Expand Down Expand Up @@ -545,17 +531,6 @@ private String[] getPermissionStringsForAliases(@NonNull String[] aliases) {
return permissionLauncher;
}

/**
* Request all of the specified permissions in the CapacitorPlugin annotation (if any)
*
* @deprecated use {@link #requestAllPermissions(PluginCall, String)} in conjunction with @CapacitorPlugin
*/
@Deprecated
public void pluginRequestAllPermissions() {
NativePlugin legacyAnnotation = handle.getLegacyPluginAnnotation();
ActivityCompat.requestPermissions(getActivity(), legacyAnnotation.permissions(), legacyAnnotation.permissionRequestCode());
}

/**
* Helper for requesting a specific permission
*
Expand Down Expand Up @@ -785,92 +760,73 @@ public void checkPermissions(PluginCall pluginCall) {
@PluginMethod
public void requestPermissions(PluginCall call) {
CapacitorPlugin annotation = handle.getPluginAnnotation();
if (annotation == null) {
handleLegacyPermission(call);
} else {
// handle permission requests for plugins defined with @CapacitorPlugin (since 3.0.0)
String[] permAliases = null;
Set<String> autoGrantPerms = new HashSet<>();

// If call was made with a list of specific permission aliases to request, save them
// to be requested
JSArray providedPerms = call.getArray("permissions");
List<String> providedPermsList = null;

if (providedPerms != null) {
try {
providedPermsList = providedPerms.toList();
} catch (JSONException ignore) {
// do nothing
}
}
String[] permAliases = null;
Set<String> autoGrantPerms = new HashSet<>();

// If call was made without any custom permissions, request all from plugin annotation
Set<String> aliasSet = new HashSet<>();
if (providedPermsList == null || providedPermsList.isEmpty()) {
for (Permission perm : annotation.permissions()) {
// If a permission is defined with no permission strings, separate it for auto-granting.
// Otherwise, the alias is added to the list to be requested.
if (perm.strings().length == 0 || (perm.strings().length == 1 && perm.strings()[0].isEmpty())) {
if (!perm.alias().isEmpty()) {
autoGrantPerms.add(perm.alias());
}
} else {
aliasSet.add(perm.alias());
}
}
// If call was made with a list of specific permission aliases to request, save them
// to be requested
JSArray providedPerms = call.getArray("permissions");
List<String> providedPermsList = null;

permAliases = aliasSet.toArray(new String[0]);
} else {
for (Permission perm : annotation.permissions()) {
if (providedPermsList.contains(perm.alias())) {
aliasSet.add(perm.alias());
}
}
if (providedPerms != null) {
try {
providedPermsList = providedPerms.toList();
} catch (JSONException ignore) {
// do nothing
}
}

if (aliasSet.isEmpty()) {
call.reject("No valid permission alias was requested of this plugin.");
// If call was made without any custom permissions, request all from plugin annotation
Set<String> aliasSet = new HashSet<>();
if (providedPermsList == null || providedPermsList.isEmpty()) {
for (Permission perm : annotation.permissions()) {
// If a permission is defined with no permission strings, separate it for auto-granting.
// Otherwise, the alias is added to the list to be requested.
if (perm.strings().length == 0 || (perm.strings().length == 1 && perm.strings()[0].isEmpty())) {
if (!perm.alias().isEmpty()) {
autoGrantPerms.add(perm.alias());
}
} else {
permAliases = aliasSet.toArray(new String[0]);
aliasSet.add(perm.alias());
}
}

if (permAliases != null && permAliases.length > 0) {
// request permissions using provided aliases or all defined on the plugin
requestPermissionForAliases(permAliases, call, "checkPermissions");
} else if (!autoGrantPerms.isEmpty()) {
// if the plugin only has auto-grant permissions, return all as GRANTED
JSObject permissionsResults = new JSObject();

for (String perm : autoGrantPerms) {
permissionsResults.put(perm, PermissionState.GRANTED.toString());
permAliases = aliasSet.toArray(new String[0]);
} else {
for (Permission perm : annotation.permissions()) {
if (providedPermsList.contains(perm.alias())) {
aliasSet.add(perm.alias());
}
}

call.resolve(permissionsResults);
if (aliasSet.isEmpty()) {
call.reject("No valid permission alias was requested of this plugin.");
} else {
// no permissions are defined on the plugin, resolve undefined
call.resolve();
permAliases = aliasSet.toArray(new String[0]);
}
}
}

@SuppressWarnings("deprecation")
private void handleLegacyPermission(PluginCall call) {
// handle permission requests for plugins defined with @NativePlugin (prior to 3.0.0)
NativePlugin legacyAnnotation = this.handle.getLegacyPluginAnnotation();
String[] perms = legacyAnnotation.permissions();
if (perms.length > 0) {
saveCall(call);
pluginRequestPermissions(perms, legacyAnnotation.permissionRequestCode());
if (permAliases != null && permAliases.length > 0) {
// request permissions using provided aliases or all defined on the plugin
requestPermissionForAliases(permAliases, call, "checkPermissions");
} else if (!autoGrantPerms.isEmpty()) {
// if the plugin only has auto-grant permissions, return all as GRANTED
JSObject permissionsResults = new JSObject();

for (String perm : autoGrantPerms) {
permissionsResults.put(perm, PermissionState.GRANTED.toString());
}

call.resolve(permissionsResults);
} else {
// no permissions are defined on the plugin, resolve undefined
call.resolve();
}
}

/**
* Handle request permissions result. A plugin using the deprecated {@link NativePlugin}
* should override this to handle the result, or this method will handle the result
* for our convenient requestPermissions call.
* Handle request permissions result. Subclasses may override this to handle the result,
* or the default implementation will handle the result for our convenient requestPermissions call.
* @deprecated in favor of using callbacks in conjunction with {@link CapacitorPlugin}
*
* @param requestCode
Expand Down
Loading
Loading