• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

frameworks/base


Commit MetaInfo

修訂ef4dc6a009752d965a158adf0995c1ad0f1bef91 (tree)
時間2011-06-04 10:38:37
作者Xavier Ducrohet <xav@andr...>
CommiterXavier Ducrohet

Log Message

Cherrypick d1ed33c4 from hc-mr1.

Fix zombie threads in the Layoutlib.

Some widgets can create (indirectly) HandlerThread which are started
but never stopped.

This patch use the delegate methods to override the thread run method
to record which threads are started to be able to call quit() on them
after a render.

Note that we should really fix layoutlib_create to be able to call
the default implementation from a delegate to remove code duplication.

Change-Id: I242f00ffd9d6d3f8f2b0871bf9876d4df6a854d0

Change Summary

差異

--- /dev/null
+++ b/tools/layoutlib/bridge/src/android/os/HandlerThread_Delegate.java
@@ -0,0 +1,80 @@
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 Map<BridgeContext, List<HandlerThread>> sThreads =
39+ new HashMap<BridgeContext, List<HandlerThread>>();
40+
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
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+}
--- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java
+++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderAction.java
@@ -29,6 +29,7 @@ import com.android.layoutlib.bridge.Bridge;
2929 import com.android.layoutlib.bridge.android.BridgeContext;
3030 import com.android.resources.ResourceType;
3131
32+import android.os.HandlerThread_Delegate;
3233 import android.util.DisplayMetrics;
3334
3435 import java.util.concurrent.TimeUnit;
@@ -228,6 +229,10 @@ public abstract class RenderAction<T extends RenderParams> extends FrameworkReso
228229 private void tearDown() {
229230 // Make sure to remove static references, otherwise we could not unload the lib
230231 mContext.disposeResources();
232+
233+ // quit HandlerThread created during this session.
234+ HandlerThread_Delegate.cleanUp(sCurrentContext);
235+
231236 sCurrentContext = null;
232237
233238 Bridge.setLog(null);
--- a/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/tools/layoutlib/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -98,6 +98,7 @@ public final class CreateInfo implements ICreateInfo {
9898 "android.content.res.Resources$Theme#resolveAttribute",
9999 "android.graphics.BitmapFactory#finishDecode",
100100 "android.os.Handler#sendMessageAtTime",
101+ "android.os.HandlerThread#run",
101102 "android.os.Build#getString",
102103 "android.view.LayoutInflater#parseInclude",
103104 "android.view.View#isInEditMode",