/*
* 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.
*/
package com.appunite.rx.android;
import java.util.concurrent.TimeUnit;
import rx.Scheduler;
import rx.Subscription;
import rx.functions.Action0;
import rx.internal.schedulers.ScheduledAction;
import rx.subscriptions.CompositeSubscription;
import rx.subscriptions.Subscriptions;
import android.os.Handler;
import android.os.Looper;
/**
* @deprecated Use {@link MyAndroidSchedulers#handlerThread}. This type will not be part of the
* public API in a future release.
*/
@Deprecated
public class MyHandlerThreadScheduler extends Scheduler {
private final Handler handler;
/**
* @deprecated Use {@link MyAndroidSchedulers#handlerThread}.
*/
@Deprecated
public MyHandlerThreadScheduler(Handler handler) {
this.handler = handler;
}
@Override
public Worker createWorker() {
return new InnerHandlerThreadScheduler(handler);
}
private static class InnerHandlerThreadScheduler extends Worker {
private final Handler handler;
private final CompositeSubscription compositeSubscription = new CompositeSubscription();
public InnerHandlerThreadScheduler(Handler handler) {
this.handler = handler;
}
@Override
public void unsubscribe() {
compositeSubscription.unsubscribe();
}
@Override
public boolean isUnsubscribed() {
return compositeSubscription.isUnsubscribed();
}
@Override
public Subscription schedule(final Action0 action, long delayTime, TimeUnit unit) {
final ScheduledAction scheduledAction = new ScheduledAction(action);
scheduledAction.add(Subscriptions.create(new Action0() {
@Override
public void call() {
handler.removeCallbacks(scheduledAction);
}
}));
scheduledAction.addParent(compositeSubscription);
compositeSubscription.add(scheduledAction);
handler.postDelayed(scheduledAction, unit.toMillis(delayTime));
return scheduledAction;
}
@Override
public Subscription schedule(final Action0 action) {
if (Looper.myLooper() == handler.getLooper()) {
action.call();
return Subscriptions.unsubscribed();
} else {
return schedule(action, 0, TimeUnit.MILLISECONDS);
}
}
}
}