StrictMode.java revision d5875d98f06817f78bd974842a8a9c2d41802d20
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;
38758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrickimport java.util.Collections;
3946d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap;
405f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map;
41bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger;
4246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
43438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/**
4432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be
4532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix
4632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them.
4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or
4915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI
5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place.  Keeping disk
5115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother,
529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications.  By keeping your application's main thread
539fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent
549fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a>
559fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users.
5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is
5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that
5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency.  It's often the case that
6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be
6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background
6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes.  If possible, it's best to assume that such
6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p>
6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your
6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or
6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's
6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method:
6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre>
7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() {
7215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     if (DEVELOPER_MODE) {
7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}()
7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskReads()
7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskWrites()
7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectNetwork()   // or .detectAll() for all detectable problems
7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
8062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick *                 .detectLeakedSqlLiteObjects()
81fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom *                 .detectLeakedClosableObjects()
8232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyDeath()
8432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     }
8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     super.onCreate();
8715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * }
8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre>
8915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected.
9132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can
9232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your
9332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen.
9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are
9615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler},
9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc.
9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds.  In particular,
9932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle.  Use
10032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident.  Network requests on the UI thread
10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though.
10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not
10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses.  While it does
10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing
10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best
10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism.  Notably, disk or network access from JNI calls
10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it.  Future versions of Android may catch
10915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode
11015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market.
111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode {
113438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static final String TAG = "StrictMode";
11482829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1161181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
1176804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE);
1181181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
119c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    /**
120c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * The boolean system property to control screen flashes on violations.
121c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     *
122c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * @hide
123c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     */
124c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual";
125c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
12646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only log a duplicate stack trace to the logs every second.
12746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_LOG_INTERVAL_MS = 1000;
12846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
12946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only show an annoying dialog at most every 30 seconds
13046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_DIALOG_INTERVAL_MS = 30000;
13146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
132e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    // How many Span tags (e.g. animations) to report.
133e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final int MAX_SPAN_TAGS = 20;
134e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
135191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // How many offending stacks to keep track of (and time) per loop
136191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // of the Looper.
137191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final int MAX_OFFENSES_PER_LOOP = 10;
138191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
13932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Thread-policy:
140438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
14115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
14315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_WRITE = 0x01;  // for ThreadPolicy
14515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
14615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick      * @hide
14815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_READ = 0x02;  // for ThreadPolicy
15015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
15115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_NETWORK = 0x04;  // for ThreadPolicy
155438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
156e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
157e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For StrictMode.noteSlowCall()
158e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
159e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @hide
160e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
161e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static final int DETECT_CUSTOM = 0x08;  // for ThreadPolicy
162e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
163e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    private static final int ALL_THREAD_DETECT_BITS =
164e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM;
165e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
16632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Process-policy:
167438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
168438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
16932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Note, a "VM_" bit, not thread.
17032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
17132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
172758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for VmPolicy
17332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
17432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
175fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * Note, a "VM_" bit, not thread.
176fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
177fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
178758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for VmPolicy
179758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
180758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
181758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * Note, a "VM_" bit, not thread.
182758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
183758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
184758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800;  // for VmPolicy
185fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
186fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
18732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
188438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
189bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000;  // for VmPolicy
190bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
191bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int ALL_VM_DETECT_BITS =
192bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS |
193bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS;
194bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
195bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
196bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
197bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
198438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
199438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
20032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
20132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
202438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
20332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
204438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
205438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
206438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
207438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
208b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
209b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
21032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
211438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
212438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
213438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
214438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
215b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
216b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
217b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
218b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
219b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
220b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
221b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
2226804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
2236804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
2246804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
2256804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
2266804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
2276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2286804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
231438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
232438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
233727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
234727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
235727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
236727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
237727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
238727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
239727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
240703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
241703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
242727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
243727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
244727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
245727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
247c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for thread policies.
24871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
249c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int THREAD_PENALTY_MASK =
250b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2516804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
25271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
253758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
254c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    /**
255c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for VM policies.
256c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     */
257c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int VM_PENALTY_MASK =
258c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick            PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX;
259c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
260c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
261758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // TODO: wrap in some ImmutableHashMap thing.
262758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // Note: must be before static initialization of sVmPolicy.
263758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>();
264758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
26571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
267758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     *
268758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * TODO: these are redundant (mask is in VmPolicy).  Should remove sVmPolicyMask.
26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
271758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
273bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
274bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
275bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
276bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
277bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
278bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
310320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * Creates {@link ThreadPolicy} instances.  Methods whose names start
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
321320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
325320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.setThreadPolicy(policy);
32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
355e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(ALL_THREAD_DETECT_BITS);
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
362e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return disable(ALL_THREAD_DETECT_BITS);
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
394e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
395e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
396e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder detectCustomSlowCalls() {
397e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
398e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
399e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
400e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
401e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
402e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
403e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder permitCustomSlowCalls() {
404e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
405e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
406e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
407e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
433b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
434b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
435b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
436b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
443b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
444b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
445b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
446b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
447b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
448b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
449b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
450b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
451b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
452b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
453b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
454b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
4556804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
4566804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4576804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4586804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4596804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4606804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4616804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
517758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP);
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
521758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        // Map from class to max number of allowed instances in memory.
522758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        final HashMap<Class, Integer> classInstanceLimit;
523758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
524758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) {
525758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            if (classInstanceLimit == null) {
526758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                throw new NullPointerException("classInstanceLimit == null");
527758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
529758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            this.classInstanceLimit = classInstanceLimit;
53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
559758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private HashMap<Class, Integer> mClassInstanceLimit;  // null until needed
560758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private boolean mClassInstanceLimitNeedCow = false;  // need copy-on-write
561758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
562758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder() {
563758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = 0;
564758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
565758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
566758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
567758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Build upon an existing VmPolicy.
568758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
569758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder(VmPolicy base) {
570758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = base.mask;
571758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimitNeedCow = true;
572758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit = base.classInstanceLimit;
573758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
574758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
575758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
576758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Set an upper bound on how many instances of a class can be in memory
577758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * at once.  Helps to prevent object leaks.
578758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
579758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder setClassInstanceLimit(Class klass, int instanceLimit) {
580758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (klass == null) {
581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    throw new NullPointerException("klass == null");
582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (mClassInstanceLimitNeedCow) {
584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    if (mClassInstanceLimit.containsKey(klass) &&
585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit.get(klass) == instanceLimit) {
586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        // no-op; don't break COW
587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        return this;
588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    }
589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimitNeedCow = false;
590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone();
591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                } else if (mClassInstanceLimit == null) {
592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = new HashMap<Class, Integer>();
593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
594bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                mMask |= DETECT_VM_INSTANCE_LEAKS;
595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit.put(klass, instanceLimit);
596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return this;
597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
599bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            /**
600bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * Detect leaks of {@link android.app.Activity} subclasses.
601bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             */
602bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            public Builder detectActivityLeaks() {
603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS);
604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
60632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
60732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
60832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
609fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
610bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * SQLite cursors, Activities, and other closable objects
611bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * but will likely expand in future releases.
61232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
61332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS |
615758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS);
61632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
61732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
61832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
61932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
62032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
62132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
62232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
62332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
62432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
62532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
62632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
62732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
63032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
63132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
632fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
633fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
634fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
635fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
636fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
637fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
638fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
639fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
640fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
641fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
642fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
643fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
65632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
65732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
66032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
66132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
66432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
66532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
66632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
66732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
66832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
689758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return new VmPolicy(mMask,
690758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP);
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
694438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
695438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
6965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
6975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
6985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
6995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
7005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
701cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
702cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
703cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
704703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
705703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
706703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
707703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
7085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
7105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
71415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
71615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
71715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
71815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
719438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
72032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
721438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
72332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
72432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
72532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
72632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
727727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
728727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
729727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
730727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
731727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
732727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
733727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
734727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
735727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
736727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
737727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
738727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
739727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
74046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
74146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
74246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
74346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
744438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
745438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
746438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
747438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
748438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
749438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
750438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
751438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
752438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
7534b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
7544b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
7557c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
7564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
7574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
7584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
7594b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
7604b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
7614e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7624e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7634e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7644e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException {
7654e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        public StrictModeViolation(int policyState, int policyViolated, String message) {
7664e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyState, policyViolated, message);
7674e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
7684e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
7694e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
7704e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7714e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7724e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7734e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeNetworkViolation extends StrictModeViolation {
7745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
7754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_NETWORK, null);
7765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7804e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7814e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7824e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends StrictModeViolation {
7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
7844e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ, null);
7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     /**
7894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7914e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick   private static class StrictModeDiskWriteViolation extends StrictModeViolation {
7925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
7934e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE, null);
7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7974e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7984e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7994e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8004e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeCustomViolation extends StrictModeViolation {
801e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        public StrictModeCustomViolation(int policyMask, String name) {
802e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            super(policyMask, DETECT_CUSTOM, name);
803e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
804e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
805e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
806438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
80715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
808438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
80932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
81032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
81132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
812438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
81332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
814438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
815438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
816438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
81832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
81932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
82032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
821e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // TODO: this was a last minute Gingerbread API change (to
822e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // introduce VmPolicy cleanly) but this isn't particularly
823e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // optimal for users who might call this method often.  This
824e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // should be in a thread-local and not allocate on each call.
82532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
82632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
82732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
82832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
82932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
83032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
83132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
83232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
83332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
83497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
83532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
83632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
83797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
83832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
83932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
84032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
84132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
84232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
84397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
84432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
84597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
84697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
84797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
84832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
84932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
85032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
85132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
85232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
85397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
85597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
85697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
85732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
85832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
85932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
86032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
86132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
86297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
86332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
86497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
86597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
866f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // We don't want to flash the screen red in the system server
867f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // process, nor do we want to modify all the call sites of
868f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // conditionallyEnableDebugLogging() in the system server,
869f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // so instead we use this to determine if we are the system server.
870f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    private static boolean amTheSystemServerProcess() {
871f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // Fast path.  Most apps don't have the system server's UID.
872f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        if (Process.myUid() != Process.SYSTEM_UID) {
873f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            return false;
874f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
875f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
876f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // The settings app, though, has the system server's UID so
877f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // look up our stack to see if we came from the system server.
878f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        Throwable stack = new Throwable();
879f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        stack.fillInStackTrace();
880f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        for (StackTraceElement ste : stack.getStackTrace()) {
881f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            String clsName = ste.getClassName();
882f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            if (clsName != null && clsName.startsWith("com.android.server.")) {
883f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                return true;
884f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            }
885f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
886f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        return false;
887f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    }
888f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
88997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
89050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
89150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
89250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
89350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
89450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
895f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        boolean doFlashes = !amTheSystemServerProcess() &&
896f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                SystemProperties.getBoolean(VISUAL_PROPERTY, IS_ENG_BUILD);
897c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
89850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
89950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
900c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD && !doFlashes) {
9017c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
90250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
90350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
904c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
905c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
906c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
907c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
908c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
909c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
910c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
911d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
912d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                threadPolicyMask |= StrictMode.PENALTY_LOG;
913d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
914c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
915c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
916c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
917c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
918c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
919c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
920c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
921c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
922c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
923c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
924d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox();
925d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
926d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                policyBuilder.penaltyLog();
927d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
928d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            setVmPolicy(policyBuilder.build());
929c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
930c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
93150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
93250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
93350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
93450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
935b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
936b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
937b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
938b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
939b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
940b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
941b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
942b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
943b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
944b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
945b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
946b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
9475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
9485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
9495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
9505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
951e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * Input is of the following forms:
952e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64"
953e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64 msg=Arbitrary text"
9545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
9555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
9565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
9575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
9585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
9605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
9615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
9645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
9655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
9685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
9695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
9705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
9715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
9745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
9755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
9765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
9775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
9795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
9805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
9845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
986e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberStartIndex = violationIndex + "violation=".length();
987e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberEndIndex = message.indexOf(' ', numberStartIndex);
988e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (numberEndIndex == -1) {
989e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            numberEndIndex = message.length();
990e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
991e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        String violationString = message.substring(numberStartIndex, numberEndIndex);
9925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
9935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
9945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
9955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
9985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
999191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
1000191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
1001191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
1002191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
1003191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
1004191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
1005191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1006bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    // Note: only access this once verifying the thread has a Looper.
1007bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() {
1008bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        @Override protected Handler initialValue() {
1009bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            return new Handler();
1010bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        }
1011bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    };
1012bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1013191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
1014191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
1015191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
1016191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1017438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
1018438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
101946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
102046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
102146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
102246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
102346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
1024438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1025438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
1026438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1027438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1028438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1034438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1035438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
1036438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
1037438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1038438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1039438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1040438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
104132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
1042438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1043438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1044191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1045191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1046191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
1048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1049cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1050438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1051438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1052e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // Not part of BlockGuard.Policy; just part of StrictMode:
1053e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        void onCustomSlowCall(String name) {
1054e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if ((mPolicyMask & DETECT_CUSTOM) == 0) {
1055e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1056e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1057e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1058e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1059e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1060e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name);
1061e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            e.fillInStackTrace();
1062e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            startHandlingViolationException(e);
1063e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1064e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
1065438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1066438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
106732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
1068438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1069438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1070191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1071191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1072191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1073cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
1074cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1075cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1076438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1077438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1078438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1079438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
108032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
1081438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1082438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1083b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
1084b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
1085b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
1086191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1087191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1088191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1089cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
1090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1092438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1093438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1094438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
1095438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1096438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1097438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
10995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
11005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
11015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
11025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
11035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
1104cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
1105cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
1106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
1107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
1110cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
1111cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
1112cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
1113cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
1114cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
1115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
1116cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1117cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
1118cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
1119cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
1120cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
1121cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
1122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
1123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
1124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
11254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
11264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // Also, as a special short-cut case when the only penalty
11274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // bit is death, we die immediately, rather than timing
11284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // the violation's duration.  This makes it convenient to
11294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // use in unit tests too, rather than waiting on a Looper.
11304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
1131cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
1132cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
11334e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            if (looper == null ||
1134c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) {
1135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
1136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
1137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1138438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1139438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
1141191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
1142cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
1143cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1144cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
1146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
1147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
1148cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
1149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
11536804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
1154cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
11556804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
11566804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
11576804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
11586804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
11596804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
11606804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
11616804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
1162bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // We post a runnable to a Handler (== delay 0 ms) for
1163bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // measuring the end time of a violation instead of using
1164bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // an IdleHandler (as was previously used) because an
1165bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // IdleHandler may not run for quite a long period of time
1166bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // if an ongoing animation is happening and continually
1167bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // posting ASAP (0 ms) animation steps.  Animations are
1168bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // throttled back to 60fps via SurfaceFlinger/View
1169bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // invalidates, _not_ by posting frame updates every 16
1170bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // milliseconds.
1171bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            threadHandler.get().post(new Runnable() {
1172bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                    public void run() {
1173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
1174bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1175bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // Note: we do this early, before handling the
1176bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // violation below, as handling the violation
1177bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // may include PENALTY_DEATH and we don't want
1178bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // to keep the red border on.
1179bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        if (windowManager != null) {
1180bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            try {
1181bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                                windowManager.showStrictModeViolation(false);
1182bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            } catch (RemoteException unused) {
1183bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            }
1184bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        }
1185bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1186cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
1187cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
1188cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
1189cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
1190cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
1191cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
1192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
1193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
1194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
1195cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
11965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1197438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
11985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
11995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
12005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
12015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
12025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
1205cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
12065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
12075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
1208438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1209cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
121046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1211cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
1212cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
1213703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
1214cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
1215703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
1216703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
12175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
12185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
12195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1221cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
12225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
12235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
12245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
12255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1226cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1227727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1228727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1229727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
123046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1231f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
123246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
123346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
123446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
123546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
123646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
123746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
123846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
123946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
124046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1241cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
124246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1243cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
12445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1245cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1246438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1247cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1248438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1249438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1250438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
125171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
125246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
125346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
125446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1255cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
125646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1257cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
125846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1259cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
126046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
126146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1262cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1263cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
126446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
126546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1266cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1267cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1268cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
126932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
127071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
1271c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX;
127271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
127371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
127471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
127571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
127671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
127771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
127871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1279bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
128071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
128171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
128271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
128371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1284438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1285727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1286727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1287727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1288727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
128932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1290727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1291438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1292438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1293cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1294cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1295438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
129646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1297727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1298727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
129932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1300438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1301438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1302438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1303cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
13044e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick                executeDeathPenalty(info);
1305438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1306438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1307438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1308727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
13094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static void executeDeathPenalty(ViolationInfo info) {
13104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
13114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        throw new StrictModeViolation(info.policy, violationBit, null);
13124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
13134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
1314bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1315bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1316bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1317bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1318bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1319bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1320bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1321bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1322bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1323bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1324bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1325bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1326bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1327bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1328bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1329bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1330bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1331bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1332bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1333bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1334bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1335bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1336bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1337bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
13381065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
13391065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
13401065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
13411065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
13421065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
13431065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
13441065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
13451065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
13461065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1347bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1348bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1349bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1350bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1351bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1352bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1353bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1354bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1355bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
13564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
13574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
13584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
13594b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
13604b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
13614b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1362727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
13635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
13645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
13655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1366703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1367703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1368703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1369703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1370703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1371703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1372703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1373703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1374703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1375703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
13765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
13775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
13785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1379bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
1380bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
1381bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    public static void conditionallyCheckInstanceCounts() {
1382bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        VmPolicy policy = getVmPolicy();
1383bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (policy.classInstanceLimit.size() == 0) {
1384bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            return;
1385bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1386bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        Runtime.getRuntime().gc();
1387bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        // Note: classInstanceLimit is immutable, so this is lock-free
13885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) {
13895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Class klass = entry.getKey();
13905f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            int limit = entry.getValue();
1391bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            long instances = VMDebug.countInstancesOfClass(klass, false);
1392bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (instances <= limit) {
1393bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                continue;
1394bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1395bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Throwable tr = new InstanceCountViolation(klass, instances, limit);
1396bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            onVmPolicyViolation(tr.getMessage(), tr);
1397bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1398bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
1399bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1400bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static long sLastInstanceCountCheckMillis = 0;
14015f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static boolean sIsIdlerRegistered = false;  // guarded by StrictMode.class
1402bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final MessageQueue.IdleHandler sProcessIdleHandler =
1403bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            new MessageQueue.IdleHandler() {
1404bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                public boolean queueIdle() {
1405bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    long now = SystemClock.uptimeMillis();
1406bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    if (now - sLastInstanceCountCheckMillis > 30 * 1000) {
1407bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        sLastInstanceCountCheckMillis = now;
1408bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        conditionallyCheckInstanceCounts();
1409bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    }
1410bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    return true;
1411bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1412bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            };
1413bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1414bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
141532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
141632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
141732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
141832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
141932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
142032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
142132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
14225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14235f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicy = policy;
14245f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicyMask = policy.mask;
14255f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
14265f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
14275f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Looper looper = Looper.getMainLooper();
14285f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (looper != null) {
14295f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                MessageQueue mq = looper.mQueue;
1430c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                if (policy.classInstanceLimit.size() == 0 ||
1431c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    (sVmPolicyMask & VM_PENALTY_MASK) == 0) {
1432bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.removeIdleHandler(sProcessIdleHandler);
1433c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    sIsIdlerRegistered = false;
1434bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                } else if (!sIsIdlerRegistered) {
1435bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.addIdleHandler(sProcessIdleHandler);
1436bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    sIsIdlerRegistered = true;
1437bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1438bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1439bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
144032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
144132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
144232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
144332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
144432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
144532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
14465f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14475f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            return sVmPolicy;
14485f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
144932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
145032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
145132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
145262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
145362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
145462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1455fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1456fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1457fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
145862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
145962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
146062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
146162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
146262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
146362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
146462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
1465e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                               .detectAll()
146662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
146762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
146862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
146962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
147062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
147132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
147232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
147332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
147432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
147532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
147632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
147732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
147832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
147932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1480fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1481fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1482fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1483fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1484fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1485fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1486fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
148732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
14884b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
14894b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
14904b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
149108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    /**
149208d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     * @hide
149308d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     */
149408d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) {
149508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block        onVmPolicyViolation(null, originStack);
149608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    }
149708d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block
1498bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Map from VM violation fingerprint to uptime millis.
1499bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>();
1500bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15014b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
15024b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
15034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
15044b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
1505bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1506bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1507bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0;
1508bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1509bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15105f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        // Erase stuff not relevant for process-wide violations
15115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.numAnimationsRunning = 0;
15125f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.tags = null;
15135f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.broadcastIntentAction = null;
15145f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
1515bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Integer fingerprint = info.hashCode();
1516bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long now = SystemClock.uptimeMillis();
1517bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long lastViolationTime = 0;
1518bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long timeSinceLastViolationMillis = Long.MAX_VALUE;
1519bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        synchronized (sLastVmViolationTime) {
1520bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (sLastVmViolationTime.containsKey(fingerprint)) {
1521bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                lastViolationTime = sLastVmViolationTime.get(fingerprint);
1522bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                timeSinceLastViolationMillis = now - lastViolationTime;
1523bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1524bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1525bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                sLastVmViolationTime.put(fingerprint, now);
1526bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
152732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
152832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1529bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1530bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Log.e(TAG, message, originStack);
1531bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1532bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1533bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask);
1534bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1535bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1536bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1537bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1538bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1539bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1540bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1541bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
154232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1543bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyDropbox && lastViolationTime == 0) {
154432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
154532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
154632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
154732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
154832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
154932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
155032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
155132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
155232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
155332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
155432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
155532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
155632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
155732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
155832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
155932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
156032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
156132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
156232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
156332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
156432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
156532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
156632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
156732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1568bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
156932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
157032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
157132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
157232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
157332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
157432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
157532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
15765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
15775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
15785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1579cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1580703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1581703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1582703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1583703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1584703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1585703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1586703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1587703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1588703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
15895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1590703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
15915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
15925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
15935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
15945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
15955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
15965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
15975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
15985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
15995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
16005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
16015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1602cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
16035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
16045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
160532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1606cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
16075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
16095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
16105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1611cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1612cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
16135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
16145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1615cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
16165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
16175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
16185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1621727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1622727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1623727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1624727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1625727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1626727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1627727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1628727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1629727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1630727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1631cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1632cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1633e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1634e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1635e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1636e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1637e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1638e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1639e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1640e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1641e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1642e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1643e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1644e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1645e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1646e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1647e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1648e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1649e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1650e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
16511181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
16521181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
16531181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
16541181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
16551181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1656e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1657e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1658e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1659e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1660e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1661e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1662e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1663e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1664e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1665e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1666e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1667e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1668e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1669e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1670e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1671e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1672e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1673e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1674e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1675e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1676e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1677e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1678e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1679e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1680e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1681e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1682e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1683e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1684e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
16851cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
16861cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
16871cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
16881cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1689e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1690e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1691e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1692e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1693e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1694e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1695e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1696e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1697e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1698e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1699e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1700e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1701e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1702e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1703e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1704e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17051181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
17061181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
17071181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
17081181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
17091181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
17101181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
17111181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1712e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1713e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1714e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1715e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1716e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1717e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1718e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1719e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1720e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1721e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1722e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1723e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1724e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1725e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1726e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1727e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1728e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1729e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1730e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1731e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1732e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1733e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1736e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1737cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1738cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1739cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1740cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1741cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1742cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1752e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1753e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1754e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1755e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1756e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
17591181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
17601181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
17611181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1762e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1763e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
17851cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1789e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1790e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
1791e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For code to note that it's slow.  This is a no-op unless the
1792e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * current thread's {@link android.os.StrictMode.ThreadPolicy} has
1793e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls}
1794e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * enabled.
1795e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
1796e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @param name a short string for the exception stack trace that's
1797e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *             built if when this fires.
1798e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
1799e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static void noteSlowCall(String name) {
1800e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
1801e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
1802e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            // StrictMode not enabled.
1803e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            return;
1804e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1805e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name);
1806e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
1807e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1808e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
18094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskRead() {
18124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onReadFromDisk();
18184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskWrite() {
18244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onWriteToDisk();
18304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18314e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18325f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    // Guarded by StrictMode.class
18335f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static final HashMap<Class, Integer> sExpectedActivityInstanceCount =
18345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new HashMap<Class, Integer>();
18355f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18364e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * Returns an object that is used to track instances of activites.
18387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * The activity should store a reference to the tracker object in one of its fields.
18397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * @hide
18407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     */
18417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    public static Object trackActivity(Object instance) {
18427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        return new InstanceTracker(instance);
18437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
18447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    /**
1846758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
1847758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
18485f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void incrementExpectedActivityCount(Class klass) {
18497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1850758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1851758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18535f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18585f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18595f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer newExpected = expected == null ? 1 : expected + 1;
18605f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sExpectedActivityInstanceCount.put(klass, newExpected);
18615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
18625f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    }
18635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18645f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    /**
18655f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     * @hide
18665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     */
18675f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void decrementExpectedActivityCount(Class klass) {
18687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1869758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1870758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        final int limit;
18735f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18777e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18785f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18797e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
18805f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (newExpected == 0) {
18815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.remove(klass);
18825f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            } else {
18835f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.put(klass, newExpected);
18845f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            }
18857e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18865f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // Note: adding 1 here to give some breathing room during
18875f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // orientation changes.  (shouldn't be necessary, though?)
18887e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            limit = newExpected + 1;
18895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
18905f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Quick check.
18927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        int actual = InstanceTracker.getInstanceCount(klass);
18937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (actual <= limit) {
18947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            return;
18957e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
18967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18977e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Do a GC and explicit count to double-check.
18987e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // This is the work that we are trying to avoid by tracking the object instances
18997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // explicity.  Running an explicit GC can be expensive (80ms) and so can walking
19007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // the heap to count instance (30ms).  This extra work can make the system feel
19017e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // noticeably less responsive during orientation changes when activities are
19027e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // being restarted.  Granted, it is only a problem when StrictMode is enabled
19037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // but it is annoying.
19047e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        Runtime.getRuntime().gc();
19057e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        long instances = VMDebug.countInstancesOfClass(klass, false);
19077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (instances > limit) {
19087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            Throwable tr = new InstanceCountViolation(klass, instances, limit);
19097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            onVmPolicyViolation(tr.getMessage(), tr);
19105f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
1911758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    }
1912758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
1913758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
1914cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
1915cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
1916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
1917cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
1918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
1919cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
1920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
1922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1923cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
1924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1926cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
1927cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1928cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
1929cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1930cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1931cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
1932cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
1933cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1934cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
1935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1937599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
1938599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
1939599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
1940599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1941599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
1942e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
1943e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
1944e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1945e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
1946e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1947e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1948cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
1949cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
1950cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
1951cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
1952cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1953cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
1954cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1955cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1956cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
1957cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
1958cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1959cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
1960cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1961cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1962bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
1963bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
1964bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
1965bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
1966bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
1967bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
1968bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * If this is a instance count violation, the number of instances in memory,
1969bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * else -1.
1970bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         */
1971bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public long numInstances = -1;
1972bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1973bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        /**
1974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
1975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1976cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
1977cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
1978cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
1979cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1980cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1981cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1982cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
1983cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1984cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
1985cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
1986cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
1987cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
1988599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
1989bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
1990bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
1991bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
1992bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
1993e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
1994bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (tr instanceof InstanceCountViolation) {
1995bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                this.numInstances = ((InstanceCountViolation) tr).mInstances;
1996bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1997e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1998e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
1999e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
2000e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
2001e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2002e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
2003e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
2004e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
2005e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
2006e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
2007e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
2008e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
2009e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
2010e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
2011e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2012e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2013cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2014cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2015f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
2016f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
2017f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
2018f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
2019f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2020f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
2021f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2022f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
2023f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
2024f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2025f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
2026f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
2027f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
2028f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
2029f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2030f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
2031f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
2032f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
2038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2040cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2041cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2042cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
2043cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
2044cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
2045cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
2047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
2048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
2049cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
2050cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
2051cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
2052cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
2053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2054cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
2055cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
2056599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
2057cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
2058bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            numInstances = in.readLong();
2059bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
2060e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
2065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2066cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
2069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
2070cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
2071599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
2072cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
2073bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            dest.writeLong(numInstances);
2074bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
2075e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
2076cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2077cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2078cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2079cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2080cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
2081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2082cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
2083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
2084cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
2085cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
2086cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
2087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2088bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (numInstances != -1) {
2089bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                pw.println(prefix + "numInstances: " + numInstances);
2090bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
2092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
2093cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2094599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2095599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
2096599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
2097cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
2098bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
2099bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
2100bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2101e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
2102e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
2103e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
2104e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
2105e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2106e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
2110bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2111bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Dummy throwable, for now, since we don't know when or where the
2112bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // leaked instances came from.  We might in the future, but for
2113bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // now we suppress the stack trace because it's useless and/or
2114bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // misleading.
2115bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static class InstanceCountViolation extends Throwable {
2116bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Class mClass;
2117bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long mInstances;
2118bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final int mLimit;
2119bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
21205f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        private static final StackTraceElement[] FAKE_STACK = {
21215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit",
21225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                                  "StrictMode.java", 1)
21235f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        };
2124bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2125bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public InstanceCountViolation(Class klass, long instances, int limit) {
21265f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            super(klass.toString() + "; instances=" + instances + "; limit=" + limit);
2127bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            setStackTrace(FAKE_STACK);
2128bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mClass = klass;
2129bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mInstances = instances;
2130bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mLimit = limit;
2131bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
2132bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
21337e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21347e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    private static final class InstanceTracker {
21357e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private static final HashMap<Class<?>, Integer> sInstanceCounts =
21367e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                new HashMap<Class<?>, Integer>();
21377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private final Class<?> mKlass;
21397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public InstanceTracker(Object instance) {
21417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            mKlass = instance.getClass();
21427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(mKlass);
21457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final int newValue = value != null ? value + 1 : 1;
21467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                sInstanceCounts.put(mKlass, newValue);
21477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        @Override
21517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        protected void finalize() throws Throwable {
21527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            try {
21537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                synchronized (sInstanceCounts) {
21547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    final Integer value = sInstanceCounts.get(mKlass);
21557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    if (value != null) {
21567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        final int newValue = value - 1;
21577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        if (newValue > 0) {
21587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.put(mKlass, newValue);
21597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        } else {
21607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.remove(mKlass);
21617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        }
21627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    }
21637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                }
21647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            } finally {
21657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                super.finalize();
21667e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public static int getInstanceCount(Class<?> klass) {
21707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(klass);
21727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return value != null ? value : 0;
21737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
2176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
2177