• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.os;
18 
19 import com.android.layoutlib.bridge.android.BridgeContext;
20 import com.android.layoutlib.bridge.impl.RenderAction;
21 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22 
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 
28 /**
29  * Delegate overriding selected methods of android.os.HandlerThread
30  *
31  * Through the layoutlib_create tool, selected methods of Handler have been replaced
32  * by calls to methods of the same name in this delegate class.
33  *
34  *
35  */
36 public class HandlerThread_Delegate {
37 
38     private static final Map<BridgeContext, List<HandlerThread>> sThreads =
39             new HashMap<BridgeContext, List<HandlerThread>>();
40 
cleanUp(BridgeContext context)41     public static void cleanUp(BridgeContext context) {
42         List<HandlerThread> list = sThreads.get(context);
43         if (list != null) {
44             for (HandlerThread thread : list) {
45                 thread.quit();
46             }
47 
48             list.clear();
49             sThreads.remove(context);
50         }
51     }
52 
53     // -------- Delegate methods
54 
55     @LayoutlibDelegate
run(HandlerThread theThread)56     /*package*/ static void run(HandlerThread theThread) {
57         // record the thread so that it can be quit() on clean up.
58         BridgeContext context = RenderAction.getCurrentContext();
59         List<HandlerThread> list = sThreads.get(context);
60         if (list == null) {
61             list = new ArrayList<HandlerThread>();
62             sThreads.put(context, list);
63         }
64 
65         list.add(theThread);
66 
67         // ---- START DEFAULT IMPLEMENTATION.
68 
69         theThread.mTid = Process.myTid();
70         Looper.prepare();
71         synchronized (theThread) {
72             theThread.mLooper = Looper.myLooper();
73             theThread.notifyAll();
74         }
75         Process.setThreadPriority(theThread.mPriority);
76         theThread.onLooperPrepared();
77         Looper.loop();
78         theThread.mTid = -1;
79     }
80 }
81