1 /* 2 * Copyright (C) 2023 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.providers.media.photopicker; 18 19 import android.os.Handler; 20 import android.os.HandlerThread; 21 22 import com.android.modules.utils.HandlerExecutor; 23 24 import java.util.concurrent.CountDownLatch; 25 import java.util.concurrent.Executor; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Thread for asynchronous event processing. This thread is configured as 30 * {@link android.os.Process#THREAD_PRIORITY_FOREGROUND}, which means more CPU 31 * resources will be dedicated to it, and it will be treated like "a user 32 * interface that the user is interacting with." 33 * <p> 34 * This thread is best suited for UI related tasks that the user is actively waiting for. 35 * (like data loading on grid, banner initialization etc.) 36 * 37 */ 38 public class DataLoaderThread extends HandlerThread { 39 private static DataLoaderThread sInstance; 40 private static Handler sHandler; 41 private static HandlerExecutor sHandlerExecutor; 42 43 // Token for cancelling tasks in handler's queue. Can be used with Handler#postDelayed. 44 public static Object TOKEN = new Object(); 45 DataLoaderThread()46 public DataLoaderThread() { 47 super("DataLoaderThread", android.os.Process.THREAD_PRIORITY_FOREGROUND); 48 } 49 ensureThreadLocked()50 private static void ensureThreadLocked() { 51 if (sInstance == null) { 52 sInstance = new DataLoaderThread(); 53 sInstance.start(); 54 sHandler = new Handler(sInstance.getLooper()); 55 sHandlerExecutor = new HandlerExecutor(sHandler); 56 } 57 } 58 59 /** 60 * Return singleton instance of DataLoaderThread. 61 */ get()62 public static DataLoaderThread get() { 63 synchronized (DataLoaderThread.class) { 64 ensureThreadLocked(); 65 return sInstance; 66 } 67 } 68 69 /** 70 * Return singleton handler of DataLoaderThread. 71 */ getHandler()72 public static Handler getHandler() { 73 synchronized (DataLoaderThread.class) { 74 ensureThreadLocked(); 75 return sHandler; 76 } 77 } 78 79 /** 80 * Return singleton executor of DataLoaderThread. 81 */ getExecutor()82 public static Executor getExecutor() { 83 synchronized (DataLoaderThread.class) { 84 ensureThreadLocked(); 85 return sHandlerExecutor; 86 } 87 } 88 89 /** 90 * Wait for thread to be idle. 91 */ waitForIdle()92 public static void waitForIdle() { 93 final CountDownLatch latch = new CountDownLatch(1); 94 getExecutor().execute(() -> { 95 latch.countDown(); 96 }); 97 try { 98 latch.await(30, TimeUnit.SECONDS); 99 } catch (InterruptedException e) { 100 throw new IllegalStateException(e); 101 } 102 } 103 } 104