StrictMode.java revision 0fa30372c7768692f9deef3e33655382e8a683fd
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
206a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    /**
207a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     * @hide
208a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     */
209a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    private static final int DETECT_VM_FILE_URI_EXPOSURE = 0x4000;  // for VmPolicy
210a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey
211bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int ALL_VM_DETECT_BITS =
212bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS |
213d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS |
214a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            DETECT_VM_REGISTRATION_LEAKS | DETECT_VM_FILE_URI_EXPOSURE;
215bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
216bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
217bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
218bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
219438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
220438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
22132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
22232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
223438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
22432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
225438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
226438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
227438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
228438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
229b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
230b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
23132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
232438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
233438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
234438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
235438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
236b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
237b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
238b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
239b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
240b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
241b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
242b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
2436804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
2446804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
2456804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
2466804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
2476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
2486804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
251438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
252438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
253438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
254727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
255727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
256727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
257727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
258727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
259727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
260727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
261703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
262703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
263727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
264727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
265727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
266727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
268c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for thread policies.
26971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
270c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int THREAD_PENALTY_MASK =
271b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2726804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
27371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
274758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
275c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    /**
276c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for VM policies.
277c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     */
278c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int VM_PENALTY_MASK =
279c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick            PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX;
280c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
281c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
282758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // TODO: wrap in some ImmutableHashMap thing.
283758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // Note: must be before static initialization of sVmPolicy.
284758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>();
285758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
28671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
288758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     *
289758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * TODO: these are redundant (mask is in VmPolicy).  Should remove sVmPolicyMask.
29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
292758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
294bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
295bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
296bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
297bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
298bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
299bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
331320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * Creates {@link ThreadPolicy} instances.  Methods whose names start
33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
342320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
346320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.setThreadPolicy(policy);
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
376e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(ALL_THREAD_DETECT_BITS);
37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
383e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return disable(ALL_THREAD_DETECT_BITS);
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
39332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
39432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
39532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
39632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
415097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang             * Enable detection of slow calls.
416e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
417e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder detectCustomSlowCalls() {
418e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
419e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
420e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
421e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
422097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang             * Disable detection of slow calls.
423e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
424e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder permitCustomSlowCalls() {
425097fbf2e98e0d32baf6cbd1a5659c4145afb191fQi Wang                return disable(DETECT_CUSTOM);
426e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
427e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
428e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
454b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
455b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
456b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
457b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
464b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
465b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
466b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
467b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
468b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
469b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
470b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
471b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
472b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
473b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
474b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
475b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
4766804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
4776804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4786804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4796804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4806804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4816804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4826804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
52032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
52132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
52232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
52332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
538758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP);
53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
542758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        // Map from class to max number of allowed instances in memory.
543758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        final HashMap<Class, Integer> classInstanceLimit;
544758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
545758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) {
546758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            if (classInstanceLimit == null) {
547758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                throw new NullPointerException("classInstanceLimit == null");
548758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
550758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            this.classInstanceLimit = classInstanceLimit;
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
57732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
57832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
57932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
580758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private HashMap<Class, Integer> mClassInstanceLimit;  // null until needed
581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private boolean mClassInstanceLimitNeedCow = false;  // need copy-on-write
582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder() {
584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = 0;
585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Build upon an existing VmPolicy.
589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder(VmPolicy base) {
591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = base.mask;
592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimitNeedCow = true;
593758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit = base.classInstanceLimit;
594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Set an upper bound on how many instances of a class can be in memory
598758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * at once.  Helps to prevent object leaks.
599758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
600758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder setClassInstanceLimit(Class klass, int instanceLimit) {
601758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (klass == null) {
602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    throw new NullPointerException("klass == null");
603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (mClassInstanceLimitNeedCow) {
605758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    if (mClassInstanceLimit.containsKey(klass) &&
606758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit.get(klass) == instanceLimit) {
607758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        // no-op; don't break COW
608758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        return this;
609758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    }
610758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimitNeedCow = false;
611758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone();
612758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                } else if (mClassInstanceLimit == null) {
613758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = new HashMap<Class, Integer>();
614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
615bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                mMask |= DETECT_VM_INSTANCE_LEAKS;
616758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit.put(klass, instanceLimit);
617758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return this;
618758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
619758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
620bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            /**
621bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * Detect leaks of {@link android.app.Activity} subclasses.
622bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             */
623bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            public Builder detectActivityLeaks() {
624758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS);
625758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
626758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
62732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
630fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
631bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * SQLite cursors, Activities, and other closable objects
632bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * but will likely expand in future releases.
63332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
63432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
635d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey                return enable(DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_CURSOR_LEAKS
636a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey                        | DETECT_VM_CLOSABLE_LEAKS | DETECT_VM_REGISTRATION_LEAKS
637a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey                        | DETECT_VM_FILE_URI_EXPOSURE);
63832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
63932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
64032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
64132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
654fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
655fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
656fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
657fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
658fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
659fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
660fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
661fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
662fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
663fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
664fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
665fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
666d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * Detect when a {@link BroadcastReceiver} or
667d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * {@link ServiceConnection} is leaked during {@link Context}
668d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             * teardown.
669d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey             */
670d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            public Builder detectLeakedRegistrationObjects() {
671d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey                return enable(DETECT_VM_REGISTRATION_LEAKS);
672d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            }
673d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
674d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey            /**
6750fa30372c7768692f9deef3e33655382e8a683fdYing Wang             * Detect when a {@code file://} {@link android.net.Uri} is exposed beyond this
676a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey             * app. The receiving app may not have access to the sent path.
677a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey             * Instead, when sharing files between apps, {@code content://}
678a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey             * should be used with permission grants.
679a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey             */
680a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            public Builder detectFileUriExposure() {
681a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey                return enable(DETECT_VM_FILE_URI_EXPOSURE);
682a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            }
683a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey
684a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey            /**
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
70132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
70232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
70332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
71832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
72032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
72132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
72332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
72432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
72532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
72632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
72832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
730758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return new VmPolicy(mMask,
731758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP);
73232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
73332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
73432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
735438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
736438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
7375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
7385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
7395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
7405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
7415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
742cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
743cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
744cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
745703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
746703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
747703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
748703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
7495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
7515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
75332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
75432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
75515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
75632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
75715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
75815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
75915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
760438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
76132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
762438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
76332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
76432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
76532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
76632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
76732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
768727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
769727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
770727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
771727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
772727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
773727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
774727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
775727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
776727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
777727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
778727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
779727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
780727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
78146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
78246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
78346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
78446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
785438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
786438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
787438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
788438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
789438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
790438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
791438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
792438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
793438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
7944b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
7954b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
7967c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
7974b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
7984b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
7994b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
8004b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
8014b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
8024e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8034e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8044e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8054e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException {
8064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        public StrictModeViolation(int policyState, int policyViolated, String message) {
8074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyState, policyViolated, message);
8084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
8094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
8104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
8114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeNetworkViolation extends StrictModeViolation {
8155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
8164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_NETWORK, null);
8175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends StrictModeViolation {
8245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
8254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ, null);
8265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     /**
8304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8314e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick   private static class StrictModeDiskWriteViolation extends StrictModeViolation {
8335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
8344e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE, null);
8355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8384e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
8394e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
8404e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
8414e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeCustomViolation extends StrictModeViolation {
842e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        public StrictModeCustomViolation(int policyMask, String name) {
843e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            super(policyMask, DETECT_CUSTOM, name);
844e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
845e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
846e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
847438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
84815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
849438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
85032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
85132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
85232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
853438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
855438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
856438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
857438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
85932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
86032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
86132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
862e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // TODO: this was a last minute Gingerbread API change (to
863e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // introduce VmPolicy cleanly) but this isn't particularly
864e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // optimal for users who might call this method often.  This
865e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // should be in a thread-local and not allocate on each call.
86632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
86732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
86832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
86932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
87032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
87132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
87232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
87332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
87432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
87597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
87632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
87732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
87897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
87932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
88032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
88132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
88232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
88332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
88497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
88532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
88697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
88797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
88897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
88932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
89032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
89132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
89232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
89332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
89497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
89532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
89697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
89797461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
89832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
89932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
90032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
90132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
90232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
90397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
90432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
90597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
90697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
907f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // We don't want to flash the screen red in the system server
908f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // process, nor do we want to modify all the call sites of
909f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // conditionallyEnableDebugLogging() in the system server,
910f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // so instead we use this to determine if we are the system server.
911f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    private static boolean amTheSystemServerProcess() {
912f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // Fast path.  Most apps don't have the system server's UID.
913f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        if (Process.myUid() != Process.SYSTEM_UID) {
914f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            return false;
915f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
916f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
917f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // The settings app, though, has the system server's UID so
918f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // look up our stack to see if we came from the system server.
919f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        Throwable stack = new Throwable();
920f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        stack.fillInStackTrace();
921f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        for (StackTraceElement ste : stack.getStackTrace()) {
922f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            String clsName = ste.getClassName();
923f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            if (clsName != null && clsName.startsWith("com.android.server.")) {
924f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                return true;
925f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            }
926f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
927f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        return false;
928f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    }
929f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
93097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
93150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
93250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
93350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
93450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
93550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
936bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, false)
937bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate                && !amTheSystemServerProcess();
938bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        final boolean suppress = SystemProperties.getBoolean(DISABLE_PROPERTY, false);
939c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
94050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
94150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
942bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (!doFlashes && (IS_USER_BUILD || suppress)) {
9437c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
94450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
94550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
946c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
947bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // Eng builds have flashes on all the time.  The suppression property
948bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // overrides this, so we force the behavior only after the short-circuit
949bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        // check above.
950bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        if (IS_ENG_BUILD) {
951bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate            doFlashes = true;
952bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate        }
953bc6f0ce8be64a74b695fb21dab5f4805b68dce16Christopher Tate
954be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // Thread policy controls BlockGuard.
955c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
956c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
957c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
958c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
959c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
960c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
961c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
962c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
963c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
964c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
965c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
966c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
967c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
968be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // VM Policy controls CloseGuard, detection of Activity leaks,
969be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // and instance counting.
970c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
971c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
972c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
973d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox();
974d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
975d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                policyBuilder.penaltyLog();
976d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
977d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            setVmPolicy(policyBuilder.build());
978c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
979c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
98050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
98150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
98250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
98350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
984b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
985b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
986b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
987b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
988b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
989b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
990b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
991b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
992b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
993b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
994b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
995b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
9965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
9985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
9995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
1000e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * Input is of the following forms:
1001e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64"
1002e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64 msg=Arbitrary text"
10035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
10045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
10055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
10065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
10075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
10085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
10095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
10105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
10135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
10145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
10175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
10185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
10195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
10205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
10235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
10245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
10255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
10265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
10275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
10285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
10335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1035e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberStartIndex = violationIndex + "violation=".length();
1036e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberEndIndex = message.indexOf(' ', numberStartIndex);
1037e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (numberEndIndex == -1) {
1038e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            numberEndIndex = message.length();
1039e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1040e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        String violationString = message.substring(numberStartIndex, numberEndIndex);
10415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
10425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
10435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
10445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
10455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
10475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1048191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
1049191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
1050191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
1051191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
1052191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
1053191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
1054191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1055bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    // Note: only access this once verifying the thread has a Looper.
1056bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() {
1057bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        @Override protected Handler initialValue() {
1058bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            return new Handler();
1059bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        }
1060bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    };
1061bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1062191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
1063191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
1064191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
1065191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1066438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
1067438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
106846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
106946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
107046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
107146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
107246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
1073438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1074438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
1075438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1076438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1077438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
10795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
10805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
10815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1083438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1084438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
1085438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
1086438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1087438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1088438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1089438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
109032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
1091438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1092438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1093191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1094191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1095191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1096cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
1097cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1098cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1099438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1100438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1101e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // Not part of BlockGuard.Policy; just part of StrictMode:
1102e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        void onCustomSlowCall(String name) {
1103e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if ((mPolicyMask & DETECT_CUSTOM) == 0) {
1104e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1105e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1106e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1107e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1108e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1109e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name);
1110e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            e.fillInStackTrace();
1111e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            startHandlingViolationException(e);
1112e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1113e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
1114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
111632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
1117438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1118438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1119191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1120191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1121191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
1123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1125438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1126438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1127438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1128438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
112932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
1130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1131438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1132b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
1133b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
1134b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
1135191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1136191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1137191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
1139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1141438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1142438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1143438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
1144438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1145438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1146438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
11475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
11485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
11495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
11505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
11515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
11525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
1153cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
1154cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
1155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
1156cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1157438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1158cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
1159cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
1160cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
1161cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
1162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
1163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
1164438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
1165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
1167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
1168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
1169cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
1170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
1171cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
1172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
1173cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
11744e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
11754e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // Also, as a special short-cut case when the only penalty
11764e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // bit is death, we die immediately, rather than timing
11774e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // the violation's duration.  This makes it convenient to
11784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // use in unit tests too, rather than waiting on a Looper.
11794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
1180cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
1181cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
11824e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            if (looper == null ||
1183c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) {
1184cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
1185cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
1186cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1187438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1188438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1189cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
1190191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
1191cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
1192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
1195cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
1196cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
1197cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
1198cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
1199cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1200cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1201cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
12026804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
1203cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
12046804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
12056804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
12066804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
12076804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
12086804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
12096804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
12106804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
1211bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // We post a runnable to a Handler (== delay 0 ms) for
1212bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // measuring the end time of a violation instead of using
1213bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // an IdleHandler (as was previously used) because an
1214bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // IdleHandler may not run for quite a long period of time
1215bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // if an ongoing animation is happening and continually
1216bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // posting ASAP (0 ms) animation steps.  Animations are
1217bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // throttled back to 60fps via SurfaceFlinger/View
1218bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // invalidates, _not_ by posting frame updates every 16
1219bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // milliseconds.
12203761f33387f980d7a6c6e06555a5a932ba7247a2Jeff Sharkey            threadHandler.get().postAtFrontOfQueue(new Runnable() {
1221bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                    public void run() {
1222cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
1223bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1224bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // Note: we do this early, before handling the
1225bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // violation below, as handling the violation
1226bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // may include PENALTY_DEATH and we don't want
1227bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // to keep the red border on.
1228bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        if (windowManager != null) {
1229bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            try {
1230bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                                windowManager.showStrictModeViolation(false);
1231bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            } catch (RemoteException unused) {
1232bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            }
1233bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        }
1234bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1235cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
1236cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
1237cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
1238cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
1239cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
1240cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
1241cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
1242cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
1243cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
1244cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
12455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1246438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
12475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
12485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
12495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
12505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
12515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
1252cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
12555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
12565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
1257438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1258cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
125946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1260cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
1261cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
1262703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
1263cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
1264703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
1265703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
12665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
12675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
12685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1269cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1270cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
12715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
12725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
12735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
12745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1275cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1276727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1277727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1278727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
127946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1280f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
128146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
128246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
128346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
128446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
128546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
128646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
128746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
128846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
128946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1290cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
129146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1292cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
12935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1294cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1295438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1296cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1297438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1298438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1299438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
130071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
130146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
130246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
130346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1304cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
130546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1306cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
130746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1308cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
130946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
131046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1311cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1312cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
131346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
131446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1315cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1316cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1317cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
131832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
131971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
1320c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX;
132171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
132271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
132371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
132471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
132571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
132671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
132771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1328bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
132971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
133071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
133171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
133271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1333438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1334727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1335727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1336727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1337727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
133832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1339727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1340438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1341438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1342cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1343cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1344438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
134546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1346727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1347727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
134832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1349438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1350438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1351438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1352cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
13534e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick                executeDeathPenalty(info);
1354438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1355438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1356438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1357727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
13584e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static void executeDeathPenalty(ViolationInfo info) {
13594e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
13604e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        throw new StrictModeViolation(info.policy, violationBit, null);
13614e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
13624e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
1363bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1364bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1365bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1366bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1367bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1368bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1369bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1370bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1371bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1372bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1373bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1374bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1375bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1376bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1377bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1378bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1379bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1380bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1381bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1382bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1383bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1384bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1385bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1386bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
13871065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
13881065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
13891065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
13901065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
13911065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
13921065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
13931065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
13941065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
13951065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1396bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1397bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1398bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1399bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1400bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1401bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1402bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1403bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1404bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
14054b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
14064b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
14074b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
14084b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
14094b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
14104b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1411727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
14125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
14135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
14145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1415703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1416703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1417703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1418703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1419703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1420703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1421703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1422703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1423703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1424703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
14255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
14265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
14275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1428bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
1429bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
1430bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    public static void conditionallyCheckInstanceCounts() {
1431bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        VmPolicy policy = getVmPolicy();
1432bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (policy.classInstanceLimit.size() == 0) {
1433bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            return;
1434bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1435bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        Runtime.getRuntime().gc();
1436bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        // Note: classInstanceLimit is immutable, so this is lock-free
14375f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) {
14385f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Class klass = entry.getKey();
14395f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            int limit = entry.getValue();
1440bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            long instances = VMDebug.countInstancesOfClass(klass, false);
1441bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (instances <= limit) {
1442bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                continue;
1443bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1444bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Throwable tr = new InstanceCountViolation(klass, instances, limit);
1445bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            onVmPolicyViolation(tr.getMessage(), tr);
1446bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1447bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
1448bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1449bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static long sLastInstanceCountCheckMillis = 0;
14505f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static boolean sIsIdlerRegistered = false;  // guarded by StrictMode.class
1451bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final MessageQueue.IdleHandler sProcessIdleHandler =
1452bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            new MessageQueue.IdleHandler() {
1453bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                public boolean queueIdle() {
1454bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    long now = SystemClock.uptimeMillis();
1455bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    if (now - sLastInstanceCountCheckMillis > 30 * 1000) {
1456bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        sLastInstanceCountCheckMillis = now;
1457bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        conditionallyCheckInstanceCounts();
1458bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    }
1459bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    return true;
1460bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1461bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            };
1462bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1463bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
146432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
146532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
146632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
146732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
146832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
146932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
147032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
14715f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14725f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicy = policy;
14735f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicyMask = policy.mask;
14745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
14755f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
14765f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Looper looper = Looper.getMainLooper();
14775f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (looper != null) {
14785f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                MessageQueue mq = looper.mQueue;
1479c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                if (policy.classInstanceLimit.size() == 0 ||
1480c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    (sVmPolicyMask & VM_PENALTY_MASK) == 0) {
1481bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.removeIdleHandler(sProcessIdleHandler);
1482c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    sIsIdlerRegistered = false;
1483bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                } else if (!sIsIdlerRegistered) {
1484bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.addIdleHandler(sProcessIdleHandler);
1485bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    sIsIdlerRegistered = true;
1486bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1487bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1488bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
148932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
149032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
149132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
149232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
149332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
149432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
14955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14965f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            return sVmPolicy;
14975f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
149832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
149932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
150032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
150162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
150262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
150362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1504fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1505fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1506fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
150762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
150862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
150962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
151062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
151162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
151262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
151362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
1514e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                               .detectAll()
151562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
151662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
151762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
151862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
151962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
152032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
152132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
152232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
152332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
152432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
152532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
152632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
152732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
152832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1529fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1530fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1531fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1532fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1533fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1534fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1535fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
1536d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static boolean vmRegistrationLeaksEnabled() {
1537d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        return (sVmPolicyMask & DETECT_VM_REGISTRATION_LEAKS) != 0;
1538d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1539d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1540d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1541d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1542d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
1543a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    public static boolean vmFileUriExposureEnabled() {
1544a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        return (sVmPolicyMask & DETECT_VM_FILE_URI_EXPOSURE) != 0;
1545a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    }
1546a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey
1547a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    /**
1548a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     * @hide
1549a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     */
155032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
15514b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
15524b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
15534b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
155408d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    /**
155508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     * @hide
155608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     */
155708d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) {
155808d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block        onVmPolicyViolation(null, originStack);
155908d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    }
156008d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block
1561d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1562d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1563d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
1564d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static void onIntentReceiverLeaked(Throwable originStack) {
1565d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        onVmPolicyViolation(null, originStack);
1566d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1567d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1568d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    /**
1569d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     * @hide
1570d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey     */
1571d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    public static void onServiceConnectionLeaked(Throwable originStack) {
1572d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey        onVmPolicyViolation(null, originStack);
1573d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey    }
1574d7026f1612a7c1e8cc9b484038fa99b7fc29cfd7Jeff Sharkey
1575a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    /**
1576a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     * @hide
1577a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey     */
1578a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    public static void onFileUriExposed(String location) {
1579a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        final String message = "file:// Uri exposed through " + location;
1580a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey        onVmPolicyViolation(message, new Throwable(message));
1581a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey    }
1582a14acd20b8d563319ea1a5974dca0e9a29f0aaefJeff Sharkey
1583bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Map from VM violation fingerprint to uptime millis.
1584bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>();
1585bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15864b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
15874b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
15884b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
15894b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
1590bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1591bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1592bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0;
1593bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1594bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        // Erase stuff not relevant for process-wide violations
15965f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.numAnimationsRunning = 0;
15975f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.tags = null;
15985f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.broadcastIntentAction = null;
15995f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
1600bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Integer fingerprint = info.hashCode();
1601bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long now = SystemClock.uptimeMillis();
1602bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long lastViolationTime = 0;
1603bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long timeSinceLastViolationMillis = Long.MAX_VALUE;
1604bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        synchronized (sLastVmViolationTime) {
1605bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (sLastVmViolationTime.containsKey(fingerprint)) {
1606bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                lastViolationTime = sLastVmViolationTime.get(fingerprint);
1607bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                timeSinceLastViolationMillis = now - lastViolationTime;
1608bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1609bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1610bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                sLastVmViolationTime.put(fingerprint, now);
1611bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
161232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
161332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1614bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1615bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Log.e(TAG, message, originStack);
1616bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1617bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1618bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask);
1619bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1620bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1621bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1622bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1623bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1624bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1625bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1626bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
162732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1628bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyDropbox && lastViolationTime == 0) {
162932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
163032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
163132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
163232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
163332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
163432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
163532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
163632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
163732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
163832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
163932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
164032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
164132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
164232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
164332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
164432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
164532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
164632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
164732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
164832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
164932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
165032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
165132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
165232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1653bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
165432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
165532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
165632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
165732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
165832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
165932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
166032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
16615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
16625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
16635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1664cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1665703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1666703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1667703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1668703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1669703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1670703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1671703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1672703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1673703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
16745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1675703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
16765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
16795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
16815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
16825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
16835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
16845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
16855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
16865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1687cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
16885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
16895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
169032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1691cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
16925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
16945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
16955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1696cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1697cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
16985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
16995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1700cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
17015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
17025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
17035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
17045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
17055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1706727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1707727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1708727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1709727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1710727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1711727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1712727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1713727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1714727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1715727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1716cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1717cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1718e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1719e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1720e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1721e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1722e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1723e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1724e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1725e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1726e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1727e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1728e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1729e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1730e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1731e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1732e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1733e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17361181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
17371181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
17381181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
17391181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
17401181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1741e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1742e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1752e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1753e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1754e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1755e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1756e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1758e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1759e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1760e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1761e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1762e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1763e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17701cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
17711cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
17721cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
17731cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1784e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1789e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17901181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
17911181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
17921181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
17931181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
17941181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
17951181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
17961181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1797e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1798e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1799e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1800e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1801e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1802e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1803e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1804e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1805e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1806e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1807e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1808e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1809e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1810e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1811e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1812e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1813e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1814e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1815e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1816e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1817e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1818e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1819e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1820e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1821e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1822cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1823cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1824cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1825cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1826cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1827cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1828e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1829e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1830e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1831e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1832e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1833e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1834e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1835e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1836e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1837e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1838e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1839e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1840e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1841e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1842e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1843e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
18441181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
18451181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
18461181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1847e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1848e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1849e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1850e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1851e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1852e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1853e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1854e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1855e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1856e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1857e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1858e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1859e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1860e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1861e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1862e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1863e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1864e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1865e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1866e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1867e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1868e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1869e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
18701cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1871e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1872e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1873e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1874e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1875e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
1876e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For code to note that it's slow.  This is a no-op unless the
1877e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * current thread's {@link android.os.StrictMode.ThreadPolicy} has
1878e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls}
1879e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * enabled.
1880e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
1881e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @param name a short string for the exception stack trace that's
1882e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *             built if when this fires.
1883e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
1884e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static void noteSlowCall(String name) {
1885e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
1886e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
1887e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            // StrictMode not enabled.
1888e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            return;
1889e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1890e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name);
1891e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
1892e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1893e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
18944e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18954e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18964e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskRead() {
18974e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18984e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18994e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
19004e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
19014e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
19024e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onReadFromDisk();
19034e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
19044e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
19054e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
19064e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
19074e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
19084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskWrite() {
19094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
19104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
19114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
19124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
19134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
19144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onWriteToDisk();
19154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
19164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
19175f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    // Guarded by StrictMode.class
19185f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static final HashMap<Class, Integer> sExpectedActivityInstanceCount =
19195f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new HashMap<Class, Integer>();
19205f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
19227e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * Returns an object that is used to track instances of activites.
19237e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * The activity should store a reference to the tracker object in one of its fields.
19247e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * @hide
19257e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     */
19267e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    public static Object trackActivity(Object instance) {
19277e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        return new InstanceTracker(instance);
19287e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
19297e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19307e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    /**
1931758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
1932758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
19335f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void incrementExpectedActivityCount(Class klass) {
19347e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1935758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1936758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
19377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19385f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
19397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
19407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
19417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
19427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19435f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
19445f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer newExpected = expected == null ? 1 : expected + 1;
19455f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sExpectedActivityInstanceCount.put(klass, newExpected);
19465f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
19475f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    }
19485f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19495f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    /**
19505f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     * @hide
19515f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     */
19525f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void decrementExpectedActivityCount(Class klass) {
19537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1954758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1955758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
19567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        final int limit;
19585f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
19597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
19607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
19617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
19627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
19647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
19655f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (newExpected == 0) {
19665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.remove(klass);
19675f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            } else {
19685f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.put(klass, newExpected);
19695f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            }
19707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19715f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // Note: adding 1 here to give some breathing room during
19725f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // orientation changes.  (shouldn't be necessary, though?)
19737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            limit = newExpected + 1;
19745f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
19755f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
19767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Quick check.
19777e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        int actual = InstanceTracker.getInstanceCount(klass);
19787e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (actual <= limit) {
19797e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            return;
19807e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
19817e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19827e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Do a GC and explicit count to double-check.
19837e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // This is the work that we are trying to avoid by tracking the object instances
19847e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // explicity.  Running an explicit GC can be expensive (80ms) and so can walking
19857e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // the heap to count instance (30ms).  This extra work can make the system feel
19867e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // noticeably less responsive during orientation changes when activities are
19877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // being restarted.  Granted, it is only a problem when StrictMode is enabled
19887e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // but it is annoying.
19897e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        Runtime.getRuntime().gc();
19907e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        long instances = VMDebug.countInstancesOfClass(klass, false);
19927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (instances > limit) {
19937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            Throwable tr = new InstanceCountViolation(klass, instances, limit);
19947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            onVmPolicyViolation(tr.getMessage(), tr);
19955f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
1996758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    }
1997758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
1998758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
1999cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
2000cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
2001cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
2002cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
2003cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
2004cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
2005cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2006cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
2007cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2008cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
2009cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2010cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2011cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
2012cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2013cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
2014cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2015cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2016cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
2017cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
2018cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2019cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
2020cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2021cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2022599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
2023599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
2024599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
2025599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
2026599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
2027e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
2028e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
2029e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
2030e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
2031e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
2032e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
2039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2040cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2041cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
2042cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
2043cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2044cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
2045cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2047bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
2048bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
2049bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
2050bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
2051bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
2052bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
2053bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * If this is a instance count violation, the number of instances in memory,
2054bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * else -1.
2055bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         */
2056bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public long numInstances = -1;
2057bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2058bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        /**
2059cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
2060cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
2063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
2064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2066cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
2070cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
2071cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
2072cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
2073599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
2074bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
2075bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
2076bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
2077bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2078e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
2079bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (tr instanceof InstanceCountViolation) {
2080bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                this.numInstances = ((InstanceCountViolation) tr).mInstances;
2081bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2082e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
2083e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
2084e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
2085e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
2086e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2087e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
2088e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
2089e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
2090e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
2091e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
2092e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
2093e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
2094e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
2095e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
2096e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2097e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2098cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2099cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2100f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
2101f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
2102f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
2103f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
2104f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2105f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
2106f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2107f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
2108f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
2109f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2110f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
2111f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
2112f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
2113f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
2114f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2115f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
2116f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
2117f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
2118cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2119cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2120cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2121cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
2122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
2123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2124cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2125cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2126cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2127cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
2128cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
2129cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
2130cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2131cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
2132cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
2133cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
2134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
2135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
2136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
2137cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
2138cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
2140cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
2141599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
2142cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
2143bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            numInstances = in.readLong();
2144bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
2145e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
2146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2148cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
2150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
2152cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
2153cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
2154cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
2155cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
2156599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
2157cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
2158bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            dest.writeLong(numInstances);
2159bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
2160e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
2161cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2162cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2163cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2164cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2165cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
2166cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2167cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
2168cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
2169cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
2170cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
2171cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
2172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2173bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (numInstances != -1) {
2174bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                pw.println(prefix + "numInstances: " + numInstances);
2175bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2176cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
2177cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
2178cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2179599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2180599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
2181599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
2182cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
2183bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
2184bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
2185bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2186e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
2187e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
2188e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
2189e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
2190e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2191e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
2195bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2196bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Dummy throwable, for now, since we don't know when or where the
2197bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // leaked instances came from.  We might in the future, but for
2198bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // now we suppress the stack trace because it's useless and/or
2199bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // misleading.
2200bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static class InstanceCountViolation extends Throwable {
2201bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Class mClass;
2202bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long mInstances;
2203bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final int mLimit;
2204bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
22055f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        private static final StackTraceElement[] FAKE_STACK = {
22065f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit",
22075f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                                  "StrictMode.java", 1)
22085f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        };
2209bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2210bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public InstanceCountViolation(Class klass, long instances, int limit) {
22115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            super(klass.toString() + "; instances=" + instances + "; limit=" + limit);
2212bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            setStackTrace(FAKE_STACK);
2213bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mClass = klass;
2214bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mInstances = instances;
2215bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mLimit = limit;
2216bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
2217bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
22187e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22197e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    private static final class InstanceTracker {
22207e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private static final HashMap<Class<?>, Integer> sInstanceCounts =
22217e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                new HashMap<Class<?>, Integer>();
22227e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22237e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private final Class<?> mKlass;
22247e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22257e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public InstanceTracker(Object instance) {
22267e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            mKlass = instance.getClass();
22277e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22287e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
22297e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(mKlass);
22307e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final int newValue = value != null ? value + 1 : 1;
22317e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                sInstanceCounts.put(mKlass, newValue);
22327e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22337e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22347e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22357e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        @Override
22367e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        protected void finalize() throws Throwable {
22377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            try {
22387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                synchronized (sInstanceCounts) {
22397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    final Integer value = sInstanceCounts.get(mKlass);
22407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    if (value != null) {
22417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        final int newValue = value - 1;
22427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        if (newValue > 0) {
22437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.put(mKlass, newValue);
22447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        } else {
22457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.remove(mKlass);
22467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        }
22477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    }
22487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                }
22497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            } finally {
22507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                super.finalize();
22517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
22547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public static int getInstanceCount(Class<?> klass) {
22557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
22567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(klass);
22577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return value != null ? value : 0;
22587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
22597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
22607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
2261438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
2262