Search in sources :

Example 11 with PluginMethod

use of com.getcapacitor.PluginMethod in project capacitor-google-fit by perfood.

the class GoogleFitPlugin method getHistoryActivity.

@PluginMethod
public Task<DataReadResponse> getHistoryActivity(final PluginCall call) throws ParseException {
    final GoogleSignInAccount account = getAccount();
    if (account == null) {
        call.reject("No access");
        return null;
    }
    long startTime = dateToTimestamp(call.getString("startTime"));
    long endTime = dateToTimestamp(call.getString("endTime"));
    if (startTime == -1 || endTime == -1) {
        call.reject("Must provide a start time and end time");
        return null;
    }
    DataReadRequest readRequest = new DataReadRequest.Builder().aggregate(DataType.TYPE_STEP_COUNT_DELTA).aggregate(DataType.AGGREGATE_STEP_COUNT_DELTA).aggregate(DataType.TYPE_DISTANCE_DELTA).aggregate(DataType.AGGREGATE_DISTANCE_DELTA).aggregate(DataType.TYPE_SPEED).aggregate(DataType.TYPE_CALORIES_EXPENDED).aggregate(DataType.AGGREGATE_CALORIES_EXPENDED).aggregate(DataType.TYPE_WEIGHT).setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS).bucketByActivitySegment(1, TimeUnit.MINUTES).enableServerQueries().build();
    return Fitness.getHistoryClient(getActivity(), account).readData(readRequest).addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {

        @Override
        public void onSuccess(DataReadResponse dataReadResponse) {
            List<Bucket> buckets = dataReadResponse.getBuckets();
            JSONArray activities = new JSONArray();
            for (Bucket bucket : buckets) {
                JSONObject summary = new JSONObject();
                try {
                    summary.put("start", timestampToDate(bucket.getStartTime(TimeUnit.MILLISECONDS)));
                    summary.put("end", timestampToDate(bucket.getEndTime(TimeUnit.MILLISECONDS)));
                    List<DataSet> dataSets = bucket.getDataSets();
                    for (DataSet dataSet : dataSets) {
                        if (dataSet.getDataPoints().size() > 0) {
                            switch(dataSet.getDataType().getName()) {
                                case "com.google.distance.delta":
                                    summary.put("distance", dataSet.getDataPoints().get(0).getValue(Field.FIELD_DISTANCE));
                                    break;
                                case "com.google.speed.summary":
                                    summary.put("speed", dataSet.getDataPoints().get(0).getValue(Field.FIELD_AVERAGE));
                                    break;
                                case "com.google.calories.expended":
                                    summary.put("calories", dataSet.getDataPoints().get(0).getValue(Field.FIELD_CALORIES));
                                    break;
                                case "com.google.weight.summary":
                                    summary.put("weight", dataSet.getDataPoints().get(0).getValue(Field.FIELD_AVERAGE));
                                    break;
                                case "com.google.step_count.delta":
                                    summary.put("steps", dataSet.getDataPoints().get(0).getValue(Field.FIELD_STEPS));
                                    break;
                                default:
                                    Log.i(TAG, "need to handle " + dataSet.getDataType().getName());
                            }
                        }
                    }
                    summary.put("activity", bucket.getActivity());
                } catch (JSONException e) {
                    call.reject(e.getMessage());
                    return;
                }
                activities.put(summary);
            }
            JSObject result = new JSObject();
            result.put("activities", activities);
            call.resolve(result);
        }
    });
}
Also used : DataSet(com.google.android.gms.fitness.data.DataSet) DataReadResponse(com.google.android.gms.fitness.result.DataReadResponse) JSONArray(org.json.JSONArray) DataReadRequest(com.google.android.gms.fitness.request.DataReadRequest) JSONException(org.json.JSONException) JSObject(com.getcapacitor.JSObject) GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) JSONObject(org.json.JSONObject) Bucket(com.google.android.gms.fitness.data.Bucket) List(java.util.List) PluginMethod(com.getcapacitor.PluginMethod)

Example 12 with PluginMethod

use of com.getcapacitor.PluginMethod in project google-maps by capacitor-community.

the class CapacitorGoogleMaps method addMarker.

