1 /* 2 * Copyright (C) 2017 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 package com.android.launcher3.util; 17 18 import android.os.Handler; 19 import android.os.Looper; 20 21 import java.util.List; 22 import java.util.concurrent.AbstractExecutorService; 23 import java.util.concurrent.TimeUnit; 24 25 /** 26 * Extension of {@link AbstractExecutorService} which executed on a provided looper. 27 */ 28 public class LooperExecutor extends AbstractExecutorService { 29 30 private final Handler mHandler; 31 32 public LooperExecutor(Looper looper) { 33 mHandler = new Handler(looper); 34 } 35 36 public Handler getHandler() { 37 return mHandler; 38 } 39 40 @Override 41 public void execute(Runnable runnable) { 42 if (mHandler.getLooper() == Looper.myLooper()) { 43 runnable.run(); 44 } else { 45 mHandler.post(runnable); 46 } 47 } 48 49 /** 50 * Not supported and throws an exception when used. 51 */ 52 @Override 53 @Deprecated 54 public void shutdown() { 55 throw new UnsupportedOperationException(); 56 } 57 58 /** 59 * Not supported and throws an exception when used. 60 */ 61 @Override 62 @Deprecated 63 public List<Runnable> shutdownNow() { 64 throw new UnsupportedOperationException(); 65 } 66 67 @Override 68 public boolean isShutdown() { 69 return false; 70 } 71 72 @Override 73 public boolean isTerminated() { 74 return false; 75 } 76 77 /** 78 * Not supported and throws an exception when used. 79 */ 80 @Override 81 @Deprecated 82 public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException { 83 throw new UnsupportedOperationException(); 84 } 85 } 86