StrictMode.java revision be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68
1438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/*
2438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project
3438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
4438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License");
5438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * you may not use this file except in compliance with the License.
6438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * You may obtain a copy of the License at
7438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
8438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *      http://www.apache.org/licenses/LICENSE-2.0
9438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick *
10438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software
11438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS,
12438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * See the License for the specific language governing permissions and
14438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * limitations under the License.
15438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
16438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpackage android.os;
17438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
18599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrickimport android.animation.ValueAnimator;
19438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ActivityManagerNative;
20bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.app.ActivityThread;
21438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ApplicationErrorReport;
221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrickimport android.app.IActivityManager;
23bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent;
24438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log;
25cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer;
26cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton;
276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager;
28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
29438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit;
30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
31438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard;
32fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard;
33bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrickimport dalvik.system.VMDebug;
34438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter;
365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter;
375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList;
3846d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap;
395f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrickimport java.util.Map;
40bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger;
4146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
42438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/**
4332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be
4432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix
4532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them.
4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or
4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI
4915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place.  Keeping disk
5015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother,
519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications.  By keeping your application's main thread
529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent
539fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a>
549fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users.
5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is
5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that
5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency.  It's often the case that
5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be
6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background
6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes.  If possible, it's best to assume that such
6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p>
6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your
6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or
6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's
6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method:
6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre>
7015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() {
7115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     if (DEVELOPER_MODE) {
7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}()
7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskReads()
7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskWrites()
7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectNetwork()   // or .detectAll() for all detectable problems
7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
7732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
7962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick *                 .detectLeakedSqlLiteObjects()
80fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom *                 .detectLeakedClosableObjects()
8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyDeath()
8332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     }
8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     super.onCreate();
8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * }
8715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre>
8815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected.
9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can
9132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your
9232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen.
9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are
9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler},
9615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc.
9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds.  In particular,
9832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle.  Use
9932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident.  Network requests on the UI thread
10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though.
10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not
10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses.  While it does
10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing
10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best
10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism.  Notably, disk or network access from JNI calls
10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it.  Future versions of Android may catch
10815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode
10915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market.
110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode {
112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static final String TAG = "StrictMode";
11382829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
1166804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE);
1171181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
118c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    /**
119c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * The boolean system property to control screen flashes on violations.
120c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     *
121c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * @hide
122c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     */
123c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual";
124c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
12546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only log a duplicate stack trace to the logs every second.
12646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_LOG_INTERVAL_MS = 1000;
12746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
12846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only show an annoying dialog at most every 30 seconds
12946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_DIALOG_INTERVAL_MS = 30000;
13046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
131e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    // How many Span tags (e.g. animations) to report.
132e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final int MAX_SPAN_TAGS = 20;
133e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
134191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // How many offending stacks to keep track of (and time) per loop
135191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // of the Looper.
136191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final int MAX_OFFENSES_PER_LOOP = 10;
137191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
13832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Thread-policy:
139438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
14015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
14215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_WRITE = 0x01;  // for ThreadPolicy
14415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
14515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick      * @hide
14715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_READ = 0x02;  // for ThreadPolicy
14915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
15015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_NETWORK = 0x04;  // for ThreadPolicy
154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
155e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
156e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For StrictMode.noteSlowCall()
157e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
158e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @hide
159e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
160e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static final int DETECT_CUSTOM = 0x08;  // for ThreadPolicy
161e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
162e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    private static final int ALL_THREAD_DETECT_BITS =
163e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK | DETECT_CUSTOM;
164e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
16532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Process-policy:
166438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
167438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
16832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Note, a "VM_" bit, not thread.
16932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
17032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
171758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for VmPolicy
17232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
17332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
174fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * Note, a "VM_" bit, not thread.
175fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
176fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
177758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for VmPolicy
178758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
179758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
180758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * Note, a "VM_" bit, not thread.
181758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
182758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
183758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    public static final int DETECT_VM_ACTIVITY_LEAKS = 0x800;  // for VmPolicy
184fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
185fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
18632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
187438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
188bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int DETECT_VM_INSTANCE_LEAKS = 0x1000;  // for VmPolicy
189bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
190bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final int ALL_VM_DETECT_BITS =
191bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS |
192bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            DETECT_VM_ACTIVITY_LEAKS | DETECT_VM_INSTANCE_LEAKS;
193bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
194bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
195bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
196bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
197438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
198438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
19932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
20032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
201438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
20232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
203438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
204438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
205438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
206438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
207b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
208b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
20932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
210438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
211438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
212438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
213438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
214b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
215b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
216b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
217b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
218b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
219b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
220b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
2216804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
2226804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
2236804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
2246804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
2256804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
2266804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
22832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
229438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
230438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
231438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
232727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
233727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
234727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
235727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
236727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
237727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
238727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
239703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
240703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
241727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
242727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
243727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
244727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
246c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for thread policies.
24771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
248c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int THREAD_PENALTY_MASK =
249b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
25171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
252758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
253c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    /**
254c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     * Mask of all the penalty bits valid for VM policies.
255c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick     */
256c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick    private static final int VM_PENALTY_MASK =
257c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick            PENALTY_LOG | PENALTY_DEATH | PENALTY_DROPBOX;
258c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
259c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick
260758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // TODO: wrap in some ImmutableHashMap thing.
261758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    // Note: must be before static initialization of sVmPolicy.
262758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static final HashMap<Class, Integer> EMPTY_CLASS_LIMIT_MAP = new HashMap<Class, Integer>();
263758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
26471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
266758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     *
267758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * TODO: these are redundant (mask is in VmPolicy).  Should remove sVmPolicyMask.
26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
270758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    private static volatile VmPolicy sVmPolicy = VmPolicy.LAX;
27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
272bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
273bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
274bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
275bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
276bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
277bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
309320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * Creates {@link ThreadPolicy} instances.  Methods whose names start
31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
320320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
324320274c5f17057a3a823fed50b7027cbd46fc025Brad Fitzpatrick         * StrictMode.setThreadPolicy(policy);
32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
354e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(ALL_THREAD_DETECT_BITS);
35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
361e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return disable(ALL_THREAD_DETECT_BITS);
36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
38632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
38732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
38932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
39032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
39132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
39232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
393e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
394e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
395e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder detectCustomSlowCalls() {
396e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
397e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
398e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
399e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
400e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             * Enable detection of disk reads.
401e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick             */
402e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            public Builder permitCustomSlowCalls() {
403e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return enable(DETECT_CUSTOM);
404e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
405e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
406e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            /**
40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
432b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
433b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
434b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
435b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
442b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
443b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
444b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
445b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
446b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
447b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
448b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
449b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
450b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
451b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
452b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
453b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
4546804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
4556804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4566804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4576804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4586804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4596804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4606804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
516758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0, EMPTY_CLASS_LIMIT_MAP);
51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
520758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        // Map from class to max number of allowed instances in memory.
521758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        final HashMap<Class, Integer> classInstanceLimit;
522758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
523758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        private VmPolicy(int mask, HashMap<Class, Integer> classInstanceLimit) {
524758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            if (classInstanceLimit == null) {
525758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                throw new NullPointerException("classInstanceLimit == null");
526758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
528758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            this.classInstanceLimit = classInstanceLimit;
52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
558758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private HashMap<Class, Integer> mClassInstanceLimit;  // null until needed
559758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            private boolean mClassInstanceLimitNeedCow = false;  // need copy-on-write
560758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
561758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder() {
562758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = 0;
563758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
564758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
565758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
566758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Build upon an existing VmPolicy.
567758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
568758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder(VmPolicy base) {
569758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mMask = base.mask;
570758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimitNeedCow = true;
571758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit = base.classInstanceLimit;
572758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
573758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
574758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            /**
575758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * Set an upper bound on how many instances of a class can be in memory
576758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             * at once.  Helps to prevent object leaks.
577758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick             */
578758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            public Builder setClassInstanceLimit(Class klass, int instanceLimit) {
579758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (klass == null) {
580758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    throw new NullPointerException("klass == null");
581758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
582758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                if (mClassInstanceLimitNeedCow) {
583758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    if (mClassInstanceLimit.containsKey(klass) &&
584758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit.get(klass) == instanceLimit) {
585758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        // no-op; don't break COW
586758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        return this;
587758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    }
588758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimitNeedCow = false;
589758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = (HashMap<Class, Integer>) mClassInstanceLimit.clone();
590758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                } else if (mClassInstanceLimit == null) {
591758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                    mClassInstanceLimit = new HashMap<Class, Integer>();
592758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                }
593bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                mMask |= DETECT_VM_INSTANCE_LEAKS;
594758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                mClassInstanceLimit.put(klass, instanceLimit);
595758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return this;
596758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
597758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
598bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            /**
599bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * Detect leaks of {@link android.app.Activity} subclasses.
600bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             */
601bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            public Builder detectActivityLeaks() {
602758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS);
603758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            }
604758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
60532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
60632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
60732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
608fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
609bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * SQLite cursors, Activities, and other closable objects
610bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick             * but will likely expand in future releases.
61132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
61232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
613758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return enable(DETECT_VM_ACTIVITY_LEAKS |
614758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS);
61532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
61632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
61732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
61832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
61932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
62032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
62132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
62232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
62332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
62432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
62532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
62632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
62732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
62832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
62932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
63032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
631fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
632fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
633fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
634fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
635fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
636fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
637fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
638fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
639fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
640fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
641fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
642fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
64432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
64532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
64632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
64732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
65232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
65332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
65632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
65732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
65832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
65932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
66032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
66132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
66432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
66532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
66632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
66732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
66832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
688758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                return new VmPolicy(mMask,
689758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick                        mClassInstanceLimit != null ? mClassInstanceLimit : EMPTY_CLASS_LIMIT_MAP);
69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
693438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
694438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
6955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
6965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
6975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
6985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
6995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
700cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
701cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
702cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
703703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
704703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
705703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
706703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
7075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
7095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
71315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
71515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
71615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
71715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
718438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
720438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
72132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
72232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
72332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
72432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
72532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
726727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
727727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
728727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
729727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
730727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
731727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
732727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
733727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
734727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
735727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
736727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
737727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
738727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
73946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
74046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
74146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
74246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
743438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
744438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
745438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
746438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
747438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
748438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
749438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
750438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
751438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
7524b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
7534b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
7547c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
7554b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
7564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
7574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
7584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
7594b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
7604e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7614e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7624e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7634e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeViolation extends BlockGuard.BlockGuardPolicyException {
7644e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        public StrictModeViolation(int policyState, int policyViolated, String message) {
7654e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyState, policyViolated, message);
7664e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
7674e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
7684e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
7694e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7704e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7714e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7724e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static class StrictModeNetworkViolation extends StrictModeViolation {
7735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
7744e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_NETWORK, null);
7755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7784e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7794e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7804e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7814e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends StrictModeViolation {
7825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
7834e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ, null);
7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7874e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     /**
7884e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7894e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7904e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick   private static class StrictModeDiskWriteViolation extends StrictModeViolation {
7915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
7924e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE, null);
7935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
7964e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
7974e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
7984e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
7994e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static class StrictModeCustomViolation extends StrictModeViolation {
800e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        public StrictModeCustomViolation(int policyMask, String name) {
801e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            super(policyMask, DETECT_CUSTOM, name);
802e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
803e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
804e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
805438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
80615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
807438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
80832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
80932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
81032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
811438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
81232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
813438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
814438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
815438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
81732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
81832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
81932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
820e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // TODO: this was a last minute Gingerbread API change (to
821e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // introduce VmPolicy cleanly) but this isn't particularly
822e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // optimal for users who might call this method often.  This
823e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // should be in a thread-local and not allocate on each call.
82432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
82532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
82632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
82732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
82832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
82932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
83032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
83132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
83232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
83397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
83432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
83532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
83697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
83732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
83832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
83932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
84032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
84132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
84297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
84332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
84497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
84597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
84697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
84732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
84832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
84932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
85032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
85132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
85297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
85332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
85497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
85597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
85632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
85732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
85832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
85932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
86032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
86197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
86232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
86397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
86497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
865f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // We don't want to flash the screen red in the system server
866f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // process, nor do we want to modify all the call sites of
867f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // conditionallyEnableDebugLogging() in the system server,
868f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    // so instead we use this to determine if we are the system server.
869f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    private static boolean amTheSystemServerProcess() {
870f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // Fast path.  Most apps don't have the system server's UID.
871f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        if (Process.myUid() != Process.SYSTEM_UID) {
872f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            return false;
873f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
874f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
875f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // The settings app, though, has the system server's UID so
876f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        // look up our stack to see if we came from the system server.
877f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        Throwable stack = new Throwable();
878f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        stack.fillInStackTrace();
879f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        for (StackTraceElement ste : stack.getStackTrace()) {
880f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            String clsName = ste.getClassName();
881f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            if (clsName != null && clsName.startsWith("com.android.server.")) {
882f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                return true;
883f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick            }
884f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        }
885f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        return false;
886f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick    }
887f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick
88897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
88950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
89050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
89150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
89250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
89350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
894f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick        boolean doFlashes = !amTheSystemServerProcess() &&
895f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick                SystemProperties.getBoolean(VISUAL_PROPERTY, IS_ENG_BUILD);
896c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
89750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
89850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
899c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD && !doFlashes) {
9007c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
90150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
90250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
903c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
904be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // Thread policy controls BlockGuard.
905c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
906c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
907c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
908c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
909c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
910c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
911c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
912c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
913c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
914c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
915c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
916c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
917c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
918be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // VM Policy controls CloseGuard, detection of Activity leaks,
919be7c29c9f7e3d7b75d4374c8b5c0ca43c9d09c68Jeff Brown        // and instance counting.
920c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
921c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
922c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
923d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            VmPolicy.Builder policyBuilder = new VmPolicy.Builder().detectAll().penaltyDropBox();
924d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            if (IS_ENG_BUILD) {
925d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown                policyBuilder.penaltyLog();
926d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            }
927d5875d98f06817f78bd974842a8a9c2d41802d20Jeff Brown            setVmPolicy(policyBuilder.build());
928c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
929c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
93050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
93150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
93250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
93350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
934b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
935b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
936b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
937b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
938b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
939b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
940b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
941b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
942b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
943b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
944b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
945b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
9465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
9475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
9485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
9495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
950e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * Input is of the following forms:
951e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64"
952e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *     "policy=137 violation=64 msg=Arbitrary text"
9535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
9545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
9555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
9565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
9575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
9595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
9605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
9635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
9645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
9675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
9685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
9695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
9705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
9735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
9745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
9755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
9765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
9775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
9795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
985e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberStartIndex = violationIndex + "violation=".length();
986e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        int numberEndIndex = message.indexOf(' ', numberStartIndex);
987e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (numberEndIndex == -1) {
988e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            numberEndIndex = message.length();
989e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
990e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        String violationString = message.substring(numberStartIndex, numberEndIndex);
9915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
9925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
9935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
9945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
9955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
9965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
998191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
999191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
1000191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
1001191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
1002191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
1003191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
1004191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1005bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    // Note: only access this once verifying the thread has a Looper.
1006bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() {
1007bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        @Override protected Handler initialValue() {
1008bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            return new Handler();
1009bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick        }
1010bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick    };
1011bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1012191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
1013191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
1014191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
1015191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
1016438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
1017438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
101846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
101946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
102046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
102146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
102246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
1023438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1024438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
1025438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1026438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1027438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
10295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
10325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
1033438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1034438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
1035438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
1036438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1037438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1038438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1039438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
104032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
1041438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1042438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1043191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1044191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1045191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
1047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1049438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1050438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1051e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        // Not part of BlockGuard.Policy; just part of StrictMode:
1052e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        void onCustomSlowCall(String name) {
1053e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if ((mPolicyMask & DETECT_CUSTOM) == 0) {
1054e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1055e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1056e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1057e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                return;
1058e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            }
1059e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeCustomViolation(mPolicyMask, name);
1060e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            e.fillInStackTrace();
1061e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            startHandlingViolationException(e);
1062e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1063e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick
1064438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1065438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
106632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
1067438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1068438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1069191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1070191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1071191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1072cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
1073cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1074cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1075438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1076438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1077438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
1078438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
107932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
1080438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
1081438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1082b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
1083b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
1084b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
1085191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
1086191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
1087191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
1088cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
1089cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
1090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
1091438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1092438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1093438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
1094438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
1095438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1096438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
10975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
10985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
10995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
11005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
11015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
11025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
1103cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
1104cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
1105cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
1106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1107438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
1109cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
1110cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
1111cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
1112cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
1113cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
1114438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
1115cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1116cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
1117cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
1118cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
1119cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
1120cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
1121cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
1122cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
1123cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
11244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
11254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // Also, as a special short-cut case when the only penalty
11264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // bit is death, we die immediately, rather than timing
11274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // the violation's duration.  This makes it convenient to
11284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // use in unit tests too, rather than waiting on a Looper.
11294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            //
1130cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
1131cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
11324e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            if (looper == null ||
1133c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                (info.policy & THREAD_PENALTY_MASK) == PENALTY_DEATH) {
1134cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
1135cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
1136cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1138438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1139cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
1140191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
1141cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
1142cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1143cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1144cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
1145cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
1146cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
1147cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
1148cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
1149cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
1150cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1151cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
11526804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
1153cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
11546804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
11556804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
11566804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
11576804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
11586804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
11596804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
11606804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
1161bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // We post a runnable to a Handler (== delay 0 ms) for
1162bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // measuring the end time of a violation instead of using
1163bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // an IdleHandler (as was previously used) because an
1164bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // IdleHandler may not run for quite a long period of time
1165bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // if an ongoing animation is happening and continually
1166bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // posting ASAP (0 ms) animation steps.  Animations are
1167bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // throttled back to 60fps via SurfaceFlinger/View
1168bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // invalidates, _not_ by posting frame updates every 16
1169bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            // milliseconds.
1170bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick            threadHandler.get().post(new Runnable() {
1171bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                    public void run() {
1172cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
1173bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1174bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // Note: we do this early, before handling the
1175bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // violation below, as handling the violation
1176bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // may include PENALTY_DEATH and we don't want
1177bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        // to keep the red border on.
1178bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        if (windowManager != null) {
1179bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            try {
1180bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                                windowManager.showStrictModeViolation(false);
1181bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            } catch (RemoteException unused) {
1182bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                            }
1183bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick                        }
1184bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick
1185cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
1186cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
1187cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
1188cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
1189cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
1190cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
1191cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
1192cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
1193cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
1194cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
11955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1196438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
11975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
11985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
11995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
12005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
12015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
1202cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
1203cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
1204cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
12055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
12065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
1207438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1208cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
120946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1210cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
1211cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
1212703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
1213cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
1214703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
1215703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
12165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
12175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
12185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1219cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1220cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
12215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
12225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
12235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
12245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1225cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1226727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1227727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1228727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
122946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1230f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
123146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
123246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
123346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
123446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
123546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
123646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
123746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
123846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
123946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1240cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
124146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1242cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
12435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1244cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1245438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1246cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1247438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1248438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1249438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
125071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
125146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
125246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
125346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1254cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
125546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1256cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
125746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1258cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
125946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
126046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1261cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1262cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
126346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
126446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1265cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1266cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1267cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
126832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
126971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
1270c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                final boolean justDropBox = (info.policy & THREAD_PENALTY_MASK) == PENALTY_DROPBOX;
127171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
127271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
127371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
127471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
127571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
127671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
127771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1278bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
127971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
128071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
128171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
128271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1283438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1284727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1285727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1286727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1287727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
128832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1289727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1290438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1291438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1292cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1293cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1294438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
129546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1296727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1297727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
129832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1299438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1300438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1301438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1302cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
13034e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick                executeDeathPenalty(info);
1304438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1305438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1306438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1307727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
13084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    private static void executeDeathPenalty(ViolationInfo info) {
13094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
13104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        throw new StrictModeViolation(info.policy, violationBit, null);
13114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
13124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
1313bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1314bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1315bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1316bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1317bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1318bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1319bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1320bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1321bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1322bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1323bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1324bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1325bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1326bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1327bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1328bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1329bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1330bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1331bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1332bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1333bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1334bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1335bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1336bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
13371065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
13381065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
13391065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
13401065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
13411065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
13421065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
13431065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
13441065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
13451065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1346bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1347bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1348bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1349bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1350bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1351bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1352bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1353bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1354bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
13554b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
13564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
13574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
13584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
13594b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
13604b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1361727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
13625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
13635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
13645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1365703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1366703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1367703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1368703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1369703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1370703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1371703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1372703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1373703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1374703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
13755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
13765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
13775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1378bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     * @hide
1379bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick     */
1380bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    public static void conditionallyCheckInstanceCounts() {
1381bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        VmPolicy policy = getVmPolicy();
1382bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (policy.classInstanceLimit.size() == 0) {
1383bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            return;
1384bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1385bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        Runtime.getRuntime().gc();
1386bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        // Note: classInstanceLimit is immutable, so this is lock-free
13875f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        for (Map.Entry<Class, Integer> entry : policy.classInstanceLimit.entrySet()) {
13885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Class klass = entry.getKey();
13895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            int limit = entry.getValue();
1390bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            long instances = VMDebug.countInstancesOfClass(klass, false);
1391bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (instances <= limit) {
1392bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                continue;
1393bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1394bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Throwable tr = new InstanceCountViolation(klass, instances, limit);
1395bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            onVmPolicyViolation(tr.getMessage(), tr);
1396bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1397bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
1398bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1399bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static long sLastInstanceCountCheckMillis = 0;
14005f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static boolean sIsIdlerRegistered = false;  // guarded by StrictMode.class
1401bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final MessageQueue.IdleHandler sProcessIdleHandler =
1402bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            new MessageQueue.IdleHandler() {
1403bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                public boolean queueIdle() {
1404bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    long now = SystemClock.uptimeMillis();
1405bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    if (now - sLastInstanceCountCheckMillis > 30 * 1000) {
1406bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        sLastInstanceCountCheckMillis = now;
1407bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                        conditionallyCheckInstanceCounts();
1408bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    }
1409bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    return true;
1410bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1411bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            };
1412bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1413bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    /**
141432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
141532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
141632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
141732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
141832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
141932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
142032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
14215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicy = policy;
14235f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sVmPolicyMask = policy.mask;
14245f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
14255f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
14265f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Looper looper = Looper.getMainLooper();
14275f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (looper != null) {
14285f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                MessageQueue mq = looper.mQueue;
1429c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                if (policy.classInstanceLimit.size() == 0 ||
1430c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    (sVmPolicyMask & VM_PENALTY_MASK) == 0) {
1431bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.removeIdleHandler(sProcessIdleHandler);
1432c0bb0bb5e3425b77b6e7820ebe25fe72bdd07782Brad Fitzpatrick                    sIsIdlerRegistered = false;
1433bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                } else if (!sIsIdlerRegistered) {
1434bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    mq.addIdleHandler(sProcessIdleHandler);
1435bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                    sIsIdlerRegistered = true;
1436bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                }
1437bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1438bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
143932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
144032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
144132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
144232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
144332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
144432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
14455f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
14465f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            return sVmPolicy;
14475f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
144832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
144932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
145032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
145162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
145262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
145362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1454fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1455fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1456fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
145762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
145862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
145962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
146062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
146162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
146262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
146362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
1464e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick                               .detectAll()
146562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
146662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
146762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
146862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
146962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
147032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
147132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
147232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
147332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
147432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
147532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
147632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
147732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
147832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1479fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1480fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1481fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1482fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1483fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1484fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1485fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
148632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
14874b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
14884b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
14894b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
149008d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    /**
149108d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     * @hide
149208d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block     */
149308d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    public static void onWebViewMethodCalledOnWrongThread(Throwable originStack) {
149408d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block        onVmPolicyViolation(null, originStack);
149508d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block    }
149608d584cd1cba05284ccd2d0ea128c297624f0c47Steve Block
1497bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Map from VM violation fingerprint to uptime millis.
1498bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static final HashMap<Integer, Long> sLastVmViolationTime = new HashMap<Integer, Long>();
1499bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15004b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
15014b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
15024b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
15034b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
1504bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1505bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1506bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final boolean penaltyLog = (sVmPolicyMask & PENALTY_LOG) != 0;
1507bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1508bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
15095f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        // Erase stuff not relevant for process-wide violations
15105f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.numAnimationsRunning = 0;
15115f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.tags = null;
15125f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        info.broadcastIntentAction = null;
15135f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
1514bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Integer fingerprint = info.hashCode();
1515bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long now = SystemClock.uptimeMillis();
1516bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long lastViolationTime = 0;
1517bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        long timeSinceLastViolationMillis = Long.MAX_VALUE;
1518bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        synchronized (sLastVmViolationTime) {
1519bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (sLastVmViolationTime.containsKey(fingerprint)) {
1520bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                lastViolationTime = sLastVmViolationTime.get(fingerprint);
1521bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                timeSinceLastViolationMillis = now - lastViolationTime;
1522bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1523bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1524bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                sLastVmViolationTime.put(fingerprint, now);
1525bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
152632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
152732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1528bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyLog && timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1529bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            Log.e(TAG, message, originStack);
1530bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
1531bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1532bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | (ALL_VM_DETECT_BITS & sVmPolicyMask);
1533bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1534bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1535bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1536bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1537bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1538bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1539bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1540bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
154132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1542bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        if (penaltyDropbox && lastViolationTime == 0) {
154332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
154432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
154532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
154632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
154732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
154832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
154932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
155032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
155132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
155232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
155332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
155432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
155532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
155632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
155732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
155832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
155932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
156032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
156132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
156232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
156332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
156432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
156532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
156632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1567bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
156832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
156932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
157032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
157132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
157232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
157332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
157432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
15755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
15765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
15775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1578cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1579703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1580703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1581703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1582703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1583703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1584703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1585703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1586703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1587703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
15885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1589703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
15905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
15915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
15925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
15935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
15945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
15955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
15965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
15975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
15985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
15995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
16005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1601cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
16025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
16035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
160432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1605cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
16065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
16085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
16095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1610cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1611cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
16125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
16135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1614cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
16155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
16165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
16175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
16185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
16195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1620727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1621727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1622727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1623727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1624727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1625727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1626727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1627727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1628727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1629727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1630cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1631cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1632e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1633e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1634e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1635e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1636e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1637e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1638e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1639e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1640e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1641e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1642e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1643e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1644e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1645e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1646e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1647e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1648e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1649e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
16501181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
16511181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
16521181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
16531181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
16541181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1655e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1656e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1657e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1658e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1659e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1660e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1661e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1662e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1663e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1664e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1665e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1666e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1667e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1668e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1669e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1670e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1671e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1672e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1673e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1674e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1675e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1676e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1677e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1678e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1679e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1680e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1681e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1682e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1683e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
16841cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
16851cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
16861cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
16871cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1688e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1689e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1690e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1691e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1692e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1693e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1694e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1695e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1696e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1697e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1698e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1699e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1700e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1701e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1702e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1703e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
17041181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
17051181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
17061181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
17071181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
17081181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
17091181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
17101181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1711e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1712e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1713e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1714e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1715e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1716e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1717e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1718e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1719e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1720e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1721e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1722e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1723e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1724e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1725e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1726e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1727e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1728e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1729e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1730e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1731e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1732e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1733e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1734e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1735e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1736cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1737cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1738cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1739cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1740cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1741cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1742e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1743e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1744e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1745e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1746e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1747e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1748e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1749e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1750e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1751e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1752e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1753e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1754e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1755e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1756e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1757e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
17581181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
17591181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
17601181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1761e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1762e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1763e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1764e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1765e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1766e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1767e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1768e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1769e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1770e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1771e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1772e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1773e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1774e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1775e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1776e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1777e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1778e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1779e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1780e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1781e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1782e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1783e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
17841cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1785e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1786e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1787e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1788e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1789e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    /**
1790e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * For code to note that it's slow.  This is a no-op unless the
1791e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * current thread's {@link android.os.StrictMode.ThreadPolicy} has
1792e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * {@link android.os.StrictMode.ThreadPolicy.Builder#detectCustomSlowCalls}
1793e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * enabled.
1794e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *
1795e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     * @param name a short string for the exception stack trace that's
1796e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     *             built if when this fires.
1797e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick     */
1798e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    public static void noteSlowCall(String name) {
1799e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
1800e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
1801e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            // StrictMode not enabled.
1802e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick            return;
1803e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        }
1804e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onCustomSlowCall(name);
1805e36f9bf123c7cd07ce1007a16de564b2840ea1feBrad Fitzpatrick    }
1806e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1807e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
18084e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18094e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18104e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskRead() {
18114e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18124e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18134e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18144e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18154e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18164e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onReadFromDisk();
18174e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18184e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18194e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18204e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     * @hide
18214e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick     */
18224e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    public static void noteDiskWrite() {
18234e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
18244e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
18254e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            // StrictMode not enabled.
18264e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick            return;
18274e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        }
18284e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick        ((AndroidBlockGuardPolicy) policy).onWriteToDisk();
18294e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    }
18304e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick
18315f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    // Guarded by StrictMode.class
18325f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    private static final HashMap<Class, Integer> sExpectedActivityInstanceCount =
18335f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new HashMap<Class, Integer>();
18345f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18354e920f70f38d52d3a74c6a3133388a2e2cb6c175Brad Fitzpatrick    /**
18367e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * Returns an object that is used to track instances of activites.
18377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * The activity should store a reference to the tracker object in one of its fields.
18387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     * @hide
18397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown     */
18407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    public static Object trackActivity(Object instance) {
18417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        return new InstanceTracker(instance);
18427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
18437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    /**
1845758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     * @hide
1846758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick     */
18475f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void incrementExpectedActivityCount(Class klass) {
18487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1849758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1850758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18525f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18575f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18585f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer newExpected = expected == null ? 1 : expected + 1;
18595f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            sExpectedActivityInstanceCount.put(klass, newExpected);
18605f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
18615f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    }
18625f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18635f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    /**
18645f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     * @hide
18655f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick     */
18665f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick    public static void decrementExpectedActivityCount(Class klass) {
18677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (klass == null) {
1868758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick            return;
1869758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick        }
18707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        final int limit;
18725f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        synchronized (StrictMode.class) {
18737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
18747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return;
18757e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
18767e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18775f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            Integer expected = sExpectedActivityInstanceCount.get(klass);
18787e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
18795f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            if (newExpected == 0) {
18805f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.remove(klass);
18815f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            } else {
18825f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                sExpectedActivityInstanceCount.put(klass, newExpected);
18835f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            }
18847e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18855f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // Note: adding 1 here to give some breathing room during
18865f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            // orientation changes.  (shouldn't be necessary, though?)
18877e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            limit = newExpected + 1;
18885f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
18895f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick
18907e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Quick check.
18917e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        int actual = InstanceTracker.getInstanceCount(klass);
18927e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (actual <= limit) {
18937e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            return;
18947e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
18957e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
18967e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // Do a GC and explicit count to double-check.
18977e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // This is the work that we are trying to avoid by tracking the object instances
18987e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // explicity.  Running an explicit GC can be expensive (80ms) and so can walking
18997e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // the heap to count instance (30ms).  This extra work can make the system feel
19007e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // noticeably less responsive during orientation changes when activities are
19017e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // being restarted.  Granted, it is only a problem when StrictMode is enabled
19027e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        // but it is annoying.
19037e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        Runtime.getRuntime().gc();
19047e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
19057e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        long instances = VMDebug.countInstancesOfClass(klass, false);
19067e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        if (instances > limit) {
19077e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            Throwable tr = new InstanceCountViolation(klass, instances, limit);
19087e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            onVmPolicyViolation(tr.getMessage(), tr);
19095f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        }
1910758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    }
1911758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick
1912758035757f11a085a12b54daa9467f1d6bb251efBrad Fitzpatrick    /**
1913cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
1914cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
1915cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
1916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
1917cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
1918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
1919cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
1921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
1923cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
1926cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1927cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
1928cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1929cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1930cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
1931cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
1932cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1933cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
1934cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1936599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
1937599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
1938599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
1939599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1940599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
1941e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
1942e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
1943e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1944e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
1945e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1946e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1947cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
1948cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
1949cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
1950cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
1951cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1952cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
1953cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1954cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1955cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
1956cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
1957cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1958cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
1959cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1960cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1961bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
1962bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
1963bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
1964bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
1965bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
1966bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
1967bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * If this is a instance count violation, the number of instances in memory,
1968bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         * else -1.
1969bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick         */
1970bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public long numInstances = -1;
1971bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
1972bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        /**
1973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
1974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
1976cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
1977cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
1978cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1979cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1980cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1981cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
1982cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1983cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
1984cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
1985cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
1986cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
1987599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
1988bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
1989bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
1990bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
1991bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
1992e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
1993bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (tr instanceof InstanceCountViolation) {
1994bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                this.numInstances = ((InstanceCountViolation) tr).mInstances;
1995bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
1996e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1997e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
1998e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
1999e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
2000e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2001e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
2002e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
2003e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
2004e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
2005e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
2006e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
2007e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
2008e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
2009e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
2010e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2011e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2012cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2013cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2014f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
2015f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
2016f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
2017f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
2018f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2019f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
2020f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2021f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
2022f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
2023f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2024f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
2025f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
2026f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
2027f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
2028f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
2029f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
2030f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
2031f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
2032cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2034cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
2036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
2037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2040cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
2041cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
2042cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
2043cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
2044cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2045cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
2046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
2047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
2048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
2049cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
2050cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
2051cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
2052cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
2054cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
2055599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
2056cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
2057bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            numInstances = in.readLong();
2058bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
2059e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
2060cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2061cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
2064cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
2066cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
2067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
2068cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
2069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
2070599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
2071cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
2072bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            dest.writeLong(numInstances);
2073bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
2074e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
2075cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2076cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2077cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2078cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
2079cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
2080cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
2081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
2082cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
2083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
2084cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
2085cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
2086cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2087bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            if (numInstances != -1) {
2088bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick                pw.println(prefix + "numInstances: " + numInstances);
2089bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            }
2090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
2091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
2092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
2093599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
2094599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
2095599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
2096cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
2097bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
2098bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
2099bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
2100e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
2101e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
2102e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
2103e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
2104e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
2105e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
2106cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
2107cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
2108cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
2109bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2110bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // Dummy throwable, for now, since we don't know when or where the
2111bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // leaked instances came from.  We might in the future, but for
2112bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // now we suppress the stack trace because it's useless and/or
2113bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    // misleading.
2114bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    private static class InstanceCountViolation extends Throwable {
2115bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final Class mClass;
2116bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final long mInstances;
2117bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        final int mLimit;
2118bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
21195f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        private static final StackTraceElement[] FAKE_STACK = {
21205f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            new StackTraceElement("android.os.StrictMode", "setClassInstanceLimit",
21215f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick                                  "StrictMode.java", 1)
21225f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick        };
2123bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick
2124bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        public InstanceCountViolation(Class klass, long instances, int limit) {
21255f8b5c191cae77f536ee64f0b625e4a7f8596787Brad Fitzpatrick            super(klass.toString() + "; instances=" + instances + "; limit=" + limit);
2126bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            setStackTrace(FAKE_STACK);
2127bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mClass = klass;
2128bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mInstances = instances;
2129bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick            mLimit = limit;
2130bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick        }
2131bfbe5771106a07f9c8e8685e402b1003db40526fBrad Fitzpatrick    }
21327e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21337e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    private static final class InstanceTracker {
21347e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private static final HashMap<Class<?>, Integer> sInstanceCounts =
21357e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                new HashMap<Class<?>, Integer>();
21367e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21377e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        private final Class<?> mKlass;
21387e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21397e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public InstanceTracker(Object instance) {
21407e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            mKlass = instance.getClass();
21417e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21427e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21437e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(mKlass);
21447e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final int newValue = value != null ? value + 1 : 1;
21457e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                sInstanceCounts.put(mKlass, newValue);
21467e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21477e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21487e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21497e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        @Override
21507e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        protected void finalize() throws Throwable {
21517e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            try {
21527e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                synchronized (sInstanceCounts) {
21537e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    final Integer value = sInstanceCounts.get(mKlass);
21547e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    if (value != null) {
21557e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        final int newValue = value - 1;
21567e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        if (newValue > 0) {
21577e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.put(mKlass, newValue);
21587e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        } else {
21597e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                            sInstanceCounts.remove(mKlass);
21607e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                        }
21617e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                    }
21627e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                }
21637e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            } finally {
21647e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                super.finalize();
21657e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21667e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21677e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown
21687e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        public static int getInstanceCount(Class<?> klass) {
21697e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            synchronized (sInstanceCounts) {
21707e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                final Integer value = sInstanceCounts.get(klass);
21717e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown                return value != null ? value : 0;
21727e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown            }
21737e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown        }
21747e442837702a6e026c73a01fedb62c222b63cfc9Jeff Brown    }
2175438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
2176