Search in sources :

Example 11 with JSArray

use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.

the class CameraPlugin method requestPermissions.

@Override
@PluginMethod
public void requestPermissions(PluginCall call) {
    // it is not defined in the manifest then we don't need to prompt and it will just work.
    if (isPermissionDeclared(CAMERA)) {
        // just request normally
        super.requestPermissions(call);
    } else {
        // the manifest does not define camera permissions, so we need to decide what to do
        // first, extract the permissions being requested
        JSArray providedPerms = call.getArray("permissions");
        List<String> permsList = null;
        try {
            permsList = providedPerms.toList();
        } catch (JSONException e) {
        }
        if (permsList != null && permsList.size() == 1 && permsList.contains(CAMERA)) {
            // the only thing being asked for was the camera so we can just return the current state
            checkPermissions(call);
        } else {
            // we need to ask about photos so request storage permissions
            requestPermissionForAlias(PHOTOS, call, "checkPermissions");
        }
    }
}
Also used : JSArray(com.getcapacitor.JSArray) JSONException(org.json.JSONException) PluginMethod(com.getcapacitor.PluginMethod)

Example 12 with JSArray

use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.

the class CameraPlugin method processPickedImages.

@ActivityCallback
public void processPickedImages(PluginCall call, ActivityResult result) {
    Intent data = result.getData();
    if (data != null) {
        Executor executor = Executors.newSingleThreadExecutor();
        executor.execute(() -> {
            JSObject ret = new JSObject();
            JSArray photos = new JSArray();
            if (data.getClipData() != null) {
                int count = data.getClipData().getItemCount();
                for (int i = 0; i < count; i++) {
                    Uri imageUri = data.getClipData().getItemAt(i).getUri();
                    JSObject processResult = processPickedImages(imageUri);
                    if (processResult.getString("error") != null && !processResult.getString("error").isEmpty()) {
                        call.reject(processResult.getString("error"));
                        return;
                    } else {
                        photos.put(processResult);
                    }
                }
            } else if (data.getData() != null) {
                Uri imageUri = data.getData();
                JSObject processResult = processPickedImages(imageUri);
                if (processResult.getString("error") != null && !processResult.getString("error").isEmpty()) {
                    call.reject(processResult.getString("error"));
                    return;
                } else {
                    photos.put(processResult);
                }
            } else if (data.getExtras() != null) {
                Bundle bundle = data.getExtras();
                if (bundle.keySet().contains("selectedItems")) {
                    ArrayList<Parcelable> fileUris = bundle.getParcelableArrayList("selectedItems");
                    if (fileUris != null) {
                        for (Parcelable fileUri : fileUris) {
                            if (fileUri instanceof Uri) {
                                Uri imageUri = (Uri) fileUri;
                                try {
                                    JSObject processResult = processPickedImages(imageUri);
                                    if (processResult.getString("error") != null && !processResult.getString("error").isEmpty()) {
                                        call.reject(processResult.getString("error"));
                                        return;
                                    } else {
                                        photos.put(processResult);
                                    }
                                } catch (SecurityException ex) {
                                    call.reject("SecurityException");
                                }
                            }
                        }
                    }
                }
            }
            ret.put("photos", photos);
            call.resolve(ret);
        });
    } else {
        call.reject("No images picked");
    }
}
Also used : Executor(java.util.concurrent.Executor) Bundle(android.os.Bundle) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject) Intent(android.content.Intent) Parcelable(android.os.Parcelable) Uri(android.net.Uri) ActivityCallback(com.getcapacitor.annotation.ActivityCallback)

Example 13 with JSArray

use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.

the class ActionSheetPlugin method showActions.

