Skip to content

Instantly share code, notes, and snippets.

@donnfelker
Last active November 22, 2019 23:46
Show Gist options
  • Save donnfelker/db72a05cc03ef523ee74 to your computer and use it in GitHub Desktop.
Save donnfelker/db72a05cc03ef523ee74 to your computer and use it in GitHub Desktop.
RxJava Bound Service AIDL Abstraction
package com.donnfelker.rxexample;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class OrderActivity extends ActionBarActivity {
private Subscription orderSub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
// Inject via dagger of course ...
RxOrderService orderService = new RxOrderServiceImpl(this);
orderSub = orderService.getOrder(123)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Order>() {
@Override
public void onCompleted() {
// all done
}
@Override
public void onError(Throwable e) {
// oh no
}
@Override
public void onNext(Order order) {
// do something with the order.
}
});
}
@Override
protected void onPause() {
super.onPause();
if (orderSub != null && !orderSub.isUnsubscribed()) {
orderSub.unsubscribe();
}
}
}
package com.donnfelker.rxexample;
import rx.Observable;
public interface RxOrderService {
Observable<Order> getOrder(long orderId);
}
package com.donnfelker.rxexample;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import rx.Observable;
import rx.Subscription;
import rx.functions.Action1;
import rx.subjects.BehaviorSubject;
import rx.subjects.PublishSubject;
public class RxOrderServiceImpl implements RxOrderService, AutoCloseable {
// IBoundOrderService is the AIDL service
private BehaviorSubject<IBoundOrderService> orderServiceSubject = BehaviorSubject.create();
private CompositeSubscription compositeSubscription;
public RxOrderServiceImpl(Context context) {
compositeSubscription = new CompositeSubscription();
Intent orderServiceIntent = new Intent(); // brevity
context.bindService(orderServiceIntent,
orderServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection orderServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
orderServiceSubject.onNext(IBoundOrderService.Stub.asInterface(service));
}
@Override
public void onServiceDisconnected(ComponentName name) {
orderServiceSubject.onCompleted();
}
};
@Override
public Observable<Order> getOrder(long orderId) {
final PublishSubject<Order> orderSubject = PublishSubject.create();
Subscription orderSubscription =
orderServiceSubject.subscribe(new Action1<IBoundOrderService>() {
@Override
public void call(IBoundOrderService orderService) {
try {
orderService.getCustomer(orderId, new IBoundOrderServiceListener.Stub() {
@Override
public void onResponse(Order order) throws RemoteException {
orderSubject.onNext(order);
}
});
} catch (RemoteException e) {
orderSubject.onError(e);
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
orderSubject.onError(throwable);
}
});
compositeSubscription.add(orderSubscription);
return orderSubject.asObservable();
}
@Override
public void close() throws Exception {
context.unbindService(orderServiceConnection);
if (compositeSubscription != null && !compositeSubscription.isUnsubscribed()) {
compositeSubscription.unsubscribe();
}
}
}
@pishguy
Copy link

pishguy commented Mar 17, 2017

Thanks. how can i manage rotate device with this library, you suppose when i call that on MainActivity and on onCreate, how can i manage that when user change device orientation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment