StrictMode.java revision c1a968a8ed45181312f7d4bcdbba0cc8ddc201ba
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;
33438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter;
355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter;
365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList;
3746d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap;
38bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger;
3946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
40438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/**
4132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be
4232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix
4332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them.
4415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
4515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or
4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI
4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place.  Keeping disk
4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother,
499fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications.  By keeping your application's main thread
509fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent
519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a>
529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users.
5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is
5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that
5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency.  It's often the case that
5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be
5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background
5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes.  If possible, it's best to assume that such
6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p>
6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your
6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or
6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's
6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method:
6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre>
6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() {
6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     if (DEVELOPER_MODE) {
7032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}()
7132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskReads()
7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectDiskWrites()
7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .detectNetwork()   // or .detectAll() for all detectable problems
7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *         StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}()
7762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick *                 .detectLeakedSqlLiteObjects()
78fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom *                 .detectLeakedClosableObjects()
7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyLog()
8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .penaltyDeath()
8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick *                 .build());
8215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     }
8315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *     super.onCreate();
8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * }
8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre>
8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected.
8832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can
8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your
9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen.
9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are
9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler},
9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc.
9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds.  In particular,
9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle.  Use
9732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident.  Network requests on the UI thread
9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though.
9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick *
10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not
10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses.  While it does
10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing
10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best
10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism.  Notably, disk or network access from JNI calls
10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it.  Future versions of Android may catch
10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode
10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market.
108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */
109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode {
110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static final String TAG = "StrictMode";
11182829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick    private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE);
112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1131181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE);
1146804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE);
1151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
116c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    /**
117c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * The boolean system property to control screen flashes on violations.
118c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     *
119c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     * @hide
120c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick     */
121c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick    public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual";
122c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
12346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only log a duplicate stack trace to the logs every second.
12446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_LOG_INTERVAL_MS = 1000;
12546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
12646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    // Only show an annoying dialog at most every 30 seconds
12746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick    private static final long MIN_DIALOG_INTERVAL_MS = 30000;
12846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
129e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    // How many Span tags (e.g. animations) to report.
130e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final int MAX_SPAN_TAGS = 20;
131e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
132191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // How many offending stacks to keep track of (and time) per loop
133191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    // of the Looper.
134191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final int MAX_OFFENSES_PER_LOOP = 10;
135191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
13632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Thread-policy:
137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
13815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
13932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
14015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_WRITE = 0x01;  // for ThreadPolicy
14215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
14315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick      * @hide
14515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
14632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_DISK_READ = 0x02;  // for ThreadPolicy
14715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick
14815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick    /**
14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     */
15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_NETWORK = 0x04;  // for ThreadPolicy
152438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Process-policy:
154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
155438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
15632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Note, a "VM_" bit, not thread.
15732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
15832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
15932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final int DETECT_VM_CURSOR_LEAKS = 0x200;  // for ProcessPolicy
16032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
16132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
162fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * Note, a "VM_" bit, not thread.
163fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
164fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
165fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400;  // for ProcessPolicy
166fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
167fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
16832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
170438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_LOG = 0x10;  // normal android.util.Log
171438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
17232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    // Used for both process and thread policy:
17332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
174438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
17532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
177438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DIALOG = 0x20;
178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
180b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death on any detected violation.
181b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
18232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
183438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
184438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DEATH = 0x40;
185438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
186438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
187b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Death just for detected network usage.
188b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
189b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
190b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
191b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static final int PENALTY_DEATH_ON_NETWORK = 0x200;
192b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
193b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
1946804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * Flash the screen during violations.
1956804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     *
1966804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     * @hide
1976804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick     */
1986804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    public static final int PENALTY_FLASH = 0x800;
1996804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
2006804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick    /**
20132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
202438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
203438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    public static final int PENALTY_DROPBOX = 0x80;
204438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
205727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
206727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Non-public penalty mode which overrides all the other penalty
207727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * bits and signals that we're in a Binder call and we should
208727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * ignore the other penalty bits and instead serialize back all
209727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * our offending stack traces to the caller to ultimately handle
210727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * in the originating process.
211727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     *
212703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * This must be kept in sync with the constant in libs/binder/Parcel.cpp
213703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     *
214727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * @hide
215727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
216727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    public static final int PENALTY_GATHER = 0x100;
217727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
21832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
21971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     * Mask of all the penalty bits.
22071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick     */
22171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    private static final int PENALTY_MASK =
222b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER |
2236804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH;
22471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
22571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick    /**
22632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * The current VmPolicy in effect.
22732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
22832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static volatile int sVmPolicyMask = 0;
22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
230bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
231bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * The number of threads trying to do an async dropbox write.
232bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * Just to limit ourselves out of paranoia.
233bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
234bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0);
235bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private StrictMode() {}
23732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
23832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
23932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to a certain thread.
24032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
24132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setThreadPolicy}.  The current policy
24232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can be retrieved with {@link #getThreadPolicy}.
24332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
24432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Note that multiple penalties may be provided and they're run
24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * in order from least to most severe (logging before process
24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * death, for example).  There's currently no mechanism to choose
24732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * different penalties for different detected actions.
24832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
24932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class ThreadPolicy {
25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final ThreadPolicy LAX = new ThreadPolicy(0);
25432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
25532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
25632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
25732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private ThreadPolicy(int mask) {
25832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
25932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
26032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
26132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
26332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.ThreadPolicy; mask=" + mask + "]";
26432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates ThreadPolicy instances.  Methods whose names start
26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
27432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
27532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask = 0;
28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Create a Builder that detects nothing and has no
29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations.  (but note that {@link #build} will default
29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to enabling {@link #penaltyLog} if no other penalties
29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * are specified)
29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder() {
29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = 0;
29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Initialize a Builder from an existing ThreadPolicy.
30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder(ThreadPolicy policy) {
30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask = policy.mask;
30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>As of the Gingerbread release this includes network and
30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * disk operations but will likely expand in future releases.
31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK);
31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable the detection of everything.
31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitAll() {
31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK);
32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of network operations.
32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectNetwork() {
32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_NETWORK);
32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of network operations.
33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitNetwork() {
33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_NETWORK);
33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk reads.
33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskReads() {
34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_READ);
34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk reads.
34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskReads() {
34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_READ);
34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detection of disk writes.
35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectDiskWrites() {
35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_DISK_WRITE);
35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Disable detection of disk writes.
35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder permitDiskWrites() {
36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return disable(DETECT_DISK_WRITE);
36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Show an annoying dialog to the developer on detected
36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violations, rate-limited to be only a little annoying.
36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDialog() {
36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DIALOG);
37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crash the whole process on violation.  This penalty runs at
37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so you'll still get
37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * see logging or other violations before the process dies.
376b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
377b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies
378b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * to disk reads, disk writes, and network usage if their
379b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * corresponding detect flags are set.
38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
386b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * Crash the whole process on any network usage.  Unlike
387b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * {@link #penaltyDeath}, this penalty runs
388b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <em>before</em> anything else.  You must still have
389b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * called {@link #detectNetwork} to enable this.
390b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             *
391b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             * <p>In the Honeycomb or later SDKs, this is on by default.
392b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick             */
393b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            public Builder penaltyDeathOnNetwork() {
394b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                return enable(PENALTY_DEATH_ON_NETWORK);
395b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
396b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
397b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            /**
3986804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             * Flash the screen during a violation.
3996804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick             */
4006804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            public Builder penaltyFlashScreen() {
4016804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                return enable(PENALTY_FLASH);
4026804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
4036804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
4046804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            /**
40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder disable(int bit) {
42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask &= ~bit;
42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the ThreadPolicy instance.
43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public ThreadPolicy build() {
43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new ThreadPolicy(mMask);
44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link StrictMode} policy applied to all threads in the virtual machine's process.
45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>The policy is enabled by {@link #setVmPolicy}.
45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static final class VmPolicy {
45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * The default, lax policy which doesn't catch anything.
45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final VmPolicy LAX = new VmPolicy(0);
46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        final int mask;
46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        private VmPolicy(int mask) {
46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            this.mask = mask;
46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        @Override
46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public String toString() {
47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            return "[StrictMode.VmPolicy; mask=" + mask + "]";
47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        /**
47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * Creates {@link VmPolicy} instances.  Methods whose names start
47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * with {@code detect} specify what problems we should look
47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * for.  Methods whose names start with {@code penalty} specify what
47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * we should do when we detect a problem.
47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>You can call as many {@code detect} and {@code penalty}
48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * methods as you like. Currently order is insignificant: all
48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * penalties apply to all detected problems.
48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *
48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <p>For example, detect everything and log anything that's found:
48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * <pre>
48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder()
48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .detectAll()
48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .penaltyLog()
48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         *     .build();
48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * StrictMode.setVmPolicy(policy);
49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         * </pre>
49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick         */
49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        public static final class Builder {
49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private int mMask;
49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect everything that's potentially suspect.
49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
498fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>In the Honeycomb release this includes leaks of
499fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * SQLite cursors and other closable objects but will
500fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * likely expand in future releases.
50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectAll() {
503fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS);
50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Detect when an
50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * {@link android.database.sqlite.SQLiteCursor} or other
50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * SQLite object is finalized without having been closed.
51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>You always want to explicitly close your SQLite
51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * cursors to avoid unnecessary database contention and
51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * temporary memory leaks.
51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder detectLeakedSqlLiteObjects() {
51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(DETECT_VM_CURSOR_LEAKS);
51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
520fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * Detect when an {@link java.io.Closeable} or other
521fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * object with a explict termination method is finalized
522fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * without having been closed.
523fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             *
524fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * <p>You always want to explicitly close such objects to
525fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             * avoid unnecessary resources leaks.
526fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom             */
527fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            public Builder detectLeakedClosableObjects() {
528fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                return enable(DETECT_VM_CLOSABLE_LEAKS);
529fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            }
530fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
531fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom            /**
53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Crashes the whole process on violation.  This penalty runs at
53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * the end of all enabled penalties so yo you'll still get
53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * your logging or other violations before the process dies.
53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDeath() {
53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DEATH);
53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Log detected violations to the system log.
54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyLog() {
54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_LOG);
54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Enable detected violations log a stacktrace and timing data
54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * to the {@link android.os.DropBoxManager DropBox} on policy
55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * violation.  Intended mostly for platform integrators doing
55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * beta user field data collection.
55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public Builder penaltyDropBox() {
55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return enable(PENALTY_DROPBOX);
55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            private Builder enable(int bit) {
55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                mMask |= bit;
55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return this;
56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            /**
56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * Construct the VmPolicy instance.
56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             *
56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <p>Note: if no penalties are enabled before calling
56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * <code>build</code>, {@link #penaltyLog} is implicitly
56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             * set.
56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick             */
56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            public VmPolicy build() {
57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // If there are detection bits set but no violation bits
57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // set, enable simple logging.
57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                if (mMask != 0 &&
57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    (mMask & (PENALTY_DEATH | PENALTY_LOG |
57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                              PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) {
57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    penaltyLog();
57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                }
57732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                return new VmPolicy(mMask);
57832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
57932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
58032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
581438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
582438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
5835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Log of strict mode violation stack traces that have occurred
5845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * during a Binder call, to be serialized back later to the caller
5855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * via Parcel.writeNoException() (amusingly) where the caller can
5865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * choose how to react.
5875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
588cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations =
589cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
590cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
591703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // Starts null to avoid unnecessary allocations when
592703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // checking whether there are any violations or not in
593703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            // hasGatheredViolations() below.
594703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            return null;
5955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
5965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    };
5975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
5985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
59932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions on the current thread should
60032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * be detected, as well as the penalty if such actions occur.
60115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     *
60232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * <p>Internally this sets a thread-local variable which is
60315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * propagated across cross-process IPC calls, meaning you can
60415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * catch violations when a system service or another process
60515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * accesses the disk or network on your behalf.
606438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
60732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
608438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
60932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setThreadPolicy(final ThreadPolicy policy) {
61032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        setThreadPolicyMask(policy.mask);
61132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
61232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
61332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    private static void setThreadPolicyMask(final int policyMask) {
614727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // In addition to the Java-level thread-local in Dalvik's
615727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // BlockGuard, we also need to keep a native thread-local in
616727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // Binder in order to propagate the value across Binder calls,
617727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // even across native-only processes.  The two are kept in
618727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // sync via the callback to onStrictModePolicyChange, below.
619727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(policyMask);
620727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
621727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        // And set the Android native version...
622727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        Binder.setThreadStrictModePolicy(policyMask);
623727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
624727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
625727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    // Sets the policy in Dalvik/libcore (BlockGuard)
626727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void setBlockGuardPolicy(final int policyMask) {
62746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        if (policyMask == 0) {
62846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY);
62946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            return;
63046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        }
631438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
632438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        if (!(policy instanceof AndroidBlockGuardPolicy)) {
633438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask));
634438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        } else {
635438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy;
636438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            androidPolicy.setPolicyMask(policyMask);
637438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
638438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
639438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
6404b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    // Sets up CloseGuard in Dalvik/libcore
6414b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static void setCloseGuardEnabled(boolean enabled) {
6427c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick        if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) {
6434b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            CloseGuard.setReporter(new AndroidCloseGuardReporter());
6444b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
6454b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        CloseGuard.setEnabled(enabled);
6464b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
6474b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
6485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class StrictModeNetworkViolation extends BlockGuard.BlockGuardPolicyException {
6495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeNetworkViolation(int policyMask) {
65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            super(policyMask, DETECT_NETWORK);
6515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
6525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
6535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
6545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class StrictModeDiskReadViolation extends BlockGuard.BlockGuardPolicyException {
6555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskReadViolation(int policyMask) {
65632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            super(policyMask, DETECT_DISK_READ);
6575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
6585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
6595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
6605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class StrictModeDiskWriteViolation extends BlockGuard.BlockGuardPolicyException {
6615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public StrictModeDiskWriteViolation(int policyMask) {
66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            super(policyMask, DETECT_DISK_WRITE);
6635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
6645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
6655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
666438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    /**
66715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick     * Returns the bitmask of the current thread's policy.
668438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     *
66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled
67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
672438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick     */
67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static int getThreadPolicyMask() {
674438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        return BlockGuard.getThreadPolicy().getPolicyMask();
675438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
676438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
6775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Returns the current thread's policy.
67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy getThreadPolicy() {
68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(getThreadPolicyMask());
68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit both disk reads &amp; writes, and sets the new policy
68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
69097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to {@link #setThreadPolicy} to
69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *         restore the policy at the end of a block
69397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskWrites() {
69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ);
69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
69997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
70197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
70297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
70397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * A convenience wrapper that takes the current
70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it
70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * to permit disk reads, and sets the new policy
70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * with {@link #setThreadPolicy}, returning the old policy so you
70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * can restore it at the end of a block.
70997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *
71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @return the old policy, to be passed to setThreadPolicy to
71197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     *         restore the policy.
71297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick     */
71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static ThreadPolicy allowThreadDiskReads() {
71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int oldPolicyMask = getThreadPolicyMask();
71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ);
71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if (newPolicyMask != oldPolicyMask) {
71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            setThreadPolicyMask(newPolicyMask);
71897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick        }
71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new ThreadPolicy(oldPolicyMask);
72097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    }
72197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick
72297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick    /**
72350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * Enable DropBox logging for debug phone builds.
72450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     *
72550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     * @hide
72650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick     */
72750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    public static boolean conditionallyEnableDebugLogging() {
728c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        boolean doFlashes = SystemProperties.getBoolean(VISUAL_PROPERTY, IS_ENG_BUILD);
729c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
73050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // For debug builds, log event loop stalls to dropbox for analysis.
73150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        // Similar logic also appears in ActivityThread.java for system apps.
732c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD && !doFlashes) {
7337c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick            setCloseGuardEnabled(false);
73450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick            return false;
73550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        }
736c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
737c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        int threadPolicyMask = StrictMode.DETECT_DISK_WRITE |
738c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_DISK_READ |
739c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                StrictMode.DETECT_NETWORK;
740c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
741c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (!IS_USER_BUILD) {
742c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_DROPBOX;
743c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
744c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (doFlashes) {
745c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            threadPolicyMask |= StrictMode.PENALTY_FLASH;
746c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
747c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
748c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        StrictMode.setThreadPolicyMask(threadPolicyMask);
749c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick
750c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        if (IS_USER_BUILD) {
751c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(false);
752c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        } else {
753c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS |
754c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                    StrictMode.DETECT_VM_CLOSABLE_LEAKS |
755c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick                    StrictMode.PENALTY_DROPBOX;
756c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick            setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
757c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick        }
75850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick        return true;
75950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    }
76050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick
76150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick    /**
762b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * Used by the framework to make network usage on the main
763b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * thread a fatal error.
764b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     *
765b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     * @hide
766b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick     */
767b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    public static void enableDeathOnNetwork() {
768b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int oldPolicy = getThreadPolicyMask();
769b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK;
770b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick        setThreadPolicyMask(newPolicy);
771b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    }
772b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick
773b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick    /**
7745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Parses the BlockGuard policy mask out from the Exception's
7755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * getMessage() String value.  Kinda gross, but least
7765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * invasive.  :/
7775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
7785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Input is of form "policy=137 violation=64"
7795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     *
7805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Returns 0 on failure, which is a valid policy, but not a
7815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * valid policy during a violation (else there must've been
7825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * some policy in effect to violate).
7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parsePolicyFromMessage(String message) {
7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null || !message.startsWith("policy=")) {
7865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
7875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int spaceIndex = message.indexOf(' ');
7895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (spaceIndex == -1) {
7905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
7915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String policyString = message.substring(7, spaceIndex);
7935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(policyString).intValue();
7955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
7965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
7975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
7985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
7995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
8005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
8015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Like parsePolicyFromMessage(), but returns the violation.
8025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
8035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static int parseViolationFromMessage(String message) {
8045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (message == null) {
8055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
8065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int violationIndex = message.indexOf("violation=");
8085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        if (violationIndex == -1) {
8095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
8105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String violationString = message.substring(violationIndex + 10);
8125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        try {
8135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return Integer.valueOf(violationString).intValue();
8145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        } catch (NumberFormatException e) {
8155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return 0;
8165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
8185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
819191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed =
820191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            new ThreadLocal<ArrayList<ViolationInfo>>() {
821191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        @Override protected ArrayList<ViolationInfo> initialValue() {
822191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            return new ArrayList<ViolationInfo>();
823191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        }
824191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    };
825191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
826191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    private static boolean tooManyViolationsThisLoop() {
827191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick        return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP;
828191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick    }
829191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick
830438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    private static class AndroidBlockGuardPolicy implements BlockGuard.Policy {
831438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        private int mPolicyMask;
83246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
83346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // Map from violation stacktrace hashcode -> uptimeMillis of
83446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // last violation.  No locking needed, as this is only
83546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        // accessed by the same thread.
83646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick        private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>();
837438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
838438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public AndroidBlockGuardPolicy(final int policyMask) {
839438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
840438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
841438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        @Override
8435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        public String toString() {
8445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask;
8455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
8465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
847438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
848438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public int getPolicyMask() {
849438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            return mPolicyMask;
850438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
851438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
852438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
853438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onWriteToDisk() {
85432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_WRITE) == 0) {
855438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
856438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
857191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
858191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
859191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
860cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask);
861cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
862cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
863438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
864438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
865438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
866438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onReadFromDisk() {
86732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_DISK_READ) == 0) {
868438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
869438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
870191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
871191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
872191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
873cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask);
874cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
875cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
876438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
877438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
878438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        // Part of BlockGuard.Policy interface:
879438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void onNetwork() {
88032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            if ((mPolicyMask & DETECT_NETWORK) == 0) {
881438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                return;
882438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
883b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) {
884b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick                throw new NetworkOnMainThreadException();
885b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick            }
886191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (tooManyViolationsThisLoop()) {
887191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick                return;
888191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            }
889cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask);
890cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            e.fillInStackTrace();
891cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            startHandlingViolationException(e);
892438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
893438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
894438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        public void setPolicyMask(int policyMask) {
895438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            mPolicyMask = policyMask;
896438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
897438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
8985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Start handling a violation that just started and hasn't
8995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // actually run yet (e.g. no disk write or network operation
9005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // has yet occurred).  This sees if we're in an event loop
9015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread and, if so, uses it to roughly measure how long the
9025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation took.
9035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) {
904cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ViolationInfo info = new ViolationInfo(e, e.getPolicy());
905cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.violationUptimeMillis = SystemClock.uptimeMillis();
906cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            handleViolationWithTimingAttempt(info);
907cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
908438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
909cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // Attempts to fill in the provided ViolationInfo's
910cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // durationMillis field if this thread has a Looper we can use
911cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // to measure with.  We measure from the time of violation
912cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // until the time the looper is idle again (right before
913cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        // the next epoll_wait)
914cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolationWithTimingAttempt(final ViolationInfo info) {
915438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            Looper looper = Looper.myLooper();
916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
917cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // Without a Looper, we're unable to time how long the
918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // violation takes place.  This case should be rare, as
919cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // most users will care about timing violations that
920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // happen on their main UI thread.  Note that this case is
921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // also hit when a violation takes place in a Binder
922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // thread, in "gather" mode.  In this case, the duration
923cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // of the violation is computed by the ultimate caller and
924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // its Looper, if any.
925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            // TODO: if in gather mode, ignore Looper.myLooper() and always
926cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            //       go into this immediate mode?
927438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            if (looper == null) {
928cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                info.durationMillis = -1;  // unknown (redundant, already set)
929cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                handleViolation(info);
930cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
931438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
932438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
933cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            MessageQueue queue = Looper.myQueue();
934cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            final ArrayList<ViolationInfo> records = violationsBeingTimed.get();
935191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick            if (records.size() >= MAX_OFFENSES_PER_LOOP) {
936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // Not worth measuring.  Too many offenses in one loop.
937cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
938cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
939cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            records.add(info);
940cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (records.size() > 1) {
941cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // There's already been a violation this loop, so we've already
942cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // registered an idle handler to process the list of violations
943cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                // at the end of this Looper's loop.
944cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                return;
945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
946cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
9476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ?
948cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick                    sWindowManager.get() : null;
9496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            if (windowManager != null) {
9506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                try {
9516804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                    windowManager.showStrictModeViolation(true);
9526804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                } catch (RemoteException unused) {
9536804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                }
9546804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick            }
9556804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick
956cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            queue.addIdleHandler(new MessageQueue.IdleHandler() {
957cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    public boolean queueIdle() {
958cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        long loopFinishTime = SystemClock.uptimeMillis();
959cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        for (int n = 0; n < records.size(); ++n) {
960cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            ViolationInfo v = records.get(n);
961cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.violationNumThisLoop = n + 1;
962cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            v.durationMillis =
963cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                                    (int) (loopFinishTime - v.violationUptimeMillis);
964cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                            handleViolation(v);
965cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        }
966cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        records.clear();
9676804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                        if (windowManager != null) {
9686804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                            try {
9696804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                                windowManager.showStrictModeViolation(false);
9706804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                            } catch (RemoteException unused) {
9716804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                            }
9726804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick                        }
973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        return false;  // remove this idle handler from the array
974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    }
975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                });
9765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
977438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Note: It's possible (even quite likely) that the
9795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // thread-local policy mask has changed from the time the
9805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // violation fired and now (after the violating code ran) due
9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // to people who push/pop temporary policy in regions of code,
9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // hence the policy being passed around.
983cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        void handleViolation(final ViolationInfo info) {
984cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) {
985cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                Log.wtf(TAG, "unexpected null stacktrace");
9865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                return;
9875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
988438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
989cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy);
99046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
991cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_GATHER) != 0) {
992cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ArrayList<ViolationInfo> violations = gatheredViolations.get();
993703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                if (violations == null) {
994cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    violations = new ArrayList<ViolationInfo>(1);
995703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                    gatheredViolations.set(violations);
996703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                } else if (violations.size() >= 5) {
9975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    // Too many.  In a loop or something?  Don't gather them all.
9985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    return;
9995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1000cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                for (ViolationInfo previous : violations) {
1001cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) {
10025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        // Duplicate. Don't log.
10035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                        return;
10045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    }
10055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                }
1006cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violations.add(info);
1007727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                return;
1008727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick            }
1009727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
101046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // Not perfect, but fast and good enough for dup suppression.
1011f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            Integer crashFingerprint = info.hashCode();
101246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long lastViolationTime = 0;
101346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            if (mLastViolationTime.containsKey(crashFingerprint)) {
101446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                lastViolationTime = mLastViolationTime.get(crashFingerprint);
101546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
101646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long now = SystemClock.uptimeMillis();
101746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            mLastViolationTime.put(crashFingerprint, now);
101846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            long timeSinceLastViolationMillis = lastViolationTime == 0 ?
101946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Long.MAX_VALUE : (now - lastViolationTime);
102046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1021cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_LOG) != 0 &&
102246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) {
1023cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                if (info.durationMillis != -1) {
10245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation; ~duration=" +
1025cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                          info.durationMillis + " ms: " + info.crashInfo.stackTrace);
1026438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } else {
1027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                    Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace);
1028438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1029438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1030438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
103171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick            // The violationMaskSubset, passed to ActivityManager, is a
103246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
103346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // only the bit violated and penalty bits to be executed
103446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            // by the ActivityManagerService remaining set.
1035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int violationMaskSubset = 0;
103646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1037cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DIALOG) != 0 &&
103846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) {
1039cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DIALOG;
104046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
104146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1042cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) {
1043cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= PENALTY_DROPBOX;
104446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick            }
104546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick
1046cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationMaskSubset != 0) {
1047cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage);
1048cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                violationMaskSubset |= violationBit;
104932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                final int savedPolicyMask = getThreadPolicyMask();
105071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
105171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX;
105271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                if (justDropBox) {
105371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // If all we're going to ask the activity manager
105471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // to do is dropbox it (the common case during
105571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // platform development), we can avoid doing this
105671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // call synchronously which Binder data suggests
105771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // isn't always super fast, despite the implementation
105871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    // in the ActivityManager trying to be mostly async.
1059bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    dropboxViolationAsync(violationMaskSubset, info);
106071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                    return;
106171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                }
106271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick
106371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick                // Normal synchronous call to the ActivityManager.
1064438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                try {
1065727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // First, remove any policy before we call into the Activity Manager,
1066727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // otherwise we'll infinite recurse as we try to log policy violations
1067727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // to disk, thus violating policy, thus requiring logging, etc...
1068727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // We restore the current policy below, in the finally block.
106932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(0);
1070727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1071438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                    ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
1072438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                        RuntimeInit.getApplicationObject(),
1073cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        violationMaskSubset,
1074cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                        info);
1075438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                } catch (RemoteException e) {
107646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick                    Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
1077727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                } finally {
1078727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick                    // Restore the policy.
107932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    setThreadPolicyMask(savedPolicyMask);
1080438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                }
1081438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1082438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick
1083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if ((info.policy & PENALTY_DEATH) != 0) {
1084438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                System.err.println("StrictMode policy violation with POLICY_DEATH; shutting down.");
1085438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                Process.killProcess(Process.myPid());
1086438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick                System.exit(10);
1087438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick            }
1088438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick        }
1089438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick    }
1090727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick
1091bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    /**
1092bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * In the common case, as set by conditionallyEnableDebugLogging,
1093bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * we're just dropboxing any violations but not showing a dialog,
1094bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * not loggging, and not killing the process.  In these cases we
1095bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * don't need to do a synchronous call to the ActivityManager.
1096bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * This is used by both per-thread and vm-wide violations when
1097bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     * applicable.
1098bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick     */
1099bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    private static void dropboxViolationAsync(
1100bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            final int violationMaskSubset, final ViolationInfo info) {
1101bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int outstanding = sDropboxCallsInFlight.incrementAndGet();
1102bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (outstanding > 20) {
1103bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // What's going on?  Let's not make make the situation
1104bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // worse and just not log.
1105bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            sDropboxCallsInFlight.decrementAndGet();
1106bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1107bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
1108bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1109bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding);
1110bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1111bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        new Thread("callActivityManagerForStrictModeDropbox") {
1112bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            public void run() {
1113bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
1114bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                try {
11151065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    IActivityManager am = ActivityManagerNative.getDefault();
11161065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    if (am == null) {
11171065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        Log.d(TAG, "No activity manager; failed to Dropbox violation.");
11181065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    } else {
11191065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                        am.handleApplicationStrictModeViolation(
11201065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            RuntimeInit.getApplicationObject(),
11211065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            violationMaskSubset,
11221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                            info);
11231065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick                    }
1124bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                } catch (RemoteException e) {
1125bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                    Log.e(TAG, "RemoteException handling StrictMode violation", e);
1126bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                }
1127bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                int outstanding = sDropboxCallsInFlight.decrementAndGet();
1128bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick                if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding);
1129bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            }
1130bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }.start();
1131bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick    }
1132bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
11334b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    private static class AndroidCloseGuardReporter implements CloseGuard.Reporter {
11344b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        public void report (String message, Throwable allocationSite) {
11354b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom            onVmPolicyViolation(message, allocationSite);
11364b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        }
11374b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
11384b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
1139727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    /**
11405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
11415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
11425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static boolean hasGatheredViolations() {
1143703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        return gatheredViolations.get() != null;
1144703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    }
1145703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick
1146703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /**
1147703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Called from Parcel.writeException(), so we drop this memory and
1148703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * don't incorrectly attribute it to the wrong caller on the next
1149703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     * Binder call on this thread.
1150703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick     */
1151703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick    /* package */ static void clearGatheredViolations() {
1152703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
11535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
11545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
11555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
115632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Sets the policy for what actions in the VM process (on any
115732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * thread) should be detected, as well as the penalty if such
115832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * actions occur.
115932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     *
116032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @param policy the policy to put into place
116132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
116232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void setVmPolicy(final VmPolicy policy) {
116332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        sVmPolicyMask = policy.mask;
11644b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        setCloseGuardEnabled(vmClosableObjectLeaksEnabled());
116532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
116632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
116732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
116832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * Gets the current VM policy.
116932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
117032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static VmPolicy getVmPolicy() {
117132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return new VmPolicy(sVmPolicyMask);
117232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
117332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
117432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
117562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * Enable the recommended StrictMode defaults, with violations just being logged.
117662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     *
117762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     * <p>This catches disk and network access on the main thread, as
1178fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * well as leaked SQLite cursors and unclosed resources.  This is
1179fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * simply a wrapper around {@link #setVmPolicy} and {@link
1180fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * #setThreadPolicy}.
118162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick     */
118262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    public static void enableDefaults() {
118362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
118462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .detectAll()
118562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .penaltyLog()
118662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                                   .build());
118762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
118862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .detectLeakedSqlLiteObjects()
1189fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom                               .detectLeakedClosableObjects()
119062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .penaltyLog()
119162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick                               .build());
119262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    }
119362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick
119462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick    /**
119532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
119632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
119732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static boolean vmSqliteObjectLeaksEnabled() {
119832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0;
119932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
120032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
120132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
120232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     * @hide
120332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick     */
1204fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    public static boolean vmClosableObjectLeaksEnabled() {
1205fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom        return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0;
1206fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    }
1207fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom
1208fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom    /**
1209fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     * @hide
1210fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom     */
121132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    public static void onSqliteObjectLeaked(String message, Throwable originStack) {
12124b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom        onVmPolicyViolation(message, originStack);
12134b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    }
12144b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom
12154b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    /**
12164b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     * @hide
12174b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom     */
12184b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom    public static void onVmPolicyViolation(String message, Throwable originStack) {
121932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        if ((sVmPolicyMask & PENALTY_LOG) != 0) {
122032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Log.e(TAG, message, originStack);
122132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
122232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1223bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0;
1224bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0;
1225bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1226bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        int violationMaskSubset = PENALTY_DROPBOX | DETECT_VM_CURSOR_LEAKS;
1227bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask);
1228bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick
1229bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox && !penaltyDeath) {
1230bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // Common case for userdebug/eng builds.  If no death and
1231bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // just dropboxing, we can do the ActivityManager call
1232bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            // asynchronously.
1233bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            dropboxViolationAsync(violationMaskSubset, info);
1234bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick            return;
1235bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        }
123632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1237bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDropbox) {
123832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // The violationMask, passed to ActivityManager, is a
123932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // subset of the original StrictMode policy bitmask, with
124032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // only the bit violated and penalty bits to be executed
124132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            // by the ActivityManagerService remaining set.
124232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            final int savedPolicyMask = getThreadPolicyMask();
124332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            try {
124432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // First, remove any policy before we call into the Activity Manager,
124532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // otherwise we'll infinite recurse as we try to log policy violations
124632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // to disk, thus violating policy, thus requiring logging, etc...
124732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // We restore the current policy below, in the finally block.
124832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(0);
124932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
125032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                ActivityManagerNative.getDefault().handleApplicationStrictModeViolation(
125132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    RuntimeInit.getApplicationObject(),
125232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    violationMaskSubset,
125332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                    info);
125432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } catch (RemoteException e) {
125532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                Log.e(TAG, "RemoteException trying to handle StrictMode violation", e);
125632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            } finally {
125732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                // Restore the policy.
125832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick                setThreadPolicyMask(savedPolicyMask);
125932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            }
126032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
126132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
1262bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick        if (penaltyDeath) {
126332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down.");
126432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            Process.killProcess(Process.myPid());
126532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick            System.exit(10);
126632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        }
126732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    }
126832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick
126932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick    /**
12705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.writeNoException()
12715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
12725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void writeGatheredViolationsToParcel(Parcel p) {
1273cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        ArrayList<ViolationInfo> violations = gatheredViolations.get();
1274703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        if (violations == null) {
1275703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(0);
1276703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        } else {
1277703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            p.writeInt(violations.size());
1278703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            for (int i = 0; i < violations.size(); ++i) {
1279703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick                violations.get(i).writeToParcel(p, 0 /* unused flags? */);
1280703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            }
1281703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size());
1282703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick            violations.clear(); // somewhat redundant, as we're about to null the threadlocal
12835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
1284703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick        gatheredViolations.set(null);
12855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
12865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
12875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    private static class LogStackTrace extends Exception {}
12885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
12895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
12905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS,
12915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     * we here read back all the encoded violations.
12925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick     */
12935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /* package */ static void readAndHandleBinderCallViolations(Parcel p) {
12945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        // Our own stack trace to append
12955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        StringWriter sw = new StringWriter();
1296cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        new LogStackTrace().printStackTrace(new PrintWriter(sw));
12975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        String ourStack = sw.toString();
12985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
129932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick        int policyMask = getThreadPolicyMask();
1300cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0;
13015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
13025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        int numViolations = p.readInt();
13035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        for (int i = 0; i < numViolations; ++i) {
13045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call.  i=" + i);
1305cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            ViolationInfo info = new ViolationInfo(p, !currentlyGathering);
1306cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack;
13075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            BlockGuard.Policy policy = BlockGuard.getThreadPolicy();
13085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            if (policy instanceof AndroidBlockGuardPolicy) {
1309cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info);
13105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick            }
13115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick        }
13125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    }
13135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick
13145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick    /**
1315727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * Called from android_util_Binder.cpp's
1316727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * android_os_Parcel_enforceInterface when an incoming Binder call
1317727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * requires changing the StrictMode policy mask.  The role of this
1318727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * function is to ask Binder for its current (native) thread-local
1319727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * policy value and synchronize it to libcore's (Java)
1320727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     * thread-local policy value.
1321727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick     */
1322727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    private static void onBinderStrictModePolicyChange(int newPolicy) {
1323727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick        setBlockGuardPolicy(newPolicy);
1324727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick    }
1325cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1326cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    /**
1327e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * A tracked, critical time span.  (e.g. during an animation.)
1328e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1329e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * The object itself is a linked list node, to avoid any allocations
1330e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * during rapid span entries and exits.
1331e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1332e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1333e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1334e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static class Span {
1335e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private String mName;
1336e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private long mCreateMillis;
1337e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mNext;
1338e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private Span mPrev;  // not used when in freeList, only active
1339e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        private final ThreadSpanState mContainerState;
1340e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1341e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span(ThreadSpanState threadState) {
1342e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            mContainerState = threadState;
1343e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1344e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
13451181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        // Empty constructor for the NO_OP_SPAN
13461181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        protected Span() {
13471181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            mContainerState = null;
13481181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
13491181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1350e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1351e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * To be called when the critical span is complete (i.e. the
1352e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * animation is done animating).  This can be called on any
1353e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread (even a different one from where the animation was
1354e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * taking place), but that's only a defensive implementation
1355e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * measure.  It really makes no sense for you to call this on
1356e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * thread other than that where you created it.
1357e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         *
1358e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * @hide
1359e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1360e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public void finish() {
1361e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = mContainerState;
1362e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1363e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mName == null) {
1364e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    // Duplicate finish call.  Ignore.
1365e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    return;
1366e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1367e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1368e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Remove ourselves from the active list.
1369e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mPrev != null) {
1370e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mPrev.mNext = mNext;
1371e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1372e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (mNext != null) {
1373e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    mNext.mPrev = mPrev;
1374e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1375e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mActiveHead == this) {
1376e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mActiveHead = mNext;
1377e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1378e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
13791cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                state.mActiveSize--;
13801cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
13811cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick                if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize);
13821cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick
1383e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mCreateMillis = -1;
1384e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mName = null;
1385e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mPrev = null;
1386e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                this.mNext = null;
1387e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1388e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Add ourselves to the freeList, if it's not already
1389e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // too big.
1390e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (state.mFreeListSize < 5) {
1391e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.mNext = state.mFreeListHead;
1392e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListHead = this;
1393e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    state.mFreeListSize++;
1394e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1395e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1396e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1397e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1398e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
13991181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    // The no-op span that's used in user builds.
14001181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick    private static final Span NO_OP_SPAN = new Span() {
14011181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            public void finish() {
14021181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick                // Do nothing.
14031181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            }
14041181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        };
14051181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick
1406e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1407e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Linked lists of active spans and a freelist.
1408e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1409e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Locking notes: there's one of these structures per thread and
1410e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * all members of this structure (as well as the Span nodes under
1411e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * it) are guarded by the ThreadSpanState object instance.  While
1412e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * in theory there'd be no locking required because it's all local
1413e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * per-thread, the finish() method above is defensive against
1414e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * people calling it on a different thread from where they created
1415e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * the Span, hence the locking.
1416e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1417e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static class ThreadSpanState {
1418e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mActiveHead;    // doubly-linked list.
1419e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mActiveSize;
1420e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public Span mFreeListHead;  // singly-linked list.  only changes at head.
1421e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public int mFreeListSize;
1422e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1423e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1424e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState =
1425e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            new ThreadLocal<ThreadSpanState>() {
1426e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        @Override protected ThreadSpanState initialValue() {
1427e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            return new ThreadSpanState();
1428e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1429e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    };
1430e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1431cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() {
1432cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        protected IWindowManager create() {
1433cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick            return IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
1434cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick        }
1435cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick    };
1436cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick
1437e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1438e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * Enter a named critical span (e.g. an animation)
1439e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1440e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>The name is an arbitary label (or tag) that will be applied
1441e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * to any strictmode violation that happens while this span is
1442e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * active.  You must call finish() on the span when done.
1443e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1444e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>This will never return null, but on devices without debugging
1445e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * enabled, this may return a dummy object on which the finish()
1446e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * method is a no-op.
1447e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1448e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * <p>TODO: add CloseGuard to this, verifying callers call finish.
1449e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     *
1450e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     * @hide
1451e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick     */
1452e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    public static Span enterCriticalSpan(String name) {
14531181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        if (IS_USER_BUILD) {
14541181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick            return NO_OP_SPAN;
14551181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick        }
1456e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        if (name == null || name.isEmpty()) {
1457e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            throw new IllegalArgumentException("name must be non-null and non-empty");
1458e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1459e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        ThreadSpanState state = sThisThreadSpanState.get();
1460e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        Span span = null;
1461e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        synchronized (state) {
1462e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (state.mFreeListHead != null) {
1463e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = state.mFreeListHead;
1464e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListHead = span.mNext;
1465e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                state.mFreeListSize--;
1466e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            } else {
1467e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                // Shouldn't have to do this often.
1468e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span = new Span(state);
1469e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1470e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mName = name;
1471e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mCreateMillis = SystemClock.uptimeMillis();
1472e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mNext = state.mActiveHead;
1473e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            span.mPrev = null;
1474e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveHead = span;
1475e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            state.mActiveSize++;
1476e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (span.mNext != null) {
1477e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                span.mNext.mPrev = span;
1478e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
14791cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick            if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize);
1480e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        }
1481e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        return span;
1482e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    }
1483e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1484e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1485e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick    /**
1486cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * Parcelable that gets sent in Binder call headers back to callers
1487cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * to report violations that happened during a cross-process call.
1488cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     *
1489cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     * @hide
1490cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick     */
1491cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    public static class ViolationInfo {
1492cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1493cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Stack and other stuff info.
1494cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1495cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final ApplicationErrorReport.CrashInfo crashInfo;
1496cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1497cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1498cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The strict mode policy mask at the time of violation.
1499cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1500cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public final int policy;
1501cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1502cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1503cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The wall time duration of the violation, when known.  -1 when
1504cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * not known.
1505cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1506cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int durationMillis = -1;
1507cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1508cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1509599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         * The number of animations currently running.
1510599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick         */
1511599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        public int numAnimationsRunning = 0;
1512599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick
1513599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick        /**
1514e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * List of tags from active Span instances during this
1515e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         * violation, or null for none.
1516e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick         */
1517e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        public String[] tags;
1518e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick
1519e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick        /**
1520cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Which violation number this was (1-based) since the last Looper loop,
1521cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * from the perspective of the root caller (if it crossed any processes
1522cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * via Binder calls).  The value is 0 if the root caller wasn't on a Looper
1523cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * thread.
1524cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1525cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public int violationNumThisLoop;
1526cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1527cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1528cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * The time (in terms of SystemClock.uptimeMillis()) that the
1529cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * violation occurred.
1530cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1531cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public long violationUptimeMillis;
1532cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1533cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1534bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * The action of the Intent being broadcast to somebody's onReceive
1535bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         * on this thread right now, or null.
1536bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick         */
1537bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        public String broadcastIntentAction;
1538bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick
1539bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick        /**
1540cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an uninitialized instance of ViolationInfo
1541cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1542cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo() {
1543cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = null;
1544cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            policy = 0;
1545cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1546cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1547cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1548cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from an exception.
1549cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1550cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Throwable tr, int policy) {
1551cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(tr);
1552cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = SystemClock.uptimeMillis();
1553cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this.policy = policy;
1554599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount();
1555bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast();
1556bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntent != null) {
1557bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                broadcastIntentAction = broadcastIntent.getAction();
1558bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
1559e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            ThreadSpanState state = sThisThreadSpanState.get();
1560e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            synchronized (state) {
1561e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int spanActiveCount = state.mActiveSize;
1562e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount > MAX_SPAN_TAGS) {
1563e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    spanActiveCount = MAX_SPAN_TAGS;
1564e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1565e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                if (spanActiveCount != 0) {
1566e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    this.tags = new String[spanActiveCount];
1567e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    Span iter = state.mActiveHead;
1568e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    int index = 0;
1569e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    while (iter != null && index < spanActiveCount) {
1570e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        this.tags[index] = iter.mName;
1571e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        index++;
1572e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                        iter = iter.mNext;
1573e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    }
1574e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1575e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1576cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1577cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1578f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        @Override
1579f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        public int hashCode() {
1580f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            int result = 17;
1581f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            result = 37 * result + crashInfo.stackTrace.hashCode();
1582f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (numAnimationsRunning != 0) {
1583f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result *= 37;
1584f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
1585f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (broadcastIntentAction != null) {
1586f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                result = 37 * result + broadcastIntentAction.hashCode();
1587f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
1588f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            if (tags != null) {
1589f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                for (String tag : tags) {
1590f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                    result = 37 * result + tag.hashCode();
1591f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick                }
1592f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            }
1593f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick            return result;
1594f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick        }
1595f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick
1596cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1597cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
1598cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1599cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in) {
1600cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            this(in, false);
1601cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1602cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1603cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1604cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Create an instance of ViolationInfo initialized from a Parcel.
1605cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *
1606cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * @param unsetGatheringBit if true, the caller is the root caller
1607cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         *   and the gathering penalty should be removed.
1608cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1609cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public ViolationInfo(Parcel in, boolean unsetGatheringBit) {
1610cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo = new ApplicationErrorReport.CrashInfo(in);
1611cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            int rawPolicy = in.readInt();
1612cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (unsetGatheringBit) {
1613cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy & ~PENALTY_GATHER;
1614cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            } else {
1615cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                policy = rawPolicy;
1616cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1617cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            durationMillis = in.readInt();
1618cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationNumThisLoop = in.readInt();
1619599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            numAnimationsRunning = in.readInt();
1620cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            violationUptimeMillis = in.readLong();
1621bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            broadcastIntentAction = in.readString();
1622e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            tags = in.readStringArray();
1623cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1624cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1625cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1626cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Save a ViolationInfo instance to a parcel.
1627cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1628cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void writeToParcel(Parcel dest, int flags) {
1629cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.writeToParcel(dest, flags);
1630cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(policy);
1631cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(durationMillis);
1632cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeInt(violationNumThisLoop);
1633599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            dest.writeInt(numAnimationsRunning);
1634cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            dest.writeLong(violationUptimeMillis);
1635bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            dest.writeString(broadcastIntentAction);
1636e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            dest.writeStringArray(tags);
1637cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1638cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1639cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1640cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        /**
1641cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         * Dump a ViolationInfo instance to a Printer.
1642cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick         */
1643cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        public void dump(Printer pw, String prefix) {
1644cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            crashInfo.dump(pw, prefix);
1645cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "policy: " + policy);
1646cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (durationMillis != -1) {
1647cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "durationMillis: " + durationMillis);
1648cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1649cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            if (violationNumThisLoop != 0) {
1650cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick                pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop);
1651cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            }
1652599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            if (numAnimationsRunning != 0) {
1653599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick                pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning);
1654599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick            }
1655cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick            pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis);
1656bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            if (broadcastIntentAction != null) {
1657bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick                pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction);
1658bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick            }
1659e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            if (tags != null) {
1660e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                int index = 0;
1661e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                for (String tag : tags) {
1662e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                    pw.println(prefix + "tag[" + (index++) + "]: " + tag);
1663e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick                }
1664e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick            }
1665cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick        }
1666cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick
1667cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick    }
1668438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick}
1669