Search in sources :

Example 6 with ActivityCallback

use of com.getcapacitor.annotation.ActivityCallback 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

ActivityCallback (com.getcapacitor.annotation.ActivityCallback)6 Uri (android.net.Uri)4 Intent (android.content.Intent)3 JSObject (com.getcapacitor.JSObject)3 Bundle (android.os.Bundle)2 Activity (android.app.Activity)1 Context (android.content.Context)1 PackageManager (android.content.pm.PackageManager)1 Configuration (android.content.res.Configuration)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Parcelable (android.os.Parcelable)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 JSArray (com.getcapacitor.JSArray)1