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);
}
});
}
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);
}
});
}
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();
}
});
}
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!");
}
}
});
}
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();
}
Aggregations