1 /*
2  * Copyright (C) 2020 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 android.view;
18 
19 import android.os.Handler;
20 import android.os.HandlerThread;
21 import android.os.Trace;
22 
23 /**
24  * Thread to be used for inset animations to be running off the main thread.
25  * @hide
26  */
27 public class InsetsAnimationThread extends HandlerThread {
28 
29     private static InsetsAnimationThread sInstance;
30     private static Handler sHandler;
31 
InsetsAnimationThread()32     private InsetsAnimationThread() {
33         // TODO: Should this use higher priority?
34         super("InsetsAnimations");
35     }
36 
ensureThreadLocked()37     private static void ensureThreadLocked() {
38         if (sInstance == null) {
39             sInstance = new InsetsAnimationThread();
40             sInstance.start();
41             sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_VIEW);
42             sHandler = new Handler(sInstance.getLooper());
43         }
44     }
45 
release()46     public static void release() {
47         synchronized (InsetsAnimationThread.class) {
48             if (sInstance == null) {
49                 return;
50             }
51             sInstance.getLooper().quitSafely();
52             sInstance = null;
53             sHandler = null;
54         }
55     }
56 
get()57     public static InsetsAnimationThread get() {
58         synchronized (InsetsAnimationThread.class) {
59             ensureThreadLocked();
60             return sInstance;
61         }
62     }
63 
getHandler()64     public static Handler getHandler() {
65         synchronized (InsetsAnimationThread.class) {
66             ensureThreadLocked();
67             return sHandler;
68         }
69     }
70 }
71