forked from mcharmas/Android-ReactiveLocation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Activity Recognition
Using broadcast receiver instead of service for activity recognition. Polished activity recognition sample.
- Loading branch information
1 parent
f0c543e
commit 89efbfa
Showing
17 changed files
with
449 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="pl.charmas.android.reactivelocation" > | ||
<application/> | ||
<manifest package="pl.charmas.android.reactivelocation"> | ||
|
||
<application></application> | ||
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...src/main/java/pl/charmas/android/reactivelocation/observables/BaseActivityObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> extends BaseObservable<T> { | ||
|
||
|
||
protected BaseActivityObservable(Context ctx) { | ||
super(ctx, ActivityRecognition.API); | ||
} | ||
|
||
} |
153 changes: 76 additions & 77 deletions
153
...src/main/java/pl/charmas/android/reactivelocation/observables/BaseLocationObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> implements Observable.OnSubscribe<T> { | ||
// | ||
// private final Context ctx; | ||
// | ||
// protected BaseLocationObservable(Context ctx) { | ||
// this.ctx = ctx; | ||
// } | ||
// | ||
// @Override | ||
// public void call(Subscriber<? super T> 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<? super T> observer); | ||
// | ||
// private class LocationConnectionCallbacks implements | ||
// GoogleApiClient.ConnectionCallbacks, | ||
// GoogleApiClient.OnConnectionFailedListener { | ||
// final private Observer<? super T> observer; | ||
// private GoogleApiClient apiClient; | ||
// | ||
// private LocationConnectionCallbacks(Observer<? super T> 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<T> extends BaseObservable<T> { | ||
|
||
public abstract class BaseLocationObservable<T> implements Observable.OnSubscribe<T> { | ||
|
||
private final Context ctx; | ||
|
||
protected BaseLocationObservable(Context ctx) { | ||
this.ctx = ctx; | ||
} | ||
|
||
@Override | ||
public void call(Subscriber<? super T> 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<? super T> observer); | ||
|
||
private class LocationConnectionCallbacks implements | ||
GoogleApiClient.ConnectionCallbacks, | ||
GoogleApiClient.OnConnectionFailedListener { | ||
final private Observer<? super T> observer; | ||
private GoogleApiClient apiClient; | ||
|
||
private LocationConnectionCallbacks(Observer<? super T> 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; | ||
} | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...ocation/src/main/java/pl/charmas/android/reactivelocation/observables/BaseObservable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> implements Observable.OnSubscribe<T> { | ||
|
||
private static final String TAG = BaseObservable.class.getSimpleName(); | ||
|
||
private final Context ctx; | ||
private final List<Api<? extends Api.ApiOptions.NotRequiredOptions>> services; | ||
|
||
|
||
protected BaseObservable(Context ctx, Api<? extends Api.ApiOptions.NotRequiredOptions>... services) { | ||
this.ctx = ctx; | ||
this.services = Arrays.asList(services); | ||
} | ||
|
||
@Override | ||
public void call(Subscriber<? super T> 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<? super T> subscriber) { | ||
|
||
ApiClientConnectionCallbacks apiClientConnectionCallbacks = new ApiClientConnectionCallbacks(subscriber); | ||
|
||
GoogleApiClient.Builder apiClientBuilder = new GoogleApiClient.Builder(ctx); | ||
|
||
|
||
for (Api<? extends Api.ApiOptions.NotRequiredOptions> 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<? super T> observer); | ||
|
||
private class ApiClientConnectionCallbacks implements | ||
GoogleApiClient.ConnectionCallbacks, | ||
GoogleApiClient.OnConnectionFailedListener { | ||
|
||
final private Observer<? super T> observer; | ||
|
||
private GoogleApiClient apiClient; | ||
|
||
private ApiClientConnectionCallbacks(Observer<? super T> 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; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.