@PluginMethod
public void showActions(final PluginCall call) {
    String title = call.getString("title");
    JSArray options = call.getArray("options");
    if (title == null) {
        call.reject("Must supply a title");
        return;
    }
    if (options == null) {
        call.reject("Must supply options");
        return;
    }
    if (getActivity().isFinishing()) {
        call.reject("App is finishing");
        return;
    }
    try {
        List<Object> optionsList = options.toList();
        ActionSheetOption[] actionOptions = new ActionSheetOption[optionsList.size()];
        for (int i = 0; i < optionsList.size(); i++) {
            JSObject o = JSObject.fromJSONObject((JSONObject) optionsList.get(i));
            String titleOption = o.getString("title", "");
            actionOptions[i] = new ActionSheetOption(titleOption);
        }
        implementation.setTitle(title);
        implementation.setOptions(actionOptions);
        implementation.setCancelable(false);
        implementation.setOnSelectedListener(index -> {
            JSObject ret = new JSObject();
            ret.put("index", index);
            call.resolve(ret);
            implementation.dismiss();
        });
        implementation.show(getActivity().getSupportFragmentManager(), "capacitorModalsActionSheet");
    } catch (JSONException ex) {
        Logger.error("JSON error processing an option for showActions", ex);
        call.reject("JSON error processing an option for showActions", ex);
    }
}
Also used : JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) JSObject(com.getcapacitor.JSObject) PluginMethod(com.getcapacitor.PluginMethod)

Example 14 with JSArray

use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.

the class LocalNotificationsPlugin method registerActionTypes.

@PluginMethod
public void registerActionTypes(PluginCall call) {
    JSArray types = call.getArray("types");
    Map<String, NotificationAction[]> typesArray = NotificationAction.buildTypes(types);
    notificationStorage.writeActionGroup(typesArray);
    call.resolve();
}
Also used : JSArray(com.getcapacitor.JSArray) PluginMethod(com.getcapacitor.PluginMethod)

Example 15 with JSArray

use of com.getcapacitor.JSArray in project capacitor-plugins by ionic-team.

the class PushNotificationsPlugin method getDeliveredNotifications.

@PluginMethod
public void getDeliveredNotifications(PluginCall call) {
    JSArray notifications = new JSArray();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        StatusBarNotification[] activeNotifications = notificationManager.getActiveNotifications();
        for (StatusBarNotification notif : activeNotifications) {
            JSObject jsNotif = new JSObject();
            jsNotif.put("id", notif.getId());
            Notification notification = notif.getNotification();
            if (notification != null) {
                jsNotif.put("title", notification.extras.getCharSequence(Notification.EXTRA_TITLE));
                jsNotif.put("body", notification.extras.getCharSequence(Notification.EXTRA_TEXT));
                jsNotif.put("group", notification.getGroup());
                jsNotif.put("groupSummary", 0 != (notification.flags & Notification.FLAG_GROUP_SUMMARY));
                JSObject extras = new JSObject();
                for (String key : notification.extras.keySet()) {
                    extras.put(key, notification.extras.get(key));
                }
                jsNotif.put("data", extras);
            }
            notifications.put(jsNotif);
        }
    }
    JSObject result = new JSObject();
    result.put("notifications", notifications);
    call.resolve(result);
}
Also used : StatusBarNotification(android.service.notification.StatusBarNotification) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification) PluginMethod(com.getcapacitor.PluginMethod)

Aggregations

JSArray (com.getcapacitor.JSArray)20 JSObject (com.getcapacitor.JSObject)14 PluginMethod (com.getcapacitor.PluginMethod)12 JSONException (org.json.JSONException)10 JSONObject (org.json.JSONObject)7 Intent (android.content.Intent)3 ArrayList (java.util.ArrayList)3 NotificationChannel (android.app.NotificationChannel)2 LatLng (com.google.android.libraries.maps.model.LatLng)2 Notification (android.app.Notification)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 Parcelable (android.os.Parcelable)1 StatusBarNotification (android.service.notification.StatusBarNotification)1 ActivityCallback (com.getcapacitor.annotation.ActivityCallback)1 PolygonOptions (com.google.android.libraries.maps.model.PolygonOptions)1 PolylineOptions (com.google.android.libraries.maps.model.PolylineOptions)1 ParseException (java.text.ParseException)1 Executor (java.util.concurrent.Executor)1 JSONArray (org.json.JSONArray)1