1 /* 2 * Copyright (C) 2019 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.util; 18 19 import android.os.Handler; 20 import android.os.HandlerThread; 21 22 import java.util.concurrent.CountDownLatch; 23 import java.util.concurrent.Executor; 24 import java.util.concurrent.TimeUnit; 25 26 /** 27 * Thread for asynchronous event processing. This thread is configured as 28 * {@link android.os.Process#THREAD_PRIORITY_BACKGROUND}, which means fewer CPU 29 * resources will be dedicated to it, and it will "have less chance of impacting 30 * the responsiveness of the user interface." 31 * <p> 32 * This thread is best suited for tasks that the user is not actively waiting 33 * for, or for tasks that the user expects to be executed eventually. 34 * 35 * @see ForegroundThread 36 */ 37 public final class BackgroundThread extends HandlerThread { 38 private static BackgroundThread sInstance; 39 private static Handler sHandler; 40 private static HandlerExecutor sHandlerExecutor; 41 BackgroundThread()42 private BackgroundThread() { 43 super("bg", android.os.Process.THREAD_PRIORITY_BACKGROUND); 44 } 45 ensureThreadLocked()46 private static void ensureThreadLocked() { 47 if (sInstance == null) { 48 sInstance = new BackgroundThread(); 49 sInstance.start(); 50 sHandler = new Handler(sInstance.getLooper()); 51 sHandlerExecutor = new HandlerExecutor(sHandler); 52 } 53 } 54 get()55 public static BackgroundThread get() { 56 synchronized (BackgroundThread.class) { 57 ensureThreadLocked(); 58 return sInstance; 59 } 60 } 61 getHandler()62 public static Handler getHandler() { 63 synchronized (BackgroundThread.class) { 64 ensureThreadLocked(); 65 return sHandler; 66 } 67 } 68 getExecutor()69 public static Executor getExecutor() { 70 synchronized (BackgroundThread.class) { 71 ensureThreadLocked(); 72 return sHandlerExecutor; 73 } 74 } 75 waitForIdle()76 public static void waitForIdle() { 77 final CountDownLatch latch = new CountDownLatch(1); 78 getExecutor().execute(() -> { 79 latch.countDown(); 80 }); 81 try { 82 latch.await(30, TimeUnit.SECONDS); 83 } catch (InterruptedException e) { 84 throw new IllegalStateException(e); 85 } 86 } 87 } 88