1438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/*
2438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project
3438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
4438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License");
5438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * you may not use this file except in compliance with the License.
6438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * You may obtain a copy of the License at
7438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
8438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *      http://www.apache.org/licenses/LICENSE-2.0
9438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
10438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software
11438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS,
12438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * See the License for the specific language governing permissions and
14438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * limitations under the License.
15438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
16438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpackage android.os;
17438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
18599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrickimport android.animation.ValueAnimator;
19438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ActivityManagerNative;
20bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.app.ActivityThread;
21438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ApplicationErrorReport;
221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrickimport android.app.IActivityManager;
23bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent;
24438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log;
25cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer;
26cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton;
276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager;
28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
29438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit;
30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
31438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard;
32fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard;
33bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrickimport dalvik.system.VMDebug;
34438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter;
365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter;
375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList;
3846d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap;
395f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map;
40bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger;
4146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
42438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/**
4332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be
4432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix
4532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them.
4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or
4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI
4915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place.  Keeping disk
5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother,
519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications.  By keeping your application's main thread
529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent
539fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a>
549fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users.
5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is
5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that
5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency.  It's often the case that
5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be
6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background
6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes.  If possible, it's best to assume that such
6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p>
6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your
6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or
6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's
6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method:
6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre>
7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() {
7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     if (DEVELOPER_MODE) {
7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}()
7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskReads()
7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskWrites()
7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectNetwork()   // or .detectAll() for all detectable problems
7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
7962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick *                 .detectLeakedSqlLiteObjects()
80fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom *                 .detectLeakedClosableObjects()
8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyDeath()
8332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     }
8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     super.onCreate();
8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * }
8715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre>
8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected.
9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can
9132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your
9232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen.
9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are
9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler},
9615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc.
9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds.  In particular,
9832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle.  Use
9932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident.  Network requests on the UI thread
10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though.
10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not
10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses.  While it does
10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing
10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best
10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism.  Notably, disk or network access from JNI calls
10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it.  Future versions of Android may catch
10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode
1094d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Dougherty * enabled in applications distributed on Google Play.
110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode {
112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static final String TAG = "StrictMode";
11382829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
1166804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE);
1171181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
118c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    /**
119bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * Boolean system property to disable strict mode checks outright.
120bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * Set this to 'true' to force disable; 'false' has no effect on other
121bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * enable/disable policy.
122bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * @hide
123bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     */
124bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate    public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable";
125bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate
126bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate    /**
127c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * The boolean system property to control screen flashes on violations.
128c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     *
129c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * @hide
130c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     */
131c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual";
132c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
13346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only log a duplicate stack trace to the logs every second.
13446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_LOG_INTERVAL_MS = 1000;
13546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
13646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only show an annoying dialog at most every 30 seconds
13746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_DIALOG_INTERVAL_MS = 30000;
13846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
139e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    // How many Span tags (e.g. animations) to report.
140e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final int MAX_SPAN_TAGS = 20;
141e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
142191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // How many offending stacks to keep track of (and time) per loop
143191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // of the Looper.
144191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final int MAX_OFFENSES_PER_LOOP = 10;
145191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
14632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Thread-policy:
147438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
14815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_WRITE = 0x01;  // for ThreadPolicy
15215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
15315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick      * @hide
15515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_READ = 0x02;  // for ThreadPolicy
15715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
15815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
16015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
16132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_NETWORK = 0x04;  // for ThreadPolicy
162438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
163e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
164e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For StrictMode.noteSlowCall()
165e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
166e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @hide
167e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
168e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static final int DETECT_CUSTOM = 0x08;  // for ThreadPolicy
169e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
170e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    private static final int ALL_THREAD_DETECT_BITS =
171e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM;
172e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
17332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Process-policy:
174438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
175438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
17632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Note, a "VM_" bit, not thread.
17732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
17832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
179758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for VmPolicy
18032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
18132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
182fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * Note, a "VM_" bit, not thread.
183fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
184fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
185758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for VmPolicy
186758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
187758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
188758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * Note, a "VM_" bit, not thread.
189758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
190758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
191758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800;  // for VmPolicy
192fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
193fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
19432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
195438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
196bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000;  // for VmPolicy
197bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
198bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int ALL_VM_DETECT_BITS =
199bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS |
200bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS;
201bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
202bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
203bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
204bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
205438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
206438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
20732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
20832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
209438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
21032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
211438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
212438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
213438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
214438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
215b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
216b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
21732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
218438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
219438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
220438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
221438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
222b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
223b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
224b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
225b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
226b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
227b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
228b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
2296804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
2306804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
2316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
2326804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
2336804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
2346804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2356804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
237438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
238438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
239438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
240727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
241727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
242727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
243727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
244727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
245727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
246727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
247703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
248703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
249727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
250727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
251727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
252727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
254c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for thread policies.
25571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
256c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int THREAD_PENALTY_MASK =
257b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2586804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
25971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
260758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
261c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    /**
262c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for VM policies.
263c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     */
264c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int VM_PENALTY_MASK =
265c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick            PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX;
266c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
267c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
268758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // TODO: wrap in some ImmutableHashMap thing.
269758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // Note: must be before static initialization of sVmPolicy.
270758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>();
271758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
27271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
274758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     *
275758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * TODO: these are redundant (mask is in VmPolicy).  Should remove sVmPolicyMask.
27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
278758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
280bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
281bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
282bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
283bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
284bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
285bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
317320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * Creates {@link ThreadPolicy} instances.  Methods whose names start
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
328320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
332320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.setThreadPolicy(policy);
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
362e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(ALL_THREAD_DETECT_BITS);
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
369e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return disable(ALL_THREAD_DETECT_BITS);
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
401e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
402e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
403e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder detectCustomSlowCalls() {
404e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
405e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
406e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
407e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
408e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
409e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
410e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder permitCustomSlowCalls() {
411e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
412e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
413e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
414e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
440b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
441b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
442b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
443b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
450b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
451b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
452b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
453b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
454b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
455b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
456b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
457b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
458b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
459b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
460b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
461b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
4626804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
4636804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4646804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4656804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4666804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4676804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4686804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
524758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP);
52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
528758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        // Map from class to max number of allowed instances in memory.
529758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        final HashMap<Class, Integer> classInstanceLimit;
530758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
531758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) {
532758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            if (classInstanceLimit == null) {
533758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                throw new NullPointerException("classInstanceLimit == null");
534758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
536758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            this.classInstanceLimit = classInstanceLimit;
53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
566758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private HashMap<Class, Integer> mClassInstanceLimit;  // null until needed
567758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private boolean mClassInstanceLimitNeedCow = false;  // need copy-on-write
568758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
569758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder() {
570758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = 0;
571758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
572758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
573758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
574758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Build upon an existing VmPolicy.
575758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
576758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder(VmPolicy base) {
577758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = base.mask;
578758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimitNeedCow = true;
579758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit = base.classInstanceLimit;
580758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Set an upper bound on how many instances of a class can be in memory
584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * at once.  Helps to prevent object leaks.
585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder setClassInstanceLimit(Class klass, int instanceLimit) {
587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (klass == null) {
588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    throw new NullPointerException("klass == null");
589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (mClassInstanceLimitNeedCow) {
591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    if (mClassInstanceLimit.containsKey(klass) &&
592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit.get(klass) == instanceLimit) {
593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        // no-op; don't break COW
594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        return this;
595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    }
596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimitNeedCow = false;
597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone();
598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                } else if (mClassInstanceLimit == null) {
599758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = new HashMap<Class, Integer>();
600758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
601bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                mMask |= DETECT_VM_INSTANCE_LEAKS;
602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit.put(klass, instanceLimit);
603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return this;
604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
606bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            /**
607bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * Detect leaks of {@link android.app.Activity} subclasses.
608bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             */
609bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            public Builder detectActivityLeaks() {
610758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS);
611758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
612758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
61332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
61432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
61532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
616fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
617bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * SQLite cursors, Activities, and other closable objects
618bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * but will likely expand in future releases.
61932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
62032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
621758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS |
622758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS);
62332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
62432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
62532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
62632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
62732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
63032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
63132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
63232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
63332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
63432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
63532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
63632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
63732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
63832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
639fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
640fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
641fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
642fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
643fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
644fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
645fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
646fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
647fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
648fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
649fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
650fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
65632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
65732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
66032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
66132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
66432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
66532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
66632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
66732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
66832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
696758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return new VmPolicy(mMask,
697758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP);
69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
701438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
702438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
7035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
7045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
7055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
7065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
7075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
708cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
709cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
710cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
711703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
712703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
713703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
714703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
7155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
7175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
72032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
72115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
72315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
72415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
72515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
726438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
728438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
73032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
73132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
73232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
73332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
734727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
735727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
736727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
737727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
738727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
739727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
740727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
741727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
742727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
743727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
744727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
745727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
746727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
74746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
74846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
74946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
75046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
751438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
752438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
753438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
754438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
755438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
756438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
757438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
758438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
759438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
7604b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
7614b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
7627c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
7634b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
7644b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
7654b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
7664b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
7674b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
7684e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7694e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7704e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7714e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException {
7724e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        public StrictModeViolation(int policyState, int policyViolated, String message) {
7734e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyState, policyViolated, message);
7744e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
7754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
7764e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
7774e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7804e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeNetworkViolation extends StrictModeViolation {
7815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
7824e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_NETWORK, null);
7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7864e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7874e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends StrictModeViolation {
7905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
7914e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ, null);
7925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7954e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     /**
7964e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7974e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7984e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick   private static class StrictModeDiskWriteViolation extends StrictModeViolation {
7995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
8004e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE, null);
8015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8044e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8054e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeCustomViolation extends StrictModeViolation {
808e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        public StrictModeCustomViolation(int policyMask, String name) {
809e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            super(policyMask, DETECT_CUSTOM, name);
810e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
811e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
812e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
813438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
81415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
815438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
81632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
81732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
81832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
819438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
82032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
821438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
822438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
823438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
82532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
82632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
82732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
828e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // TODO: this was a last minute Gingerbread API change (to
829e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // introduce VmPolicy cleanly) but this isn't particularly
830e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // optimal for users who might call this method often.  This
831e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // should be in a thread-local and not allocate on each call.
83232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
83332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
83432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
83532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
83632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
83732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
83832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
83932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
84032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
84197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
84232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
84332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
84497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
84532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
84632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
84732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
84832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
84932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
85097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
85132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
85297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
85397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
85497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
85532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
85632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
85732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
85832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
85932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
86097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
86132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
86297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
86397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
86432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
86532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
86632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
86732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
86832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
86997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
87032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
87197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
87297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
873f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // We don't want to flash the screen red in the system server
874f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // process, nor do we want to modify all the call sites of
875f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // conditionallyEnableDebugLogging() in the system server,
876f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // so instead we use this to determine if we are the system server.
877f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    private static boolean amTheSystemServerProcess() {
878f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // Fast path.  Most apps don't have the system server's UID.
879f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        if (Process.myUid() != Process.SYSTEM_UID) {
880f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            return false;
881f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
882f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
883f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // The settings app, though, has the system server's UID so
884f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // look up our stack to see if we came from the system server.
885f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        Throwable stack = new Throwable();
886f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        stack.fillInStackTrace();
887f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        for (StackTraceElement ste : stack.getStackTrace()) {
888f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            String clsName = ste.getClassName();
889f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            if (clsName != null && clsName.startsWith("com.android.server.")) {
890f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                return true;
891f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            }
892f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
893f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        return false;
894f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    }
895f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
89697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
89750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
89850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
89950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
90050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
90150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
902bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, false)
903bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate                && !amTheSystemServerProcess();
904bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        final boolean suppress = SystemProperties.getBoolean(DISABLE_PROPERTY, false);
905c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
90650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
90750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
908bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (!doFlashes && (IS_USER_BUILD || suppress)) {
9097c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
91050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
91150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
912c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
913bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // Eng builds have flashes on all the time.  The suppression property
914bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // overrides this, so we force the behavior only after the short-circuit
915bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // check above.
916bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (IS_ENG_BUILD) {
917bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate            doFlashes = true;
918bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        }
919bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate
920be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // Thread policy controls BlockGuard.
921c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
922c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
923c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
924c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
925c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
926c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
927c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
928c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
929c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
930c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
931c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
932c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
933c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
934be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // VM Policy controls CloseGuard, detection of Activity leaks,
935be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // and instance counting.
936c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
937c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
938c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
939d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox();
940d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
941d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                policyBuilder.penaltyLog();
942d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
943d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            setVmPolicy(policyBuilder.build());
944c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
945c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
94650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
94750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
94850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
94950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
950b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
951b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
952b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
953b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
954b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
955b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
956b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
957b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
958b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
959b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
960b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
961b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
9625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
9635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
9645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
9655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
966e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * Input is of the following forms:
967e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64"
968e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64 msg=Arbitrary text"
9695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
9705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
9715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
9725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
9735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
9755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
9765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
9795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
9805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
9845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
9855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
9865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
9895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
9905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
9915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
9925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
9945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
9955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
9985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
9995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1001e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberStartIndex = violationIndex + "violation=".length();
1002e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberEndIndex = message.indexOf(' ', numberStartIndex);
1003e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (numberEndIndex == -1) {
1004e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            numberEndIndex = message.length();
1005e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1006e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        String violationString = message.substring(numberStartIndex, numberEndIndex);
10075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
10085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
10095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
10105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
10135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1014191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
1015191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
1016191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
1017191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
1018191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
1019191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
1020191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1021bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    // Note: only access this once verifying the thread has a Looper.
1022bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() {
1023bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        @Override protected Handler initialValue() {
1024bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            return new Handler();
1025bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        }
1026bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    };
1027bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1028191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
1029191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
1030191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
1031191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1032438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
1033438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
103446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
103546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
103646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
103746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
103846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
1039438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1040438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
1041438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1042438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1043438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
10455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
10465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
10475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1049438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1050438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
1051438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
1052438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1053438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1054438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1055438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
105632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
1057438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1058438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1059191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1060191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1061191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
1063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1065438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1066438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1067e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // Not part of BlockGuard.Policy; just part of StrictMode:
1068e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        void onCustomSlowCall(String name) {
1069e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if ((mPolicyMask & DETECT_CUSTOM) == 0) {
1070e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1071e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1072e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1073e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1074e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1075e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name);
1076e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            e.fillInStackTrace();
1077e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            startHandlingViolationException(e);
1078e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1079e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
1080438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1081438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
108232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
1083438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1084438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1085191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1086191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1087191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1088cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
1089cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1091438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1092438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1093438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1094438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
109532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
1096438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1097438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1098b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
1099b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
1100b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
1101191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1102191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1103191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1104cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
1105cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1107438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
1110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
11135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
11145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
11155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
11165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
11175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
11185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
1119cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
1120cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
1121cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
1122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1123438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
1125cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
1126cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
1127cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
1128cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
1129cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
1130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
1131cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1132cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
1133cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
1134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
1135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
1136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
1137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
1138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
1139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
11404e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
11414e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // Also, as a special short-cut case when the only penalty
11424e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // bit is death, we die immediately, rather than timing
11434e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // the violation's duration.  This makes it convenient to
11444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // use in unit tests too, rather than waiting on a Looper.
11454e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
1146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
1147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
11484e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            if (looper == null ||
1149c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) {
1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
1152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1153438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
1156191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
1157cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
1158cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1159cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1160cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
1161cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
1162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
1163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
1164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
1165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
11686804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
1169cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
11706804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
11716804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
11726804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
11736804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
11746804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
11756804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
11766804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
1177bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // We post a runnable to a Handler (== delay 0 ms) for
1178bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // measuring the end time of a violation instead of using
1179bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // an IdleHandler (as was previously used) because an
1180bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // IdleHandler may not run for quite a long period of time
1181bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // if an ongoing animation is happening and continually
1182bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // posting ASAP (0 ms) animation steps.  Animations are
1183bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // throttled back to 60fps via SurfaceFlinger/View
1184bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // invalidates, _not_ by posting frame updates every 16
1185bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // milliseconds.
1186bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            threadHandler.get().post(new Runnable() {
1187bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                    public void run() {
1188cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
1189bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1190bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // Note: we do this early, before handling the
1191bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // violation below, as handling the violation
1192bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // may include PENALTY_DEATH and we don't want
1193bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // to keep the red border on.
1194bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        if (windowManager != null) {
1195bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            try {
1196bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                                windowManager.showStrictModeViolation(false);
1197bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            } catch (RemoteException unused) {
1198bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            }
1199bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        }
1200bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1201cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
1202cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
1205cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
1206cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
1207cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
1208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
1209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
1210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
12115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1212438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
12135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
12145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
12155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
12165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
12175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
1218cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
1219cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
1220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
12215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
12225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
1223438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1224cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
122546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1226cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
1227cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
1228703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
1229cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
1230703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
1231703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
12325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
12335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
12345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1235cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1236cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
12375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
12385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
12395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
12405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1241cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1242727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1243727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1244727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
124546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1246f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
124746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
124846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
124946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
125046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
125146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
125246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
125346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
125446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
125546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1256cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
125746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1258cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
12595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1260cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1261438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1262cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1263438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1264438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1265438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
126671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
126746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
126846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
126946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1270cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
127146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1272cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
127346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1274cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
127546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
127646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1277cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1278cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
127946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
128046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1281cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1282cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1283cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
128432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
128571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
1286c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX;
128771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
128871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
128971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
129071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
129171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
129271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
129371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1294bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
129571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
129671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
129771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
129871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1299438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1300727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1301727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1302727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1303727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
130432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1305727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1306438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1307438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1308cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1309cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1310438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
131146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1312727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1313727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
131432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1315438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1316438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1317438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1318cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
13194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick                executeDeathPenalty(info);
1320438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1321438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1322438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1323727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
13244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static void executeDeathPenalty(ViolationInfo info) {
13254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
13264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        throw new StrictModeViolation(info.policy, violationBit, null);
13274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
13284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
1329bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1330bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1331bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1332bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1333bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1334bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1335bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1336bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1337bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1338bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1339bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1340bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1341bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1342bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1343bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1344bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1345bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1346bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1347bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1348bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1349bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1350bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1351bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1352bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
13531065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
13541065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
13551065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
13561065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
13571065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
13581065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
13591065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
13601065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
13611065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1362bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1363bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1364bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1365bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1366bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1367bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1368bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1369bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1370bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
13714b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
13724b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
13734b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
13744b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
13754b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
13764b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1377727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
13785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
13795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
13805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1381703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1382703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1383703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1384703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1385703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1386703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1387703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1388703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1389703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1390703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
13915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
13925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
13935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1394bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
1395bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
1396bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    public static void conditionallyCheckInstanceCounts() {
1397bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        VmPolicy policy = getVmPolicy();
1398bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (policy.classInstanceLimit.size() == 0) {
1399bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            return;
1400bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1401bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        Runtime.getRuntime().gc();
1402bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        // Note: classInstanceLimit is immutable, so this is lock-free
14035f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) {
14045f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Class klass = entry.getKey();
14055f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            int limit = entry.getValue();
1406bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            long instances = VMDebug.countInstancesOfClass(klass, false);
1407bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (instances <= limit) {
1408bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                continue;
1409bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1410bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Throwable tr = new InstanceCountViolation(klass, instances, limit);
1411bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            onVmPolicyViolation(tr.getMessage(), tr);
1412bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1413bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
1414bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1415bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static long sLastInstanceCountCheckMillis = 0;
14165f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static boolean sIsIdlerRegistered = false;  // guarded by StrictMode.class
1417bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final MessageQueue.IdleHandler sProcessIdleHandler =
1418bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            new MessageQueue.IdleHandler() {
1419bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                public boolean queueIdle() {
1420bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    long now = SystemClock.uptimeMillis();
1421bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    if (now - sLastInstanceCountCheckMillis > 30 * 1000) {
1422bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        sLastInstanceCountCheckMillis = now;
1423bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        conditionallyCheckInstanceCounts();
1424bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    }
1425bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    return true;
1426bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1427bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            };
1428bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1429bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
143032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
143132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
143232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
143332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
143432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
143532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
143632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
14375f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14385f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicy = policy;
14395f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicyMask = policy.mask;
14405f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
14415f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
14425f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Looper looper = Looper.getMainLooper();
14435f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (looper != null) {
14445f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                MessageQueue mq = looper.mQueue;
1445c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                if (policy.classInstanceLimit.size() == 0 ||
1446c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    (sVmPolicyMask & VM_PENALTY_MASK) == 0) {
1447bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.removeIdleHandler(sProcessIdleHandler);
1448c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    sIsIdlerRegistered = false;
1449bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                } else if (!sIsIdlerRegistered) {
1450bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.addIdleHandler(sProcessIdleHandler);
1451bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    sIsIdlerRegistered = true;
1452bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1453bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1454bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
145532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
145632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
145732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
145832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
145932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
146032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
14615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14625f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            return sVmPolicy;
14635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
146432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
146532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
146632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
146762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
146862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
146962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1470fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1471fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1472fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
147362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
147462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
147562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
147662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
147762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
147862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
147962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
1480e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                               .detectAll()
148162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
148262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
148362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
148462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
148562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
148632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
148732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
148832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
148932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
149032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
149132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
149232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
149332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
149432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1495fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1496fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1497fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1498fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1499fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1500fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1501fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
150232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
15034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
15044b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
15054b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
150608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    /**
150708d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     * @hide
150808d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     */
150908d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) {
151008d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block        onVmPolicyViolation(null, originStack);
151108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    }
151208d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block
1513bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Map from VM violation fingerprint to uptime millis.
1514bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>();
1515bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15164b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
15174b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
15184b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
15194b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
1520bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1521bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1522bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0;
1523bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1524bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15255f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        // Erase stuff not relevant for process-wide violations
15265f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.numAnimationsRunning = 0;
15275f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.tags = null;
15285f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.broadcastIntentAction = null;
15295f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
1530bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Integer fingerprint = info.hashCode();
1531bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long now = SystemClock.uptimeMillis();
1532bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long lastViolationTime = 0;
1533bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long timeSinceLastViolationMillis = Long.MAX_VALUE;
1534bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        synchronized (sLastVmViolationTime) {
1535bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (sLastVmViolationTime.containsKey(fingerprint)) {
1536bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                lastViolationTime = sLastVmViolationTime.get(fingerprint);
1537bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                timeSinceLastViolationMillis = now - lastViolationTime;
1538bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1539bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1540bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                sLastVmViolationTime.put(fingerprint, now);
1541bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
154232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
154332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1544bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1545bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Log.e(TAG, message, originStack);
1546bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1547bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1548bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask);
1549bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1550bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1551bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1552bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1553bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1554bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1555bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1556bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
155732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1558bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyDropbox && lastViolationTime == 0) {
155932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
156032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
156132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
156232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
156332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
156432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
156532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
156632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
156732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
156832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
156932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
157032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
157132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
157232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
157332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
157432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
157532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
157632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
157732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
157832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
157932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
158032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
158132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
158232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1583bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
158432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
158532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
158632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
158732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
158832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
158932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
159032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
15915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
15925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
15935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1594cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1595703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1596703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1597703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1598703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1599703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1600703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1601703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1602703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1603703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
16045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1605703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
16065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
16095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
16115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
16125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
16135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
16145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
16155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
16165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1617cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
16185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
16195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
162032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1621cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
16225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
16245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
16255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1626cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1627cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
16285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
16295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1630cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
16315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
16325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
16335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1636727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1637727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1638727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1639727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1640727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1641727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1642727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1643727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1644727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1645727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1646cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1647cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1648e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1649e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1650e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1651e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1652e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1653e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1654e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1655e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1656e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1657e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1658e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1659e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1660e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1661e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1662e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1663e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1664e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1665e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
16661181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
16671181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
16681181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
16691181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
16701181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1671e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1672e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1673e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1674e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1675e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1676e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1677e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1678e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1679e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1680e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1681e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1682e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1683e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1684e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1685e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1686e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1687e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1688e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1689e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1690e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1691e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1692e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1693e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1694e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1695e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1696e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1697e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1698e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1699e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17001cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
17011cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
17021cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
17031cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1704e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1705e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1706e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1707e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1708e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1709e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1710e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1711e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1712e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1713e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1714e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1715e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1716e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1717e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1718e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1719e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17201181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
17211181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
17221181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
17231181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
17241181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
17251181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
17261181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1727e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1728e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1729e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1730e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1731e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1732e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1733e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1736e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1737e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1738e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1739e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1740e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1741e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1742e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1752cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1753cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1754cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1755cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1756cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1757cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1759e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1760e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1761e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1762e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1763e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
17741181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
17751181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
17761181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1789e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1790e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1791e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1792e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1793e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1794e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1795e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1796e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1797e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1798e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1799e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
18001cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1801e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1802e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1803e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1804e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1805e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
1806e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For code to note that it's slow.  This is a no-op unless the
1807e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * current thread's {@link android.os.StrictMode.ThreadPolicy} has
1808e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls}
1809e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * enabled.
1810e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
1811e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @param name a short string for the exception stack trace that's
1812e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *             built if when this fires.
1813e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
1814e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static void noteSlowCall(String name) {
1815e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
1816e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
1817e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            // StrictMode not enabled.
1818e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            return;
1819e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1820e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name);
1821e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
1822e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1823e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
18244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskRead() {
18274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18314e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onReadFromDisk();
18334e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18344e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18354e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18364e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18374e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18384e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskWrite() {
18394e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18404e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18414e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18424e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18434e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onWriteToDisk();
18454e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18464e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18475f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    // Guarded by StrictMode.class
18485f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static final HashMap<Class, Integer> sExpectedActivityInstanceCount =
18495f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new HashMap<Class, Integer>();
18505f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18514e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * Returns an object that is used to track instances of activites.
18537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * The activity should store a reference to the tracker object in one of its fields.
18547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * @hide
18557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     */
18567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    public static Object trackActivity(Object instance) {
18577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        return new InstanceTracker(instance);
18587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
18597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    /**
1861758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
1862758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
18635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void incrementExpectedActivityCount(Class klass) {
18647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1865758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1866758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18685f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18735f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer newExpected = expected == null ? 1 : expected + 1;
18755f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sExpectedActivityInstanceCount.put(klass, newExpected);
18765f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
18775f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    }
18785f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18795f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    /**
18805f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     * @hide
18815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     */
18825f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void decrementExpectedActivityCount(Class klass) {
18837e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1884758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1885758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18867e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        final int limit;
18885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18897e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18907e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18935f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
18955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (newExpected == 0) {
18965f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.remove(klass);
18975f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            } else {
18985f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.put(klass, newExpected);
18995f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            }
19007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19015f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // Note: adding 1 here to give some breathing room during
19025f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // orientation changes.  (shouldn't be necessary, though?)
19037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            limit = newExpected + 1;
19045f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
19055f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Quick check.
19077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        int actual = InstanceTracker.getInstanceCount(klass);
19087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (actual <= limit) {
19097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            return;
19107e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
19117e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19127e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Do a GC and explicit count to double-check.
19137e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // This is the work that we are trying to avoid by tracking the object instances
19147e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // explicity.  Running an explicit GC can be expensive (80ms) and so can walking
19157e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // the heap to count instance (30ms).  This extra work can make the system feel
19167e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // noticeably less responsive during orientation changes when activities are
19177e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // being restarted.  Granted, it is only a problem when StrictMode is enabled
19187e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // but it is annoying.
19197e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        Runtime.getRuntime().gc();
19207e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19217e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        long instances = VMDebug.countInstancesOfClass(klass, false);
19227e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (instances > limit) {
19237e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            Throwable tr = new InstanceCountViolation(klass, instances, limit);
19247e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            onVmPolicyViolation(tr.getMessage(), tr);
19255f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
1926758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    }
1927758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
1928758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
1929cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
1930cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
1931cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
1932cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
1933cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
1934cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
1935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
1937cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1938cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
1939cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1940cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1941cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
1942cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1943cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
1944cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1946cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
1947cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
1948cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1949cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
1950cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1951cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1952599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
1953599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
1954599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
1955599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1956599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
1957e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
1958e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
1959e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1960e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
1961e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1962e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1963cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
1964cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
1965cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
1966cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
1967cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1968cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
1969cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1970cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
1972cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
1973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
1975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1976cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1977bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
1978bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
1979bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
1980bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
1981bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
1982bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
1983bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * If this is a instance count violation, the number of instances in memory,
1984bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * else -1.
1985bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         */
1986bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public long numInstances = -1;
1987bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1988bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        /**
1989cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
1990cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1991cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
1992cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
1993cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
1994cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1995cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1996cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1997cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
1998cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1999cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
2000cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
2001cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
2002cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
2003599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
2004bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
2005bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
2006bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
2007bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2008e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
2009bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (tr instanceof InstanceCountViolation) {
2010bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                this.numInstances = ((InstanceCountViolation) tr).mInstances;
2011bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2012e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
2013e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
2014e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
2015e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
2016e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2017e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
2018e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
2019e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
2020e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
2021e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
2022e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
2023e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
2024e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
2025e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
2026e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2027e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2029cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2030f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
2031f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
2032f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
2033f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
2034f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2035f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
2036f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2037f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
2038f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
2039f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2040f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
2041f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
2042f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
2043f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
2044f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2045f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
2046f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
2047f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
2048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2049cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2050cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2051cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
2052cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
2053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2054cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2055cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2056cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2057cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
2058cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
2059cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
2060cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
2063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
2064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
2065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
2066cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
2070cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
2071599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
2072cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
2073bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            numInstances = in.readLong();
2074bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
2075e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
2076cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2077cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2078cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2079cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
2080cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
2082cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
2083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
2084cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
2085cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
2086599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
2087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
2088bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            dest.writeLong(numInstances);
2089bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
2090e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
2091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2093cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2094cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2095cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
2096cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2097cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
2098cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
2099cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
2100cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
2101cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
2102cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2103bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (numInstances != -1) {
2104bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                pw.println(prefix + "numInstances: " + numInstances);
2105bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
2107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
2108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2109599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2110599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
2111599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
2112cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
2113bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
2114bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
2115bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2116e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
2117e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
2118e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
2119e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
2120e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2121e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
2125bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2126bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Dummy throwable, for now, since we don't know when or where the
2127bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // leaked instances came from.  We might in the future, but for
2128bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // now we suppress the stack trace because it's useless and/or
2129bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // misleading.
2130bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static class InstanceCountViolation extends Throwable {
2131bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Class mClass;
2132bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long mInstances;
2133bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final int mLimit;
2134bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
21355f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        private static final StackTraceElement[] FAKE_STACK = {
21365f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit",
21375f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                                  "StrictMode.java", 1)
21385f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        };
2139bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2140bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public InstanceCountViolation(Class klass, long instances, int limit) {
21415f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            super(klass.toString() + "; instances=" + instances + "; limit=" + limit);
2142bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            setStackTrace(FAKE_STACK);
2143bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mClass = klass;
2144bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mInstances = instances;
2145bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mLimit = limit;
2146bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
2147bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
21487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    private static final class InstanceTracker {
21507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private static final HashMap<Class<?>, Integer> sInstanceCounts =
21517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                new HashMap<Class<?>, Integer>();
21527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private final Class<?> mKlass;
21547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public InstanceTracker(Object instance) {
21567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            mKlass = instance.getClass();
21577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(mKlass);
21607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final int newValue = value != null ? value + 1 : 1;
21617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                sInstanceCounts.put(mKlass, newValue);
21627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        @Override
21667e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        protected void finalize() throws Throwable {
21677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            try {
21687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                synchronized (sInstanceCounts) {
21697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    final Integer value = sInstanceCounts.get(mKlass);
21707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    if (value != null) {
21717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        final int newValue = value - 1;
21727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        if (newValue > 0) {
21737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.put(mKlass, newValue);
21747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        } else {
21757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.remove(mKlass);
21767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        }
21777e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    }
21787e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                }
21797e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            } finally {
21807e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                super.finalize();
21817e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21827e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21837e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21847e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public static int getInstanceCount(Class<?> klass) {
21857e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21867e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(klass);
21877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return value != null ? value : 0;
21887e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21897e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21907e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
2191438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
2192