@PluginMethod()
public void addMarker(final PluginCall call) {
    final Double latitude = call.getDouble("latitude", 0d);
    final Double longitude = call.getDouble("longitude", 0d);
    final Float opacity = call.getFloat("opacity", 1.0f);
    final String title = call.getString("title", "");
    final String snippet = call.getString("snippet", "");
    final Boolean isFlat = call.getBoolean("isFlat", true);
    final JSObject metadata = call.getObject("metadata");
    final String url = call.getString("iconUrl", "");
    final Boolean draggable = call.getBoolean("draggable", false);
    if (googleMap == null) {
        call.reject("Map is not ready");
        return;
    }
    Bitmap imageBitmap = getBitmapFromURL(url);
    getBridge().getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LatLng latLng = new LatLng(latitude, longitude);
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(latLng);
            markerOptions.alpha(opacity);
            markerOptions.title(title);
            markerOptions.snippet(snippet);
            markerOptions.flat(isFlat);
            markerOptions.draggable(draggable);
            if (imageBitmap != null) {
                markerOptions.icon(BitmapDescriptorFactory.fromBitmap(imageBitmap));
            }
            Marker marker = googleMap.addMarker(markerOptions);
            // set metadata to marker
            marker.setTag(metadata);
            // get auto-generated id of the just added marker,
            // put this marker into a hashmap with the corresponding id,
            // so we can retrieve the marker by id later on
            mHashMap.put(marker.getId(), marker);
            // initialize JSObject to return when resolving this call
            JSObject result = new JSObject();
            JSObject markerResult = new JSObject();
            // get marker specific values
            markerResult.put("id", marker.getId());
            result.put("marker", markerResult);
            call.resolve(result);
        }
    });
}
Also used : Bitmap(android.graphics.Bitmap) MarkerOptions(com.google.android.libraries.maps.model.MarkerOptions) JSObject(com.getcapacitor.JSObject) LatLng(com.google.android.libraries.maps.model.LatLng) Marker(com.google.android.libraries.maps.model.Marker) PluginMethod(com.getcapacitor.PluginMethod)

Example 13 with PluginMethod

use of com.getcapacitor.PluginMethod in project google-maps by capacitor-community.

the class CapacitorGoogleMaps method addPolyline.

