Search in sources :

Example 91 with JSObject

use of com.getcapacitor.JSObject in project capacitor-radar by radarlabs.

the class RadarPlugin method getContext.

@PluginMethod()
public void getContext(final PluginCall call) {
    Radar.RadarContextCallback callback = new Radar.RadarContextCallback() {

        @Override
        public void onComplete(@NotNull Radar.RadarStatus status, @Nullable Location location, @Nullable RadarContext context) {
            if (status == Radar.RadarStatus.SUCCESS && location != null && context != null) {
                JSObject ret = new JSObject();
                ret.put("status", status.toString());
                ret.put("location", RadarPlugin.jsObjectForJSONObject(Radar.jsonForLocation(location)));
                ret.put("context", RadarPlugin.jsObjectForJSONObject(context.toJson()));
                call.resolve(ret);
            } else {
                call.reject(status.toString());
            }
        }
    };
    if (call.hasOption("latitude") && call.hasOption("longitude")) {
        double latitude = call.getDouble("latitude");
        double longitude = call.getDouble("longitude");
        Location location = new Location("RadarSDK");
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setAccuracy(5);
        Radar.getContext(location, callback);
    } else {
        Radar.getContext(callback);
    }
}
Also used : Radar(io.radar.sdk.Radar) JSObject(com.getcapacitor.JSObject) RadarContext(io.radar.sdk.model.RadarContext) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) Location(android.location.Location) PluginMethod(com.getcapacitor.PluginMethod)

Example 92 with JSObject

use of com.getcapacitor.JSObject in project capacitor-radar by radarlabs.

the class RadarPlugin method setForegroundServiceOptions.

@PluginMethod()
public void setForegroundServiceOptions(PluginCall call) {
    JSObject optionsObj = call.getObject("options");
    JSONObject optionsJson = RadarPlugin.jsonObjectForJSObject(optionsObj);
    RadarTrackingOptionsForegroundService options = RadarTrackingOptionsForegroundService.fromJson(optionsJson);
    Radar.setForegroundServiceOptions(options);
    call.resolve();
}
Also used : JSONObject(org.json.JSONObject) RadarTrackingOptionsForegroundService(io.radar.sdk.RadarTrackingOptions.RadarTrackingOptionsForegroundService) JSObject(com.getcapacitor.JSObject) PluginMethod(com.getcapacitor.PluginMethod)

Example 93 with JSObject

use of com.getcapacitor.JSObject in project capacitor-calendar by Fir3st.

the class CapacitorCalendar method deleteCalendarEventById.

protected void deleteCalendarEventById(PluginCall call) {
    JSObject data = call.getData();
    JSObject ret = new JSObject();
    String id = data.getString("id", null);
    if (id == null)
        throw new IllegalArgumentException("Event id not specified.");
    long evDtStart = -1;
    {
        Cursor cur = queryEvents(new String[] { Events.DTSTART }, Events._ID + " = ?", new String[] { id }, Events.DTSTART);
        if (cur.moveToNext()) {
            evDtStart = cur.getLong(0);
        }
        cur.close();
    }
    if (evDtStart == -1)
        throw new RuntimeException("Could not find event.");
    int deleted = this.getActivity().getContentResolver().delete(ContentUris.withAppendedId(Events.CONTENT_URI, Long.valueOf(id)), null, null);
    ret.put("result", deleted > 0);
    call.resolve(ret);
}
Also used : JSObject(com.getcapacitor.JSObject) Cursor(android.database.Cursor)

Example 94 with JSObject

use of com.getcapacitor.JSObject in project capacitor-calendar by Fir3st.

the class CapacitorCalendar method createCalendarEvent.

protected void createCalendarEvent(PluginCall call) {
    ContentResolver cr = this.getActivity().getContentResolver();
    ContentValues values = new ContentValues();
    JSObject data = call.getData();
    try {
        Long startTime = data.has("startDate") ? data.getLong("startDate") : new Date().getTime();
        Long endTime = data.has("endDate") ? data.getLong("endDate") : new Date().getTime();
        final boolean hasAllDayEventConfig = data.has("allDay");
        if (hasAllDayEventConfig != false) {
            final boolean allDayEventConfig = data.getBoolean("allDay");
            values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
            values.put(Events.ALL_DAY, allDayEventConfig);
            values.put(Events.DTSTART, startTime);
            values.put(Events.DTEND, endTime);
        } else {
            final boolean allDayEvent = isAllDayEvent(new Date(startTime), new Date(endTime));
            if (allDayEvent) {
                values.put(Events.EVENT_TIMEZONE, "UTC");
                values.put(Events.ALL_DAY, true);
                values.put(Events.DTSTART, startTime + TimeZone.getDefault().getOffset(startTime));
                values.put(Events.DTEND, endTime + TimeZone.getDefault().getOffset(endTime));
            } else {
                values.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getID());
                values.put(Events.ALL_DAY, false);
                values.put(Events.DTSTART, startTime);
                values.put(Events.DTEND, endTime);
            }
        }
        String selectedCalendarId = data.has("calendarId") ? data.getString("calendarId").replaceAll("\"", "") : "";
        List<String> activeCalendars = Arrays.asList(getActiveCalendarIds());
        int calendarId = data.has("calendarId") && activeCalendars.contains(selectedCalendarId) ? Integer.parseInt(selectedCalendarId) : getDefaultCalendarId();
        values.put(Events.TITLE, call.getString("title", ""));
        values.put(Events.DESCRIPTION, call.getString("notes", ""));
        values.put(Events.EVENT_LOCATION, call.getString("location", ""));
        values.put(Events.AVAILABILITY, Events.AVAILABILITY_BUSY);
        values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Fail to parse data", e);
        call.reject(e.getMessage());
    }
    try {
        Uri uri = cr.insert(Events.CONTENT_URI, values);
        String createdEventID = uri.getLastPathSegment();
        Log.d(LOG_TAG, "Created event with ID " + createdEventID);
        JSObject ret = new JSObject();
        ret.put("id", createdEventID);
        call.resolve(ret);
    } catch (SecurityException e) {
        Log.e(LOG_TAG, "Permission denied", e);
        call.error(e.getMessage());
    } catch (Exception e) {
        Log.e(LOG_TAG, "Fail to create an event", e);
        call.error(e.getMessage());
    }
}
Also used : ContentValues(android.content.ContentValues) JSObject(com.getcapacitor.JSObject) Uri(android.net.Uri) Date(java.util.Date) JSONException(org.json.JSONException) ContentResolver(android.content.ContentResolver)

Example 95 with JSObject

use of com.getcapacitor.JSObject in project capacitor-calendar by Fir3st.

the class CapacitorCalendar method getAvailableCalendars.

@PluginMethod()
public void getAvailableCalendars(PluginCall call) {
    if (!hasRequiredPermissions()) {
        requestPermissionsCalendar(call);
    } else {
        List<Calendar> availableCalendars = getAvailableCalendarsList();
        JSObject ret = new JSObject();
        ret.put("availableCalendars", new Gson().toJson(availableCalendars));
        call.success(ret);
    }
}
Also used : JSObject(com.getcapacitor.JSObject) Gson(com.google.gson.Gson) PluginMethod(com.getcapacitor.PluginMethod)

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