BugreportProgressService.java revision 8648a15406d43c8af12e9cfe2355b1eee201d479
1b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme/*
2b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * Copyright (C) 2015 The Android Open Source Project
3b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme *
4b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * Licensed under the Apache License, Version 2.0 (the "License");
5b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * you may not use this file except in compliance with the License.
6b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * You may obtain a copy of the License at
7b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme *
8b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme *      http://www.apache.org/licenses/LICENSE-2.0
9b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme *
10b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * Unless required by applicable law or agreed to in writing, software
11b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * distributed under the License is distributed on an "AS IS" BASIS,
12b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * See the License for the specific language governing permissions and
14b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme * limitations under the License.
15b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme */
16b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
17b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemepackage com.android.shell;
18b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
19d1e0f12979441733753b538611f6d73e5527c43cFelipe Lemeimport static android.os.Process.THREAD_PRIORITY_BACKGROUND;
20b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport static com.android.shell.BugreportPrefs.STATE_SHOW;
21b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport static com.android.shell.BugreportPrefs.getWarningState;
22b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
23b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.BufferedOutputStream;
244967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Lemeimport java.io.ByteArrayInputStream;
25b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.File;
2669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport java.io.FileDescriptor;
27b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.FileInputStream;
28b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.FileOutputStream;
29b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.IOException;
30b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.io.InputStream;
3169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport java.io.PrintWriter;
324967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Lemeimport java.nio.charset.StandardCharsets;
3369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport java.text.NumberFormat;
34b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.util.ArrayList;
354967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Lemeimport java.util.Enumeration;
36d1e0f12979441733753b538611f6d73e5527c43cFelipe Lemeimport java.util.List;
37b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.util.zip.ZipEntry;
384967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Lemeimport java.util.zip.ZipFile;
39b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport java.util.zip.ZipOutputStream;
40b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
41b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport libcore.io.Streams;
42b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
43bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport com.android.internal.annotations.VisibleForTesting;
446605bd89c53494b59717a826f9a17641bc32da41Felipe Lemeimport com.android.internal.logging.MetricsLogger;
456605bd89c53494b59717a826f9a17641bc32da41Felipe Lemeimport com.android.internal.logging.MetricsProto.MetricsEvent;
46b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport com.google.android.collect.Lists;
47b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
48b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.accounts.Account;
49b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.accounts.AccountManager;
50bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.annotation.SuppressLint;
51bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.app.AlertDialog;
52b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.app.Notification;
539cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Lemeimport android.app.Notification.Action;
54b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.app.NotificationManager;
55b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.app.PendingIntent;
56b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.app.Service;
57b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.content.ClipData;
58b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.content.Context;
59d1e0f12979441733753b538611f6d73e5527c43cFelipe Lemeimport android.content.ContextWrapper;
60bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.content.DialogInterface;
61b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.content.Intent;
62b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.content.res.Configuration;
63b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.net.Uri;
64b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.os.AsyncTask;
6569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.os.Handler;
6669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.os.HandlerThread;
67b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.os.IBinder;
6869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.os.Looper;
6969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.os.Message;
70c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Lemeimport android.os.Parcel;
7169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.os.Parcelable;
72b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.os.SystemProperties;
73d1e0f12979441733753b538611f6d73e5527c43cFelipe Lemeimport android.os.Vibrator;
74b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.support.v4.content.FileProvider;
75bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.text.TextUtils;
7669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.text.format.DateUtils;
77b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.util.Log;
78b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.util.Patterns;
7969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Lemeimport android.util.SparseArray;
80bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.view.View;
81bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.view.WindowManager;
82bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.view.View.OnFocusChangeListener;
83bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.view.inputmethod.EditorInfo;
84bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.widget.Button;
85bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Lemeimport android.widget.EditText;
86b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemeimport android.widget.Toast;
87b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
8869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme/**
8946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme * Service used to keep progress of bugreport processes ({@code dumpstate}).
9069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <p>
9169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * The workflow is:
9269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <ol>
93fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme * <li>When {@code dumpstate} starts, it sends a {@code BUGREPORT_STARTED} with a sequential id,
94fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme * its pid, and the estimated total effort.
9569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>{@link BugreportReceiver} receives the intent and delegates it to this service.
9669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>Upon start, this service:
9769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <ol>
9869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>Issues a system notification so user can watch the progresss (which is 0% initially).
9969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>Polls the {@link SystemProperties} for updates on the {@code dumpstate} progress.
10069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>If the progress changed, it updates the system notification.
10169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * </ol>
10269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>As {@code dumpstate} progresses, it updates the system property.
10369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>When {@code dumpstate} finishes, it sends a {@code BUGREPORT_FINISHED} intent.
10469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>{@link BugreportReceiver} receives the intent and delegates it to this service, which in
10569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * turn:
10669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <ol>
10746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme * <li>Updates the system notification so user can share the bugreport.
10869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>Stops monitoring that {@code dumpstate} process.
10969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * <li>Stops itself if it doesn't have any process left to monitor.
11069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * </ol>
11169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme * </ol>
11269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme */
113b9238b37838d653c38ce4e712421adb61978fc22Felipe Lemepublic class BugreportProgressService extends Service {
114c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme    private static final String TAG = "BugreportProgressService";
11569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    private static final boolean DEBUG = false;
116b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
117b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    private static final String AUTHORITY = "com.android.shell";
118b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
11946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    // External intents sent by dumpstate.
12069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String INTENT_BUGREPORT_STARTED = "android.intent.action.BUGREPORT_STARTED";
12169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String INTENT_BUGREPORT_FINISHED = "android.intent.action.BUGREPORT_FINISHED";
122226940ed8550c02875a987f7e46699e6003ec1c0Michal Karpinski    static final String INTENT_REMOTE_BUGREPORT_FINISHED =
123226940ed8550c02875a987f7e46699e6003ec1c0Michal Karpinski            "android.intent.action.REMOTE_BUGREPORT_FINISHED";
124226940ed8550c02875a987f7e46699e6003ec1c0Michal Karpinski    static final String INTENT_REMOTE_BUGREPORT_DISPATCH =
125226940ed8550c02875a987f7e46699e6003ec1c0Michal Karpinski            "android.intent.action.REMOTE_BUGREPORT_DISPATCH";
12646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
12746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    // Internal intents used on notification actions.
1289cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Leme    static final String INTENT_BUGREPORT_CANCEL = "android.intent.action.BUGREPORT_CANCEL";
12946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    static final String INTENT_BUGREPORT_SHARE = "android.intent.action.BUGREPORT_SHARE";
130bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    static final String INTENT_BUGREPORT_INFO_LAUNCH =
131bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            "android.intent.action.BUGREPORT_INFO_LAUNCH";
132d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    static final String INTENT_BUGREPORT_SCREENSHOT =
133d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            "android.intent.action.BUGREPORT_SCREENSHOT";
13469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
135b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    static final String EXTRA_BUGREPORT = "android.intent.extra.BUGREPORT";
136b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    static final String EXTRA_SCREENSHOT = "android.intent.extra.SCREENSHOT";
137fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    static final String EXTRA_ID = "android.intent.extra.ID";
13869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String EXTRA_PID = "android.intent.extra.PID";
13969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String EXTRA_MAX = "android.intent.extra.MAX";
14069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String EXTRA_NAME = "android.intent.extra.NAME";
141bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    static final String EXTRA_TITLE = "android.intent.extra.TITLE";
142bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    static final String EXTRA_DESCRIPTION = "android.intent.extra.DESCRIPTION";
14369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    static final String EXTRA_ORIGINAL_INTENT = "android.intent.extra.ORIGINAL_INTENT";
144c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme    static final String EXTRA_INFO = "android.intent.extra.INFO";
14569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
14669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    private static final int MSG_SERVICE_COMMAND = 1;
14769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    private static final int MSG_POLL = 2;
148d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static final int MSG_DELAYED_SCREENSHOT = 3;
149d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static final int MSG_SCREENSHOT_REQUEST = 4;
150d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static final int MSG_SCREENSHOT_RESPONSE = 5;
151d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1528648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme    // Passed to Message.obtain() when msg.arg2 is not used.
1538648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme    private static final int UNUSED_ARG2 = -2;
1548648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme
155d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
156d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Delay before a screenshot is taken.
157d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * <p>
158d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Should be at least 3 seconds, otherwise its toast might show up in the screenshot.
159d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
160d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    static final int SCREENSHOT_DELAY_SECONDS = 3;
16169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
16269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    /** Polling frequency, in milliseconds. */
163bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    static final long POLLING_FREQUENCY = 2 * DateUtils.SECOND_IN_MILLIS;
16469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
16569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    /** How long (in ms) a dumpstate process will be monitored if it didn't show progress. */
1661eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme    private static final long INACTIVITY_TIMEOUT = 10 * DateUtils.MINUTE_IN_MILLIS;
16769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
168719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme    /** System properties used for monitoring progress. */
169719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme    private static final String DUMPSTATE_PREFIX = "dumpstate.";
170719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme    private static final String PROGRESS_SUFFIX = ".progress";
171719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme    private static final String MAX_SUFFIX = ".max";
172bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private static final String NAME_SUFFIX = ".name";
1739cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Leme
174bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /** System property (and value) used to stop dumpstate. */
175d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    // TODO: should call ActiveManager API instead
176719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme    private static final String CTL_STOP = "ctl.stop";
1774cc863338d5e43b6189e05498d7cb53ebba135e1Felipe Leme    private static final String BUGREPORT_SERVICE = "bugreportplus";
17869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
179d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
180d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Directory on Shell's data storage where screenshots will be stored.
181d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * <p>
182d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Must be a path supported by its FileProvider.
183d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
184d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static final String SCREENSHOT_DIR = "bugreports";
185d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
186fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    /** Managed dumpstate processes (keyed by id) */
18769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    private final SparseArray<BugreportInfo> mProcesses = new SparseArray<>();
18869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
189d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private Context mContext;
190d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private ServiceHandler mMainHandler;
191d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private ScreenshotHandler mScreenshotHandler;
19269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
193bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private final BugreportInfoDialog mInfoDialog = new BugreportInfoDialog();
194bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
195d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private File mScreenshotsDir;
196d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
197d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
198d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Flag indicating whether a screenshot is being taken.
199d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * <p>
200d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * This is the only state that is shared between the 2 handlers and hence must have synchronized
201d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * access.
202d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
203d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private boolean mTakingScreenshot;
204d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
20569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    @Override
20669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    public void onCreate() {
207d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mContext = getApplicationContext();
208d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mMainHandler = new ServiceHandler("BugreportProgressServiceMainThread");
209d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mScreenshotHandler = new ScreenshotHandler("BugreportProgressServiceScreenshotThread");
210d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
211d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mScreenshotsDir = new File(new ContextWrapper(mContext).getFilesDir(), SCREENSHOT_DIR);
212d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (!mScreenshotsDir.exists()) {
213d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.i(TAG, "Creating directory " + mScreenshotsDir + " to store temporary screenshots");
214d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (!mScreenshotsDir.mkdir()) {
215d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                Log.w(TAG, "Could not create directory " + mScreenshotsDir);
216d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
217d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
21869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    }
219b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
220b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    @Override
221b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    public int onStartCommand(Intent intent, int flags, int startId) {
222b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        if (intent != null) {
22369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            // Handle it in a separate thread.
224d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final Message msg = mMainHandler.obtainMessage();
22569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            msg.what = MSG_SERVICE_COMMAND;
22669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            msg.obj = intent;
227d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            mMainHandler.sendMessage(msg);
228b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
22969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
23069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        // If service is killed it cannot be recreated because it would not know which
231fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        // dumpstate IDs it would have to watch.
232b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return START_NOT_STICKY;
233b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
234b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
235b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    @Override
236b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    public IBinder onBind(Intent intent) {
237b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return null;
238b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
239b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
24069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    @Override
24169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    public void onDestroy() {
242d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mMainHandler.getLooper().quit();
243d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mScreenshotHandler.getLooper().quit();
24469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        super.onDestroy();
24569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    }
246b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
24769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    @Override
24869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
249d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final int size = mProcesses.size();
250d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (size == 0) {
251d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            writer.printf("No monitored processes");
252d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
253d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
254d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        writer.printf("Monitored dumpstate processes\n");
255d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        writer.printf("-----------------------------\n");
256d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        for (int i = 0; i < size; i++) {
257d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            writer.printf("%s\n", mProcesses.valueAt(i));
25869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
25969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    }
26069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
261d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
262d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Main thread used to handle all requests but taking screenshots.
263d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
26469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    private final class ServiceHandler extends Handler {
265d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        public ServiceHandler(String name) {
266d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            super(newLooper(name));
26769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
26869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
26969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        @Override
27069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        public void handleMessage(Message msg) {
27169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            if (msg.what == MSG_POLL) {
272923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                poll();
27369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                return;
27469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
27569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
276d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (msg.what == MSG_DELAYED_SCREENSHOT) {
277d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                takeScreenshot(msg.arg1, msg.arg2);
278d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                return;
279d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
280d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
281d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (msg.what == MSG_SCREENSHOT_RESPONSE) {
282d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                handleScreenshotResponse(msg);
283d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                return;
284d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
285d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
28669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            if (msg.what != MSG_SERVICE_COMMAND) {
28769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                // Sanity check.
28869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                Log.e(TAG, "Invalid message type: " + msg.what);
28969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                return;
29069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
29169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
29246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            // At this point it's handling onStartCommand(), with the intent passed as an Extra.
29369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            if (!(msg.obj instanceof Intent)) {
29469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                // Sanity check.
295af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme                Log.wtf(TAG, "handleMessage(): invalid msg.obj type: " + msg.obj);
29669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                return;
29769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
29869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            final Parcelable parcel = ((Intent) msg.obj).getParcelableExtra(EXTRA_ORIGINAL_INTENT);
29946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            final Intent intent;
30046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            if (parcel instanceof Intent) {
30146d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                // The real intent was passed to BugreportReceiver, which delegated to the service.
30246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                intent = (Intent) parcel;
30346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            } else {
30446d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                intent = (Intent) msg.obj;
30569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
30669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            final String action = intent.getAction();
30746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            final int pid = intent.getIntExtra(EXTRA_PID, 0);
30885ae3cf46ad66d71e5a29a93e89a0f569d74288bFelipe Leme            final int id = intent.getIntExtra(EXTRA_ID, 0);
30946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            final int max = intent.getIntExtra(EXTRA_MAX, -1);
31046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            final String name = intent.getStringExtra(EXTRA_NAME);
31169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
312fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            if (DEBUG)
313fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                Log.v(TAG, "action: " + action + ", name: " + name + ", id: " + id + ", pid: "
314fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                        + pid + ", max: " + max);
31569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            switch (action) {
31669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                case INTENT_BUGREPORT_STARTED:
317fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    if (!startProgress(name, id, pid, max)) {
31869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                        stopSelfWhenDone();
31969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                        return;
32069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                    }
32146d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                    poll();
32269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                    break;
32369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                case INTENT_BUGREPORT_FINISHED:
324fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    if (id == 0) {
32569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                        // Shouldn't happen, unless BUGREPORT_FINISHED is received from a legacy,
32669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                        // out-of-sync dumpstate process.
327fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                        Log.w(TAG, "Missing " + EXTRA_ID + " on intent " + intent);
32869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                    }
329fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    onBugreportFinished(id, intent);
33046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                    break;
331bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                case INTENT_BUGREPORT_INFO_LAUNCH:
332fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    launchBugreportInfoDialog(id);
333bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    break;
334d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                case INTENT_BUGREPORT_SCREENSHOT:
335fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    takeScreenshot(id, true);
336d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    break;
33746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                case INTENT_BUGREPORT_SHARE:
338fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    shareBugreport(id, (BugreportInfo) intent.getParcelableExtra(EXTRA_INFO));
33969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                    break;
3409cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Leme                case INTENT_BUGREPORT_CANCEL:
341fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    cancel(id);
3429cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Leme                    break;
34369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                default:
34469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                    Log.w(TAG, "Unsupported intent: " + action);
34569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
34669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            return;
34769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
34869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
34969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
350923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        private void poll() {
351923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            if (pollProgress()) {
352923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                // Keep polling...
353923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                sendEmptyMessageDelayed(MSG_POLL, POLLING_FREQUENCY);
35446d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            } else {
35546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                Log.i(TAG, "Stopped polling");
35669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
357923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        }
358923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
35969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
360923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
361d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Separate thread used only to take screenshots so it doesn't block the main thread.
362d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
363d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private final class ScreenshotHandler extends Handler {
364d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        public ScreenshotHandler(String name) {
365d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            super(newLooper(name));
366d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
367d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
368d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        @Override
369d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        public void handleMessage(Message msg) {
370d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (msg.what != MSG_SCREENSHOT_REQUEST) {
371d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                Log.e(TAG, "Invalid message type: " + msg.what);
372d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                return;
373d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
374d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            handleScreenshotRequest(msg);
375d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
376d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
377d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
378fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private BugreportInfo getInfo(int id) {
379fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = mProcesses.get(id);
380d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
381fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.w(TAG, "Not monitoring process with ID " + id);
382d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
383d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        return info;
384d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
385d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
386d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
387923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * Creates the {@link BugreportInfo} for a process and issue a system notification to
388923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * indicate its progress.
389923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     *
390923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * @return whether it succeeded or not.
391923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
392fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private boolean startProgress(String name, int id, int pid, int max) {
393923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        if (name == null) {
394923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            Log.w(TAG, "Missing " + EXTRA_NAME + " on start intent");
395923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        }
396fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        if (id == -1) {
397fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.e(TAG, "Missing " + EXTRA_ID + " on start intent");
398fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            return false;
399fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        }
400923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        if (pid == -1) {
401923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            Log.e(TAG, "Missing " + EXTRA_PID + " on start intent");
402923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            return false;
403923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        }
404923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        if (max <= 0) {
405923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            Log.e(TAG, "Invalid value for extra " + EXTRA_MAX + ": " + max);
406923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            return false;
40769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
40869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
409fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = new BugreportInfo(mContext, id, pid, name, max);
410fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        if (mProcesses.indexOfKey(id) >= 0) {
411fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.w(TAG, "ID " + id + " already watched");
412d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } else {
413fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mProcesses.put(info.id, info);
414923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        }
415d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        // Take initial screenshot.
416fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        takeScreenshot(id, false);
417923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        updateProgress(info);
418923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        return true;
419923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
42069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
421923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
42246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * Updates the system notification for a given bugreport.
423923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
424923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    private void updateProgress(BugreportInfo info) {
425923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        if (info.max <= 0 || info.progress < 0) {
426923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            Log.e(TAG, "Invalid progress values for " + info);
427923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            return;
42869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
42969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
430923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        final NumberFormat nf = NumberFormat.getPercentInstance();
431923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        nf.setMinimumFractionDigits(2);
432923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        nf.setMaximumFractionDigits(2);
433923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        final String percentText = nf.format((double) info.progress / info.max);
434d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Action cancelAction = new Action.Builder(null, mContext.getString(
435d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                com.android.internal.R.string.cancel), newCancelIntent(mContext, info)).build();
436d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Intent infoIntent = new Intent(mContext, BugreportProgressService.class);
437bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        infoIntent.setAction(INTENT_BUGREPORT_INFO_LAUNCH);
438fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        infoIntent.putExtra(EXTRA_ID, info.id);
439bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        final Action infoAction = new Action.Builder(null,
440d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                mContext.getString(R.string.bugreport_info_action),
441fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                PendingIntent.getService(mContext, info.id, infoIntent,
442bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        PendingIntent.FLAG_UPDATE_CURRENT)).build();
443d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Intent screenshotIntent = new Intent(mContext, BugreportProgressService.class);
444d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        screenshotIntent.setAction(INTENT_BUGREPORT_SCREENSHOT);
445fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        screenshotIntent.putExtra(EXTRA_ID, info.id);
446d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        PendingIntent screenshotPendingIntent = mTakingScreenshot ? null : PendingIntent
447fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                .getService(mContext, info.id, screenshotIntent,
448d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                        PendingIntent.FLAG_UPDATE_CURRENT);
449d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Action screenshotAction = new Action.Builder(null,
450d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                mContext.getString(R.string.bugreport_screenshot_action),
451d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                screenshotPendingIntent).build();
452d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
453fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final String title = mContext.getString(R.string.bugreport_in_progress_title, info.id);
454923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme
455923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        final String name =
456d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                info.name != null ? info.name : mContext.getString(R.string.bugreport_unnamed);
457923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme
458d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Notification notification = new Notification.Builder(mContext)
459923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
460923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setContentTitle(title)
461923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setTicker(title)
462923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setContentText(name)
463923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setContentInfo(percentText)
464923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setProgress(info.max, info.progress, false)
465923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setOngoing(true)
466923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .setLocalOnly(true)
467d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                .setColor(mContext.getColor(
468923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                        com.android.internal.R.color.system_notification_accent_color))
469bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                .addAction(infoAction)
470d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                .addAction(screenshotAction)
471923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .addAction(cancelAction)
472923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme                .build();
473923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme
4742288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme        if (info.finished) {
4752288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme            Log.w(TAG, "Not sending progress notification because bugreport has finished already ("
4762288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                    + info + ")");
4772288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme            return;
4782288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme        }
479262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme        if (DEBUG) {
480262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme            Log.d(TAG, "Sending 'Progress' notification for id " + info.id + "(pid " + info.pid
481262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                    + "): " + percentText);
482262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme        }
483fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        NotificationManager.from(mContext).notify(TAG, info.id, notification);
484923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
485923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme
486923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
48746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * Creates a {@link PendingIntent} for a notification action used to cancel a bugreport.
48846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     */
48946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    private static PendingIntent newCancelIntent(Context context, BugreportInfo info) {
49046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        final Intent intent = new Intent(INTENT_BUGREPORT_CANCEL);
49146d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        intent.setClass(context, BugreportProgressService.class);
492fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        intent.putExtra(EXTRA_ID, info.id);
493fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        return PendingIntent.getService(context, info.id, intent,
494bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                PendingIntent.FLAG_UPDATE_CURRENT);
49546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    }
49646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
49746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    /**
49846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * Finalizes the progress on a given bugreport and cancel its notification.
499923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
500fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void stopProgress(int id) {
501fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        if (mProcesses.indexOfKey(id) < 0) {
502fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.w(TAG, "ID not watched: " + id);
503d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } else {
504fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.d(TAG, "Removing ID " + id);
505fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mProcesses.remove(id);
50669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
507d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        stopSelfWhenDone();
508fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        Log.v(TAG, "stopProgress(" + id + "): cancel notification");
509fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        NotificationManager.from(mContext).cancel(TAG, id);
510923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
511923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme
512923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
513923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * Cancels a bugreport upon user's request.
514923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
515fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void cancel(int id) {
5166605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_CANCEL);
517fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        Log.v(TAG, "cancel: ID=" + id);
518fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = getInfo(id);
519d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info != null && !info.finished) {
520fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.i(TAG, "Cancelling bugreport service (ID=" + id + ") on user's request");
521d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            setSystemProperty(CTL_STOP, BUGREPORT_SERVICE);
522d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            deleteScreenshots(info);
523bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
524fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        stopProgress(id);
525923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
5269cadb75714bea7d435f34c8b7d06f698fa907a8dFelipe Leme
527923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
528923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * Poll {@link SystemProperties} to get the progress on each monitored process.
529923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     *
530923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * @return whether it should keep polling.
531923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
532923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    private boolean pollProgress() {
533d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final int total = mProcesses.size();
534d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (total == 0) {
535d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.d(TAG, "No process to poll progress.");
536d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
537d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        int activeProcesses = 0;
538d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        for (int i = 0; i < total; i++) {
539d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final BugreportInfo info = mProcesses.valueAt(i);
540af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            if (info == null) {
541fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                Log.wtf(TAG, "pollProgress(): null info at index " + i + "(ID = "
542af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme                        + mProcesses.keyAt(i) + ")");
543af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme                continue;
544af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            }
545af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme
546af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            final int pid = info.pid;
547fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            final int id = info.id;
548d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (info.finished) {
54985ae3cf46ad66d71e5a29a93e89a0f569d74288bFelipe Leme                if (DEBUG) Log.v(TAG, "Skipping finished process " + pid + " (id: " + id + ")");
550d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                continue;
551923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme            }
552d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            activeProcesses++;
553d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final String progressKey = DUMPSTATE_PREFIX + pid + PROGRESS_SUFFIX;
554d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final int progress = SystemProperties.getInt(progressKey, 0);
555d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (progress == 0) {
556d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                Log.v(TAG, "System property " + progressKey + " is not set yet");
557d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
558d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final int max = SystemProperties.getInt(DUMPSTATE_PREFIX + pid + MAX_SUFFIX, 0);
559d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final boolean maxChanged = max > 0 && max != info.max;
560d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final boolean progressChanged = progress > 0 && progress != info.progress;
561d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
562d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (progressChanged || maxChanged) {
563d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                if (progressChanged) {
564fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    if (DEBUG) Log.v(TAG, "Updating progress for PID " + pid + "(id: " + id
565fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                            + ") from " + info.progress + " to " + progress);
566d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    info.progress = progress;
56746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                }
568d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                if (maxChanged) {
569fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    Log.i(TAG, "Updating max progress for PID " + pid + "(id: " + id
570fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                            + ") from " + info.max + " to " + max);
571d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    info.max = max;
57269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                }
573d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                info.lastUpdate = System.currentTimeMillis();
574d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                updateProgress(info);
575d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            } else {
576d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                long inactiveTime = System.currentTimeMillis() - info.lastUpdate;
577d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                if (inactiveTime >= INACTIVITY_TIMEOUT) {
578fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    Log.w(TAG, "No progress update for PID " + pid + " since "
579d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                            + info.getFormattedLastUpdate());
580fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                    stopProgress(info.id);
58169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                }
58269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            }
58369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
584d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (DEBUG) Log.v(TAG, "pollProgress() total=" + total + ", actives=" + activeProcesses);
585d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        return activeProcesses > 0;
586923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
58769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
588923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    /**
589bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Fetches a {@link BugreportInfo} for a given process and launches a dialog where the user can
590bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * change its values.
591bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
592fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void launchBugreportInfoDialog(int id) {
5936605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_DETAILS);
594bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        // Copy values so it doesn't lock mProcesses while UI is being updated
595bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        final String name, title, description;
596fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = getInfo(id);
597d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
5981eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // Most likely am killed Shell before user tapped the notification. Since system might
5991eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // be too busy anwyays, it's better to ignore the notification and switch back to the
6001eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // non-interactive mode (where the bugerport will be shared upon completion).
6011eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            Log.d(TAG, "launchBugreportInfoDialog(" + id + "): cancel notification");
6021eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // TODO: add test case to make sure notification is canceled.
6031eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            NotificationManager.from(mContext).cancel(TAG, id);
604d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
605d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
606d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
607d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        collapseNotificationBar();
608fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        mInfoDialog.initialize(mContext, info);
609d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
610d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
611d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
612d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Starting point for taking a screenshot.
613d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * <p>
614d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * If {@code delayed} is set, it first display a toast message and waits
615d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * {@link #SCREENSHOT_DELAY_SECONDS} seconds before taking it, otherwise it takes the screenshot
616d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * right away.
617d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * <p>
618d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Typical usage is delaying when taken from the notification action, and taking it right away
619d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * upon receiving a {@link #INTENT_BUGREPORT_STARTED}.
620d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
621fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void takeScreenshot(int id, boolean delayed) {
6226605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_SCREENSHOT);
6231eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme        if (getInfo(id) == null) {
6241eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // Most likely am killed Shell before user tapped the notification. Since system might
6251eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // be too busy anwyays, it's better to ignore the notification and switch back to the
6261eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // non-interactive mode (where the bugerport will be shared upon completion).
6271eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            Log.d(TAG, "takeScreenshot(" + id + ", " + delayed + "): cancel notification");
6281eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            // TODO: add test case to make sure notification is canceled.
6291eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            NotificationManager.from(mContext).cancel(TAG, id);
6301eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme            return;
6311eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme        }
632d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        setTakingScreenshot(true);
633d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (delayed) {
634d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            collapseNotificationBar();
635d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final String msg = mContext.getResources()
636d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    .getQuantityString(com.android.internal.R.plurals.bugreport_countdown,
637d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                            SCREENSHOT_DELAY_SECONDS, SCREENSHOT_DELAY_SECONDS);
638d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.i(TAG, msg);
639d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            // Show a toast just once, otherwise it might be captured in the screenshot.
640d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
641d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
642fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            takeScreenshot(id, SCREENSHOT_DELAY_SECONDS);
643d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } else {
644fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            takeScreenshot(id, 0);
645d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
646d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
647d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
648d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
649d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Takes a screenshot after {@code delay} seconds.
650d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
651fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void takeScreenshot(int id, int delay) {
652d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (delay > 0) {
653fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.d(TAG, "Taking screenshot for " + id + " in " + delay + " seconds");
654d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final Message msg = mMainHandler.obtainMessage();
655d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            msg.what = MSG_DELAYED_SCREENSHOT;
656fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            msg.arg1 = id;
657d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            msg.arg2 = delay - 1;
658d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            mMainHandler.sendMessageDelayed(msg, DateUtils.SECOND_IN_MILLIS);
659d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
660d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
661d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
662d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        // It's time to take the screenshot: let the proper thread handle it
663fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = getInfo(id);
664d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
665d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
666d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
667d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final String screenshotPath =
668d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                new File(mScreenshotsDir, info.getPathNextScreenshot()).getAbsolutePath();
669d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
6708648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme        Message.obtain(mScreenshotHandler, MSG_SCREENSHOT_REQUEST, id, UNUSED_ARG2, screenshotPath)
6718648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme                .sendToTarget();
672d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
673d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
674d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
675d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Sets the internal {@code mTakingScreenshot} state and updates all notifications so their
676d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * SCREENSHOT button is enabled or disabled accordingly.
677d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
678d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void setTakingScreenshot(boolean flag) {
679d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        synchronized (BugreportProgressService.this) {
680d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            mTakingScreenshot = flag;
681d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            for (int i = 0; i < mProcesses.size(); i++) {
6822288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                final BugreportInfo info = mProcesses.valueAt(i);
6832288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                if (info.finished) {
6842288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                    Log.d(TAG, "Not updating progress because share notification was already sent");
6852288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                    continue;
6862288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                }
6872288129d5208cd26ab41191db69a418d15ead9eeFelipe Leme                updateProgress(info);
688bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
689bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
690d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
691bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
692d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void handleScreenshotRequest(Message requestMsg) {
693d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        String screenshotFile = (String) requestMsg.obj;
694d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        boolean taken = takeScreenshot(mContext, screenshotFile);
695d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        setTakingScreenshot(false);
696d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
6978648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme        Message.obtain(mMainHandler, MSG_SCREENSHOT_RESPONSE, requestMsg.arg1, taken ? 1 : 0,
6988648a15406d43c8af12e9cfe2355b1eee201d479Felipe Leme                screenshotFile).sendToTarget();
699d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
700bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
701d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void handleScreenshotResponse(Message resultMsg) {
702d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final boolean taken = resultMsg.arg2 != 0;
703d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final BugreportInfo info = getInfo(resultMsg.arg1);
704d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
705d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
706d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
707d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final File screenshotFile = new File((String) resultMsg.obj);
708d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
709d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final int msgId;
710d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (taken) {
711d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            info.addScreenshot(screenshotFile);
712c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            if (info.finished) {
713c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                Log.d(TAG, "Screenshot finished after bugreport; updating share notification");
714c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                info.renameScreenshots(mScreenshotsDir);
715c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                sendBugreportNotification(mContext, info);
716c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
717d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            msgId = R.string.bugreport_screenshot_taken;
718d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } else {
719d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            // TODO: try again using Framework APIs instead of relying on screencap.
720d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            msgId = R.string.bugreport_screenshot_failed;
721d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
722d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final String msg = mContext.getString(msgId);
723d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        Log.d(TAG, msg);
724d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
725d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
726d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
727d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
728d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Deletes all screenshots taken for a given bugreport.
729d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
730d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void deleteScreenshots(BugreportInfo info) {
731d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        for (File file : info.screenshotFiles) {
732d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.i(TAG, "Deleting screenshot file " + file);
733d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            file.delete();
734d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
735bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
736bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
737bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
738923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     * Finishes the service when it's not monitoring any more processes.
739923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme     */
740923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    private void stopSelfWhenDone() {
741d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (mProcesses.size() > 0) {
742fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            if (DEBUG) Log.d(TAG, "Staying alive, waiting for IDs " + mProcesses);
743d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
74469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
745fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        Log.v(TAG, "No more processes to handle, shutting down");
746d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        stopSelf();
747923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme    }
74869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
749bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
750bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Handles the BUGREPORT_FINISHED intent sent by {@code dumpstate}.
751bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
752fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void onBugreportFinished(int id, Intent intent) {
753af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme        final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
754af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme        if (bugreportFile == null) {
755af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            // Should never happen, dumpstate always set the file.
756af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            Log.wtf(TAG, "Missing " + EXTRA_BUGREPORT + " on intent " + intent);
757af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            return;
758af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme        }
759fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        mInfoDialog.onBugreportFinished(id);
760fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        BugreportInfo info = getInfo(id);
761d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
762d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            // Happens when BUGREPORT_FINISHED was received without a BUGREPORT_STARTED first.
763fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.v(TAG, "Creating info for untracked ID " + id);
764fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            info = new BugreportInfo(mContext, id);
765fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mProcesses.put(id, info);
766d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
767d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        info.renameScreenshots(mScreenshotsDir);
768af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme        info.bugreportFile = bugreportFile;
769af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme
770510e922e47fec69839dd48c5473540f93d79a508Felipe Leme        final int max = intent.getIntExtra(EXTRA_MAX, -1);
771510e922e47fec69839dd48c5473540f93d79a508Felipe Leme        if (max != -1) {
772510e922e47fec69839dd48c5473540f93d79a508Felipe Leme            MetricsLogger.histogram(this, "dumpstate_duration", max);
773510e922e47fec69839dd48c5473540f93d79a508Felipe Leme            info.max = max;
774510e922e47fec69839dd48c5473540f93d79a508Felipe Leme        }
775510e922e47fec69839dd48c5473540f93d79a508Felipe Leme
776d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final File screenshot = getFileExtra(intent, EXTRA_SCREENSHOT);
777d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (screenshot != null) {
778d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            info.addScreenshot(screenshot);
77946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        }
780d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        info.finished = true;
78169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
782d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Configuration conf = mContext.getResources().getConfiguration();
783923afa9fe1bbc9a5395309622e596ad03c3d8481Felipe Leme        if ((conf.uiMode & Configuration.UI_MODE_TYPE_MASK) != Configuration.UI_MODE_TYPE_WATCH) {
784d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            triggerLocalNotification(mContext, info);
785b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
786b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
787b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
788b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
78969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme     * Responsible for triggering a notification that allows the user to start a "share" intent with
79046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * the bugreport. On watches we have other methods to allow the user to start this intent
79169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme     * (usually by triggering it on another connected device); we don't need to display the
79269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme     * notification in this case.
793b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
794d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void triggerLocalNotification(final Context context, final BugreportInfo info) {
79546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        if (!info.bugreportFile.exists() || !info.bugreportFile.canRead()) {
79646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            Log.e(TAG, "Could not read bugreport file " + info.bugreportFile);
797d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Toast.makeText(context, R.string.bugreport_unreadable_text, Toast.LENGTH_LONG).show();
798fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            stopProgress(info.id);
799b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            return;
800b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
801b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
80246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        boolean isPlainText = info.bugreportFile.getName().toLowerCase().endsWith(".txt");
803b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        if (!isPlainText) {
804b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            // Already zipped, send it right away.
80546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            sendBugreportNotification(context, info);
806b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        } else {
807b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            // Asynchronously zip the file first, then send it.
80846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            sendZippedBugreportNotification(context, info);
809b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
810b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
811b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
812b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    private static Intent buildWarningIntent(Context context, Intent sendIntent) {
813b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final Intent intent = new Intent(context, BugreportWarningActivity.class);
814b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.putExtra(Intent.EXTRA_INTENT, sendIntent);
815b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return intent;
816b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
817b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
818b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
819b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     * Build {@link Intent} that can be used to share the given bugreport.
820b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
821bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private static Intent buildSendIntent(Context context, BugreportInfo info) {
822bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        // Files are kept on private storage, so turn into Uris that we can
823bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        // grant temporary permissions for.
824bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        final Uri bugreportUri = getUri(context, info.bugreportFile);
825bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
826b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
827b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final String mimeType = "application/vnd.android.bugreport";
828b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
829b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.addCategory(Intent.CATEGORY_DEFAULT);
830b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.setType(mimeType);
831b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
832c8e2b6092c0fbf87e71f81fd2cffbb29ff8d9039Felipe Leme        final String subject = !TextUtils.isEmpty(info.title) ?
833c8e2b6092c0fbf87e71f81fd2cffbb29ff8d9039Felipe Leme                info.title : bugreportUri.getLastPathSegment();
834bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
835b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
836b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        // EXTRA_TEXT should be an ArrayList, but some clients are expecting a single String.
837b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        // So, to avoid an exception on Intent.migrateExtraStreamToClipData(), we need to manually
838b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        // create the ClipData object with the attachments URIs.
839d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final StringBuilder messageBody = new StringBuilder("Build info: ")
840bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            .append(SystemProperties.get("ro.build.description"))
841bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            .append("\nSerial number: ")
842bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            .append(SystemProperties.get("ro.serialno"));
843bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        if (!TextUtils.isEmpty(info.description)) {
844bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            messageBody.append("\nDescription: ").append(info.description);
845bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
846bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        intent.putExtra(Intent.EXTRA_TEXT, messageBody.toString());
847b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final ClipData clipData = new ClipData(null, new String[] { mimeType },
848b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                new ClipData.Item(null, null, null, bugreportUri));
849b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final ArrayList<Uri> attachments = Lists.newArrayList(bugreportUri);
850d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        for (File screenshot : info.screenshotFiles) {
851d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final Uri screenshotUri = getUri(context, screenshot);
852b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            clipData.addItem(new ClipData.Item(null, null, null, screenshotUri));
853b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            attachments.add(screenshotUri);
854b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
855b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.setClipData(clipData);
856b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
857b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
858b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final Account sendToAccount = findSendToAccount(context);
859b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        if (sendToAccount != null) {
860b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            intent.putExtra(Intent.EXTRA_EMAIL, new String[] { sendToAccount.name });
861b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
862b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
863b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return intent;
864b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
865b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
866b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
86746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * Shares the bugreport upon user's request by issuing a {@link Intent#ACTION_SEND_MULTIPLE}
86846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * intent, but issuing a warning dialog the first time.
869b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
870fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void shareBugreport(int id, BugreportInfo sharedInfo) {
8716605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_SHARE);
872fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        BugreportInfo info = getInfo(id);
873d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
874c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            // Service was terminated but notification persisted
875c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            info = sharedInfo;
876fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            Log.d(TAG, "shareBugreport(): no info for ID " + id + " on managed processes ("
877c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                    + mProcesses + "), using info from intent instead (" + info + ")");
87846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        }
8794967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
88018b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        addDetailsToZipFile(mContext, info);
8814967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
882d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final Intent sendIntent = buildSendIntent(mContext, info);
88346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        final Intent notifIntent;
884b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
885b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        // Send through warning dialog by default
886d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (getWarningState(mContext, STATE_SHOW) == STATE_SHOW) {
887d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            notifIntent = buildWarningIntent(mContext, sendIntent);
888b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        } else {
889b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            notifIntent = sendIntent;
890b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
891b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        notifIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
892b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
89346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        // Send the share intent...
894d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        mContext.startActivity(notifIntent);
89546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
89646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        // ... and stop watching this process.
897fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        stopProgress(id);
89846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    }
89946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
90046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    /**
9012758d5d93970f26867d778c944605371e55b751eFelipe Leme     * Sends a notification indicating the bugreport has finished so use can share it.
90246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     */
90346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme    private static void sendBugreportNotification(Context context, BugreportInfo info) {
90418b5892950b7f21e66c9268129323cbc0e865699Felipe Leme
90518b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        // Since adding the details can take a while, do it before notifying user.
90618b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        addDetailsToZipFile(context, info);
90718b5892950b7f21e66c9268129323cbc0e865699Felipe Leme
90846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        final Intent shareIntent = new Intent(INTENT_BUGREPORT_SHARE);
90946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        shareIntent.setClass(context, BugreportProgressService.class);
91046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        shareIntent.setAction(INTENT_BUGREPORT_SHARE);
911fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        shareIntent.putExtra(EXTRA_ID, info.id);
912c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        shareIntent.putExtra(EXTRA_INFO, info);
91346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
914fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final String title = context.getString(R.string.bugreport_finished_title, info.id);
915b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final Notification.Builder builder = new Notification.Builder(context)
916b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
91769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                .setContentTitle(title)
91869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                .setTicker(title)
919b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                .setContentText(context.getString(R.string.bugreport_finished_text))
920fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                .setContentIntent(PendingIntent.getService(context, info.id, shareIntent,
921bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        PendingIntent.FLAG_UPDATE_CURRENT))
92246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                .setDeleteIntent(newCancelIntent(context, info))
923b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                .setLocalOnly(true)
924b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                .setColor(context.getColor(
925b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                        com.android.internal.R.color.system_notification_accent_color));
926b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
927bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        if (!TextUtils.isEmpty(info.name)) {
928bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            builder.setContentInfo(info.name);
929bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
930bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
931fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        Log.v(TAG, "Sending 'Share' notification for ID " + info.id + ": " + title);
932fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        NotificationManager.from(context).notify(TAG, info.id, builder.build());
933b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
934b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
935b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
9362758d5d93970f26867d778c944605371e55b751eFelipe Leme     * Sends a notification indicating the bugreport is being updated so the user can wait until it
9372758d5d93970f26867d778c944605371e55b751eFelipe Leme     * finishes - at this point there is nothing to be done other than waiting, hence it has no
9382758d5d93970f26867d778c944605371e55b751eFelipe Leme     * pending action.
9392758d5d93970f26867d778c944605371e55b751eFelipe Leme     */
940fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private static void sendBugreportBeingUpdatedNotification(Context context, int id) {
9412758d5d93970f26867d778c944605371e55b751eFelipe Leme        final String title = context.getString(R.string.bugreport_updating_title);
9422758d5d93970f26867d778c944605371e55b751eFelipe Leme        final Notification.Builder builder = new Notification.Builder(context)
9432758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
9442758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setContentTitle(title)
9452758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setTicker(title)
9462758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setContentText(context.getString(R.string.bugreport_updating_wait))
9472758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setLocalOnly(true)
9482758d5d93970f26867d778c944605371e55b751eFelipe Leme                .setColor(context.getColor(
9492758d5d93970f26867d778c944605371e55b751eFelipe Leme                        com.android.internal.R.color.system_notification_accent_color));
950fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        Log.v(TAG, "Sending 'Updating zip' notification for ID " + id + ": " + title);
951fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        NotificationManager.from(context).notify(TAG, id, builder.build());
9522758d5d93970f26867d778c944605371e55b751eFelipe Leme    }
9532758d5d93970f26867d778c944605371e55b751eFelipe Leme
9542758d5d93970f26867d778c944605371e55b751eFelipe Leme    /**
955b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     * Sends a zipped bugreport notification.
956b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
957b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    private static void sendZippedBugreportNotification(final Context context,
95846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            final BugreportInfo info) {
959b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        new AsyncTask<Void, Void, Void>() {
960b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            @Override
961b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            protected Void doInBackground(Void... params) {
9624967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                zipBugreport(info);
96346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme                sendBugreportNotification(context, info);
964b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                return null;
965b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            }
966b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }.execute();
967b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
968b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
969b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
970b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     * Zips a bugreport file, returning the path to the new file (or to the
971b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     * original in case of failure).
972b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
9734967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    private static void zipBugreport(BugreportInfo info) {
9744967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final String bugreportPath = info.bugreportFile.getAbsolutePath();
9754967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final String zippedPath = bugreportPath.replace(".txt", ".zip");
976b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        Log.v(TAG, "zipping " + bugreportPath + " as " + zippedPath);
9774967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final File bugreportZippedFile = new File(zippedPath);
9784967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        try (InputStream is = new FileInputStream(info.bugreportFile);
97969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                ZipOutputStream zos = new ZipOutputStream(
98069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme                        new BufferedOutputStream(new FileOutputStream(bugreportZippedFile)))) {
9814967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            addEntry(zos, info.bugreportFile.getName(), is);
9824967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            // Delete old file
9834967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            final boolean deleted = info.bugreportFile.delete();
984b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            if (deleted) {
985b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                Log.v(TAG, "deleted original bugreport (" + bugreportPath + ")");
986b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            } else {
987b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                Log.e(TAG, "could not delete original bugreport (" + bugreportPath + ")");
988b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            }
9894967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            info.bugreportFile = bugreportZippedFile;
990b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        } catch (IOException e) {
99169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            Log.e(TAG, "exception zipping file " + zippedPath, e);
992b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
993b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
994b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
995b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    /**
9964967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme     * Adds the user-provided info into the bugreport zip file.
9974967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme     * <p>
9984967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme     * If user provided a title, it will be saved into a {@code title.txt} entry; similarly, the
9994967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme     * description will be saved on {@code description.txt}.
10004967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme     */
100118b5892950b7f21e66c9268129323cbc0e865699Felipe Leme    private static void addDetailsToZipFile(Context context, BugreportInfo info) {
1002c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        if (info.bugreportFile == null) {
1003c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            // One possible reason is a bug in the Parcelization code.
1004af6fd4086c8f24e8b70a810fe83081b67e5db236Felipe Leme            Log.wtf(TAG, "addDetailsToZipFile(): no bugreportFile on " + info);
1005c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            return;
1006c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1007b9d598c47b69406174b015ec9f064448e4b3b8b5Felipe Leme        if (TextUtils.isEmpty(info.title) && TextUtils.isEmpty(info.description)) {
1008b9d598c47b69406174b015ec9f064448e4b3b8b5Felipe Leme            Log.d(TAG, "Not touching zip file since neither title nor description are set");
1009b9d598c47b69406174b015ec9f064448e4b3b8b5Felipe Leme            return;
1010b9d598c47b69406174b015ec9f064448e4b3b8b5Felipe Leme        }
101118b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        if (info.addedDetailsToZip || info.addingDetailsToZip) {
101218b5892950b7f21e66c9268129323cbc0e865699Felipe Leme            Log.d(TAG, "Already added details to zip file for " + info);
101318b5892950b7f21e66c9268129323cbc0e865699Felipe Leme            return;
101418b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        }
101518b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        info.addingDetailsToZip = true;
10162758d5d93970f26867d778c944605371e55b751eFelipe Leme
10174967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        // It's not possible to add a new entry into an existing file, so we need to create a new
10184967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        // zip, copy all entries, then rename it.
1019fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        sendBugreportBeingUpdatedNotification(context, info.id); // ...and that takes time
10204967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final File dir = info.bugreportFile.getParentFile();
10214967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final File tmpZip = new File(dir, "tmp-" + info.bugreportFile.getName());
10222758d5d93970f26867d778c944605371e55b751eFelipe Leme        Log.d(TAG, "Writing temporary zip file (" + tmpZip + ") with title and/or description");
10234967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        try (ZipFile oldZip = new ZipFile(info.bugreportFile);
10244967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpZip))) {
10254967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10264967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            // First copy contents from original zip.
10274967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            Enumeration<? extends ZipEntry> entries = oldZip.entries();
10284967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            while (entries.hasMoreElements()) {
10294967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                final ZipEntry entry = entries.nextElement();
10304967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                final String entryName = entry.getName();
10314967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                if (!entry.isDirectory()) {
10324967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                    addEntry(zos, entryName, entry.getTime(), oldZip.getInputStream(entry));
10334967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                } else {
10344967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                    Log.w(TAG, "skipping directory entry: " + entryName);
10354967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme                }
10364967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            }
10374967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10384967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            // Then add the user-provided info.
10394967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            addEntry(zos, "title.txt", info.title);
10404967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            addEntry(zos, "description.txt", info.description);
10414967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        } catch (IOException e) {
104218b5892950b7f21e66c9268129323cbc0e865699Felipe Leme            info.addingDetailsToZip = false;
10434967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            Log.e(TAG, "exception zipping file " + tmpZip, e);
10444967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            return;
10454967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        }
10464967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10474967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        if (!tmpZip.renameTo(info.bugreportFile)) {
10484967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            Log.e(TAG, "Could not rename " + tmpZip + " to " + info.bugreportFile);
10494967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        }
105018b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        info.addedDetailsToZip = true;
105118b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        info.addingDetailsToZip = false;
10524967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    }
10534967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10544967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    private static void addEntry(ZipOutputStream zos, String entry, String text)
10554967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            throws IOException {
10564967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        if (DEBUG) Log.v(TAG, "adding entry '" + entry + "': " + text);
10574967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        if (!TextUtils.isEmpty(text)) {
10584967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            addEntry(zos, entry, new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)));
10594967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        }
10604967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    }
10614967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10624967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    private static void addEntry(ZipOutputStream zos, String entryName, InputStream is)
10634967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            throws IOException {
10644967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        addEntry(zos, entryName, System.currentTimeMillis(), is);
10654967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    }
10664967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10674967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    private static void addEntry(ZipOutputStream zos, String entryName, long timestamp,
10684967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme            InputStream is) throws IOException {
10694967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final ZipEntry entry = new ZipEntry(entryName);
10704967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        entry.setTime(timestamp);
10714967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        zos.putNextEntry(entry);
10724967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        final int totalBytes = Streams.copy(is, zos);
10734967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        if (DEBUG) Log.v(TAG, "size of '" + entryName + "' entry: " + totalBytes + " bytes");
10744967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme        zos.closeEntry();
10754967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    }
10764967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme
10774967f737d9906e3d5c9bf3a0584a7b7cf83b5a8cFelipe Leme    /**
1078b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     * Find the best matching {@link Account} based on build properties.
1079b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme     */
1080b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    private static Account findSendToAccount(Context context) {
1081b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final AccountManager am = (AccountManager) context.getSystemService(
1082b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                Context.ACCOUNT_SERVICE);
1083b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
1084b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        String preferredDomain = SystemProperties.get("sendbug.preferred.domain");
1085b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        if (!preferredDomain.startsWith("@")) {
1086b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            preferredDomain = "@" + preferredDomain;
1087b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
1088b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
1089b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final Account[] accounts = am.getAccounts();
1090b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        Account foundAccount = null;
1091b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        for (Account account : accounts) {
1092b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
1093b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                if (!preferredDomain.isEmpty()) {
1094b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    // if we have a preferred domain and it matches, return; otherwise keep
1095b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    // looking
1096b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    if (account.name.endsWith(preferredDomain)) {
1097b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                        return account;
1098b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    } else {
1099b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                        foundAccount = account;
1100b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    }
1101b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    // if we don't have a preferred domain, just return since it looks like
1102b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    // an email address
1103b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                } else {
1104b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                    return account;
1105b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme                }
1106b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            }
1107b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
1108b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return foundAccount;
1109b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
1110b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
1111226940ed8550c02875a987f7e46699e6003ec1c0Michal Karpinski    static Uri getUri(Context context, File file) {
1112b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        return file != null ? FileProvider.getUriForFile(context, AUTHORITY, file) : null;
1113b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
1114b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme
1115b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    static File getFileExtra(Intent intent, String key) {
1116b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        final String path = intent.getStringExtra(key);
1117b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        if (path != null) {
1118b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            return new File(path);
1119b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        } else {
1120b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme            return null;
1121b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme        }
1122b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme    }
112369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
1124bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private static boolean setSystemProperty(String key, String value) {
1125bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        try {
112685ae3cf46ad66d71e5a29a93e89a0f569d74288bFelipe Leme            if (DEBUG) Log.v(TAG, "Setting system property " + key + " to " + value);
1127bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            SystemProperties.set(key, value);
1128bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        } catch (IllegalArgumentException e) {
1129bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            Log.e(TAG, "Could not set property " + key + " to " + value, e);
1130bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            return false;
1131bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
1132bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        return true;
1133bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
1134bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1135bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
1136bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Updates the system property used by {@code dumpstate} to rename the final bugreport files.
1137bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
1138bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private boolean setBugreportNameProperty(int pid, String name) {
1139bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        Log.d(TAG, "Updating bugreport name to " + name);
1140bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        final String key = DUMPSTATE_PREFIX + pid + NAME_SUFFIX;
1141bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        return setSystemProperty(key, name);
1142bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
1143bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1144bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
1145bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Updates the user-provided details of a bugreport.
1146bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
1147fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme    private void updateBugreportInfo(int id, String name, String title, String description) {
1148fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final BugreportInfo info = getInfo(id);
1149d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        if (info == null) {
1150d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return;
1151d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
11526605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        if (title != null && !title.equals(info.title)) {
11536605bd89c53494b59717a826f9a17641bc32da41Felipe Leme            MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_TITLE_CHANGED);
11546605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        }
1155d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        info.title = title;
11566605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        if (description != null && !description.equals(info.description)) {
11576605bd89c53494b59717a826f9a17641bc32da41Felipe Leme            MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_DESCRIPTION_CHANGED);
11586605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        }
1159d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        info.description = description;
11601eee1996f5d57b42d4beed2790321480fbe03a4cFelipe Leme        if (name != null && !name.equals(info.name)) {
11616605bd89c53494b59717a826f9a17641bc32da41Felipe Leme            MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_NAME_CHANGED);
1162d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            info.name = name;
1163d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            updateProgress(info);
1164d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
1165d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
1166d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1167d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private void collapseNotificationBar() {
1168d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
1169d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
1170d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1171d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static Looper newLooper(String name) {
1172d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final HandlerThread thread = new HandlerThread(name, THREAD_PRIORITY_BACKGROUND);
1173d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        thread.start();
1174d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        return thread.getLooper();
1175d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    }
1176d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1177d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    /**
1178d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     * Takes a screenshot and save it to the given location.
1179d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme     */
1180d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme    private static boolean takeScreenshot(Context context, String screenshotFile) {
1181d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE))
1182d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                .vibrate(150);
1183d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        final ProcessBuilder screencap = new ProcessBuilder()
1184d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                .command("/system/bin/screencap", "-p", screenshotFile);
1185d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        Log.d(TAG, "Taking screenshot using " + screencap.command());
1186d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        try {
1187d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final int exitValue = screencap.start().waitFor();
1188d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (exitValue == 0) {
1189d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                return true;
1190bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1191d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.e(TAG, "screencap (" + screencap.command() + ") failed: " + exitValue);
1192d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } catch (IOException e) {
1193d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.e(TAG, "screencap (" + screencap.command() + ") failed", e);
1194d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        } catch (InterruptedException e) {
1195d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Log.w(TAG, "Thread interrupted while screencap still running");
1196d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            Thread.currentThread().interrupt();
1197bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
1198d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        return false;
1199bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
1200bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1201bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
1202bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Checks whether a character is valid on bugreport names.
1203bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
1204bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    @VisibleForTesting
1205bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    static boolean isValid(char c) {
1206bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
1207bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                || c == '_' || c == '-';
1208bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
1209bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1210bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    /**
1211bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * Helper class encapsulating the UI elements and logic used to display a dialog where user
1212bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     * can change the details of a bugreport.
1213bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme     */
1214bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    private final class BugreportInfoDialog {
1215bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private EditText mInfoName;
1216bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private EditText mInfoTitle;
1217bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private EditText mInfoDescription;
1218bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private AlertDialog mDialog;
1219bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private Button mOkButton;
1220fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        private int mId;
1221bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private int mPid;
1222bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1223bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
1224bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Last "committed" value of the bugreport name.
1225bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * <p>
1226bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Once initially set, it's only updated when user clicks the OK button.
1227bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1228bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private String mSavedName;
1229bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1230bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
1231bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Last value of the bugreport name as entered by the user.
1232bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * <p>
1233bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Every time it's changed the equivalent system property is changed as well, but if the
1234bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * user clicks CANCEL, the old value (stored on {@code mSavedName} is restored.
1235bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * <p>
1236bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * This logic handles the corner-case scenario where {@code dumpstate} finishes after the
1237bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * user changed the name but didn't clicked OK yet (for example, because the user is typing
1238bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * the description). The only drawback is that if the user changes the name while
1239bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * {@code dumpstate} is running but clicks CANCEL after it finishes, then the final name
1240bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * will be the one that has been canceled. But when {@code dumpstate} finishes the {code
1241bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * name} UI is disabled and the old name restored anyways, so the user will be "alerted" of
1242bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * such drawback.
1243bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1244bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        private String mTempName;
1245bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1246bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
1247bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Sets its internal state and displays the dialog.
1248bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
12496605bd89c53494b59717a826f9a17641bc32da41Felipe Leme        private void initialize(final Context context, BugreportInfo info) {
1250262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme            final String dialogTitle =
1251262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                    context.getString(R.string.bugreport_info_dialog_title, info.id);
1252bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // First initializes singleton.
1253bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            if (mDialog == null) {
1254bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                @SuppressLint("InflateParams")
1255bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                // It's ok pass null ViewRoot on AlertDialogs.
1256bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                final View view = View.inflate(context, R.layout.dialog_bugreport_info, null);
1257bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1258bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoName = (EditText) view.findViewById(R.id.name);
1259bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoTitle = (EditText) view.findViewById(R.id.title);
1260bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoDescription = (EditText) view.findViewById(R.id.description);
1261bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1262bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoName.setOnFocusChangeListener(new OnFocusChangeListener() {
1263bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1264bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    @Override
1265bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    public void onFocusChange(View v, boolean hasFocus) {
1266bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        if (hasFocus) {
1267bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                            return;
1268bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        }
1269bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        sanitizeName();
1270bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    }
1271bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                });
1272bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1273bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mDialog = new AlertDialog.Builder(context)
1274bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        .setView(view)
1275262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                        .setTitle(dialogTitle)
1276bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        .setCancelable(false)
1277bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        .setPositiveButton(context.getString(com.android.internal.R.string.ok),
1278bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                null)
1279bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        .setNegativeButton(context.getString(com.android.internal.R.string.cancel),
1280bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                new DialogInterface.OnClickListener()
1281bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                {
1282bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                    @Override
1283bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                    public void onClick(DialogInterface dialog, int id)
1284bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                    {
12856605bd89c53494b59717a826f9a17641bc32da41Felipe Leme                                        MetricsLogger.action(context,
12866605bd89c53494b59717a826f9a17641bc32da41Felipe Leme                                                MetricsEvent.ACTION_BUGREPORT_DETAILS_CANCELED);
1287bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                        if (!mTempName.equals(mSavedName)) {
1288bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                            // Must restore dumpstate's name since it was changed
1289bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                            // before user clicked OK.
1290bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                            setBugreportNameProperty(mPid, mSavedName);
1291bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                        }
1292bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                    }
1293bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                })
1294bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        .create();
1295bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1296bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mDialog.getWindow().setAttributes(
1297bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        new WindowManager.LayoutParams(
1298bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                                WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG));
1299bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1300262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme            } else {
1301262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                // Re-use view, but reset fields first.
1302262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                mDialog.setTitle(dialogTitle);
1303262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                mInfoName.setText(null);
1304262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                mInfoTitle.setText(null);
1305262887834c7938b7a7bbbd7ef026b5965d6412fcFelipe Leme                mInfoDescription.setText(null);
1306bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1307bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1308bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // Then set fields.
1309fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mSavedName = mTempName = info.name;
1310fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mId = info.id;
1311fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            mPid = info.pid;
1312fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            if (!TextUtils.isEmpty(info.name)) {
1313fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                mInfoName.setText(info.name);
1314bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1315fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            if (!TextUtils.isEmpty(info.title)) {
1316fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                mInfoTitle.setText(info.title);
1317bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1318fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            if (!TextUtils.isEmpty(info.description)) {
1319fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                mInfoDescription.setText(info.description);
1320bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1321bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1322bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // And finally display it.
1323bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            mDialog.show();
1324bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1325bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // TODO: in a traditional AlertDialog, when the positive button is clicked the
1326bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // dialog is always closed, but we need to validate the name first, so we need to
1327bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // get a reference to it, which is only available after it's displayed.
1328bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // It would be cleaner to use a regular dialog instead, but let's keep this
1329bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // workaround for now and change it later, when we add another button to take
1330bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // extra screenshots.
1331bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            if (mOkButton == null) {
1332bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mOkButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
1333bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mOkButton.setOnClickListener(new View.OnClickListener() {
1334bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1335bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    @Override
1336bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    public void onClick(View view) {
13376605bd89c53494b59717a826f9a17641bc32da41Felipe Leme                        MetricsLogger.action(context, MetricsEvent.ACTION_BUGREPORT_DETAILS_SAVED);
1338bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        sanitizeName();
1339bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        final String name = mInfoName.getText().toString();
1340bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        final String title = mInfoTitle.getText().toString();
1341bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        final String description = mInfoDescription.getText().toString();
1342bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1343fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme                        updateBugreportInfo(mId, name, title, description);
1344bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                        mDialog.dismiss();
1345bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    }
1346bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                });
1347bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1348bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
1349bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1350bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
1351bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Sanitizes the user-provided value for the {@code name} field, automatically replacing
1352bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * invalid characters if necessary.
1353bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1354d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        private void sanitizeName() {
1355bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            String name = mInfoName.getText().toString();
1356bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            if (name.equals(mTempName)) {
1357bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                if (DEBUG) Log.v(TAG, "name didn't change, no need to sanitize: " + name);
1358bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                return;
1359bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1360bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            final StringBuilder safeName = new StringBuilder(name.length());
1361bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            boolean changed = false;
1362bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            for (int i = 0; i < name.length(); i++) {
1363bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                final char c = name.charAt(i);
1364bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                if (isValid(c)) {
1365bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    safeName.append(c);
1366bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                } else {
1367bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    changed = true;
1368bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    safeName.append('_');
1369bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                }
1370bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1371bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            if (changed) {
1372bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                Log.v(TAG, "changed invalid name '" + name + "' to '" + safeName + "'");
1373bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                name = safeName.toString();
1374bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoName.setText(name);
1375bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1376bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            mTempName = name;
1377bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1378bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // Must update system property for the cases where dumpstate finishes
1379bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // while the user is still entering other fields (like title or
1380bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            // description)
138185ae3cf46ad66d71e5a29a93e89a0f569d74288bFelipe Leme            setBugreportNameProperty(mPid, name);
1382bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
1383bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1384bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme       /**
1385bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * Notifies the dialog that the bugreport has finished so it disables the {@code name}
1386bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * field.
1387bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * <p>Once the bugreport is finished dumpstate has already generated the final files, so
1388bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * changing the name would have no effect.
1389bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1390fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        private void onBugreportFinished(int id) {
1391bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            if (mInfoName != null) {
1392bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoName.setEnabled(false);
1393bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                mInfoName.setText(mSavedName);
1394bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme            }
1395bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        }
1396bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1397bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme    }
1398bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
139969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    /**
140046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme     * Information about a bugreport process while its in progress.
140169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme     */
1402c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme    private static final class BugreportInfo implements Parcelable {
1403719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme        private final Context context;
1404719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme
140569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        /**
1406fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme         * Sequential, user-friendly id used to identify the bugreport.
1407fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme         */
1408fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        final int id;
1409fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme
1410fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        /**
141146d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * {@code pid} of the {@code dumpstate} process generating the bugreport.
141269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         */
141369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        final int pid;
141469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
141569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        /**
141646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Name of the bugreport, will be used to rename the final files.
141769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         * <p>
141846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Initial value is the bugreport filename reported by {@code dumpstate}, but user can
141969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         * change it later to a more meaningful name.
142069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         */
1421719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme        String name;
142269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
142369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        /**
1424bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * User-provided, one-line summary of the bug; when set, will be used as the subject
1425bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * of the {@link Intent#ACTION_SEND_MULTIPLE} intent.
1426bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1427bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        String title;
1428bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1429bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
1430bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * User-provided, detailed description of the bugreport; when set, will be added to the body
1431bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         * of the {@link Intent#ACTION_SEND_MULTIPLE} intent.
1432bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme         */
1433bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        String description;
1434bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme
1435bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme        /**
143646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Maximum progress of the bugreport generation.
143769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         */
1438719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme        int max;
143969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
144069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        /**
144146d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Current progress of the bugreport generation.
144269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         */
144369c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        int progress;
144469c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
144569c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        /**
144669c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         * Time of the last progress update.
144769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme         */
144869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        long lastUpdate = System.currentTimeMillis();
144969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
145046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        /**
1451c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme         * Time of the last progress update when Parcel was created.
1452c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme         */
1453c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        String formattedLastUpdate;
1454c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1455c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        /**
145646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Path of the main bugreport file.
145746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         */
145846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        File bugreportFile;
145946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
146046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        /**
1461d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         * Path of the screenshot files.
146246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         */
1463d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        List<File> screenshotFiles = new ArrayList<>(1);
146446d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
146546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        /**
146646d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Whether dumpstate sent an intent informing it has finished.
146746d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         */
146846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        boolean finished;
146946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
147046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        /**
147118b5892950b7f21e66c9268129323cbc0e865699Felipe Leme         * Whether the details entries have been added to the bugreport yet.
147218b5892950b7f21e66c9268129323cbc0e865699Felipe Leme         */
147318b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        boolean addingDetailsToZip;
147418b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        boolean addedDetailsToZip;
147518b5892950b7f21e66c9268129323cbc0e865699Felipe Leme
147618b5892950b7f21e66c9268129323cbc0e865699Felipe Leme        /**
1477d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         * Internal counter used to name screenshot files.
1478d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         */
1479d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        int screenshotCounter;
1480d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1481d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        /**
148246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Constructor for tracked bugreports - typically called upon receiving BUGREPORT_STARTED.
148346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         */
1484fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        BugreportInfo(Context context, int id, int pid, String name, int max) {
1485719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme            this.context = context;
1486fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            this.id = id;
148769c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            this.pid = pid;
148869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            this.name = name;
148969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            this.max = max;
149069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
149169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
149246d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        /**
149346d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * Constructor for untracked bugreports - typically called upon receiving BUGREPORT_FINISHED
149446d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         * without a previous call to BUGREPORT_STARTED.
149546d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme         */
1496fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme        BugreportInfo(Context context, int id) {
1497fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            this(context, id, id, null, 0);
149846d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme            this.finished = true;
149946d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme        }
150046d47911ea8bbbe0b8d7a6029b80da6b1eb94393Felipe Leme
1501d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        /**
1502d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         * Gets the name for next screenshot file.
1503d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         */
1504d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        String getPathNextScreenshot() {
1505d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            screenshotCounter ++;
1506d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            return "screenshot-" + pid + "-" + screenshotCounter + ".png";
1507d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
1508d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1509d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        /**
1510d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         * Saves the location of a taken screenshot so it can be sent out at the end.
1511d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         */
1512d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        void addScreenshot(File screenshot) {
1513d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            screenshotFiles.add(screenshot);
1514d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
1515d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
1516d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        /**
1517d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         * Rename all screenshots files so that they contain the user-generated name instead of pid.
1518d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme         */
1519d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        void renameScreenshots(File screenshotDir) {
1520d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            if (TextUtils.isEmpty(name)) {
1521d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                return;
1522d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
1523d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            final List<File> renamedFiles = new ArrayList<>(screenshotFiles.size());
1524d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            for (File oldFile : screenshotFiles) {
1525d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                final String oldName = oldFile.getName();
152685ae3cf46ad66d71e5a29a93e89a0f569d74288bFelipe Leme                final String newName = oldName.replaceFirst(Integer.toString(pid), name);
1527d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                final File newFile;
1528d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                if (!newName.equals(oldName)) {
1529d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    final File renamedFile = new File(screenshotDir, newName);
1530d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    newFile = oldFile.renameTo(renamedFile) ? renamedFile : oldFile;
1531d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                } else {
1532d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    Log.w(TAG, "Name didn't change: " + oldName); // Shouldn't happen.
1533d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    newFile = oldFile;
1534d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                }
1535d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                renamedFiles.add(newFile);
1536d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            }
1537d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme            screenshotFiles = renamedFiles;
1538d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme        }
1539d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme
154069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        String getFormattedLastUpdate() {
1541c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            if (context == null) {
1542c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                // Restored from Parcel
1543c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                return formattedLastUpdate == null ?
1544c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                        Long.toString(lastUpdate) : formattedLastUpdate;
1545c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
1546719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme            return DateUtils.formatDateTime(context, lastUpdate,
1547719aaae3c167c2b15525dbe5c7db514a2c0c8269Felipe Leme                    DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);
154869c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
154969c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme
155069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        @Override
155169c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        public String toString() {
155269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme            final float percent = ((float) progress * 100 / max);
1553fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            return "id: " + id + ", pid: " + pid + ", name: " + name + ", finished: " + finished
1554bc73ffc06fd2b5b30802cc7e8874a986626b897dFelipe Leme                    + "\n\ttitle: " + title + "\n\tdescription: " + description
1555d1e0f12979441733753b538611f6d73e5527c43cFelipe Leme                    + "\n\tfile: " + bugreportFile + "\n\tscreenshots: " + screenshotFiles
1556510e922e47fec69839dd48c5473540f93d79a508Felipe Leme                    + "\n\tprogress: " + progress + "/" + max + " (" + percent + ")"
155718b5892950b7f21e66c9268129323cbc0e865699Felipe Leme                    + "\n\tlast_update: " + getFormattedLastUpdate()
155818b5892950b7f21e66c9268129323cbc0e865699Felipe Leme                    + "\naddingDetailsToZip: " + addingDetailsToZip
155918b5892950b7f21e66c9268129323cbc0e865699Felipe Leme                    + " addedDetailsToZip: " + addedDetailsToZip;
156069c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme        }
1561c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1562c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        // Parcelable contract
1563c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        protected BugreportInfo(Parcel in) {
1564c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            context = null;
1565fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            id = in.readInt();
1566c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            pid = in.readInt();
1567c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            name = in.readString();
1568c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            title = in.readString();
1569c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            description = in.readString();
1570c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            max = in.readInt();
1571c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            progress = in.readInt();
1572c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            lastUpdate = in.readLong();
1573c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            formattedLastUpdate = in.readString();
1574c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            bugreportFile = readFile(in);
1575c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1576c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            int screenshotSize = in.readInt();
1577c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            for (int i = 1; i <= screenshotSize; i++) {
1578c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                  screenshotFiles.add(readFile(in));
1579c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
1580c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1581c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            finished = in.readInt() == 1;
1582c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            screenshotCounter = in.readInt();
1583c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1584c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1585c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        @Override
1586c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        public void writeToParcel(Parcel dest, int flags) {
1587fd8ea077c0ded14002c32ee346df4fa22a30625aFelipe Leme            dest.writeInt(id);
1588c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(pid);
1589c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeString(name);
1590c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeString(title);
1591c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeString(description);
1592c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(max);
1593c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(progress);
1594c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeLong(lastUpdate);
1595c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeString(getFormattedLastUpdate());
1596c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            writeFile(dest, bugreportFile);
1597c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1598c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(screenshotFiles.size());
1599c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            for (File screenshotFile : screenshotFiles) {
1600c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                writeFile(dest, screenshotFile);
1601c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
1602c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1603c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(finished ? 1 : 0);
1604c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeInt(screenshotCounter);
1605c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1606c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1607c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        @Override
1608c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        public int describeContents() {
1609c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            return 0;
1610c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1611c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1612c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        private void writeFile(Parcel dest, File file) {
1613c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            dest.writeString(file == null ? null : file.getPath());
1614c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1615c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1616c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        private File readFile(Parcel in) {
1617c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            final String path = in.readString();
1618c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            return path == null ? null : new File(path);
1619c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        }
1620c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1621c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        public static final Parcelable.Creator<BugreportInfo> CREATOR =
1622c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                new Parcelable.Creator<BugreportInfo>() {
1623c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            public BugreportInfo createFromParcel(Parcel source) {
1624c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                return new BugreportInfo(source);
1625c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
1626c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
1627c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            public BugreportInfo[] newArray(int size) {
1628c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme                return new BugreportInfo[size];
1629c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme            }
1630c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme        };
1631c4f6467702e308844ef0769ba17dcb7b7b32a9e6Felipe Leme
163269c0292affe8be51e10afb2dbf58f0133918a2c3Felipe Leme    }
1633b9238b37838d653c38ce4e712421adb61978fc22Felipe Leme}
1634