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 17 package androidx.arch.core.executor; 18 19 import android.os.Build; 20 import android.os.Handler; 21 import android.os.Looper; 22 23 import androidx.annotation.RequiresApi; 24 import androidx.annotation.RestrictTo; 25 26 import org.jspecify.annotations.NonNull; 27 import org.jspecify.annotations.Nullable; 28 29 import java.lang.reflect.InvocationTargetException; 30 import java.util.concurrent.ExecutorService; 31 import java.util.concurrent.Executors; 32 import java.util.concurrent.ThreadFactory; 33 import java.util.concurrent.atomic.AtomicInteger; 34 35 /** 36 */ 37 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP_PREFIX) 38 public class DefaultTaskExecutor extends TaskExecutor { 39 40 private final Object mLock = new Object(); 41 42 private final ExecutorService mDiskIO = Executors.newFixedThreadPool(4, new ThreadFactory() { 43 private static final String THREAD_NAME_STEM = "arch_disk_io_"; 44 45 private final AtomicInteger mThreadId = new AtomicInteger(0); 46 47 @Override 48 public Thread newThread(Runnable r) { 49 Thread t = new Thread(r); 50 t.setName(THREAD_NAME_STEM + mThreadId.getAndIncrement()); 51 return t; 52 } 53 }); 54 55 private volatile @Nullable Handler mMainHandler; 56 57 @Override executeOnDiskIO(@onNull Runnable runnable)58 public void executeOnDiskIO(@NonNull Runnable runnable) { 59 mDiskIO.execute(runnable); 60 } 61 62 @Override postToMainThread(@onNull Runnable runnable)63 public void postToMainThread(@NonNull Runnable runnable) { 64 if (mMainHandler == null) { 65 synchronized (mLock) { 66 if (mMainHandler == null) { 67 mMainHandler = createAsync(Looper.getMainLooper()); 68 } 69 } 70 } 71 //noinspection ConstantConditions 72 mMainHandler.post(runnable); 73 } 74 75 @Override isMainThread()76 public boolean isMainThread() { 77 return Looper.getMainLooper().getThread() == Thread.currentThread(); 78 } 79 80 @SuppressWarnings("JavaReflectionMemberAccess") createAsync(@onNull Looper looper)81 private static @NonNull Handler createAsync(@NonNull Looper looper) { 82 if (Build.VERSION.SDK_INT >= 28) { 83 return Api28Impl.createAsync(looper); 84 } else { 85 try { 86 // This constructor was added as private in JB MR1: 87 // https://android.googlesource.com/platform/frameworks/base/+/refs/heads/jb-mr1-release/core/java/android/os/Handler.java 88 return Handler.class.getDeclaredConstructor(Looper.class, Handler.Callback.class, 89 boolean.class) 90 .newInstance(looper, null, true); 91 } catch (IllegalAccessException ignored) { 92 } catch (InstantiationException ignored) { 93 } catch (NoSuchMethodException ignored) { 94 } catch (InvocationTargetException e) { 95 return new Handler(looper); 96 } 97 } 98 return new Handler(looper); 99 } 100 101 @RequiresApi(28) 102 private static class Api28Impl { Api28Impl()103 private Api28Impl() { 104 // Non-instantiable. 105 } 106 createAsync(@onNull Looper looper)107 public static @NonNull Handler createAsync(@NonNull Looper looper) { 108 return Handler.createAsync(looper); 109 } 110 } 111 } 112