Search in sources :

Example 1 with PermissionCallback

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

the class Plugin method initializeActivityLaunchers.

/**
 * Registers activity result launchers defined on plugins, used for permission requests and
 * activities started for result.
 */
void initializeActivityLaunchers() {
    List<Method> pluginClassMethods = new ArrayList<>();
    for (Class<?> pluginCursor = getClass(); !pluginCursor.getName().equals(Object.class.getName()); pluginCursor = pluginCursor.getSuperclass()) {
        pluginClassMethods.addAll(Arrays.asList(pluginCursor.getDeclaredMethods()));
    }
    for (final Method method : pluginClassMethods) {
        if (method.isAnnotationPresent(ActivityCallback.class)) {
            // register callbacks annotated with ActivityCallback for activity results
            ActivityResultLauncher<Intent> launcher = bridge.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> triggerActivityCallback(method, result));
            activityLaunchers.put(method.getName(), launcher);
        } else if (method.isAnnotationPresent(PermissionCallback.class)) {
            // register callbacks annotated with PermissionCallback for permission results
            ActivityResultLauncher<String[]> launcher = bridge.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), permissions -> triggerPermissionCallback(method, permissions));
            permissionLaunchers.put(method.getName(), launcher);
        }
    }
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) NonNull(androidx.annotation.NonNull) Uri(android.net.Uri) Intent(android.content.Intent) HashMap(java.util.HashMap) AppCompatActivity(androidx.appcompat.app.AppCompatActivity) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ActivityCallback(com.getcapacitor.annotation.ActivityCallback) JSONException(org.json.JSONException) Locale(java.util.Locale) Map(java.util.Map) Method(java.lang.reflect.Method) PermissionHelper(com.getcapacitor.util.PermissionHelper) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher) ActivityCompat(androidx.core.app.ActivityCompat) CapacitorPlugin(com.getcapacitor.annotation.CapacitorPlugin) Set(java.util.Set) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) Nullable(androidx.annotation.Nullable) PermissionCallback(com.getcapacitor.annotation.PermissionCallback) Configuration(android.content.res.Configuration) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) Activity(android.app.Activity) Permission(com.getcapacitor.annotation.Permission) ActivityResult(androidx.activity.result.ActivityResult) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Intent(android.content.Intent) ActivityResultContracts(androidx.activity.result.contract.ActivityResultContracts) Method(java.lang.reflect.Method) PermissionCallback(com.getcapacitor.annotation.PermissionCallback) ActivityResultLauncher(androidx.activity.result.ActivityResultLauncher)

Aggregations

Activity (android.app.Activity)1 Context (android.content.Context)1 Intent (android.content.Intent)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 ActivityResult (androidx.activity.result.ActivityResult)1 ActivityResultLauncher (androidx.activity.result.ActivityResultLauncher)1 ActivityResultContracts (androidx.activity.result.contract.ActivityResultContracts)1 NonNull (androidx.annotation.NonNull)1 Nullable (androidx.annotation.Nullable)1 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)1 ActivityCompat (androidx.core.app.ActivityCompat)1 ActivityCallback (com.getcapacitor.annotation.ActivityCallback)1 CapacitorPlugin (com.getcapacitor.annotation.CapacitorPlugin)1 Permission (com.getcapacitor.annotation.Permission)1 PermissionCallback (com.getcapacitor.annotation.PermissionCallback)1 PermissionHelper (com.getcapacitor.util.PermissionHelper)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1