StrictMode.java revision 097fbf2e98e0d32baf6cbd1a5659c4145afb191f
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;
23d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.BroadcastReceiver;
24d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.Context;
25bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent;
26d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkeyimport android.content.ServiceConnection;
27438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log;
28cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer;
29cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton;
306804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager;
31438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
32438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit;
33438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
34438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard;
35fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard;
36bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrickimport dalvik.system.VMDebug;
37438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter;
395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter;
405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList;
4146d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap;
425f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map;
43bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger;
4446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
45438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/**
4632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be
4732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix
4832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them.
4915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or
5115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI
5215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place.  Keeping disk
5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother,
549fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications.  By keeping your application's main thread
559fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent
569fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a>
579fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users.
5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is
6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that
6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency.  It's often the case that
6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be
6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background
6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes.  If possible, it's best to assume that such
6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p>
6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your
6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or
6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's
7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method:
7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
7215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre>
7315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() {
7415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     if (DEVELOPER_MODE) {
7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}()
7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskReads()
7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskWrites()
7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectNetwork()   // or .detectAll() for all detectable problems
7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
8262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick *                 .detectLeakedSqlLiteObjects()
83fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom *                 .detectLeakedClosableObjects()
8432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyDeath()
8632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     }
8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     super.onCreate();
8915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * }
9015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre>
9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected.
9332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can
9432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your
9532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen.
9615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are
9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler},
9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc.
10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds.  In particular,
10132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle.  Use
10232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident.  Network requests on the UI thread
10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though.
10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not
10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses.  While it does
10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing
10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best
10915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism.  Notably, disk or network access from JNI calls
11015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it.  Future versions of Android may catch
11115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode
1124d7bc65538c7cd9fbb1fbbcf22d1da47fcee1219Dirk Dougherty * enabled in applications distributed on Google Play.
113438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode {
115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static final String TAG = "StrictMode";
11682829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
117438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1181181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
1196804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE);
1201181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
121c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    /**
122bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * Boolean system property to disable strict mode checks outright.
123bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * Set this to 'true' to force disable; 'false' has no effect on other
124bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * enable/disable policy.
125bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     * @hide
126bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate     */
127bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate    public static final String DISABLE_PROPERTY = "persist.sys.strictmode.disable";
128bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate
129bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate    /**
130c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * The boolean system property to control screen flashes on violations.
131c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     *
132c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * @hide
133c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     */
134c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual";
135c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
13646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only log a duplicate stack trace to the logs every second.
13746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_LOG_INTERVAL_MS = 1000;
13846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
13946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only show an annoying dialog at most every 30 seconds
14046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_DIALOG_INTERVAL_MS = 30000;
14146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
142e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    // How many Span tags (e.g. animations) to report.
143e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final int MAX_SPAN_TAGS = 20;
144e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
145191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // How many offending stacks to keep track of (and time) per loop
146191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // of the Looper.
147191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final int MAX_OFFENSES_PER_LOOP = 10;
148191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Thread-policy:
150438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
15115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_WRITE = 0x01;  // for ThreadPolicy
15515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
15615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick      * @hide
15815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_READ = 0x02;  // for ThreadPolicy
16015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
16115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
16232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
16315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
16432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_NETWORK = 0x04;  // for ThreadPolicy
165438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
166e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
167e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For StrictMode.noteSlowCall()
168e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
169e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @hide
170e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
171e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static final int DETECT_CUSTOM = 0x08;  // for ThreadPolicy
172e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
173e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    private static final int ALL_THREAD_DETECT_BITS =
174e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM;
175e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
17632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Process-policy:
177438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
17932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Note, a "VM_" bit, not thread.
18032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
18132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
182758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for VmPolicy
18332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
18432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
185fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * Note, a "VM_" bit, not thread.
186fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
187fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
188758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for VmPolicy
189758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
190758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
191758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * Note, a "VM_" bit, not thread.
192758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
193758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
194758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800;  // for VmPolicy
195fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
196fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
19732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
198438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
199bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000;  // for VmPolicy
200bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
201d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
202d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
203d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
204d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static final int DETECT_VM_REGISTRATION_LEAKS = 0x2000;  // for VmPolicy
205d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
206bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int ALL_VM_DETECT_BITS =
207bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS |
208d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS |
209d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            DETECT_VM_REGISTRATION_LEAKS;
210bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
211bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
212bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
213bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
214438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
215438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
21632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
21732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
218438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
21932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
220438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
221438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
222438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
223438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
224b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
225b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
22632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
227438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
228438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
229438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
231b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
232b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
233b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
234b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
235b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
236b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
237b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
2386804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
2396804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
2406804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
2416804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
2426804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
2436804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2446804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
246438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
247438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
248438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
249727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
250727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
251727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
252727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
253727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
254727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
255727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
256703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
257703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
258727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
259727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
260727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
261727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
263c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for thread policies.
26471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
265c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int THREAD_PENALTY_MASK =
266b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2676804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
26871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
269758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
270c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    /**
271c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for VM policies.
272c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     */
273c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int VM_PENALTY_MASK =
274c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick            PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX;
275c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
276c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
277758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // TODO: wrap in some ImmutableHashMap thing.
278758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // Note: must be before static initialization of sVmPolicy.
279758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>();
280758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
28171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
283758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     *
284758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * TODO: these are redundant (mask is in VmPolicy).  Should remove sVmPolicyMask.
28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
287758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
289bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
290bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
291bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
292bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
293bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
294bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
326320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * Creates {@link ThreadPolicy} instances.  Methods whose names start
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
337320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
341320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.setThreadPolicy(policy);
34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
371e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(ALL_THREAD_DETECT_BITS);
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
378e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return disable(ALL_THREAD_DETECT_BITS);
37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
410097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang             * Enable detection of slow calls.
411e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
412e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder detectCustomSlowCalls() {
413e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
414e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
415e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
416e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
417097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang             * Disable detection of slow calls.
418e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
419e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder permitCustomSlowCalls() {
420097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang                return disable(DETECT_CUSTOM);
421e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
422e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
423e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
449b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
450b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
451b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
452b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
459b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
460b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
461b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
462b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
463b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
464b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
465b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
466b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
467b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
468b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
469b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
470b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
4716804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
4726804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4736804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4746804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4756804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4766804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4776804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
533758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP);
53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
537758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        // Map from class to max number of allowed instances in memory.
538758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        final HashMap<Class, Integer> classInstanceLimit;
539758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
540758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) {
541758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            if (classInstanceLimit == null) {
542758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                throw new NullPointerException("classInstanceLimit == null");
543758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
545758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            this.classInstanceLimit = classInstanceLimit;
54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
575758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private HashMap<Class, Integer> mClassInstanceLimit;  // null until needed
576758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private boolean mClassInstanceLimitNeedCow = false;  // need copy-on-write
577758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
578758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder() {
579758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = 0;
580758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Build upon an existing VmPolicy.
584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder(VmPolicy base) {
586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = base.mask;
587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimitNeedCow = true;
588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit = base.classInstanceLimit;
589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Set an upper bound on how many instances of a class can be in memory
593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * at once.  Helps to prevent object leaks.
594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder setClassInstanceLimit(Class klass, int instanceLimit) {
596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (klass == null) {
597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    throw new NullPointerException("klass == null");
598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
599758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (mClassInstanceLimitNeedCow) {
600758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    if (mClassInstanceLimit.containsKey(klass) &&
601758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit.get(klass) == instanceLimit) {
602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        // no-op; don't break COW
603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        return this;
604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    }
605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimitNeedCow = false;
606758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone();
607758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                } else if (mClassInstanceLimit == null) {
608758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = new HashMap<Class, Integer>();
609758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
610bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                mMask |= DETECT_VM_INSTANCE_LEAKS;
611758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit.put(klass, instanceLimit);
612758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return this;
613758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
615bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            /**
616bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * Detect leaks of {@link android.app.Activity} subclasses.
617bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             */
618bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            public Builder detectActivityLeaks() {
619758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS);
620758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
621758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
62232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
62332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
62432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
625fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
626bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * SQLite cursors, Activities, and other closable objects
627bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * but will likely expand in future releases.
62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
630d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey                return enable(DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_CURSOR_LEAKS
631d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey                        | DETECT_VM_CLOSABLE_LEAKS | DETECT_VM_REGISTRATION_LEAKS);
63232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
63332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
63432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
63532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
63632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
63732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
63832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
63932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
64032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
64132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
648fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
649fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
650fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
651fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
652fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
653fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
654fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
655fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
656fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
657fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
658fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
659fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
660d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * Detect when a {@link BroadcastReceiver} or
661d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * {@link ServiceConnection} is leaked during {@link Context}
662d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * teardown.
663d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             */
664d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            public Builder detectLeakedRegistrationObjects() {
665d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey                return enable(DETECT_VM_REGISTRATION_LEAKS);
666d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            }
667d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
668d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            /**
66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
70132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
70232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
70332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
714758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return new VmPolicy(mMask,
715758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP);
71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
71832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
719438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
720438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
7215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
7225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
7235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
7245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
7255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
726cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
727cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
728cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
729703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
730703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
731703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
732703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
7335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
7355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
73732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
73832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
73915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
74032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
74115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
74215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
74315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
744438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
74532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
746438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
74732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
74832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
74932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
75032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
75132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
752727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
753727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
754727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
755727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
756727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
757727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
758727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
759727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
760727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
761727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
762727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
763727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
764727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
76546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
76646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
76746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
76846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
769438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
770438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
771438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
772438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
773438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
774438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
775438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
776438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
777438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
7784b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
7794b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
7807c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
7814b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
7824b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
7834b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
7844b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
7854b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
7864e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7874e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException {
7904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        public StrictModeViolation(int policyState, int policyViolated, String message) {
7914e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyState, policyViolated, message);
7924e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
7934e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
7944e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
7954e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7964e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7974e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7984e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeNetworkViolation extends StrictModeViolation {
7995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
8004e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_NETWORK, null);
8015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8044e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8054e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends StrictModeViolation {
8085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
8094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ, null);
8105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     /**
8144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick   private static class StrictModeDiskWriteViolation extends StrictModeViolation {
8175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
8184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE, null);
8195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeCustomViolation extends StrictModeViolation {
826e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        public StrictModeCustomViolation(int policyMask, String name) {
827e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            super(policyMask, DETECT_CUSTOM, name);
828e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
829e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
830e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
831438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
83215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
833438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
83432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
83532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
83632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
837438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
83832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
839438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
840438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
841438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
84332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
84432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
84532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
846e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // TODO: this was a last minute Gingerbread API change (to
847e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // introduce VmPolicy cleanly) but this isn't particularly
848e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // optimal for users who might call this method often.  This
849e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // should be in a thread-local and not allocate on each call.
85032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
85132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
85232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
85332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
85532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
85632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
85732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
85832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
85997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
86032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
86132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
86297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
86332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
86432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
86532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
86632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
86732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
86897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
86932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
87097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
87197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
87297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
87332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
87432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
87532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
87632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
87732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
87897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
87932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
88097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
88197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
88232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
88332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
88432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
88532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
88632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
88797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
88832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
88997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
89097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
891f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // We don't want to flash the screen red in the system server
892f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // process, nor do we want to modify all the call sites of
893f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // conditionallyEnableDebugLogging() in the system server,
894f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // so instead we use this to determine if we are the system server.
895f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    private static boolean amTheSystemServerProcess() {
896f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // Fast path.  Most apps don't have the system server's UID.
897f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        if (Process.myUid() != Process.SYSTEM_UID) {
898f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            return false;
899f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
900f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
901f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // The settings app, though, has the system server's UID so
902f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // look up our stack to see if we came from the system server.
903f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        Throwable stack = new Throwable();
904f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        stack.fillInStackTrace();
905f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        for (StackTraceElement ste : stack.getStackTrace()) {
906f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            String clsName = ste.getClassName();
907f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            if (clsName != null && clsName.startsWith("com.android.server.")) {
908f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                return true;
909f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            }
910f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
911f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        return false;
912f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    }
913f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
91497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
91550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
91650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
91750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
91850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
91950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
920bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, false)
921bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate                && !amTheSystemServerProcess();
922bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        final boolean suppress = SystemProperties.getBoolean(DISABLE_PROPERTY, false);
923c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
92450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
92550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
926bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (!doFlashes && (IS_USER_BUILD || suppress)) {
9277c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
92850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
92950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
930c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
931bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // Eng builds have flashes on all the time.  The suppression property
932bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // overrides this, so we force the behavior only after the short-circuit
933bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // check above.
934bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (IS_ENG_BUILD) {
935bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate            doFlashes = true;
936bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        }
937bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate
938be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // Thread policy controls BlockGuard.
939c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
940c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
941c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
942c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
943c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
944c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
945c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
946c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
947c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
948c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
949c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
950c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
951c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
952be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // VM Policy controls CloseGuard, detection of Activity leaks,
953be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // and instance counting.
954c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
955c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
956c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
957d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox();
958d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
959d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                policyBuilder.penaltyLog();
960d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
961d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            setVmPolicy(policyBuilder.build());
962c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
963c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
96450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
96550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
96650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
96750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
968b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
969b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
970b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
971b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
972b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
973b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
974b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
975b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
976b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
977b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
978b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
979b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
9805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
984e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * Input is of the following forms:
985e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64"
986e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64 msg=Arbitrary text"
9875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
9885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
9895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
9905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
9915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
9935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
9945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
9985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
10015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
10025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
10035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
10045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
10075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
10085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
10095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
10105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
10115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
10125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
10135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
10165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
10175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1019e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberStartIndex = violationIndex + "violation=".length();
1020e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberEndIndex = message.indexOf(' ', numberStartIndex);
1021e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (numberEndIndex == -1) {
1022e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            numberEndIndex = message.length();
1023e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1024e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        String violationString = message.substring(numberStartIndex, numberEndIndex);
10255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
10265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
10275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
10285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1032191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
1033191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
1034191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
1035191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
1036191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
1037191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
1038191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1039bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    // Note: only access this once verifying the thread has a Looper.
1040bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() {
1041bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        @Override protected Handler initialValue() {
1042bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            return new Handler();
1043bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        }
1044bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    };
1045bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1046191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
1047191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
1048191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
1049191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1050438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
1051438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
105246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
105346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
105446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
105546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
105646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
1057438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1058438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
1059438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1060438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1061438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
10635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
10645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
10655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1067438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1068438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
1069438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
1070438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1071438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1072438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1073438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
107432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
1075438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1076438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1077191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1078191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1079191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1080cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
1081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1082cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1083438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1084438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1085e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // Not part of BlockGuard.Policy; just part of StrictMode:
1086e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        void onCustomSlowCall(String name) {
1087e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if ((mPolicyMask & DETECT_CUSTOM) == 0) {
1088e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1089e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1090e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1091e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1092e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1093e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name);
1094e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            e.fillInStackTrace();
1095e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            startHandlingViolationException(e);
1096e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1097e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
1098438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1099438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
110032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
1101438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1102438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1103191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1104191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1105191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
1107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
111332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
1114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1116b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
1117b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
1118b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
1119191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1120191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1121191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
1123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1125438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1126438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1127438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
1128438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1129438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
11315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
11325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
11335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
11345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
11355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
11365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
1137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
1138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
1139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
1140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1141438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1142cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
1143cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
1144cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
1145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
1146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
1147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
1148438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
1149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
1152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
1153cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
1154cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
1155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
1156cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
1157cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
11584e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
11594e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // Also, as a special short-cut case when the only penalty
11604e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // bit is death, we die immediately, rather than timing
11614e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // the violation's duration.  This makes it convenient to
11624e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // use in unit tests too, rather than waiting on a Looper.
11634e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
1164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
1165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
11664e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            if (looper == null ||
1167c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) {
1168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
1169cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
1170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1171438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1172438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
1174191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
1175cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
1176cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
1179cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
1180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
1181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
1182cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
1183cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1184cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1185cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
11866804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
1187cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
11886804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
11896804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
11906804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
11916804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
11926804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
11936804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
11946804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
1195bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // We post a runnable to a Handler (== delay 0 ms) for
1196bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // measuring the end time of a violation instead of using
1197bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // an IdleHandler (as was previously used) because an
1198bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // IdleHandler may not run for quite a long period of time
1199bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // if an ongoing animation is happening and continually
1200bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // posting ASAP (0 ms) animation steps.  Animations are
1201bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // throttled back to 60fps via SurfaceFlinger/View
1202bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // invalidates, _not_ by posting frame updates every 16
1203bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // milliseconds.
12043761f33387f980d7a6c6e06555a5a932ba7247a2Jeff Sharkey            threadHandler.get().postAtFrontOfQueue(new Runnable() {
1205bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                    public void run() {
1206cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
1207bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1208bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // Note: we do this early, before handling the
1209bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // violation below, as handling the violation
1210bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // may include PENALTY_DEATH and we don't want
1211bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // to keep the red border on.
1212bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        if (windowManager != null) {
1213bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            try {
1214bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                                windowManager.showStrictModeViolation(false);
1215bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            } catch (RemoteException unused) {
1216bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            }
1217bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        }
1218bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1219cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
1220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
1221cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
1222cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
1223cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
1224cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
1225cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
1226cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
1227cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
1228cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
12295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
12315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
12325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
12335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
12345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
12355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
1236cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
1237cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
1238cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
12395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
12405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
1241438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1242cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
124346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1244cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
1245cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
1246703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
1247cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
1248703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
1249703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
12505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
12515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
12525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
12555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
12565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
12575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
12585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1259cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1260727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1261727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1262727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
126346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1264f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
126546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
126646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
126746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
126846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
126946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
127046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
127146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
127246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
127346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1274cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
127546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1276cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
12775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1278cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1279438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1280cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1281438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1282438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1283438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
128471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
128546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
128646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
128746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1288cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
128946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1290cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
129146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1292cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
129346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
129446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1295cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1296cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
129746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
129846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1299cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1300cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1301cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
130232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
130371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
1304c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX;
130571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
130671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
130771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
130871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
130971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
131071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
131171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1312bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
131371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
131471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
131571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
131671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1317438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1318727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1319727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1320727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1321727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
132232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1323727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1324438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1325438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1326cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1327cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1328438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
132946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1330727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1331727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
133232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1333438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1334438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1335438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1336cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
13374e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick                executeDeathPenalty(info);
1338438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1339438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1340438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1341727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
13424e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static void executeDeathPenalty(ViolationInfo info) {
13434e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
13444e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        throw new StrictModeViolation(info.policy, violationBit, null);
13454e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
13464e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
1347bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1348bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1349bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1350bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1351bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1352bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1353bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1354bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1355bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1356bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1357bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1358bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1359bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1360bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1361bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1362bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1363bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1364bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1365bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1366bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1367bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1368bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1369bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1370bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
13711065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
13721065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
13731065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
13741065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
13751065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
13761065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
13771065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
13781065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
13791065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1380bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1381bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1382bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1383bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1384bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1385bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1386bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1387bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1388bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
13894b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
13904b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
13914b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
13924b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
13934b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
13944b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1395727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
13965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
13975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
13985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1399703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1400703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1401703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1402703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1403703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1404703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1405703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1406703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1407703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1408703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
14095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
14105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
14115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1412bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
1413bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
1414bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    public static void conditionallyCheckInstanceCounts() {
1415bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        VmPolicy policy = getVmPolicy();
1416bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (policy.classInstanceLimit.size() == 0) {
1417bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            return;
1418bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1419bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        Runtime.getRuntime().gc();
1420bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        // Note: classInstanceLimit is immutable, so this is lock-free
14215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) {
14225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Class klass = entry.getKey();
14235f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            int limit = entry.getValue();
1424bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            long instances = VMDebug.countInstancesOfClass(klass, false);
1425bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (instances <= limit) {
1426bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                continue;
1427bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1428bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Throwable tr = new InstanceCountViolation(klass, instances, limit);
1429bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            onVmPolicyViolation(tr.getMessage(), tr);
1430bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1431bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
1432bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1433bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static long sLastInstanceCountCheckMillis = 0;
14345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static boolean sIsIdlerRegistered = false;  // guarded by StrictMode.class
1435bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final MessageQueue.IdleHandler sProcessIdleHandler =
1436bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            new MessageQueue.IdleHandler() {
1437bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                public boolean queueIdle() {
1438bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    long now = SystemClock.uptimeMillis();
1439bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    if (now - sLastInstanceCountCheckMillis > 30 * 1000) {
1440bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        sLastInstanceCountCheckMillis = now;
1441bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        conditionallyCheckInstanceCounts();
1442bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    }
1443bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    return true;
1444bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1445bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            };
1446bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1447bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
144832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
144932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
145032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
145132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
145232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
145332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
145432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
14555f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14565f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicy = policy;
14575f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicyMask = policy.mask;
14585f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
14595f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
14605f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Looper looper = Looper.getMainLooper();
14615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (looper != null) {
14625f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                MessageQueue mq = looper.mQueue;
1463c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                if (policy.classInstanceLimit.size() == 0 ||
1464c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    (sVmPolicyMask & VM_PENALTY_MASK) == 0) {
1465bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.removeIdleHandler(sProcessIdleHandler);
1466c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    sIsIdlerRegistered = false;
1467bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                } else if (!sIsIdlerRegistered) {
1468bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.addIdleHandler(sProcessIdleHandler);
1469bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    sIsIdlerRegistered = true;
1470bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1471bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1472bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
147332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
147432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
147532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
147632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
147732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
147832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
14795f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14805f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            return sVmPolicy;
14815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
148232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
148332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
148432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
148562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
148662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
148762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1488fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1489fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1490fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
149162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
149262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
149362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
149462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
149562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
149662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
149762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
1498e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                               .detectAll()
149962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
150062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
150162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
150262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
150362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
150432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
150532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
150632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
150732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
150832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
150932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
151032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
151132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
151232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1513fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1514fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1515fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1516fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1517fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1518fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1519fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
1520d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static boolean vmRegistrationLeaksEnabled() {
1521d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        return (sVmPolicyMask & DETECT_VM_REGISTRATION_LEAKS) != 0;
1522d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1523d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1524d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1525d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1526d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
152732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
15284b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
15294b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
15304b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
153108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    /**
153208d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     * @hide
153308d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     */
153408d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) {
153508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block        onVmPolicyViolation(null, originStack);
153608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    }
153708d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block
1538d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1539d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1540d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
1541d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static void onIntentReceiverLeaked(Throwable originStack) {
1542d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        onVmPolicyViolation(null, originStack);
1543d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1544d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1545d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1546d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1547d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
1548d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static void onServiceConnectionLeaked(Throwable originStack) {
1549d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        onVmPolicyViolation(null, originStack);
1550d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1551d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1552bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Map from VM violation fingerprint to uptime millis.
1553bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>();
1554bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15554b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
15564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
15574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
15584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
1559bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1560bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1561bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0;
1562bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1563bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15645f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        // Erase stuff not relevant for process-wide violations
15655f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.numAnimationsRunning = 0;
15665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.tags = null;
15675f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.broadcastIntentAction = null;
15685f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
1569bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Integer fingerprint = info.hashCode();
1570bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long now = SystemClock.uptimeMillis();
1571bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long lastViolationTime = 0;
1572bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long timeSinceLastViolationMillis = Long.MAX_VALUE;
1573bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        synchronized (sLastVmViolationTime) {
1574bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (sLastVmViolationTime.containsKey(fingerprint)) {
1575bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                lastViolationTime = sLastVmViolationTime.get(fingerprint);
1576bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                timeSinceLastViolationMillis = now - lastViolationTime;
1577bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1578bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1579bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                sLastVmViolationTime.put(fingerprint, now);
1580bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
158132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
158232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1583bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1584bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Log.e(TAG, message, originStack);
1585bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1586bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1587bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask);
1588bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1589bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1590bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1591bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1592bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1593bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1594bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1595bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
159632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1597bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyDropbox && lastViolationTime == 0) {
159832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
159932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
160032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
160132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
160232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
160332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
160432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
160532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
160632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
160732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
160832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
160932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
161032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
161132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
161232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
161332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
161432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
161532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
161632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
161732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
161832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
161932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
162032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
162132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1622bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
162332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
162432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
162532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
162632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
162732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
162832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
162932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
16305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
16315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
16325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1633cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1634703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1635703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1636703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1637703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1638703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1639703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1640703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1641703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1642703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
16435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1644703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
16455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
16485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
16505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
16515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
16525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
16535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
16545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
16555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1656cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
16575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
16585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
165932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1660cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
16615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
16635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
16645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1665cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1666cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
16675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
16685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1669cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
16705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
16715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
16725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1675727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1676727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1677727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1678727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1679727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1680727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1681727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1682727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1683727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1684727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1685cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1686cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1687e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1688e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1689e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1690e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1691e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1692e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1693e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1694e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1695e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1696e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1697e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1698e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1699e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1700e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1701e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1702e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1703e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1704e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17051181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
17061181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
17071181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
17081181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
17091181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1710e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1711e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1712e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1713e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1714e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1715e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1716e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1717e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1718e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1719e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1720e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1721e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1722e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1723e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1724e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1725e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1726e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1727e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1728e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1729e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1730e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1731e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1732e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1733e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1736e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1737e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1738e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17391cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
17401cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
17411cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
17421cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1752e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1753e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1754e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1755e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1756e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17591181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
17601181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
17611181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
17621181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
17631181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
17641181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
17651181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1789e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1790e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1791cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1792cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1793cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1794cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1795cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1796cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1797e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1798e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1799e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1800e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1801e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1802e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1803e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1804e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1805e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1806e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1807e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1808e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1809e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1810e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1811e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1812e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
18131181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
18141181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
18151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1816e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1817e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1818e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1819e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1820e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1821e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1822e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1823e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1824e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1825e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1826e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1827e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1828e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1829e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1830e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1831e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1832e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1833e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1834e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1835e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1836e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1837e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1838e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
18391cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1840e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1841e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1842e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1843e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1844e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
1845e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For code to note that it's slow.  This is a no-op unless the
1846e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * current thread's {@link android.os.StrictMode.ThreadPolicy} has
1847e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls}
1848e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * enabled.
1849e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
1850e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @param name a short string for the exception stack trace that's
1851e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *             built if when this fires.
1852e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
1853e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static void noteSlowCall(String name) {
1854e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
1855e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
1856e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            // StrictMode not enabled.
1857e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            return;
1858e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1859e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name);
1860e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
1861e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1862e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
18634e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18644e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18654e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskRead() {
18664e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18674e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18684e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18694e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18704e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18714e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onReadFromDisk();
18724e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18734e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18744e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18764e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18774e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskWrite() {
18784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18804e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18814e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18824e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18834e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onWriteToDisk();
18844e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18854e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18865f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    // Guarded by StrictMode.class
18875f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static final HashMap<Class, Integer> sExpectedActivityInstanceCount =
18885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new HashMap<Class, Integer>();
18895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * Returns an object that is used to track instances of activites.
18927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * The activity should store a reference to the tracker object in one of its fields.
18937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * @hide
18947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     */
18957e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    public static Object trackActivity(Object instance) {
18967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        return new InstanceTracker(instance);
18977e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
18987e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    /**
1900758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
1901758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
19025f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void incrementExpectedActivityCount(Class klass) {
19037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1904758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1905758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
19067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19075f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
19087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
19097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
19107e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
19117e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19125f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
19135f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer newExpected = expected == null ? 1 : expected + 1;
19145f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sExpectedActivityInstanceCount.put(klass, newExpected);
19155f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
19165f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    }
19175f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19185f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    /**
19195f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     * @hide
19205f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     */
19215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void decrementExpectedActivityCount(Class klass) {
19227e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1923758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1924758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
19257e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19267e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        final int limit;
19275f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
19287e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
19297e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
19307e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
19317e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19325f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
19337e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
19345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (newExpected == 0) {
19355f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.remove(klass);
19365f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            } else {
19375f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.put(klass, newExpected);
19385f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            }
19397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19405f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // Note: adding 1 here to give some breathing room during
19415f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // orientation changes.  (shouldn't be necessary, though?)
19427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            limit = newExpected + 1;
19435f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
19445f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Quick check.
19467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        int actual = InstanceTracker.getInstanceCount(klass);
19477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (actual <= limit) {
19487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            return;
19497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
19507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Do a GC and explicit count to double-check.
19527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // This is the work that we are trying to avoid by tracking the object instances
19537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // explicity.  Running an explicit GC can be expensive (80ms) and so can walking
19547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // the heap to count instance (30ms).  This extra work can make the system feel
19557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // noticeably less responsive during orientation changes when activities are
19567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // being restarted.  Granted, it is only a problem when StrictMode is enabled
19577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // but it is annoying.
19587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        Runtime.getRuntime().gc();
19597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        long instances = VMDebug.countInstancesOfClass(klass, false);
19617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (instances > limit) {
19627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            Throwable tr = new InstanceCountViolation(klass, instances, limit);
19637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            onVmPolicyViolation(tr.getMessage(), tr);
19645f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
1965758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    }
1966758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
1967758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
1968cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
1969cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
1970cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
1971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
1972cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
1973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
1974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
1976cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1977cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
1978cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1979cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1980cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
1981cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1982cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
1983cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1984cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1985cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
1986cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
1987cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1988cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
1989cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1990cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1991599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
1992599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
1993599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
1994599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1995599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
1996e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
1997e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
1998e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1999e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
2000e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
2001e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
2002cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
2003cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
2004cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
2005cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
2006cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2007cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
2008cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2009cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2010cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
2011cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
2012cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2013cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
2014cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2015cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2016bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
2017bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
2018bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
2019bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
2020bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
2021bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
2022bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * If this is a instance count violation, the number of instances in memory,
2023bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * else -1.
2024bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         */
2025bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public long numInstances = -1;
2026bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2027bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        /**
2028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
2029cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2030cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
2031cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
2032cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
2039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
2040cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
2041cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
2042599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
2043bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
2044bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
2045bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
2046bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2047e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
2048bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (tr instanceof InstanceCountViolation) {
2049bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                this.numInstances = ((InstanceCountViolation) tr).mInstances;
2050bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2051e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
2052e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
2053e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
2054e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
2055e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2056e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
2057e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
2058e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
2059e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
2060e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
2061e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
2062e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
2063e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
2064e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
2065e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2066e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2069f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
2070f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
2071f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
2072f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
2073f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2074f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
2075f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2076f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
2077f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
2078f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2079f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
2080f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
2081f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
2082f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
2083f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2084f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
2085f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
2086f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
2087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2088cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2089cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
2091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
2092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2093cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2094cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2095cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2096cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
2097cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
2098cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
2099cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2100cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
2101cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
2102cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
2103cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
2104cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
2105cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
2106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
2107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
2109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
2110599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
2111cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
2112bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            numInstances = in.readLong();
2113bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
2114e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
2115cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2116cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2117cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2118cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
2119cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2120cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
2121cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
2122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
2123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
2124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
2125599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
2126cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
2127bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            dest.writeLong(numInstances);
2128bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
2129e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
2130cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2131cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2132cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2133cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
2135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
2137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
2138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
2139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
2140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
2141cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2142bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (numInstances != -1) {
2143bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                pw.println(prefix + "numInstances: " + numInstances);
2144bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
2146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
2147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2148599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2149599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
2150599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
2151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
2152bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
2153bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
2154bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2155e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
2156e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
2157e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
2158e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
2159e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2160e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2161cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
2164bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2165bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Dummy throwable, for now, since we don't know when or where the
2166bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // leaked instances came from.  We might in the future, but for
2167bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // now we suppress the stack trace because it's useless and/or
2168bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // misleading.
2169bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static class InstanceCountViolation extends Throwable {
2170bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Class mClass;
2171bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long mInstances;
2172bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final int mLimit;
2173bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
21745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        private static final StackTraceElement[] FAKE_STACK = {
21755f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit",
21765f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                                  "StrictMode.java", 1)
21775f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        };
2178bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2179bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public InstanceCountViolation(Class klass, long instances, int limit) {
21805f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            super(klass.toString() + "; instances=" + instances + "; limit=" + limit);
2181bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            setStackTrace(FAKE_STACK);
2182bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mClass = klass;
2183bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mInstances = instances;
2184bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mLimit = limit;
2185bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
2186bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
21877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21887e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    private static final class InstanceTracker {
21897e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private static final HashMap<Class<?>, Integer> sInstanceCounts =
21907e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                new HashMap<Class<?>, Integer>();
21917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private final Class<?> mKlass;
21937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public InstanceTracker(Object instance) {
21957e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            mKlass = instance.getClass();
21967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21977e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21987e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(mKlass);
21997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final int newValue = value != null ? value + 1 : 1;
22007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                sInstanceCounts.put(mKlass, newValue);
22017e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22027e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22047e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        @Override
22057e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        protected void finalize() throws Throwable {
22067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            try {
22077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                synchronized (sInstanceCounts) {
22087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    final Integer value = sInstanceCounts.get(mKlass);
22097e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    if (value != null) {
22107e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        final int newValue = value - 1;
22117e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        if (newValue > 0) {
22127e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.put(mKlass, newValue);
22137e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        } else {
22147e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.remove(mKlass);
22157e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        }
22167e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    }
22177e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                }
22187e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            } finally {
22197e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                super.finalize();
22207e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22217e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22227e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22237e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public static int getInstanceCount(Class<?> klass) {
22247e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
22257e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(klass);
22267e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return value != null ? value : 0;
22277e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22287e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22297e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
2230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
2231