Search in sources :

Example 1 with CapacitorPlugin

use of com.getcapacitor.annotation.CapacitorPlugin in project capacitor by ionic-team.

the class Bridge method registerPlugin.

/**
 * Register a plugin class
 * @param pluginClass a class inheriting from Plugin
 */
public void registerPlugin(Class<? extends Plugin> pluginClass) {
    String pluginName;
    CapacitorPlugin pluginAnnotation = pluginClass.getAnnotation(CapacitorPlugin.class);
    if (pluginAnnotation == null) {
        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 (!pluginName.equals("")) {
        pluginId = pluginName;
    }
    Logger.debug("Registering plugin: " + pluginId);
    try {
        this.plugins.put(pluginId, new PluginHandle(this, pluginClass));
    } catch (InvalidPluginException ex) {
        Logger.error("NativePlugin " + pluginClass.getName() + " is invalid. Ensure the @CapacitorPlugin annotation exists on the plugin class and" + " the class extends Plugin");
    } catch (PluginLoadException ex) {
        Logger.error("NativePlugin " + pluginClass.getName() + " failed to load", ex);
    }
}
Also used : CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin)

Example 2 with CapacitorPlugin

use of com.getcapacitor.annotation.CapacitorPlugin in project capacitor by ionic-team.

the class Bridge method getPluginWithRequestCode.

/**
 * Find the plugin handle that responds to the given request code. This will
 * fire after certain Android OS intent results/permission checks/etc.
 * @param requestCode
 * @return
 */
@Deprecated
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) {
                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;
}
Also used : CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin)

Example 3 with CapacitorPlugin

use of com.getcapacitor.annotation.CapacitorPlugin in project capacitor by ionic-team.

the class Bridge method getPermissionStates.

/**
 * Helper to check all permissions and see the current states of each permission.
 *
 * @since 3.0.0
 * @return A mapping of permission aliases to the associated granted status.
 */
protected Map<String, PermissionState> getPermissionStates(Plugin plugin) {
    Map<String, PermissionState> permissionsResults = new HashMap<>();
    CapacitorPlugin annotation = plugin.getPluginHandle().getPluginAnnotation();
    for (Permission perm : annotation.permissions()) {
        // Otherwise, get its true state.
        if (perm.strings().length == 0 || (perm.strings().length == 1 && perm.strings()[0].isEmpty())) {
            String key = perm.alias();
            if (!key.isEmpty()) {
                PermissionState existingResult = permissionsResults.get(key);
                // auto set permission state to GRANTED if the alias is empty.
                if (existingResult == null) {
                    permissionsResults.put(key, PermissionState.GRANTED);
                }
            }
        } else {
            for (String permString : perm.strings()) {
                String key = perm.alias().isEmpty() ? permString : perm.alias();
                PermissionState permissionStatus;
                if (ActivityCompat.checkSelfPermission(this.getContext(), permString) == PackageManager.PERMISSION_GRANTED) {
                    permissionStatus = PermissionState.GRANTED;
                } else {
                    permissionStatus = PermissionState.PROMPT;
                    // Check if there is a cached permission state for the "Never ask again" state
                    SharedPreferences prefs = getContext().getSharedPreferences(PERMISSION_PREFS_NAME, Activity.MODE_PRIVATE);
                    String state = prefs.getString(permString, null);
                    if (state != null) {
                        permissionStatus = PermissionState.byState(state);
                    }
                }
                PermissionState existingResult = permissionsResults.get(key);
                // multiple permissions with the same alias must all be true, otherwise all false.
                if (existingResult == null || existingResult == PermissionState.GRANTED) {
                    permissionsResults.put(key, permissionStatus);
                }
            }
        }
    }
    return permissionsResults;
}
Also used : HashMap(java.util.HashMap) CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin) SharedPreferences(android.content.SharedPreferences) Permission(com.getcapacitor.annotation.Permission)

Example 4 with CapacitorPlugin

use of com.getcapacitor.annotation.CapacitorPlugin in project capacitor by ionic-team.

the class Plugin method requestPermissions.

/**
 * Exported plugin call to request all permissions for this plugin.
 * To manually request permissions within a plugin use:
 * {@link #requestAllPermissions(PluginCall, String)}, or
 * {@link #requestPermissionForAlias(String, PluginCall, String)}, or
 * {@link #requestPermissionForAliases(String[], PluginCall, String)}
 *
 * @param call the plugin call
 */
@PluginMethod
public void requestPermissions(PluginCall call) {
    CapacitorPlugin annotation = handle.getPluginAnnotation();
    if (annotation == null) {
        // 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());
        } else {
            call.resolve();
        }
    } 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;
        try {
            providedPermsList = providedPerms.toList();
        } catch (JSONException ignore) {
        // do nothing
        }
        // 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()) {
                // 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());
                }
            }
            permAliases = aliasSet.toArray(new String[0]);
        } else {
            for (Permission perm : annotation.permissions()) {
                if (providedPermsList.contains(perm.alias())) {
                    aliasSet.add(perm.alias());
                }
            }
            if (aliasSet.isEmpty()) {
                call.reject("No valid permission alias was requested of this plugin.");
            } else {
                permAliases = aliasSet.toArray(new String[0]);
            }
        }
        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();
        }
    }
}
Also used : CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin) Permission(com.getcapacitor.annotation.Permission) JSONException(org.json.JSONException) HashSet(java.util.HashSet)

Example 5 with CapacitorPlugin

use of com.getcapacitor.annotation.CapacitorPlugin in project capacitor by ionic-team.

the class Plugin method getPermissionStringsForAliases.

/**
 * Gets the Android permission strings defined on the {@link CapacitorPlugin} annotation with
 * the provided aliases.
 *
 * @param aliases aliases for permissions defined on the plugin
 * @return Android permission strings associated with the provided aliases, if exists
 */
private String[] getPermissionStringsForAliases(@NonNull String[] aliases) {
    CapacitorPlugin annotation = handle.getPluginAnnotation();
    HashSet<String> perms = new HashSet<>();
    for (Permission perm : annotation.permissions()) {
        if (Arrays.asList(aliases).contains(perm.alias())) {
            perms.addAll(Arrays.asList(perm.strings()));
        }
    }
    return perms.toArray(new String[0]);
}
Also used : CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin) Permission(com.getcapacitor.annotation.Permission) HashSet(java.util.HashSet)

Aggregations

CapacitorPlugin (com.getcapacitor.annotation.CapacitorPlugin)7 Permission (com.getcapacitor.annotation.Permission)5 HashSet (java.util.HashSet)3 SharedPreferences (android.content.SharedPreferences)1 HashMap (java.util.HashMap)1 JSONException (org.json.JSONException)1