From 0e11b9eb9c9333359f816871e01098858706ab23 Mon Sep 17 00:00:00 2001 From: prabirshrestha Date: Tue, 13 Aug 2013 19:14:04 -0400 Subject: [PATCH] AndroidScheduler.java --- .../java/rx/concurrency/AndroidScheduler.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 rxjava-contrib/rxjava-android/src/main/java/rx/concurrency/AndroidScheduler.java diff --git a/rxjava-contrib/rxjava-android/src/main/java/rx/concurrency/AndroidScheduler.java b/rxjava-contrib/rxjava-android/src/main/java/rx/concurrency/AndroidScheduler.java new file mode 100644 index 0000000000..4f066badf3 --- /dev/null +++ b/rxjava-contrib/rxjava-android/src/main/java/rx/concurrency/AndroidScheduler.java @@ -0,0 +1,69 @@ +/** + * Copyright 2013 Netflix, Inc. + * + * 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 rx.concurrency; + +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; + +import rx.Scheduler; +import rx.Subscription; +import rx.concurrency.ExecutorScheduler; +import rx.util.AtomicObservableSubscription; +import rx.util.functions.Func2; + +import android.os.Handler; +import android.os.Looper; + +/** + * Executes work on the Android UI thread. + * This scheduler should only be used to update the ui. + */ +public class AndroidScheduler extends Scheduler { + + private static final AndroidScheduler INSTANCE = new AndroidScheduler(); + + public static AndroidScheduler getInstance() { + return INSTANCE; + } + + private AndroidScheduler() { + } + + @Override + public Subscription schedule(final T state, final Func2 action) { + final AtomicObservableSubscription subscription = new AtomicObservableSubscription(); + final Scheduler _scheduler = this; + + Handler handler = new Handler(Looper.getMainLooper()); + + handler.post(new Runnable() { + @Override + public void run() { + subscription.wrap(action.call(_scheduler, state)); + } + }); + + return subscription; + } + + @Override + public Subscription schedule(T state, Func2 action, long dueTime, TimeUnit unit) { + return null; + } + + +} +