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.HandlerThread; 20 import android.os.Looper; 21 import android.os.Process; 22 23 import java.util.List; 24 import java.util.concurrent.AbstractExecutorService; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * Extension of {@link AbstractExecutorService} which executed on a provided looper. 29 */ 30 public class LooperExecutor extends AbstractExecutorService { 31 32 private final Handler mHandler; 33 LooperExecutor(Looper looper)34 public LooperExecutor(Looper looper) { 35 mHandler = new Handler(looper); 36 } 37 getHandler()38 public Handler getHandler() { 39 return mHandler; 40 } 41 42 @Override execute(Runnable runnable)43 public void execute(Runnable runnable) { 44 if (mHandler.getLooper() == Looper.myLooper()) { 45 runnable.run(); 46 } else { 47 mHandler.post(runnable); 48 } 49 } 50 51 /** 52 * Same as execute, but never runs the action inline. 53 */ post(Runnable runnable)54 public void post(Runnable runnable) { 55 mHandler.post(runnable); 56 } 57 58 /** 59 * Not supported and throws an exception when used. 60 */ 61 @Override 62 @Deprecated shutdown()63 public void shutdown() { 64 throw new UnsupportedOperationException(); 65 } 66 67 /** 68 * Not supported and throws an exception when used. 69 */ 70 @Override 71 @Deprecated shutdownNow()72 public List<Runnable> shutdownNow() { 73 throw new UnsupportedOperationException(); 74 } 75 76 @Override isShutdown()77 public boolean isShutdown() { 78 return false; 79 } 80 81 @Override isTerminated()82 public boolean isTerminated() { 83 return false; 84 } 85 86 /** 87 * Not supported and throws an exception when used. 88 */ 89 @Override 90 @Deprecated awaitTermination(long l, TimeUnit timeUnit)91 public boolean awaitTermination(long l, TimeUnit timeUnit) { 92 throw new UnsupportedOperationException(); 93 } 94 95 /** 96 * Returns the thread for this executor 97 */ getThread()98 public Thread getThread() { 99 return mHandler.getLooper().getThread(); 100 } 101 102 /** 103 * Returns the looper for this executor 104 */ getLooper()105 public Looper getLooper() { 106 return mHandler.getLooper(); 107 } 108 109 /** 110 * Set the priority of a thread, based on Linux priorities. 111 * @param priority Linux priority level, from -20 for highest scheduling priority 112 * to 19 for lowest scheduling priority. 113 * @see Process#setThreadPriority(int, int) 114 */ setThreadPriority(int priority)115 public void setThreadPriority(int priority) { 116 Process.setThreadPriority(((HandlerThread) getThread()).getThreadId(), priority); 117 } 118 } 119