Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating to jetpack and updating location api #196

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions android-reactive-location/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ apply plugin: 'maven'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

defaultConfig {
minSdkVersion 14
Expand All @@ -19,9 +18,9 @@ android {
//TODO: local maven deployment

dependencies {
compile 'com.google.android.gms:play-services-location:11.0.4'
compile 'com.google.android.gms:play-services-places:11.0.4'
compile 'io.reactivex.rxjava2:rxjava:2.0.5'
api 'com.google.android.gms:play-services-location:16.0.0'
api 'com.google.android.gms:play-services-places:16.0.0'
api 'io.reactivex.rxjava2:rxjava:2.2.0'
}

// Comment this to deploy to local maven repository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.charmas.android.reactivelocation2;

import com.google.android.gms.tasks.OnFailureListener;

import androidx.annotation.NonNull;
import io.reactivex.ObservableEmitter;

public class BaseFailureListener<T> implements OnFailureListener {

private final ObservableEmitter<? super T> emitter;

public BaseFailureListener(ObservableEmitter<? super T> emitter) {
this.emitter = emitter;
}

@Override
public void onFailure(@NonNull Exception exception) {
if (emitter.isDisposed()) return;
emitter.onError(exception);
emitter.onComplete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package pl.charmas.android.reactivelocation2;

public class LocationNotAvailableException extends RuntimeException {
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package pl.charmas.android.reactivelocation2;

import android.os.Handler;
import android.support.annotation.Nullable;

import androidx.annotation.Nullable;

/**
* Configuration for location provider. Pleas use builder to create an instance.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.Api;
Expand All @@ -12,6 +11,7 @@
import java.util.Arrays;
import java.util.List;

import androidx.annotation.NonNull;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.Disposables;
Expand Down Expand Up @@ -44,15 +44,15 @@ public void subscribe(ObservableEmitter<T> emitter) throws Exception {
emitter.setDisposable(Disposables.fromAction(new Action() {
@Override
public void run() throws Exception {
onDisposed(apiClient);
onDisposed();
apiClient.disconnect();
}
}));
}


private GoogleApiClient createApiClient(ObservableEmitter<? super T> emitter) {
ApiClientConnectionCallbacks apiClientConnectionCallbacks = new ApiClientConnectionCallbacks(emitter);
ApiClientConnectionCallbacks apiClientConnectionCallbacks = new ApiClientConnectionCallbacks(ctx, emitter);
GoogleApiClient.Builder apiClientBuilder = new GoogleApiClient.Builder(ctx);

for (Api<? extends Api.ApiOptions.NotRequiredOptions> service : services) {
Expand All @@ -72,27 +72,30 @@ private GoogleApiClient createApiClient(ObservableEmitter<? super T> emitter) {
return apiClient;
}

protected void onDisposed(GoogleApiClient locationClient) {
protected void onDisposed() {
}

protected abstract void onGoogleApiClientReady(GoogleApiClient apiClient, ObservableEmitter<? super T> emitter);
protected abstract void onGoogleApiClientReady(Context context, GoogleApiClient googleApiClient, ObservableEmitter<? super T> emitter);

private class ApiClientConnectionCallbacks implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {

final private Context context;

final private ObservableEmitter<? super T> emitter;

private GoogleApiClient apiClient;

private ApiClientConnectionCallbacks(ObservableEmitter<? super T> emitter) {
private ApiClientConnectionCallbacks(Context context, ObservableEmitter<? super T> emitter) {
this.context = context;
this.emitter = emitter;
}

@Override
public void onConnected(Bundle bundle) {
try {
onGoogleApiClientReady(apiClient, emitter);
onGoogleApiClientReady(context, apiClient, emitter);
} catch (Throwable ex) {
if (!emitter.isDisposed()) {
emitter.onError(ex);
Expand All @@ -111,7 +114,7 @@ public void onConnectionSuspended(int cause) {
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (!emitter.isDisposed()) {
emitter.onError(new GoogleAPIConnectionException("Error connecting to GoogleApiClient.",
connectionResult));
connectionResult));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package pl.charmas.android.reactivelocation2.observables;

import android.content.Context;

import com.google.android.gms.common.api.Api;
import com.google.android.gms.common.api.GoogleApiClient;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;

public class GoogleAPIClientObservableOnSubscribe extends BaseObservableOnSubscribe<GoogleApiClient> {
public class GoogleAPIClientObservableOnSubscribe extends BaseObservableOnSubscribe<Void> {

@SafeVarargs
public static Observable<GoogleApiClient> create(ObservableContext context, ObservableFactory factory, Api<? extends Api.ApiOptions.NotRequiredOptions>... apis) {
public static Observable<Void> create(ObservableContext context, ObservableFactory factory, Api<? extends Api.ApiOptions.NotRequiredOptions>... apis) {
return factory.createObservable(new GoogleAPIClientObservableOnSubscribe(context, apis));
}

Expand All @@ -19,8 +21,8 @@ private GoogleAPIClientObservableOnSubscribe(ObservableContext ctx, Api<? extend
}

@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, ObservableEmitter<? super GoogleApiClient> emitter) {
protected void onGoogleApiClientReady(Context context, GoogleApiClient googleApiClient, ObservableEmitter<? super Void> emitter) {
if (emitter.isDisposed()) return;
emitter.onNext(apiClient);
emitter.onComplete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(T t) {
if (!emitter.isDisposed()){
if (!emitter.isDisposed()) {
emitter.onNext(t);
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.charmas.android.reactivelocation2.observables;

import com.google.android.gms.common.api.Response;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

import io.reactivex.SingleEmitter;
import io.reactivex.SingleOnSubscribe;

public class TaskSingleOnSubscribe<T extends Response> implements SingleOnSubscribe<T> {
private final Task<T> task;

public TaskSingleOnSubscribe(Task<T> task) {
this.task = task;
}

@Override
public void subscribe(final SingleEmitter<T> emitter) {
task.addOnSuccessListener(new OnSuccessListener<T>() {
@Override
public void onSuccess(T t) {
if (!emitter.isDisposed()) {
emitter.onSuccess(t);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception exception) {
if (!emitter.isDisposed()) {
emitter.onError(exception);
}
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
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.ActivityRecognitionClient;
import com.google.android.gms.location.ActivityRecognitionResult;

import io.reactivex.Observable;
Expand All @@ -22,6 +21,7 @@ public class ActivityUpdatesObservableOnSubscribe extends BaseActivityObservable

private final Context context;
private final int detectionIntervalMilliseconds;
private ActivityRecognitionClient activityRecognitionClient;
private ActivityUpdatesBroadcastReceiver receiver;

public static Observable<ActivityRecognitionResult> createObservable(ObservableContext ctx, ObservableFactory factory, int detectionIntervalMiliseconds) {
Expand All @@ -35,21 +35,23 @@ private ActivityUpdatesObservableOnSubscribe(ObservableContext context, int dete
}

@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, ObservableEmitter<? super ActivityRecognitionResult> emitter) {
protected void onActivityRecognitionClientReady(ActivityRecognitionClient activityRecognitionClient,
ObservableEmitter<? super ActivityRecognitionResult> emitter) {
this.activityRecognitionClient = activityRecognitionClient;
receiver = new ActivityUpdatesBroadcastReceiver(emitter);
context.registerReceiver(receiver, new IntentFilter(ACTION_ACTIVITY_DETECTED));
PendingIntent receiverIntent = getReceiverPendingIntent();
ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMilliseconds, receiverIntent);
activityRecognitionClient.requestActivityUpdates(detectionIntervalMilliseconds, receiverIntent);
}

private PendingIntent getReceiverPendingIntent() {
return PendingIntent.getBroadcast(context, 0, new Intent(ACTION_ACTIVITY_DETECTED), PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
protected void onDisposed(GoogleApiClient apiClient) {
if (apiClient.isConnected()) {
ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, getReceiverPendingIntent());
protected void onDisposed() {
if (activityRecognitionClient != null) {
activityRecognitionClient.removeActivityTransitionUpdates(getReceiverPendingIntent());
}
if (receiver != null) {
context.unregisterReceiver(receiver);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
package pl.charmas.android.reactivelocation2.observables.activity;

import android.content.Context;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.ActivityRecognition;
import com.google.android.gms.location.ActivityRecognitionClient;

import io.reactivex.ObservableEmitter;
import pl.charmas.android.reactivelocation2.observables.BaseObservableOnSubscribe;
import pl.charmas.android.reactivelocation2.observables.ObservableContext;

abstract class BaseActivityObservableOnSubscribe<T> extends BaseObservableOnSubscribe<T> {
BaseActivityObservableOnSubscribe(ObservableContext ctx) {
super(ctx, ActivityRecognition.API);
}

@Override
protected final void onGoogleApiClientReady(Context context, GoogleApiClient googleApiClient, ObservableEmitter<? super T> emitter) {
onActivityRecognitionClientReady(ActivityRecognition.getClient(context), emitter);
}

protected abstract void onActivityRecognitionClientReady(ActivityRecognitionClient activityRecognitionClient,
ObservableEmitter<? super T> emitter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.support.annotation.NonNull;

import com.google.android.gms.maps.model.LatLngBounds;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import androidx.annotation.NonNull;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
Expand Down
Loading