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 LooperExecuter extends AbstractExecutorService { 29 30 private final Handler mHandler; 31 LooperExecuter(Looper looper)32 public LooperExecuter(Looper looper) { 33 mHandler = new Handler(looper); 34 } 35 36 @Override execute(Runnable runnable)37 public void execute(Runnable runnable) { 38 if (mHandler.getLooper() == Looper.myLooper()) { 39 runnable.run(); 40 } else { 41 mHandler.post(runnable); 42 } 43 } 44 45 /** 46 * Not supported and throws an exception when used. 47 */ 48 @Override 49 @Deprecated shutdown()50 public void shutdown() { 51 throw new UnsupportedOperationException(); 52 } 53 54 /** 55 * Not supported and throws an exception when used. 56 */ 57 @Override 58 @Deprecated shutdownNow()59 public List<Runnable> shutdownNow() { 60 throw new UnsupportedOperationException(); 61 } 62 63 @Override isShutdown()64 public boolean isShutdown() { 65 return false; 66 } 67 68 @Override isTerminated()69 public boolean isTerminated() { 70 return false; 71 } 72 73 /** 74 * Not supported and throws an exception when used. 75 */ 76 @Override 77 @Deprecated awaitTermination(long l, TimeUnit timeUnit)78 public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedException { 79 throw new UnsupportedOperationException(); 80 } 81 } 82