/*
 * 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.android.schedulers;

import android.os.Looper;
import rx.Scheduler;
import rx.android.plugins.RxAndroidPlugins;

/** Android-specific Schedulers. */
public final class AndroidSchedulers {
    private AndroidSchedulers() {
        throw new AssertionError("No instances");
    }

    // See https://github.com/ReactiveX/RxAndroid/issues/238
    // https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom
    private static class MainThreadSchedulerHolder {
        static final Scheduler MAIN_THREAD_SCHEDULER = new LooperScheduler(Looper.getMainLooper());
    }

    /** A {@link Scheduler} which executes actions on the Android UI thread. */
    public static Scheduler mainThread() {
        Scheduler scheduler =
                RxAndroidPlugins.getInstance().getSchedulersHook().getMainThreadScheduler();
        return scheduler != null ? scheduler : MainThreadSchedulerHolder.MAIN_THREAD_SCHEDULER;
    }

    /** A {@link Scheduler} which executes actions on {@code looper}. */
    public static Scheduler from(Looper looper) {
        if (looper == null) throw new NullPointerException("looper == null");
        return new LooperScheduler(looper);
    }
}

@@ -673,16 +673,67 @@
 Plugins;
+%0Aimport rx.android.plugins.RxAndroidSchedulersHook;
 %0A%0A/** An
@@ -813,16 +813,29 @@
 private 
+static final 
 AndroidS
@@ -847,311 +847,394 @@
 lers
-() %7B%0A        throw new AssertionError(%22No instances%22);
+ INSTANCE = new AndroidSchedulers();%0A%0A    private final Scheduler mainThreadScheduler;%0A%0A    private AndroidSchedulers() %7B
 %0A    
-%7D%0A%0A
     
-// See https://github.com/ReactiveX/RxAndroid/issues/238%0A    // https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom%0A    private static class MainThreadSchedulerHolder %7B%0A        static final Scheduler MAIN_THREAD_SCHEDULER
+RxAndroidSchedulersHook hook = RxAndroidPlugins.getInstance().getSchedulersHook();%0A%0A        Scheduler main = hook.getMainThreadScheduler();%0A        if (main != null) %7B%0A            mainThreadScheduler = main;%0A        %7D else %7B%0A            mainThreadScheduler
  = n
@@ -1277,16 +1277,26 @@
 per());%0A
+        %7D%0A
     %7D%0A%0A 
@@ -1429,217 +1429,43 @@
     
-Scheduler scheduler =%0A                RxAndroidPlugins.getInstance().getSchedulersHook().getMainThreadScheduler();%0A        return scheduler != null ? scheduler : MainThreadSchedulerHolder.MAIN_THREAD_SCHEDULER
+return INSTANCE.mainThreadScheduler
 ;%0A  

Fix AndroidSchedulers to create an instance using hook only once.