Home | History | Annotate | Download | only in async
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.camera.async;
     18 
     19 import static com.google.common.base.Preconditions.checkState;
     20 
     21 import android.os.Handler;
     22 import android.os.Looper;
     23 
     24 import com.google.common.annotations.VisibleForTesting;
     25 
     26 import javax.annotation.Nonnull;
     27 
     28 public class MainThread extends HandlerExecutor {
     29     private MainThread(Handler handler) {
     30         super(handler);
     31     }
     32 
     33     public static MainThread create() {
     34         return new MainThread(new Handler(Looper.getMainLooper()));
     35     }
     36 
     37     /**
     38      * Caches whether or not the current thread is the main thread.
     39      */
     40     private static final ThreadLocal<Boolean> sIsMainThread = new ThreadLocal<Boolean>() {
     41         @Override
     42         protected Boolean initialValue() {
     43             return Looper.getMainLooper().getThread() == Thread.currentThread();
     44         }
     45     };
     46 
     47     /**
     48      * Asserts that the current thread is the main thread.
     49      */
     50     public static void checkMainThread() {
     51         checkState(sIsMainThread.get(), "Not main thread.");
     52     }
     53 
     54     /**
     55      * Returns true if the method is run on the main android thread.
     56      */
     57     public static boolean isMainThread() {
     58         return sIsMainThread.get();
     59     }
     60 
     61     /**
     62      * Returns a fake MainThreadExecutor which executes immediately.
     63      */
     64     @VisibleForTesting
     65     public static MainThread createFakeForTesting() {
     66         return new MainThread(null) {
     67             @Override
     68             public void execute(@Nonnull Runnable runnable) {
     69                 //
     70                 sIsMainThread.set(true);
     71                 try {
     72                     runnable.run();
     73                 } finally {
     74                     sIsMainThread.set(false);
     75                 }
     76             }
     77         };
     78     }
     79 }
     80