1ab84fc56114c0963c6f701de9725f5413ab76da9Winson/*
2ab84fc56114c0963c6f701de9725f5413ab76da9Winson * Copyright (C) 2015 The Android Open Source Project
3ab84fc56114c0963c6f701de9725f5413ab76da9Winson *
4ab84fc56114c0963c6f701de9725f5413ab76da9Winson * Licensed under the Apache License, Version 2.0 (the "License");
5ab84fc56114c0963c6f701de9725f5413ab76da9Winson * you may not use this file except in compliance with the License.
6ab84fc56114c0963c6f701de9725f5413ab76da9Winson * You may obtain a copy of the License at
7ab84fc56114c0963c6f701de9725f5413ab76da9Winson *
8ab84fc56114c0963c6f701de9725f5413ab76da9Winson *      http://www.apache.org/licenses/LICENSE-2.0
9ab84fc56114c0963c6f701de9725f5413ab76da9Winson *
10ab84fc56114c0963c6f701de9725f5413ab76da9Winson * Unless required by applicable law or agreed to in writing, software
11ab84fc56114c0963c6f701de9725f5413ab76da9Winson * distributed under the License is distributed on an "AS IS" BASIS,
12ab84fc56114c0963c6f701de9725f5413ab76da9Winson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ab84fc56114c0963c6f701de9725f5413ab76da9Winson * See the License for the specific language governing permissions and
14ab84fc56114c0963c6f701de9725f5413ab76da9Winson * limitations under the License.
15ab84fc56114c0963c6f701de9725f5413ab76da9Winson */
16ab84fc56114c0963c6f701de9725f5413ab76da9Winson
17ab84fc56114c0963c6f701de9725f5413ab76da9Winsonpackage com.android.systemui.recents.misc;
18ab84fc56114c0963c6f701de9725f5413ab76da9Winson
19ab84fc56114c0963c6f701de9725f5413ab76da9Winsonimport android.os.Handler;
20ab84fc56114c0963c6f701de9725f5413ab76da9Winsonimport android.os.HandlerThread;
21ab84fc56114c0963c6f701de9725f5413ab76da9Winson
22ab84fc56114c0963c6f701de9725f5413ab76da9Winson/**
23ab84fc56114c0963c6f701de9725f5413ab76da9Winson * Similar to {@link com.android.internal.os.BackgroundThread}, this is a shared singleton
24ab84fc56114c0963c6f701de9725f5413ab76da9Winson * foreground thread for each process.
25ab84fc56114c0963c6f701de9725f5413ab76da9Winson */
26ab84fc56114c0963c6f701de9725f5413ab76da9Winsonpublic final class ForegroundThread extends HandlerThread {
27ab84fc56114c0963c6f701de9725f5413ab76da9Winson    private static ForegroundThread sInstance;
28ab84fc56114c0963c6f701de9725f5413ab76da9Winson    private static Handler sHandler;
29ab84fc56114c0963c6f701de9725f5413ab76da9Winson
30ab84fc56114c0963c6f701de9725f5413ab76da9Winson    private ForegroundThread() {
3183c1b07a99a6ddd80c30cfc101f3de037a0d215dWinson        super("recents.fg");
32ab84fc56114c0963c6f701de9725f5413ab76da9Winson    }
33ab84fc56114c0963c6f701de9725f5413ab76da9Winson
34ab84fc56114c0963c6f701de9725f5413ab76da9Winson    private static void ensureThreadLocked() {
35ab84fc56114c0963c6f701de9725f5413ab76da9Winson        if (sInstance == null) {
36ab84fc56114c0963c6f701de9725f5413ab76da9Winson            sInstance = new ForegroundThread();
37ab84fc56114c0963c6f701de9725f5413ab76da9Winson            sInstance.start();
38ab84fc56114c0963c6f701de9725f5413ab76da9Winson            sHandler = new Handler(sInstance.getLooper());
39ab84fc56114c0963c6f701de9725f5413ab76da9Winson        }
40ab84fc56114c0963c6f701de9725f5413ab76da9Winson    }
41ab84fc56114c0963c6f701de9725f5413ab76da9Winson
42ab84fc56114c0963c6f701de9725f5413ab76da9Winson    public static ForegroundThread get() {
43ab84fc56114c0963c6f701de9725f5413ab76da9Winson        synchronized (ForegroundThread.class) {
44ab84fc56114c0963c6f701de9725f5413ab76da9Winson            ensureThreadLocked();
45ab84fc56114c0963c6f701de9725f5413ab76da9Winson            return sInstance;
46ab84fc56114c0963c6f701de9725f5413ab76da9Winson        }
47ab84fc56114c0963c6f701de9725f5413ab76da9Winson    }
48ab84fc56114c0963c6f701de9725f5413ab76da9Winson
49ab84fc56114c0963c6f701de9725f5413ab76da9Winson    public static Handler getHandler() {
50ab84fc56114c0963c6f701de9725f5413ab76da9Winson        synchronized (ForegroundThread.class) {
51ab84fc56114c0963c6f701de9725f5413ab76da9Winson            ensureThreadLocked();
52ab84fc56114c0963c6f701de9725f5413ab76da9Winson            return sHandler;
53ab84fc56114c0963c6f701de9725f5413ab76da9Winson        }
54ab84fc56114c0963c6f701de9725f5413ab76da9Winson    }
55ab84fc56114c0963c6f701de9725f5413ab76da9Winson}
56