blob: fad743eafdaaccda6f2d24a29b0f6b4839f1a51a [file] [log] [blame]
Jorim Jaggied7993b2017-03-28 18:50:01 +01001/*
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
17package com.android.server;
18
19import static android.os.Process.THREAD_PRIORITY_DISPLAY;
20
21import android.os.Handler;
22import android.os.Trace;
23
Garfield Tan92b93652019-08-07 17:22:35 -070024import com.android.internal.annotations.VisibleForTesting;
25
Jorim Jaggied7993b2017-03-28 18:50:01 +010026/**
Jorim Jaggi21c39a72017-10-20 15:47:51 +020027 * Thread for handling all legacy window animations, or anything that's directly impacting
28 * animations like starting windows or traversals.
Jorim Jaggied7993b2017-03-28 18:50:01 +010029 */
30public final class AnimationThread extends ServiceThread {
31 private static AnimationThread sInstance;
32 private static Handler sHandler;
33
34 private AnimationThread() {
35 super("android.anim", THREAD_PRIORITY_DISPLAY, false /*allowIo*/);
36 }
37
38 private static void ensureThreadLocked() {
39 if (sInstance == null) {
40 sInstance = new AnimationThread();
41 sInstance.start();
42 sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_WINDOW_MANAGER);
43 sHandler = new Handler(sInstance.getLooper());
44 }
45 }
46
47 public static AnimationThread get() {
48 synchronized (AnimationThread.class) {
49 ensureThreadLocked();
50 return sInstance;
51 }
52 }
53
54 public static Handler getHandler() {
55 synchronized (AnimationThread.class) {
56 ensureThreadLocked();
57 return sHandler;
58 }
59 }
Garfield Tan92b93652019-08-07 17:22:35 -070060
61 /**
62 * Disposes current animation thread if it's initialized. Should only be used in tests to set up
63 * a new environment.
64 */
65 @VisibleForTesting
66 public static void dispose() {
Riddle Hsu73f53572019-09-23 23:13:01 +080067 synchronized (AnimationThread.class) {
Garfield Tan92b93652019-08-07 17:22:35 -070068 if (sInstance == null) {
69 return;
70 }
71
72 getHandler().runWithScissors(() -> sInstance.quit(), 0 /* timeout */);
73 sInstance = null;
74 }
75 }
Jorim Jaggied7993b2017-03-28 18:50:01 +010076}