@PluginMethod()
public void addPolyline(final PluginCall call) {
    final JSArray points = call.getArray("points", new JSArray());
    getBridge().executeOnMainThread(new Runnable() {

        @Override
        public void run() {
            PolylineOptions polylineOptions = new PolylineOptions();
            for (int i = 0; i < points.length(); i++) {
                try {
                    JSONObject point = points.getJSONObject(i);
                    LatLng latLng = new LatLng(point.getDouble("latitude"), point.getDouble("longitude"));
                    polylineOptions.add(latLng);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            googleMap.addPolyline(polylineOptions);
            call.resolve();
        }
    });
}
Also used : JSONObject(org.json.JSONObject) JSArray(com.getcapacitor.JSArray) JSONException(org.json.JSONException) LatLng(com.google.android.libraries.maps.model.LatLng) PolylineOptions(com.google.android.libraries.maps.model.PolylineOptions) PluginMethod(com.getcapacitor.PluginMethod)

Example 14 with PluginMethod

use of com.getcapacitor.PluginMethod in project google-maps by capacitor-community.

the class CapacitorGoogleMaps method reverseGeocodeCoordinate.

@PluginMethod()
public void reverseGeocodeCoordinate(final PluginCall call) {
    final Double latitude = call.getDouble("latitude", 0.0);
    final Double longitude = call.getDouble("longitude", 0.0);
    getBridge().executeOnMainThread(new Runnable() {

        @Override
        public void run() {
            /*
                 * TODO: Check if can be done without adding Places SDK
                 *
                 */
            Geocoder geocoder = new Geocoder(getContext());
            try {
                List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 5);
                JSObject results = new JSObject();
                int index = 0;
                for (Address address : addressList) {
                    JSObject addressObject = new JSObject();
                    addressObject.put("administrativeArea", address.getAdminArea());
                    addressObject.put("lines", address.getAddressLine(0));
                    addressObject.put("country", address.getCountryName());
                    addressObject.put("locality", address.getLocality());
                    addressObject.put("subLocality", address.getSubLocality());
                    addressObject.put("thoroughFare", address.getThoroughfare());
                    results.put(String.valueOf(index++), addressObject);
                }
                call.resolve(results);
            } catch (IOException e) {
                call.error("Error in Geocode!");
            }
        }
    });
}
Also used : Address(android.location.Address) JSObject(com.getcapacitor.JSObject) List(java.util.List) IOException(java.io.IOException) Geocoder(android.location.Geocoder) PluginMethod(com.getcapacitor.PluginMethod)

Example 15 with PluginMethod

use of com.getcapacitor.PluginMethod in project google-maps by capacitor-community.

the class CapacitorGoogleMaps method create.

@PluginMethod()
public void create(PluginCall call) {
    final Integer width = call.getInt("width", DEFAULT_WIDTH);
    final Integer height = call.getInt("height", DEFAULT_HEIGHT);
    final Integer x = call.getInt("x", 0);
    final Integer y = call.getInt("y", 0);
    final Float zoom = call.getFloat("zoom", DEFAULT_ZOOM);
    final Double latitude = call.getDouble("latitude");
    final Double longitude = call.getDouble("longitude");
    final boolean liteMode = call.getBoolean("enabled", false);
    final CapacitorGoogleMaps ctx = this;
    getBridge().getActivity().runOnUiThread(new Runnable() {

        @Override
        public void run() {
            LatLng latLng = new LatLng(latitude, longitude);
            CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(zoom).build();
            GoogleMapOptions googleMapOptions = new GoogleMapOptions();
            googleMapOptions.camera(cameraPosition);
            googleMapOptions.liteMode(liteMode);
            if (mapViewParentId != null) {
                View viewToRemove = ((ViewGroup) getBridge().getWebView().getParent()).findViewById(mapViewParentId);
                if (viewToRemove != null) {
                    ((ViewGroup) getBridge().getWebView().getParent()).removeViewInLayout(viewToRemove);
                }
            }
            FrameLayout mapViewParent = new FrameLayout(getBridge().getContext());
            mapViewParentId = View.generateViewId();
            mapViewParent.setId(mapViewParentId);
            mapView = new MapView(getBridge().getContext(), googleMapOptions);
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(getScaledPixels(width), getScaledPixels(height));
            lp.topMargin = getScaledPixels(y);
            lp.leftMargin = getScaledPixels(x);
            mapView.setLayoutParams(lp);
            mapViewParent.addView(mapView);
            ((ViewGroup) getBridge().getWebView().getParent()).addView(mapViewParent);
            mapView.onCreate(null);
            mapView.onStart();
            mapView.getMapAsync(ctx);
        }
    });
    call.resolve();
}
Also used : View(android.view.View) MapView(com.google.android.libraries.maps.MapView) CameraPosition(com.google.android.libraries.maps.model.CameraPosition) GoogleMapOptions(com.google.android.libraries.maps.GoogleMapOptions) FrameLayout(android.widget.FrameLayout) MapView(com.google.android.libraries.maps.MapView) LatLng(com.google.android.libraries.maps.model.LatLng) PluginMethod(com.getcapacitor.PluginMethod)

Aggregations

PluginMethod (com.getcapacitor.PluginMethod)120 JSObject (com.getcapacitor.JSObject)93 JSONException (org.json.JSONException)18 MyRunnable (com.jeep.plugin.capacitor.capacitorvideoplayer.Notifications.MyRunnable)13 JSONObject (org.json.JSONObject)13 JSArray (com.getcapacitor.JSArray)12 Radar (io.radar.sdk.Radar)11 Location (android.location.Location)8 Intent (android.content.Intent)7 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)6 Uri (android.net.Uri)5 LatLng (com.google.android.libraries.maps.model.LatLng)5 GenericAd (admob.plus.core.GenericAd)4 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)4 Gson (com.google.gson.Gson)4 PackageManager (android.content.pm.PackageManager)3 AppCompatActivity (androidx.appcompat.app.AppCompatActivity)3 User (io.ionic.demo.ecommerce.data.model.User)3 Context (android.content.Context)2