Skip to content

Commit

Permalink
Add support for History (un)registerDataUpdateListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
patloew committed Jun 29, 2016
1 parent c37a935 commit 0db0ff8
Show file tree
Hide file tree
Showing 14 changed files with 309 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
/build
/captures
.idea
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Version 1.3.0

* Updated to Play Services 9.2.0.
* Updated RxJava.
* Added support for History API `un/registerDataUpdateListener()`.

## Version 1.2.1

* BREAKING CHANGE: RxFit.OnExceptionResumeNext now exposes a static `.with()` method, which returns a Single or Observable Transformer.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ A basic sample app is available in the `sample` project. You need to create an O
The lib is available on jCenter. Add the following to your `build.gradle`:

dependencies {
compile 'com.patloew.rxfit:rxfit:1.2.1'
compile 'com.patloew.rxfit:rxfit:1.3.0'
}

# Credits
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.android.tools.build:gradle:2.1.2'

classpath 'me.tatarka:gradle-retrolambda:3.3.0-beta4'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.jvmargs=-Xmx2048m

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
16 changes: 8 additions & 8 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.patloew.rxfit'
version = '1.2.1'
version = '1.3.0'
project.archivesBaseName = 'rxfit'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 5
versionName "1.2.1"
targetSdkVersion 24
versionCode 6
versionName "1.3.0"
}
buildTypes {
release {
Expand All @@ -29,8 +29,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'io.reactivex:rxjava:1.1.3'
compile 'com.google.android.gms:play-services-fitness:8.4.0'
compile 'io.reactivex:rxjava:1.1.6'
compile 'com.google.android.gms:play-services-fitness:9.2.0'

testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
Expand Down
10 changes: 5 additions & 5 deletions library/src/main/java/com/patloew/rxfit/BaseObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import rx.Observable;
Expand Down Expand Up @@ -42,7 +42,7 @@
public abstract class BaseObservable<T> extends BaseRx<T> implements Observable.OnSubscribe<T> {
private final boolean handleResolution;

private final HashMap<GoogleApiClient, Subscriber<? super T>> subscriptionInfoHashMap = new HashMap<>();
private final Map<GoogleApiClient, Subscriber<? super T>> subscriptionInfoMap = new ConcurrentHashMap<>();

protected BaseObservable(@NonNull RxFit rxFit, Long timeout, TimeUnit timeUnit) {
super(rxFit, timeout, timeUnit);
Expand All @@ -57,7 +57,7 @@ protected BaseObservable(@NonNull Context ctx, @NonNull Api<? extends Api.ApiOpt
@Override
public final void call(Subscriber<? super T> subscriber) {
final GoogleApiClient apiClient = createApiClient(new ApiClientConnectionCallbacks(subscriber));
subscriptionInfoHashMap.put(apiClient, subscriber);
subscriptionInfoMap.put(apiClient, subscriber);

try {
apiClient.connect();
Expand All @@ -73,15 +73,15 @@ public void call() {
apiClient.disconnect();
}

subscriptionInfoHashMap.remove(apiClient);
subscriptionInfoMap.remove(apiClient);
}
}));
}

protected abstract void onGoogleApiClientReady(GoogleApiClient apiClient, Subscriber<? super T> subscriber);

protected final void handleResolutionResult(int resultCode, ConnectionResult connectionResult) {
for (Map.Entry<GoogleApiClient, Subscriber<? super T>> entry : subscriptionInfoHashMap.entrySet()) {
for (Map.Entry<GoogleApiClient, Subscriber<? super T>> entry : subscriptionInfoMap.entrySet()) {
if (!entry.getValue().isUnsubscribed()) {
if (resultCode == Activity.RESULT_OK) {
try {
Expand Down
10 changes: 5 additions & 5 deletions library/src/main/java/com/patloew/rxfit/BaseSingle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import rx.Single;
Expand Down Expand Up @@ -42,7 +42,7 @@
public abstract class BaseSingle<T> extends BaseRx<T> implements Single.OnSubscribe<T> {
private final boolean handleResolution;

private final HashMap<GoogleApiClient, SingleSubscriber<? super T>> subscriptionInfoHashMap = new HashMap<>();
private final Map<GoogleApiClient, SingleSubscriber<? super T>> subscriptionInfoMap = new ConcurrentHashMap<>();

protected BaseSingle(@NonNull RxFit rxFit, Long timeout, TimeUnit timeUnit) {
super(rxFit, timeout, timeUnit);
Expand All @@ -57,7 +57,7 @@ protected BaseSingle(@NonNull Context ctx, @NonNull Api<? extends Api.ApiOptions
@Override
public final void call(SingleSubscriber<? super T> subscriber) {
final GoogleApiClient apiClient = createApiClient(new ApiClientConnectionCallbacks(subscriber));
subscriptionInfoHashMap.put(apiClient, subscriber);
subscriptionInfoMap.put(apiClient, subscriber);

try {
apiClient.connect();
Expand All @@ -73,15 +73,15 @@ public void call() {
apiClient.disconnect();
}

subscriptionInfoHashMap.remove(apiClient);
subscriptionInfoMap.remove(apiClient);
}
}));
}

protected abstract void onGoogleApiClientReady(GoogleApiClient apiClient, SingleSubscriber<? super T> subscriber);

protected final void handleResolutionResult(int resultCode, ConnectionResult connectionResult) {
for (Map.Entry<GoogleApiClient, SingleSubscriber<? super T>> entry : subscriptionInfoHashMap.entrySet()) {
for (Map.Entry<GoogleApiClient, SingleSubscriber<? super T>> entry : subscriptionInfoMap.entrySet()) {
if (!entry.getValue().isUnsubscribed()) {
if (resultCode == Activity.RESULT_OK) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.patloew.rxfit;

import android.app.PendingIntent;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.data.DataSource;
import com.google.android.gms.fitness.data.DataType;
import com.google.android.gms.fitness.request.DataUpdateListenerRegistrationRequest;

import java.util.concurrent.TimeUnit;

import rx.SingleSubscriber;

/* Copyright 2016 Patrick Löwenstein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
public class HistoryRegisterDataUpdateListenerSingle extends BaseSingle<Status> {

private final DataUpdateListenerRegistrationRequest request;

HistoryRegisterDataUpdateListenerSingle(RxFit rxFit, PendingIntent pendingIntent, DataSource dataSource, DataType dataType, Long timeout, TimeUnit timeUnit) {
super(rxFit, timeout, timeUnit);

DataUpdateListenerRegistrationRequest.Builder builder = new DataUpdateListenerRegistrationRequest.Builder();
builder.setPendingIntent(pendingIntent);
if(dataSource != null) { builder.setDataSource(dataSource); }
if(dataType != null) { builder.setDataType(dataType); }

request = builder.build();
}

@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, final SingleSubscriber<? super Status> subscriber) {
ResultCallback<Status> resultCallback = new StatusResultCallBack(subscriber);

setupFitnessPendingResult(Fitness.HistoryApi.registerDataUpdateListener(apiClient, request), resultCallback);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.patloew.rxfit;

import android.app.PendingIntent;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.request.DataUpdateListenerRegistrationRequest;
import com.google.android.gms.fitness.request.DataUpdateListenerUnregistrationRequest;

import java.util.concurrent.TimeUnit;

import rx.SingleSubscriber;

/* Copyright 2016 Patrick Löwenstein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
public class HistoryUnregisterDataUpdateListenerSingle extends BaseSingle<Status> {

private final PendingIntent pendingIntent;

HistoryUnregisterDataUpdateListenerSingle(RxFit rxFit, PendingIntent pendingIntent, Long timeout, TimeUnit timeUnit) {
super(rxFit, timeout, timeUnit);
this.pendingIntent = pendingIntent;
}

@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, final SingleSubscriber<? super Status> subscriber) {
ResultCallback<Status> resultCallback = new StatusResultCallBack(subscriber);

setupFitnessPendingResult(Fitness.HistoryApi.unregisterDataUpdateListener(apiClient, pendingIntent), resultCallback);
}
}
45 changes: 45 additions & 0 deletions library/src/main/java/com/patloew/rxfit/RxFit.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.android.gms.fitness.request.DataReadRequest;
import com.google.android.gms.fitness.request.DataSourcesRequest;
import com.google.android.gms.fitness.request.DataTypeCreateRequest;
import com.google.android.gms.fitness.request.DataUpdateListenerRegistrationRequest;
import com.google.android.gms.fitness.request.DataUpdateRequest;
import com.google.android.gms.fitness.request.SensorRequest;
import com.google.android.gms.fitness.request.SessionInsertRequest;
Expand Down Expand Up @@ -393,6 +394,50 @@ private static Single<Status> updateInternal(DataUpdateRequest dataUpdateRequest
return Single.create(new HistoryUpdateDataSingle(RxFit.get(), dataUpdateRequest, timeout, timeUnit));
}

// register data update listener

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataSource dataSource) {
return registerDataUpdateListenerInternal(pendingIntent, dataSource, null, null, null);
}

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataSource dataSource, long timeout, @NonNull TimeUnit timeUnit) {
return registerDataUpdateListenerInternal(pendingIntent, dataSource, null, timeout, timeUnit);
}

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataType dataType) {
return registerDataUpdateListenerInternal(pendingIntent, null, dataType, null, null);
}

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataType dataType, long timeout, @NonNull TimeUnit timeUnit) {
return registerDataUpdateListenerInternal(pendingIntent, null, dataType, timeout, timeUnit);
}

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataSource dataSource, @NonNull DataType dataType) {
return registerDataUpdateListenerInternal(pendingIntent, dataSource, dataType, null, null);
}

public static Single<Status> registerDataUpdateListener(@NonNull PendingIntent pendingIntent, @NonNull DataSource dataSource, @NonNull DataType dataType, long timeout, @NonNull TimeUnit timeUnit) {
return registerDataUpdateListenerInternal(pendingIntent, dataSource, dataType, timeout, timeUnit);
}

private static Single<Status> registerDataUpdateListenerInternal(PendingIntent pendingIntent, DataSource dataSource, DataType dataType, Long timeout, TimeUnit timeUnit) {
return Single.create(new HistoryRegisterDataUpdateListenerSingle(RxFit.get(), pendingIntent, dataSource, dataType, timeout, timeUnit));
}

// unregister data update listener

public static Single<Status> unregisterDataUpdateListener(@NonNull PendingIntent pendingIntent) {
return unregisterDataUpdateListenerInternal(pendingIntent, null, null);
}

public static Single<Status> unregisterDataUpdateListener(@NonNull PendingIntent pendingIntent, long timeout, @NonNull TimeUnit timeUnit) {
return unregisterDataUpdateListenerInternal(pendingIntent, timeout, timeUnit);
}

private static Single<Status> unregisterDataUpdateListenerInternal(PendingIntent pendingIntent, Long timeout, TimeUnit timeUnit) {
return Single.create(new HistoryUnregisterDataUpdateListenerSingle(RxFit.get(), pendingIntent, timeout, timeUnit));
}

}


Expand Down
Loading

0 comments on commit 0db0ff8

Please sign in to comment.