Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 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 com.android.server; |
| 18 | |
| 19 | import static android.os.Process.THREAD_PRIORITY_DISPLAY; |
| 20 | |
| 21 | import android.os.Handler; |
| 22 | import android.os.Trace; |
| 23 | |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 26 | /** |
Jorim Jaggi | 21c39a7 | 2017-10-20 15:47:51 +0200 | [diff] [blame] | 27 | * Thread for handling all legacy window animations, or anything that's directly impacting |
| 28 | * animations like starting windows or traversals. |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 29 | */ |
| 30 | public 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 Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 60 | |
| 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 Hsu | 73f5357 | 2019-09-23 23:13:01 +0800 | [diff] [blame] | 67 | synchronized (AnimationThread.class) { |
Garfield Tan | 92b9365 | 2019-08-07 17:22:35 -0700 | [diff] [blame] | 68 | if (sInstance == null) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | getHandler().runWithScissors(() -> sInstance.quit(), 0 /* timeout */); |
| 73 | sInstance = null; |
| 74 | } |
| 75 | } |
Jorim Jaggi | ed7993b | 2017-03-28 18:50:01 +0100 | [diff] [blame] | 76 | } |