Search in sources :

Example 66 with JSObject

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

the class GeolocationPlugin method getJSObjectForLocation.

private JSObject getJSObjectForLocation(Location location) {
    JSObject ret = new JSObject();
    JSObject coords = new JSObject();
    ret.put("coords", coords);
    ret.put("timestamp", location.getTime());
    coords.put("latitude", location.getLatitude());
    coords.put("longitude", location.getLongitude());
    coords.put("accuracy", location.getAccuracy());
    coords.put("altitude", location.getAltitude());
    if (Build.VERSION.SDK_INT >= 26) {
        coords.put("altitudeAccuracy", location.getVerticalAccuracyMeters());
    }
    coords.put("speed", location.getSpeed());
    coords.put("heading", location.getBearing());
    return ret;
}
Also used : JSObject(com.getcapacitor.JSObject)

Example 67 with JSObject

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

the class FilesystemPlugin method stat.

@PluginMethod
public void stat(PluginCall call) {
    String path = call.getString("path");
    String directory = getDirectoryParameter(call);
    File fileObject = implementation.getFileObject(path, directory);
    if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
        requestAllPermissions(call, "permissionCallback");
    } else {
        if (!fileObject.exists()) {
            call.reject("File does not exist");
            return;
        }
        JSObject data = new JSObject();
        data.put("type", fileObject.isDirectory() ? "directory" : "file");
        data.put("size", fileObject.length());
        data.put("mtime", fileObject.lastModified());
        data.put("uri", Uri.fromFile(fileObject).toString());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
                BasicFileAttributes attr = Files.readAttributes(fileObject.toPath(), BasicFileAttributes.class);
                // use whichever is the oldest between creationTime and lastAccessTime
                if (attr.creationTime().toMillis() < attr.lastAccessTime().toMillis()) {
                    data.put("ctime", attr.creationTime().toMillis());
                } else {
                    data.put("ctime", attr.lastAccessTime().toMillis());
                }
            } catch (Exception ex) {
            }
        } else {
            data.put("ctime", null);
        }
        call.resolve(data);
    }
}
Also used : JSObject(com.getcapacitor.JSObject) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) DirectoryExistsException(com.capacitorjs.plugins.filesystem.exceptions.DirectoryExistsException) CopyFailedException(com.capacitorjs.plugins.filesystem.exceptions.CopyFailedException) JSONException(org.json.JSONException) DirectoryNotFoundException(com.capacitorjs.plugins.filesystem.exceptions.DirectoryNotFoundException) PluginMethod(com.getcapacitor.PluginMethod)

Example 68 with JSObject

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

the class FilesystemPlugin method readFile.

@PluginMethod
public void readFile(PluginCall call) {
    String path = call.getString("path");
    String directory = getDirectoryParameter(call);
    String encoding = call.getString("encoding");
    Charset charset = implementation.getEncoding(encoding);
    if (encoding != null && charset == null) {
        call.reject("Unsupported encoding provided: " + encoding);
        return;
    }
    if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
        requestAllPermissions(call, "permissionCallback");
    } else {
        try {
            String dataStr = implementation.readFile(path, directory, charset);
            JSObject ret = new JSObject();
            ret.putOpt("data", dataStr);
            call.resolve(ret);
        } catch (FileNotFoundException ex) {
            call.reject("File does not exist", ex);
        } catch (IOException ex) {
            call.reject("Unable to read file", ex);
        } catch (JSONException ex) {
            call.reject("Unable to return value for reading file", ex);
        }
    }
}
Also used : Charset(java.nio.charset.Charset) JSObject(com.getcapacitor.JSObject) JSONException(org.json.JSONException) PluginMethod(com.getcapacitor.PluginMethod)

Example 69 with JSObject

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

the class FilesystemPlugin method readdir.

@PluginMethod
public void readdir(PluginCall call) {
    String path = call.getString("path");
    String directory = getDirectoryParameter(call);
    if (isPublicDirectory(directory) && !isStoragePermissionGranted()) {
        requestAllPermissions(call, "permissionCallback");
    } else {
        try {
            String[] files = implementation.readdir(path, directory);
            if (files != null) {
                JSObject ret = new JSObject();
                ret.put("files", JSArray.from(files));
                call.resolve(ret);
            } else {
                call.reject("Unable to read directory");
            }
        } catch (DirectoryNotFoundException ex) {
            call.reject(ex.getMessage());
        }
    }
}
Also used : DirectoryNotFoundException(com.capacitorjs.plugins.filesystem.exceptions.DirectoryNotFoundException) JSObject(com.getcapacitor.JSObject) PluginMethod(com.getcapacitor.PluginMethod)

Example 70 with JSObject

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

the class NotificationChannelManager method listChannels.

public void listChannels(PluginCall call) {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        List<NotificationChannel> notificationChannels = notificationManager.getNotificationChannels();
        JSArray channels = new JSArray();
        for (NotificationChannel notificationChannel : notificationChannels) {
            JSObject channel = new JSObject();
            channel.put(CHANNEL_ID, notificationChannel.getId());
            channel.put(CHANNEL_NAME, notificationChannel.getName());
            channel.put(CHANNEL_DESCRIPTION, notificationChannel.getDescription());
            channel.put(CHANNEL_IMPORTANCE, notificationChannel.getImportance());
            channel.put(CHANNEL_VISIBILITY, notificationChannel.getLockscreenVisibility());
            channel.put(CHANNEL_SOUND, notificationChannel.getSound());
            channel.put(CHANNEL_VIBRATE, notificationChannel.shouldVibrate());
            channel.put(CHANNEL_USE_LIGHTS, notificationChannel.shouldShowLights());
            channel.put(CHANNEL_LIGHT_COLOR, String.format("#%06X", (0xFFFFFF & notificationChannel.getLightColor())));
            Logger.debug(Logger.tags("NotificationChannel"), "visibility " + notificationChannel.getLockscreenVisibility());
            Logger.debug(Logger.tags("NotificationChannel"), "importance " + notificationChannel.getImportance());
            channels.put(channel);
        }
        JSObject result = new JSObject();
        result.put("channels", channels);
        call.resolve(result);
    } else {
        call.unavailable();
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) JSArray(com.getcapacitor.JSArray) JSObject(com.getcapacitor.JSObject)

Aggregations

JSObject (com.getcapacitor.JSObject)169 PluginMethod (com.getcapacitor.PluginMethod)93 JSONException (org.json.JSONException)28 JSONObject (org.json.JSONObject)20 MyRunnable (com.jeep.plugin.capacitor.capacitorvideoplayer.Notifications.MyRunnable)16 JSArray (com.getcapacitor.JSArray)14 Radar (io.radar.sdk.Radar)12 Uri (android.net.Uri)11 Location (android.location.Location)9 JSONArray (org.json.JSONArray)7 ArrayList (java.util.ArrayList)6 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 ParseException (java.text.ParseException)5 Intent (android.content.Intent)4 FirebaseUser (com.google.firebase.auth.FirebaseUser)4 Gson (com.google.gson.Gson)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4