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.launcher3;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 
22 import java.util.List;
23 import java.util.concurrent.AbstractExecutorService;
24 import java.util.concurrent.TimeUnit;
25 
26 /**
27  * An executor service that executes its tasks on the main thread.
28  *
29  * Shutting down this executor is not supported.
30  */
31 public class MainThreadExecutor extends AbstractExecutorService {
32 
33     private Handler mHandler = new Handler(Looper.getMainLooper());
34 
35     @Override
execute(Runnable runnable)36     public void execute(Runnable runnable) {
37         if (Looper.getMainLooper() == Looper.myLooper()) {
38             runnable.run();
39         } else {
40             mHandler.post(runnable);
41         }
42     }
43 
44     /**
45      * Not supported and throws an exception when used.
46      */
47     @Override
48     @Deprecated
shutdown()49     public void shutdown() {
50         throw new UnsupportedOperationException();
51     }
52 
53     /**
54      * Not supported and throws an exception when used.
55      */
56     @Override
57     @Deprecated
shutdownNow()58     public List<Runnable> shutdownNow() {
59         throw new UnsupportedOperationException();
60     }
61 
62     @Override
isShutdown()63     public boolean isShutdown() {
64         return false;
65     }
66 
67     @Override
isTerminated()68     public boolean isTerminated() {
69         return false;
70     }
71 
72     /**
73      * Not supported and throws an exception when used.
74      */
75     @Override
76     @Deprecated
awaitTermination(long l, TimeUnit timeUnit)77     public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException {
78         throw new UnsupportedOperationException();
79     }
80 }
81