From 89efbfa341ce2a9b412a37ce4f925f85be45a2f8 Mon Sep 17 00:00:00 2001 From: cyrix Date: Sat, 3 Jan 2015 19:45:47 +0100 Subject: [PATCH] Added support for Activity Recognition Using broadcast receiver instead of service for activity recognition. Polished activity recognition sample. --- README.md | 21 +++ .../src/main/AndroidManifest.xml | 6 +- .../ReactiveLocationProvider.java | 15 +- .../observables/BaseActivityObservable.java | 15 ++ .../observables/BaseLocationObservable.java | 153 +++++++++--------- .../observables/BaseObservable.java | 118 ++++++++++++++ .../activity/ActivityUpdatesObservable.java | 70 ++++++++ .../geocode/GeodecodeObservable.java | 3 + .../geofence/AddGeofenceObservable.java | 2 +- .../geofence/RemoveGeofenceObservable.java | 2 +- .../location/LastKnownLocationObservable.java | 2 +- .../location/LocationUpdatesObservable.java | 4 +- sample/src/main/AndroidManifest.xml | 1 + .../reactivelocation/sample/MainActivity.java | 20 ++- .../utils/DetectedActivityToString.java | 30 ++++ .../sample/utils/ToMostProbableActivity.java | 13 ++ sample/src/main/res/layout/activity_main.xml | 103 +++++++----- 17 files changed, 449 insertions(+), 129 deletions(-) create mode 100644 android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseActivityObservable.java create mode 100644 android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseObservable.java create mode 100644 android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/activity/ActivityUpdatesObservable.java create mode 100644 sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/DetectedActivityToString.java create mode 100644 sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/ToMostProbableActivity.java diff --git a/README.md b/README.md index bd1cd845..983367b6 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ What can you do with that? * subscribe for location updates * manage geofences * geocode location to list of addresses +* activity recognition How does the API look like? ---------------------------- @@ -69,6 +70,26 @@ When you are done (for example in ```onStop()```) remember to unsubscribe. subscription.unsubscribe(); ``` +### Subscribing for Activity Recognition + +Getting activity recognition is just as simple + +```java + +ReactiveLocationProvider locationProvider = new ReactiveLocationProvider(context); +Subscription subscription = activityUpdatesObservable = locationProvider.getDetectedActivity(0) // detectionIntervalMillis + .filter(...) // you can filter location updates + .map(...) // you can map location to sth different + .flatMap(...) // or event flat map + ... // and do everything else that is provided by RxJava + .subscribe(new Action1() { + @Override + public void call(DetectedActivity detectedActivity) { + doSthImportantWithObtainedActivity(detectedActivity); + } + }); +``` + ### Geocode location Do you need address for location? diff --git a/android-reactive-location/src/main/AndroidManifest.xml b/android-reactive-location/src/main/AndroidManifest.xml index cf0d1805..1eb5adc1 100644 --- a/android-reactive-location/src/main/AndroidManifest.xml +++ b/android-reactive-location/src/main/AndroidManifest.xml @@ -1,5 +1,5 @@ - - + + + diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/ReactiveLocationProvider.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/ReactiveLocationProvider.java index 3a324b94..d65c5afe 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/ReactiveLocationProvider.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/ReactiveLocationProvider.java @@ -5,11 +5,13 @@ import android.location.Address; import android.location.Location; +import com.google.android.gms.location.ActivityRecognitionResult; import com.google.android.gms.location.GeofencingRequest; import com.google.android.gms.location.LocationRequest; import java.util.List; +import pl.charmas.android.reactivelocation.observables.activity.ActivityUpdatesObservable; import pl.charmas.android.reactivelocation.observables.geocode.GeodecodeObservable; import pl.charmas.android.reactivelocation.observables.geofence.AddGeofenceObservable; import pl.charmas.android.reactivelocation.observables.geofence.AddGeofenceResult; @@ -92,7 +94,7 @@ public Observable> getGeocodeObservable(double lat, double lng, in * Every exception is delivered by {@link rx.Observer#onError(Throwable)}. * * @param geofenceTransitionPendingIntent pending intent to register on geofence transition - * @param request list of request to add + * @param request list of request to add * @return observable that adds request */ public Observable addGeofences(PendingIntent geofenceTransitionPendingIntent, GeofencingRequest request) { @@ -134,4 +136,15 @@ public Observable remov public Observable removeGeofences(List requestIds) { return RemoveGeofenceObservable.createObservable(ctx, requestIds); } + + + /** + * Observable that can be used to observe activity provided by Actity Recognition mechanism. + * + * @param detectIntervalMiliseconds detecion interval + * @return observable that provides activity recognition + */ + public Observable getDetectedActivity(int detectIntervalMiliseconds) { + return ActivityUpdatesObservable.createObservable(ctx, detectIntervalMiliseconds); + } } diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseActivityObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseActivityObservable.java new file mode 100644 index 00000000..b5e4ea9e --- /dev/null +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseActivityObservable.java @@ -0,0 +1,15 @@ +package pl.charmas.android.reactivelocation.observables; + +import android.content.Context; + +import com.google.android.gms.location.ActivityRecognition; + + +public abstract class BaseActivityObservable extends BaseObservable { + + + protected BaseActivityObservable(Context ctx) { + super(ctx, ActivityRecognition.API); + } + +} diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseLocationObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseLocationObservable.java index d7ef4ed8..50a1a951 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseLocationObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseLocationObservable.java @@ -1,90 +1,89 @@ package pl.charmas.android.reactivelocation.observables; import android.content.Context; -import android.os.Bundle; -import com.google.android.gms.common.ConnectionResult; -import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationServices; -import rx.Observable; -import rx.Observer; -import rx.Subscriber; -import rx.functions.Action0; -import rx.subscriptions.Subscriptions; +//public abstract class BaseLocationObservable implements Observable.OnSubscribe { +// +// private final Context ctx; +// +// protected BaseLocationObservable(Context ctx) { +// this.ctx = ctx; +// } +// +// @Override +// public void call(Subscriber subscriber) { +// final LocationConnectionCallbacks locationConnectionCallbacks = new LocationConnectionCallbacks(subscriber); +// final GoogleApiClient apiClient = new GoogleApiClient.Builder(ctx) +// .addApi(LocationServices.API) +// .addConnectionCallbacks(locationConnectionCallbacks) +// .addOnConnectionFailedListener(locationConnectionCallbacks) +// .build(); +// locationConnectionCallbacks.setClient(apiClient); +// +// try { +// apiClient.connect(); +// } catch (Throwable ex) { +// subscriber.onError(ex); +// } +// +// subscriber.add(Subscriptions.create(new Action0() { +// @Override +// public void call() { +// if (apiClient.isConnected() || apiClient.isConnecting()) { +// onUnsubscribed(apiClient); +// apiClient.disconnect(); +// } +// } +// })); +// } +// +// protected void onUnsubscribed(GoogleApiClient locationClient) { +// } +// +// protected abstract void onGoogleApiClientReady(GoogleApiClient apiClient, Observer observer); +// +// private class LocationConnectionCallbacks implements +// GoogleApiClient.ConnectionCallbacks, +// GoogleApiClient.OnConnectionFailedListener { +// final private Observer observer; +// private GoogleApiClient apiClient; +// +// private LocationConnectionCallbacks(Observer observer) { +// this.observer = observer; +// } +// +// @Override +// public void onConnected(Bundle bundle) { +// try { +// onGoogleApiClientReady(apiClient, observer); +// } catch (Throwable ex) { +// observer.onError(ex); +// } +// } +// +// @Override +// public void onConnectionSuspended(int cause) { +// observer.onError(new LocationConnectionSuspendedException(cause)); +// } +// +// @Override +// public void onConnectionFailed(ConnectionResult connectionResult) { +// observer.onError(new LocationConnectionException("Error connecting to LocationClient.", connectionResult)); +// } +// +// public void setClient(GoogleApiClient client) { +// this.apiClient = client; +// } +// } +//} +public abstract class BaseLocationObservable extends BaseObservable { -public abstract class BaseLocationObservable implements Observable.OnSubscribe { - - private final Context ctx; protected BaseLocationObservable(Context ctx) { - this.ctx = ctx; - } - - @Override - public void call(Subscriber subscriber) { - final LocationConnectionCallbacks locationConnectionCallbacks = new LocationConnectionCallbacks(subscriber); - final GoogleApiClient apiClient = new GoogleApiClient.Builder(ctx) - .addApi(LocationServices.API) - .addConnectionCallbacks(locationConnectionCallbacks) - .addOnConnectionFailedListener(locationConnectionCallbacks) - .build(); - locationConnectionCallbacks.setClient(apiClient); - - try { - apiClient.connect(); - } catch (Throwable ex) { - subscriber.onError(ex); - } - - subscriber.add(Subscriptions.create(new Action0() { - @Override - public void call() { - if (apiClient.isConnected() || apiClient.isConnecting()) { - onUnsubscribed(apiClient); - apiClient.disconnect(); - } - } - })); - } - - protected void onUnsubscribed(GoogleApiClient locationClient) { + super(ctx, LocationServices.API); } - protected abstract void onLocationClientReady(GoogleApiClient apiClient, Observer observer); - - private class LocationConnectionCallbacks implements - GoogleApiClient.ConnectionCallbacks, - GoogleApiClient.OnConnectionFailedListener { - final private Observer observer; - private GoogleApiClient apiClient; - - private LocationConnectionCallbacks(Observer observer) { - this.observer = observer; - } - - @Override - public void onConnected(Bundle bundle) { - try { - onLocationClientReady(apiClient, observer); - } catch (Throwable ex) { - observer.onError(ex); - } - } - - @Override - public void onConnectionSuspended(int cause) { - observer.onError(new LocationConnectionSuspendedException(cause)); - } - - @Override - public void onConnectionFailed(ConnectionResult connectionResult) { - observer.onError(new LocationConnectionException("Error connecting to LocationClient.", connectionResult)); - } - - public void setClient(GoogleApiClient client) { - this.apiClient = client; - } - } } diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseObservable.java new file mode 100644 index 00000000..9a7da4fe --- /dev/null +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/BaseObservable.java @@ -0,0 +1,118 @@ +package pl.charmas.android.reactivelocation.observables; + +import android.content.Context; +import android.os.Bundle; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.api.Api; +import com.google.android.gms.common.api.GoogleApiClient; + +import java.util.Arrays; +import java.util.List; + +import rx.Observable; +import rx.Observer; +import rx.Subscriber; +import rx.functions.Action0; +import rx.subscriptions.Subscriptions; + + +public abstract class BaseObservable implements Observable.OnSubscribe { + + private static final String TAG = BaseObservable.class.getSimpleName(); + + private final Context ctx; + private final List> services; + + + protected BaseObservable(Context ctx, Api... services) { + this.ctx = ctx; + this.services = Arrays.asList(services); + } + + @Override + public void call(Subscriber subscriber) { + + final GoogleApiClient apiClient = createApiClient(subscriber); + try { + apiClient.connect(); + } catch (Throwable ex) { + subscriber.onError(ex); + } + + subscriber.add(Subscriptions.create(new Action0() { + @Override + public void call() { + if (apiClient.isConnected() || apiClient.isConnecting()) { + onUnsubscribed(apiClient); + apiClient.disconnect(); + } + } + })); + } + + + protected GoogleApiClient createApiClient(Subscriber subscriber) { + + ApiClientConnectionCallbacks apiClientConnectionCallbacks = new ApiClientConnectionCallbacks(subscriber); + + GoogleApiClient.Builder apiClientBuilder = new GoogleApiClient.Builder(ctx); + + + for (Api service : services) { + apiClientBuilder.addApi(service); + } + + apiClientBuilder.addConnectionCallbacks(apiClientConnectionCallbacks); + apiClientBuilder.addOnConnectionFailedListener(apiClientConnectionCallbacks); + + GoogleApiClient apiClient = apiClientBuilder.build(); + + apiClientConnectionCallbacks.setClient(apiClient); + + return apiClient; + + } + + protected void onUnsubscribed(GoogleApiClient locationClient) { + } + + protected abstract void onGoogleApiClientReady(GoogleApiClient apiClient, Observer observer); + + private class ApiClientConnectionCallbacks implements + GoogleApiClient.ConnectionCallbacks, + GoogleApiClient.OnConnectionFailedListener { + + final private Observer observer; + + private GoogleApiClient apiClient; + + private ApiClientConnectionCallbacks(Observer observer) { + this.observer = observer; + } + + @Override + public void onConnected(Bundle bundle) { + try { + onGoogleApiClientReady(apiClient, observer); + } catch (Throwable ex) { + observer.onError(ex); + } + } + + @Override + public void onConnectionSuspended(int cause) { + observer.onError(new LocationConnectionSuspendedException(cause)); + } + + @Override + public void onConnectionFailed(ConnectionResult connectionResult) { + observer.onError(new LocationConnectionException("Error connecting to GoogleApiClient.", connectionResult)); + } + + public void setClient(GoogleApiClient client) { + this.apiClient = client; + } + } + +} diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/activity/ActivityUpdatesObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/activity/ActivityUpdatesObservable.java new file mode 100644 index 00000000..57b9b958 --- /dev/null +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/activity/ActivityUpdatesObservable.java @@ -0,0 +1,70 @@ +package pl.charmas.android.reactivelocation.observables.activity; + +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; + +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.location.ActivityRecognition; +import com.google.android.gms.location.ActivityRecognitionResult; + +import pl.charmas.android.reactivelocation.observables.BaseActivityObservable; +import rx.Observable; +import rx.Observer; + +public class ActivityUpdatesObservable extends BaseActivityObservable { + private static final String ACTION_ACTIVITY_DETECTED = "pl.charmas.android.reactivelocation.ACTION_ACTIVITY_UPDATE_DETECTED"; + + private final Context context; + private final int detectionIntervalMilliseconds; + private ActivityUpdatesBroadcastReceiver receiver; + + public static Observable createObservable(Context ctx, int detectionIntervalMiliseconds) { + return Observable.create(new ActivityUpdatesObservable(ctx, detectionIntervalMiliseconds)); + } + + private ActivityUpdatesObservable(Context context, int detectionIntervalMilliseconds) { + super(context); + this.context = context; + this.detectionIntervalMilliseconds = detectionIntervalMilliseconds; + } + + @Override + protected void onGoogleApiClientReady(GoogleApiClient apiClient, Observer observer) { + receiver = new ActivityUpdatesBroadcastReceiver(observer); + context.registerReceiver(receiver, new IntentFilter(ACTION_ACTIVITY_DETECTED)); + PendingIntent receiverIntent = getReceiverPendingIntent(); + ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMilliseconds, receiverIntent); + } + + private PendingIntent getReceiverPendingIntent() { + return PendingIntent.getBroadcast(context, 0, new Intent(ACTION_ACTIVITY_DETECTED), PendingIntent.FLAG_UPDATE_CURRENT); + } + + @Override + protected void onUnsubscribed(GoogleApiClient apiClient) { + if (apiClient.isConnected()) { + ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, getReceiverPendingIntent()); + } + context.unregisterReceiver(receiver); + receiver = null; + } + + private static class ActivityUpdatesBroadcastReceiver extends BroadcastReceiver { + private final Observer observer; + + public ActivityUpdatesBroadcastReceiver(Observer observer) { + this.observer = observer; + } + + @Override + public void onReceive(Context context, Intent intent) { + if (ActivityRecognitionResult.hasResult(intent)) { + ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); + observer.onNext(result); + } + } + } +} diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geocode/GeodecodeObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geocode/GeodecodeObservable.java index e734227f..7a0fc6a4 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geocode/GeodecodeObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geocode/GeodecodeObservable.java @@ -11,6 +11,9 @@ import rx.Subscriber; public class GeodecodeObservable implements Observable.OnSubscribe> { + + private static final String TAG = GeodecodeObservable.class.getSimpleName(); + private final Context ctx; private final double latitude; private final double longitude; diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/AddGeofenceObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/AddGeofenceObservable.java index c57510c2..c9e81312 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/AddGeofenceObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/AddGeofenceObservable.java @@ -28,7 +28,7 @@ private AddGeofenceObservable(Context ctx, GeofencingRequest request, PendingInt } @Override - protected void onLocationClientReady(GoogleApiClient apiClient, final Observer observer) { + protected void onGoogleApiClientReady(GoogleApiClient apiClient, final Observer observer) { LocationServices.GeofencingApi.addGeofences(apiClient, request, geofenceTransitionPendingIntent) .setResultCallback(new ResultCallback() { @Override diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/RemoveGeofenceObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/RemoveGeofenceObservable.java index e8a5f548..fc38bbde 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/RemoveGeofenceObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/geofence/RemoveGeofenceObservable.java @@ -28,7 +28,7 @@ protected RemoveGeofenceObservable(Context ctx) { } @Override - protected void onLocationClientReady(GoogleApiClient apiClient, final Observer observer) { + protected void onGoogleApiClientReady(GoogleApiClient apiClient, final Observer observer) { removeGeofences(apiClient, observer); } diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LastKnownLocationObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LastKnownLocationObservable.java index 7ff0b4cd..d059712f 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LastKnownLocationObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LastKnownLocationObservable.java @@ -21,7 +21,7 @@ private LastKnownLocationObservable(Context ctx) { } @Override - protected void onLocationClientReady(GoogleApiClient apiClient, Observer observer) { + protected void onGoogleApiClientReady(GoogleApiClient apiClient, Observer observer) { observer.onNext(LocationServices.FusedLocationApi.getLastLocation(apiClient)); observer.onCompleted(); } diff --git a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LocationUpdatesObservable.java b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LocationUpdatesObservable.java index ea4ff0ff..09f89635 100644 --- a/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LocationUpdatesObservable.java +++ b/android-reactive-location/src/main/java/pl/charmas/android/reactivelocation/observables/location/LocationUpdatesObservable.java @@ -14,6 +14,8 @@ public class LocationUpdatesObservable extends BaseLocationObservable { + private static final String TAG = LocationUpdatesObservable.class.getSimpleName(); + public static Observable createObservable(Context ctx, LocationRequest locationRequest) { return Observable.create(new LocationUpdatesObservable(ctx, locationRequest)); } @@ -27,7 +29,7 @@ private LocationUpdatesObservable(Context ctx, LocationRequest locationRequest) } @Override - protected void onLocationClientReady(GoogleApiClient apiClient, final Observer observer) { + protected void onGoogleApiClientReady(GoogleApiClient apiClient, final Observer observer) { listener = new LocationListener() { @Override public void onLocationChanged(Location location) { diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index cc474232..fde1cfe3 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="pl.charmas.android.reactivelocation.sample"> + lastKnownLocationObservable; private Observable locationUpdatesObservable; + private Observable activityObservable; private Subscription lastKnownLocationSubscription; private Subscription updatableLocationSubscription; private Subscription addressSubscription; + private Subscription activitySubscription; @Override protected void onCreate(Bundle savedInstanceState) { @@ -47,17 +53,17 @@ protected void onCreate(Bundle savedInstanceState) { lastKnownLocationView = (TextView) findViewById(R.id.last_known_location_view); updatableLocationView = (TextView) findViewById(R.id.updated_location_view); addressLocationView = (TextView) findViewById(R.id.address_for_location_view); + currentActivityView = (TextView) findViewById(R.id.activity_recent_view); locationProvider = new ReactiveLocationProvider(getApplicationContext()); lastKnownLocationObservable = locationProvider.getLastKnownLocation(); - locationUpdatesObservable = locationProvider.getUpdatedLocation( LocationRequest.create() .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) .setNumUpdates(5) .setInterval(100) ); - + activityObservable = locationProvider.getDetectedActivity(50); } @Override @@ -96,14 +102,20 @@ public Address call(List
addresses) { .subscribeOn(Schedulers.io())) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisplayTextOnViewAction(addressLocationView)); + + activitySubscription = AndroidObservable.bindActivity(this, activityObservable) + .map(new ToMostProbableActivity()) + .map(new DetectedActivityToString()) + .subscribe(new DisplayTextOnViewAction(currentActivityView)); } @Override protected void onStop() { super.onStop(); - lastKnownLocationSubscription.unsubscribe(); updatableLocationSubscription.unsubscribe(); addressSubscription.unsubscribe(); + lastKnownLocationSubscription.unsubscribe(); + activitySubscription.unsubscribe(); } @Override diff --git a/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/DetectedActivityToString.java b/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/DetectedActivityToString.java new file mode 100644 index 00000000..6c2b507e --- /dev/null +++ b/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/DetectedActivityToString.java @@ -0,0 +1,30 @@ +package pl.charmas.android.reactivelocation.sample.utils; + +import com.google.android.gms.location.DetectedActivity; + +import rx.functions.Func1; + +public class DetectedActivityToString implements Func1 { + @Override + public String call(DetectedActivity detectedActivity) { + return getNameFromType(detectedActivity.getType()) + " with confidence " + detectedActivity.getConfidence(); + } + + private String getNameFromType(int activityType) { + switch (activityType) { + case DetectedActivity.IN_VEHICLE: + return "in_vehicle"; + case DetectedActivity.ON_BICYCLE: + return "on_bicycle"; + case DetectedActivity.ON_FOOT: + return "on_foot"; + case DetectedActivity.STILL: + return "still"; + case DetectedActivity.UNKNOWN: + return "unknown"; + case DetectedActivity.TILTING: + return "tilting"; + } + return "unknown"; + } +} diff --git a/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/ToMostProbableActivity.java b/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/ToMostProbableActivity.java new file mode 100644 index 00000000..de4bd5c4 --- /dev/null +++ b/sample/src/main/java/pl/charmas/android/reactivelocation/sample/utils/ToMostProbableActivity.java @@ -0,0 +1,13 @@ +package pl.charmas.android.reactivelocation.sample.utils; + +import com.google.android.gms.location.ActivityRecognitionResult; +import com.google.android.gms.location.DetectedActivity; + +import rx.functions.Func1; + +public class ToMostProbableActivity implements Func1 { + @Override + public DetectedActivity call(ActivityRecognitionResult activityRecognitionResult) { + return activityRecognitionResult.getMostProbableActivity(); + } +} diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 74db6282..c4942281 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -1,49 +1,72 @@ - + android:layout_height="match_parent" + android:layout_margin="8dp"> - + android:orientation="vertical"> - + - - - - - + + + + + + + + + + + - + - + +