Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.shell; |
| 18 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 19 | import static android.os.Process.THREAD_PRIORITY_BACKGROUND; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 20 | import static com.android.shell.BugreportPrefs.STATE_SHOW; |
| 21 | import static com.android.shell.BugreportPrefs.getWarningState; |
| 22 | |
| 23 | import java.io.BufferedOutputStream; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 24 | import java.io.ByteArrayInputStream; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 25 | import java.io.File; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 26 | import java.io.FileDescriptor; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 27 | import java.io.FileInputStream; |
| 28 | import java.io.FileOutputStream; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 31 | import java.io.PrintWriter; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 32 | import java.nio.charset.StandardCharsets; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 33 | import java.text.NumberFormat; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 35 | import java.util.Enumeration; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 36 | import java.util.List; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 37 | import java.util.zip.ZipEntry; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 38 | import java.util.zip.ZipFile; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 39 | import java.util.zip.ZipOutputStream; |
| 40 | |
| 41 | import libcore.io.Streams; |
| 42 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 44 | import com.android.internal.logging.MetricsLogger; |
| 45 | import com.android.internal.logging.MetricsProto.MetricsEvent; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 46 | import com.google.android.collect.Lists; |
| 47 | |
| 48 | import android.accounts.Account; |
| 49 | import android.accounts.AccountManager; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 50 | import android.annotation.SuppressLint; |
| 51 | import android.app.AlertDialog; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 52 | import android.app.Notification; |
Felipe Leme | 9cadb75 | 2015-11-30 09:35:59 -0800 | [diff] [blame] | 53 | import android.app.Notification.Action; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 54 | import android.app.NotificationManager; |
| 55 | import android.app.PendingIntent; |
| 56 | import android.app.Service; |
| 57 | import android.content.ClipData; |
| 58 | import android.content.Context; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 59 | import android.content.DialogInterface; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 60 | import android.content.Intent; |
| 61 | import android.content.res.Configuration; |
| 62 | import android.net.Uri; |
| 63 | import android.os.AsyncTask; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 64 | import android.os.Handler; |
| 65 | import android.os.HandlerThread; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 66 | import android.os.IBinder; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 67 | import android.os.Looper; |
| 68 | import android.os.Message; |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 69 | import android.os.Parcel; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 70 | import android.os.Parcelable; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 71 | import android.os.SystemProperties; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 72 | import android.os.Vibrator; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 73 | import android.support.v4.content.FileProvider; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 74 | import android.text.TextUtils; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 75 | import android.text.format.DateUtils; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 76 | import android.util.Log; |
| 77 | import android.util.Patterns; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 78 | import android.util.SparseArray; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 79 | import android.view.View; |
| 80 | import android.view.WindowManager; |
| 81 | import android.view.View.OnFocusChangeListener; |
| 82 | import android.view.inputmethod.EditorInfo; |
| 83 | import android.widget.Button; |
| 84 | import android.widget.EditText; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 85 | import android.widget.Toast; |
| 86 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 87 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 88 | * Service used to keep progress of bugreport processes ({@code dumpstate}). |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 89 | * <p> |
| 90 | * The workflow is: |
| 91 | * <ol> |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 92 | * <li>When {@code dumpstate} starts, it sends a {@code BUGREPORT_STARTED} with a sequential id, |
| 93 | * its pid, and the estimated total effort. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 94 | * <li>{@link BugreportReceiver} receives the intent and delegates it to this service. |
| 95 | * <li>Upon start, this service: |
| 96 | * <ol> |
| 97 | * <li>Issues a system notification so user can watch the progresss (which is 0% initially). |
| 98 | * <li>Polls the {@link SystemProperties} for updates on the {@code dumpstate} progress. |
| 99 | * <li>If the progress changed, it updates the system notification. |
| 100 | * </ol> |
| 101 | * <li>As {@code dumpstate} progresses, it updates the system property. |
| 102 | * <li>When {@code dumpstate} finishes, it sends a {@code BUGREPORT_FINISHED} intent. |
| 103 | * <li>{@link BugreportReceiver} receives the intent and delegates it to this service, which in |
| 104 | * turn: |
| 105 | * <ol> |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 106 | * <li>Updates the system notification so user can share the bugreport. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 107 | * <li>Stops monitoring that {@code dumpstate} process. |
| 108 | * <li>Stops itself if it doesn't have any process left to monitor. |
| 109 | * </ol> |
| 110 | * </ol> |
| 111 | */ |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 112 | public class BugreportProgressService extends Service { |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 113 | private static final String TAG = "BugreportProgressService"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 114 | private static final boolean DEBUG = false; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 115 | |
| 116 | private static final String AUTHORITY = "com.android.shell"; |
| 117 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 118 | // External intents sent by dumpstate. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 119 | static final String INTENT_BUGREPORT_STARTED = "android.intent.action.BUGREPORT_STARTED"; |
| 120 | static final String INTENT_BUGREPORT_FINISHED = "android.intent.action.BUGREPORT_FINISHED"; |
Michal Karpinski | 226940e | 2015-12-15 18:14:26 +0000 | [diff] [blame] | 121 | static final String INTENT_REMOTE_BUGREPORT_FINISHED = |
| 122 | "android.intent.action.REMOTE_BUGREPORT_FINISHED"; |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 123 | |
| 124 | // Internal intents used on notification actions. |
Felipe Leme | 9cadb75 | 2015-11-30 09:35:59 -0800 | [diff] [blame] | 125 | static final String INTENT_BUGREPORT_CANCEL = "android.intent.action.BUGREPORT_CANCEL"; |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 126 | static final String INTENT_BUGREPORT_SHARE = "android.intent.action.BUGREPORT_SHARE"; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 127 | static final String INTENT_BUGREPORT_INFO_LAUNCH = |
| 128 | "android.intent.action.BUGREPORT_INFO_LAUNCH"; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 129 | static final String INTENT_BUGREPORT_SCREENSHOT = |
| 130 | "android.intent.action.BUGREPORT_SCREENSHOT"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 131 | |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 132 | static final String EXTRA_BUGREPORT = "android.intent.extra.BUGREPORT"; |
| 133 | static final String EXTRA_SCREENSHOT = "android.intent.extra.SCREENSHOT"; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 134 | static final String EXTRA_ID = "android.intent.extra.ID"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 135 | static final String EXTRA_PID = "android.intent.extra.PID"; |
| 136 | static final String EXTRA_MAX = "android.intent.extra.MAX"; |
| 137 | static final String EXTRA_NAME = "android.intent.extra.NAME"; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 138 | static final String EXTRA_TITLE = "android.intent.extra.TITLE"; |
| 139 | static final String EXTRA_DESCRIPTION = "android.intent.extra.DESCRIPTION"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 140 | static final String EXTRA_ORIGINAL_INTENT = "android.intent.extra.ORIGINAL_INTENT"; |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 141 | static final String EXTRA_INFO = "android.intent.extra.INFO"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 142 | |
| 143 | private static final int MSG_SERVICE_COMMAND = 1; |
| 144 | private static final int MSG_POLL = 2; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 145 | private static final int MSG_DELAYED_SCREENSHOT = 3; |
| 146 | private static final int MSG_SCREENSHOT_REQUEST = 4; |
| 147 | private static final int MSG_SCREENSHOT_RESPONSE = 5; |
| 148 | |
Felipe Leme | 8648a15 | 2016-02-25 16:22:38 -0800 | [diff] [blame] | 149 | // Passed to Message.obtain() when msg.arg2 is not used. |
| 150 | private static final int UNUSED_ARG2 = -2; |
| 151 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 152 | /** |
| 153 | * Delay before a screenshot is taken. |
| 154 | * <p> |
| 155 | * Should be at least 3 seconds, otherwise its toast might show up in the screenshot. |
| 156 | */ |
| 157 | static final int SCREENSHOT_DELAY_SECONDS = 3; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 158 | |
| 159 | /** Polling frequency, in milliseconds. */ |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 160 | static final long POLLING_FREQUENCY = 2 * DateUtils.SECOND_IN_MILLIS; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 161 | |
| 162 | /** How long (in ms) a dumpstate process will be monitored if it didn't show progress. */ |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 163 | private static final long INACTIVITY_TIMEOUT = 10 * DateUtils.MINUTE_IN_MILLIS; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 164 | |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 165 | /** System properties used for monitoring progress. */ |
| 166 | private static final String DUMPSTATE_PREFIX = "dumpstate."; |
| 167 | private static final String PROGRESS_SUFFIX = ".progress"; |
| 168 | private static final String MAX_SUFFIX = ".max"; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 169 | private static final String NAME_SUFFIX = ".name"; |
Felipe Leme | 9cadb75 | 2015-11-30 09:35:59 -0800 | [diff] [blame] | 170 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 171 | /** System property (and value) used to stop dumpstate. */ |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 172 | // TODO: should call ActiveManager API instead |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 173 | private static final String CTL_STOP = "ctl.stop"; |
Felipe Leme | 4cc8633 | 2015-12-04 16:37:28 -0800 | [diff] [blame] | 174 | private static final String BUGREPORT_SERVICE = "bugreportplus"; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 175 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 176 | /** |
| 177 | * Directory on Shell's data storage where screenshots will be stored. |
| 178 | * <p> |
| 179 | * Must be a path supported by its FileProvider. |
| 180 | */ |
| 181 | private static final String SCREENSHOT_DIR = "bugreports"; |
| 182 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 183 | /** Managed dumpstate processes (keyed by id) */ |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 184 | private final SparseArray<BugreportInfo> mProcesses = new SparseArray<>(); |
| 185 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 186 | private Context mContext; |
| 187 | private ServiceHandler mMainHandler; |
| 188 | private ScreenshotHandler mScreenshotHandler; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 189 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 190 | private final BugreportInfoDialog mInfoDialog = new BugreportInfoDialog(); |
| 191 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 192 | private File mScreenshotsDir; |
| 193 | |
| 194 | /** |
| 195 | * Flag indicating whether a screenshot is being taken. |
| 196 | * <p> |
| 197 | * This is the only state that is shared between the 2 handlers and hence must have synchronized |
| 198 | * access. |
| 199 | */ |
| 200 | private boolean mTakingScreenshot; |
| 201 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 202 | @Override |
| 203 | public void onCreate() { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 204 | mContext = getApplicationContext(); |
| 205 | mMainHandler = new ServiceHandler("BugreportProgressServiceMainThread"); |
| 206 | mScreenshotHandler = new ScreenshotHandler("BugreportProgressServiceScreenshotThread"); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 207 | |
Felipe Leme | 4f663f6 | 2016-03-08 11:41:18 -0800 | [diff] [blame] | 208 | mScreenshotsDir = new File(getFilesDir(), SCREENSHOT_DIR); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 209 | if (!mScreenshotsDir.exists()) { |
| 210 | Log.i(TAG, "Creating directory " + mScreenshotsDir + " to store temporary screenshots"); |
| 211 | if (!mScreenshotsDir.mkdir()) { |
| 212 | Log.w(TAG, "Could not create directory " + mScreenshotsDir); |
| 213 | } |
| 214 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 215 | } |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 216 | |
| 217 | @Override |
| 218 | public int onStartCommand(Intent intent, int flags, int startId) { |
| 219 | if (intent != null) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 220 | // Handle it in a separate thread. |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 221 | final Message msg = mMainHandler.obtainMessage(); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 222 | msg.what = MSG_SERVICE_COMMAND; |
| 223 | msg.obj = intent; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 224 | mMainHandler.sendMessage(msg); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 225 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 226 | |
| 227 | // If service is killed it cannot be recreated because it would not know which |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 228 | // dumpstate IDs it would have to watch. |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 229 | return START_NOT_STICKY; |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public IBinder onBind(Intent intent) { |
| 234 | return null; |
| 235 | } |
| 236 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 237 | @Override |
| 238 | public void onDestroy() { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 239 | mMainHandler.getLooper().quit(); |
| 240 | mScreenshotHandler.getLooper().quit(); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 241 | super.onDestroy(); |
| 242 | } |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 243 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 244 | @Override |
| 245 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 246 | final int size = mProcesses.size(); |
| 247 | if (size == 0) { |
| 248 | writer.printf("No monitored processes"); |
| 249 | return; |
| 250 | } |
| 251 | writer.printf("Monitored dumpstate processes\n"); |
| 252 | writer.printf("-----------------------------\n"); |
| 253 | for (int i = 0; i < size; i++) { |
| 254 | writer.printf("%s\n", mProcesses.valueAt(i)); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 255 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 258 | /** |
| 259 | * Main thread used to handle all requests but taking screenshots. |
| 260 | */ |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 261 | private final class ServiceHandler extends Handler { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 262 | public ServiceHandler(String name) { |
| 263 | super(newLooper(name)); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void handleMessage(Message msg) { |
| 268 | if (msg.what == MSG_POLL) { |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 269 | poll(); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 270 | return; |
| 271 | } |
| 272 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 273 | if (msg.what == MSG_DELAYED_SCREENSHOT) { |
| 274 | takeScreenshot(msg.arg1, msg.arg2); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | if (msg.what == MSG_SCREENSHOT_RESPONSE) { |
| 279 | handleScreenshotResponse(msg); |
| 280 | return; |
| 281 | } |
| 282 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 283 | if (msg.what != MSG_SERVICE_COMMAND) { |
| 284 | // Sanity check. |
| 285 | Log.e(TAG, "Invalid message type: " + msg.what); |
| 286 | return; |
| 287 | } |
| 288 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 289 | // At this point it's handling onStartCommand(), with the intent passed as an Extra. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 290 | if (!(msg.obj instanceof Intent)) { |
| 291 | // Sanity check. |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 292 | Log.wtf(TAG, "handleMessage(): invalid msg.obj type: " + msg.obj); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | final Parcelable parcel = ((Intent) msg.obj).getParcelableExtra(EXTRA_ORIGINAL_INTENT); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 296 | final Intent intent; |
| 297 | if (parcel instanceof Intent) { |
| 298 | // The real intent was passed to BugreportReceiver, which delegated to the service. |
| 299 | intent = (Intent) parcel; |
| 300 | } else { |
| 301 | intent = (Intent) msg.obj; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 302 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 303 | final String action = intent.getAction(); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 304 | final int pid = intent.getIntExtra(EXTRA_PID, 0); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 305 | final int id = intent.getIntExtra(EXTRA_ID, 0); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 306 | final int max = intent.getIntExtra(EXTRA_MAX, -1); |
| 307 | final String name = intent.getStringExtra(EXTRA_NAME); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 308 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 309 | if (DEBUG) |
| 310 | Log.v(TAG, "action: " + action + ", name: " + name + ", id: " + id + ", pid: " |
| 311 | + pid + ", max: " + max); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 312 | switch (action) { |
| 313 | case INTENT_BUGREPORT_STARTED: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 314 | if (!startProgress(name, id, pid, max)) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 315 | stopSelfWhenDone(); |
| 316 | return; |
| 317 | } |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 318 | poll(); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 319 | break; |
| 320 | case INTENT_BUGREPORT_FINISHED: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 321 | if (id == 0) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 322 | // Shouldn't happen, unless BUGREPORT_FINISHED is received from a legacy, |
| 323 | // out-of-sync dumpstate process. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 324 | Log.w(TAG, "Missing " + EXTRA_ID + " on intent " + intent); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 325 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 326 | onBugreportFinished(id, intent); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 327 | break; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 328 | case INTENT_BUGREPORT_INFO_LAUNCH: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 329 | launchBugreportInfoDialog(id); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 330 | break; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 331 | case INTENT_BUGREPORT_SCREENSHOT: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 332 | takeScreenshot(id, true); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 333 | break; |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 334 | case INTENT_BUGREPORT_SHARE: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 335 | shareBugreport(id, (BugreportInfo) intent.getParcelableExtra(EXTRA_INFO)); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 336 | break; |
Felipe Leme | 9cadb75 | 2015-11-30 09:35:59 -0800 | [diff] [blame] | 337 | case INTENT_BUGREPORT_CANCEL: |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 338 | cancel(id); |
Felipe Leme | 9cadb75 | 2015-11-30 09:35:59 -0800 | [diff] [blame] | 339 | break; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 340 | default: |
| 341 | Log.w(TAG, "Unsupported intent: " + action); |
| 342 | } |
| 343 | return; |
| 344 | |
| 345 | } |
| 346 | |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 347 | private void poll() { |
| 348 | if (pollProgress()) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 349 | // Keep polling... |
| 350 | sendEmptyMessageDelayed(MSG_POLL, POLLING_FREQUENCY); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 351 | } else { |
| 352 | Log.i(TAG, "Stopped polling"); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 353 | } |
| 354 | } |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 355 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 356 | |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 357 | /** |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 358 | * Separate thread used only to take screenshots so it doesn't block the main thread. |
| 359 | */ |
| 360 | private final class ScreenshotHandler extends Handler { |
| 361 | public ScreenshotHandler(String name) { |
| 362 | super(newLooper(name)); |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void handleMessage(Message msg) { |
| 367 | if (msg.what != MSG_SCREENSHOT_REQUEST) { |
| 368 | Log.e(TAG, "Invalid message type: " + msg.what); |
| 369 | return; |
| 370 | } |
| 371 | handleScreenshotRequest(msg); |
| 372 | } |
| 373 | } |
| 374 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 375 | private BugreportInfo getInfo(int id) { |
| 376 | final BugreportInfo info = mProcesses.get(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 377 | if (info == null) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 378 | Log.w(TAG, "Not monitoring process with ID " + id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 379 | } |
| 380 | return info; |
| 381 | } |
| 382 | |
| 383 | /** |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 384 | * Creates the {@link BugreportInfo} for a process and issue a system notification to |
| 385 | * indicate its progress. |
| 386 | * |
| 387 | * @return whether it succeeded or not. |
| 388 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 389 | private boolean startProgress(String name, int id, int pid, int max) { |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 390 | if (name == null) { |
| 391 | Log.w(TAG, "Missing " + EXTRA_NAME + " on start intent"); |
| 392 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 393 | if (id == -1) { |
| 394 | Log.e(TAG, "Missing " + EXTRA_ID + " on start intent"); |
| 395 | return false; |
| 396 | } |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 397 | if (pid == -1) { |
| 398 | Log.e(TAG, "Missing " + EXTRA_PID + " on start intent"); |
| 399 | return false; |
| 400 | } |
| 401 | if (max <= 0) { |
| 402 | Log.e(TAG, "Invalid value for extra " + EXTRA_MAX + ": " + max); |
| 403 | return false; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 406 | final BugreportInfo info = new BugreportInfo(mContext, id, pid, name, max); |
| 407 | if (mProcesses.indexOfKey(id) >= 0) { |
| 408 | Log.w(TAG, "ID " + id + " already watched"); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 409 | } else { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 410 | mProcesses.put(info.id, info); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 411 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 412 | // Take initial screenshot. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 413 | takeScreenshot(id, false); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 414 | updateProgress(info); |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 419 | * Updates the system notification for a given bugreport. |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 420 | */ |
| 421 | private void updateProgress(BugreportInfo info) { |
| 422 | if (info.max <= 0 || info.progress < 0) { |
| 423 | Log.e(TAG, "Invalid progress values for " + info); |
| 424 | return; |
| 425 | } |
| 426 | |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 427 | final NumberFormat nf = NumberFormat.getPercentInstance(); |
| 428 | nf.setMinimumFractionDigits(2); |
| 429 | nf.setMaximumFractionDigits(2); |
| 430 | final String percentText = nf.format((double) info.progress / info.max); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 431 | final Action cancelAction = new Action.Builder(null, mContext.getString( |
| 432 | com.android.internal.R.string.cancel), newCancelIntent(mContext, info)).build(); |
| 433 | final Intent infoIntent = new Intent(mContext, BugreportProgressService.class); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 434 | infoIntent.setAction(INTENT_BUGREPORT_INFO_LAUNCH); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 435 | infoIntent.putExtra(EXTRA_ID, info.id); |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 436 | final PendingIntent infoPendingIntent = |
| 437 | PendingIntent.getService(mContext, info.id, infoIntent, |
| 438 | PendingIntent.FLAG_UPDATE_CURRENT); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 439 | final Action infoAction = new Action.Builder(null, |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 440 | mContext.getString(R.string.bugreport_info_action), |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 441 | infoPendingIntent).build(); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 442 | final Intent screenshotIntent = new Intent(mContext, BugreportProgressService.class); |
| 443 | screenshotIntent.setAction(INTENT_BUGREPORT_SCREENSHOT); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 444 | screenshotIntent.putExtra(EXTRA_ID, info.id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 445 | PendingIntent screenshotPendingIntent = mTakingScreenshot ? null : PendingIntent |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 446 | .getService(mContext, info.id, screenshotIntent, |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 447 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 448 | final Action screenshotAction = new Action.Builder(null, |
| 449 | mContext.getString(R.string.bugreport_screenshot_action), |
| 450 | screenshotPendingIntent).build(); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 451 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 452 | final String title = mContext.getString(R.string.bugreport_in_progress_title, info.id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 453 | |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 454 | final String name = |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 455 | info.name != null ? info.name : mContext.getString(R.string.bugreport_unnamed); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 456 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 457 | final Notification notification = new Notification.Builder(mContext) |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 458 | .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb) |
| 459 | .setContentTitle(title) |
| 460 | .setTicker(title) |
| 461 | .setContentText(name) |
| 462 | .setContentInfo(percentText) |
| 463 | .setProgress(info.max, info.progress, false) |
| 464 | .setOngoing(true) |
| 465 | .setLocalOnly(true) |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 466 | .setColor(mContext.getColor( |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 467 | com.android.internal.R.color.system_notification_accent_color)) |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 468 | .setContentIntent(infoPendingIntent) |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 469 | .addAction(infoAction) |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 470 | .addAction(screenshotAction) |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 471 | .addAction(cancelAction) |
| 472 | .build(); |
| 473 | |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 474 | if (info.finished) { |
| 475 | Log.w(TAG, "Not sending progress notification because bugreport has finished already (" |
| 476 | + info + ")"); |
| 477 | return; |
| 478 | } |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 479 | if (DEBUG) { |
| 480 | Log.d(TAG, "Sending 'Progress' notification for id " + info.id + "(pid " + info.pid |
| 481 | + "): " + percentText); |
| 482 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 483 | NotificationManager.from(mContext).notify(TAG, info.id, notification); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 487 | * Creates a {@link PendingIntent} for a notification action used to cancel a bugreport. |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 488 | */ |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 489 | private static PendingIntent newCancelIntent(Context context, BugreportInfo info) { |
| 490 | final Intent intent = new Intent(INTENT_BUGREPORT_CANCEL); |
| 491 | intent.setClass(context, BugreportProgressService.class); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 492 | intent.putExtra(EXTRA_ID, info.id); |
| 493 | return PendingIntent.getService(context, info.id, intent, |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 494 | PendingIntent.FLAG_UPDATE_CURRENT); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Finalizes the progress on a given bugreport and cancel its notification. |
| 499 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 500 | private void stopProgress(int id) { |
| 501 | if (mProcesses.indexOfKey(id) < 0) { |
| 502 | Log.w(TAG, "ID not watched: " + id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 503 | } else { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 504 | Log.d(TAG, "Removing ID " + id); |
| 505 | mProcesses.remove(id); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 506 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 507 | Log.v(TAG, "stopProgress(" + id + "): cancel notification"); |
| 508 | NotificationManager.from(mContext).cancel(TAG, id); |
Felipe Leme | 0f2daaf | 2016-03-08 12:44:22 -0800 | [diff] [blame] | 509 | stopSelfWhenDone(); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Cancels a bugreport upon user's request. |
| 514 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 515 | private void cancel(int id) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 516 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_CANCEL); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 517 | Log.v(TAG, "cancel: ID=" + id); |
| 518 | final BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 519 | if (info != null && !info.finished) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 520 | Log.i(TAG, "Cancelling bugreport service (ID=" + id + ") on user's request"); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 521 | setSystemProperty(CTL_STOP, BUGREPORT_SERVICE); |
| 522 | deleteScreenshots(info); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 523 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 524 | stopProgress(id); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Poll {@link SystemProperties} to get the progress on each monitored process. |
| 529 | * |
| 530 | * @return whether it should keep polling. |
| 531 | */ |
| 532 | private boolean pollProgress() { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 533 | final int total = mProcesses.size(); |
| 534 | if (total == 0) { |
| 535 | Log.d(TAG, "No process to poll progress."); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 536 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 537 | int activeProcesses = 0; |
| 538 | for (int i = 0; i < total; i++) { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 539 | final BugreportInfo info = mProcesses.valueAt(i); |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 540 | if (info == null) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 541 | Log.wtf(TAG, "pollProgress(): null info at index " + i + "(ID = " |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 542 | + mProcesses.keyAt(i) + ")"); |
| 543 | continue; |
| 544 | } |
| 545 | |
| 546 | final int pid = info.pid; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 547 | final int id = info.id; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 548 | if (info.finished) { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 549 | if (DEBUG) Log.v(TAG, "Skipping finished process " + pid + " (id: " + id + ")"); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | activeProcesses++; |
| 553 | final String progressKey = DUMPSTATE_PREFIX + pid + PROGRESS_SUFFIX; |
| 554 | final int progress = SystemProperties.getInt(progressKey, 0); |
| 555 | if (progress == 0) { |
| 556 | Log.v(TAG, "System property " + progressKey + " is not set yet"); |
| 557 | } |
| 558 | final int max = SystemProperties.getInt(DUMPSTATE_PREFIX + pid + MAX_SUFFIX, 0); |
| 559 | final boolean maxChanged = max > 0 && max != info.max; |
| 560 | final boolean progressChanged = progress > 0 && progress != info.progress; |
| 561 | |
| 562 | if (progressChanged || maxChanged) { |
| 563 | if (progressChanged) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 564 | if (DEBUG) Log.v(TAG, "Updating progress for PID " + pid + "(id: " + id |
| 565 | + ") from " + info.progress + " to " + progress); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 566 | info.progress = progress; |
| 567 | } |
| 568 | if (maxChanged) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 569 | Log.i(TAG, "Updating max progress for PID " + pid + "(id: " + id |
| 570 | + ") from " + info.max + " to " + max); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 571 | info.max = max; |
| 572 | } |
| 573 | info.lastUpdate = System.currentTimeMillis(); |
| 574 | updateProgress(info); |
| 575 | } else { |
| 576 | long inactiveTime = System.currentTimeMillis() - info.lastUpdate; |
| 577 | if (inactiveTime >= INACTIVITY_TIMEOUT) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 578 | Log.w(TAG, "No progress update for PID " + pid + " since " |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 579 | + info.getFormattedLastUpdate()); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 580 | stopProgress(info.id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
| 584 | if (DEBUG) Log.v(TAG, "pollProgress() total=" + total + ", actives=" + activeProcesses); |
| 585 | return activeProcesses > 0; |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | /** |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 589 | * Fetches a {@link BugreportInfo} for a given process and launches a dialog where the user can |
| 590 | * change its values. |
| 591 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 592 | private void launchBugreportInfoDialog(int id) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 593 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_DETAILS); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 594 | // Copy values so it doesn't lock mProcesses while UI is being updated |
| 595 | final String name, title, description; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 596 | final BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 597 | if (info == null) { |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 598 | // Most likely am killed Shell before user tapped the notification. Since system might |
| 599 | // be too busy anwyays, it's better to ignore the notification and switch back to the |
| 600 | // non-interactive mode (where the bugerport will be shared upon completion). |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 601 | Log.w(TAG, "launchBugreportInfoDialog(): canceling notification because id " + id |
| 602 | + " was not found"); |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 603 | // TODO: add test case to make sure notification is canceled. |
| 604 | NotificationManager.from(mContext).cancel(TAG, id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 605 | return; |
| 606 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 607 | |
| 608 | collapseNotificationBar(); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 609 | mInfoDialog.initialize(mContext, info); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
| 613 | * Starting point for taking a screenshot. |
| 614 | * <p> |
| 615 | * If {@code delayed} is set, it first display a toast message and waits |
| 616 | * {@link #SCREENSHOT_DELAY_SECONDS} seconds before taking it, otherwise it takes the screenshot |
| 617 | * right away. |
| 618 | * <p> |
| 619 | * Typical usage is delaying when taken from the notification action, and taking it right away |
| 620 | * upon receiving a {@link #INTENT_BUGREPORT_STARTED}. |
| 621 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 622 | private void takeScreenshot(int id, boolean delayed) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 623 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_SCREENSHOT); |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 624 | if (getInfo(id) == null) { |
| 625 | // Most likely am killed Shell before user tapped the notification. Since system might |
| 626 | // be too busy anwyays, it's better to ignore the notification and switch back to the |
| 627 | // non-interactive mode (where the bugerport will be shared upon completion). |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 628 | Log.w(TAG, "takeScreenshot(): canceling notification because id " + id |
| 629 | + " was not found"); |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 630 | // TODO: add test case to make sure notification is canceled. |
| 631 | NotificationManager.from(mContext).cancel(TAG, id); |
| 632 | return; |
| 633 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 634 | setTakingScreenshot(true); |
| 635 | if (delayed) { |
| 636 | collapseNotificationBar(); |
| 637 | final String msg = mContext.getResources() |
| 638 | .getQuantityString(com.android.internal.R.plurals.bugreport_countdown, |
| 639 | SCREENSHOT_DELAY_SECONDS, SCREENSHOT_DELAY_SECONDS); |
| 640 | Log.i(TAG, msg); |
| 641 | // Show a toast just once, otherwise it might be captured in the screenshot. |
| 642 | Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); |
| 643 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 644 | takeScreenshot(id, SCREENSHOT_DELAY_SECONDS); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 645 | } else { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 646 | takeScreenshot(id, 0); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * Takes a screenshot after {@code delay} seconds. |
| 652 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 653 | private void takeScreenshot(int id, int delay) { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 654 | if (delay > 0) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 655 | Log.d(TAG, "Taking screenshot for " + id + " in " + delay + " seconds"); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 656 | final Message msg = mMainHandler.obtainMessage(); |
| 657 | msg.what = MSG_DELAYED_SCREENSHOT; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 658 | msg.arg1 = id; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 659 | msg.arg2 = delay - 1; |
| 660 | mMainHandler.sendMessageDelayed(msg, DateUtils.SECOND_IN_MILLIS); |
| 661 | return; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 662 | } |
| 663 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 664 | // It's time to take the screenshot: let the proper thread handle it |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 665 | final BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 666 | if (info == null) { |
| 667 | return; |
| 668 | } |
| 669 | final String screenshotPath = |
| 670 | new File(mScreenshotsDir, info.getPathNextScreenshot()).getAbsolutePath(); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 671 | |
Felipe Leme | 8648a15 | 2016-02-25 16:22:38 -0800 | [diff] [blame] | 672 | Message.obtain(mScreenshotHandler, MSG_SCREENSHOT_REQUEST, id, UNUSED_ARG2, screenshotPath) |
| 673 | .sendToTarget(); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /** |
| 677 | * Sets the internal {@code mTakingScreenshot} state and updates all notifications so their |
| 678 | * SCREENSHOT button is enabled or disabled accordingly. |
| 679 | */ |
| 680 | private void setTakingScreenshot(boolean flag) { |
| 681 | synchronized (BugreportProgressService.this) { |
| 682 | mTakingScreenshot = flag; |
| 683 | for (int i = 0; i < mProcesses.size(); i++) { |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 684 | final BugreportInfo info = mProcesses.valueAt(i); |
| 685 | if (info.finished) { |
| 686 | Log.d(TAG, "Not updating progress because share notification was already sent"); |
| 687 | continue; |
| 688 | } |
| 689 | updateProgress(info); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | private void handleScreenshotRequest(Message requestMsg) { |
| 695 | String screenshotFile = (String) requestMsg.obj; |
| 696 | boolean taken = takeScreenshot(mContext, screenshotFile); |
| 697 | setTakingScreenshot(false); |
| 698 | |
Felipe Leme | 8648a15 | 2016-02-25 16:22:38 -0800 | [diff] [blame] | 699 | Message.obtain(mMainHandler, MSG_SCREENSHOT_RESPONSE, requestMsg.arg1, taken ? 1 : 0, |
| 700 | screenshotFile).sendToTarget(); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | private void handleScreenshotResponse(Message resultMsg) { |
| 704 | final boolean taken = resultMsg.arg2 != 0; |
| 705 | final BugreportInfo info = getInfo(resultMsg.arg1); |
| 706 | if (info == null) { |
| 707 | return; |
| 708 | } |
| 709 | final File screenshotFile = new File((String) resultMsg.obj); |
| 710 | |
Felipe Leme | 5d9000a | 2016-02-25 13:10:14 -0800 | [diff] [blame] | 711 | final String msg; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 712 | if (taken) { |
| 713 | info.addScreenshot(screenshotFile); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 714 | if (info.finished) { |
| 715 | Log.d(TAG, "Screenshot finished after bugreport; updating share notification"); |
| 716 | info.renameScreenshots(mScreenshotsDir); |
| 717 | sendBugreportNotification(mContext, info); |
| 718 | } |
Felipe Leme | 5d9000a | 2016-02-25 13:10:14 -0800 | [diff] [blame] | 719 | msg = mContext.getString(R.string.bugreport_screenshot_taken); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 720 | } else { |
| 721 | // TODO: try again using Framework APIs instead of relying on screencap. |
Felipe Leme | 5d9000a | 2016-02-25 13:10:14 -0800 | [diff] [blame] | 722 | msg = mContext.getString(R.string.bugreport_screenshot_failed); |
| 723 | Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show(); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 724 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 725 | Log.d(TAG, msg); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /** |
| 729 | * Deletes all screenshots taken for a given bugreport. |
| 730 | */ |
| 731 | private void deleteScreenshots(BugreportInfo info) { |
| 732 | for (File file : info.screenshotFiles) { |
| 733 | Log.i(TAG, "Deleting screenshot file " + file); |
| 734 | file.delete(); |
| 735 | } |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /** |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 739 | * Finishes the service when it's not monitoring any more processes. |
| 740 | */ |
| 741 | private void stopSelfWhenDone() { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 742 | if (mProcesses.size() > 0) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 743 | if (DEBUG) Log.d(TAG, "Staying alive, waiting for IDs " + mProcesses); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 744 | return; |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 745 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 746 | Log.v(TAG, "No more processes to handle, shutting down"); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 747 | stopSelf(); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 748 | } |
| 749 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 750 | /** |
| 751 | * Handles the BUGREPORT_FINISHED intent sent by {@code dumpstate}. |
| 752 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 753 | private void onBugreportFinished(int id, Intent intent) { |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 754 | final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT); |
| 755 | if (bugreportFile == null) { |
| 756 | // Should never happen, dumpstate always set the file. |
| 757 | Log.wtf(TAG, "Missing " + EXTRA_BUGREPORT + " on intent " + intent); |
| 758 | return; |
| 759 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 760 | mInfoDialog.onBugreportFinished(id); |
| 761 | BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 762 | if (info == null) { |
| 763 | // Happens when BUGREPORT_FINISHED was received without a BUGREPORT_STARTED first. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 764 | Log.v(TAG, "Creating info for untracked ID " + id); |
| 765 | info = new BugreportInfo(mContext, id); |
| 766 | mProcesses.put(id, info); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 767 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 768 | info.renameScreenshots(mScreenshotsDir); |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 769 | info.bugreportFile = bugreportFile; |
| 770 | |
Felipe Leme | 510e922 | 2016-02-22 18:07:49 -0800 | [diff] [blame] | 771 | final int max = intent.getIntExtra(EXTRA_MAX, -1); |
| 772 | if (max != -1) { |
| 773 | MetricsLogger.histogram(this, "dumpstate_duration", max); |
| 774 | info.max = max; |
| 775 | } |
| 776 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 777 | final File screenshot = getFileExtra(intent, EXTRA_SCREENSHOT); |
| 778 | if (screenshot != null) { |
| 779 | info.addScreenshot(screenshot); |
| 780 | } |
| 781 | info.finished = true; |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 782 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 783 | final Configuration conf = mContext.getResources().getConfiguration(); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 784 | if ((conf.uiMode & Configuration.UI_MODE_TYPE_MASK) != Configuration.UI_MODE_TYPE_WATCH) { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 785 | triggerLocalNotification(mContext, info); |
Felipe Leme | 923afa9 | 2015-12-04 12:15:30 -0800 | [diff] [blame] | 786 | } |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /** |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 790 | * Responsible for triggering a notification that allows the user to start a "share" intent with |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 791 | * the bugreport. On watches we have other methods to allow the user to start this intent |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 792 | * (usually by triggering it on another connected device); we don't need to display the |
| 793 | * notification in this case. |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 794 | */ |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 795 | private void triggerLocalNotification(final Context context, final BugreportInfo info) { |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 796 | if (!info.bugreportFile.exists() || !info.bugreportFile.canRead()) { |
| 797 | Log.e(TAG, "Could not read bugreport file " + info.bugreportFile); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 798 | Toast.makeText(context, R.string.bugreport_unreadable_text, Toast.LENGTH_LONG).show(); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 799 | stopProgress(info.id); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 803 | boolean isPlainText = info.bugreportFile.getName().toLowerCase().endsWith(".txt"); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 804 | if (!isPlainText) { |
| 805 | // Already zipped, send it right away. |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 806 | sendBugreportNotification(context, info); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 807 | } else { |
| 808 | // Asynchronously zip the file first, then send it. |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 809 | sendZippedBugreportNotification(context, info); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | |
| 813 | private static Intent buildWarningIntent(Context context, Intent sendIntent) { |
| 814 | final Intent intent = new Intent(context, BugreportWarningActivity.class); |
| 815 | intent.putExtra(Intent.EXTRA_INTENT, sendIntent); |
| 816 | return intent; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Build {@link Intent} that can be used to share the given bugreport. |
| 821 | */ |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 822 | private static Intent buildSendIntent(Context context, BugreportInfo info) { |
| 823 | // Files are kept on private storage, so turn into Uris that we can |
| 824 | // grant temporary permissions for. |
| 825 | final Uri bugreportUri = getUri(context, info.bugreportFile); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 826 | |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 827 | final Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 828 | final String mimeType = "application/vnd.android.bugreport"; |
| 829 | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 830 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
| 831 | intent.setType(mimeType); |
| 832 | |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 833 | final String subject = !TextUtils.isEmpty(info.title) ? |
| 834 | info.title : bugreportUri.getLastPathSegment(); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 835 | intent.putExtra(Intent.EXTRA_SUBJECT, subject); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 836 | |
| 837 | // EXTRA_TEXT should be an ArrayList, but some clients are expecting a single String. |
| 838 | // So, to avoid an exception on Intent.migrateExtraStreamToClipData(), we need to manually |
| 839 | // create the ClipData object with the attachments URIs. |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 840 | final StringBuilder messageBody = new StringBuilder("Build info: ") |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 841 | .append(SystemProperties.get("ro.build.description")) |
| 842 | .append("\nSerial number: ") |
| 843 | .append(SystemProperties.get("ro.serialno")); |
| 844 | if (!TextUtils.isEmpty(info.description)) { |
| 845 | messageBody.append("\nDescription: ").append(info.description); |
| 846 | } |
| 847 | intent.putExtra(Intent.EXTRA_TEXT, messageBody.toString()); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 848 | final ClipData clipData = new ClipData(null, new String[] { mimeType }, |
| 849 | new ClipData.Item(null, null, null, bugreportUri)); |
| 850 | final ArrayList<Uri> attachments = Lists.newArrayList(bugreportUri); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 851 | for (File screenshot : info.screenshotFiles) { |
| 852 | final Uri screenshotUri = getUri(context, screenshot); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 853 | clipData.addItem(new ClipData.Item(null, null, null, screenshotUri)); |
| 854 | attachments.add(screenshotUri); |
| 855 | } |
| 856 | intent.setClipData(clipData); |
| 857 | intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments); |
| 858 | |
| 859 | final Account sendToAccount = findSendToAccount(context); |
| 860 | if (sendToAccount != null) { |
| 861 | intent.putExtra(Intent.EXTRA_EMAIL, new String[] { sendToAccount.name }); |
| 862 | } |
| 863 | |
| 864 | return intent; |
| 865 | } |
| 866 | |
| 867 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 868 | * Shares the bugreport upon user's request by issuing a {@link Intent#ACTION_SEND_MULTIPLE} |
| 869 | * intent, but issuing a warning dialog the first time. |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 870 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 871 | private void shareBugreport(int id, BugreportInfo sharedInfo) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 872 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_NOTIFICATION_ACTION_SHARE); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 873 | BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 874 | if (info == null) { |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 875 | // Service was terminated but notification persisted |
| 876 | info = sharedInfo; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 877 | Log.d(TAG, "shareBugreport(): no info for ID " + id + " on managed processes (" |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 878 | + mProcesses + "), using info from intent instead (" + info + ")"); |
Felipe Leme | 4f663f6 | 2016-03-08 11:41:18 -0800 | [diff] [blame] | 879 | } else { |
| 880 | Log.v(TAG, "shareBugReport(): id " + id + " info = " + info); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 881 | } |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 882 | |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 883 | addDetailsToZipFile(mContext, info); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 884 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 885 | final Intent sendIntent = buildSendIntent(mContext, info); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 886 | final Intent notifIntent; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 887 | |
| 888 | // Send through warning dialog by default |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 889 | if (getWarningState(mContext, STATE_SHOW) == STATE_SHOW) { |
| 890 | notifIntent = buildWarningIntent(mContext, sendIntent); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 891 | } else { |
| 892 | notifIntent = sendIntent; |
| 893 | } |
| 894 | notifIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 895 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 896 | // Send the share intent... |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 897 | mContext.startActivity(notifIntent); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 898 | |
| 899 | // ... and stop watching this process. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 900 | stopProgress(id); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 904 | * Sends a notification indicating the bugreport has finished so use can share it. |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 905 | */ |
| 906 | private static void sendBugreportNotification(Context context, BugreportInfo info) { |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 907 | |
| 908 | // Since adding the details can take a while, do it before notifying user. |
| 909 | addDetailsToZipFile(context, info); |
| 910 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 911 | final Intent shareIntent = new Intent(INTENT_BUGREPORT_SHARE); |
| 912 | shareIntent.setClass(context, BugreportProgressService.class); |
| 913 | shareIntent.setAction(INTENT_BUGREPORT_SHARE); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 914 | shareIntent.putExtra(EXTRA_ID, info.id); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 915 | shareIntent.putExtra(EXTRA_INFO, info); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 916 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 917 | final String title = context.getString(R.string.bugreport_finished_title, info.id); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 918 | final Notification.Builder builder = new Notification.Builder(context) |
| 919 | .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb) |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 920 | .setContentTitle(title) |
| 921 | .setTicker(title) |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 922 | .setContentText(context.getString(R.string.bugreport_finished_text)) |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 923 | .setContentIntent(PendingIntent.getService(context, info.id, shareIntent, |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 924 | PendingIntent.FLAG_UPDATE_CURRENT)) |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 925 | .setDeleteIntent(newCancelIntent(context, info)) |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 926 | .setLocalOnly(true) |
| 927 | .setColor(context.getColor( |
| 928 | com.android.internal.R.color.system_notification_accent_color)); |
| 929 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 930 | if (!TextUtils.isEmpty(info.name)) { |
| 931 | builder.setContentInfo(info.name); |
| 932 | } |
| 933 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 934 | Log.v(TAG, "Sending 'Share' notification for ID " + info.id + ": " + title); |
| 935 | NotificationManager.from(context).notify(TAG, info.id, builder.build()); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | /** |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 939 | * Sends a notification indicating the bugreport is being updated so the user can wait until it |
| 940 | * finishes - at this point there is nothing to be done other than waiting, hence it has no |
| 941 | * pending action. |
| 942 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 943 | private static void sendBugreportBeingUpdatedNotification(Context context, int id) { |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 944 | final String title = context.getString(R.string.bugreport_updating_title); |
| 945 | final Notification.Builder builder = new Notification.Builder(context) |
| 946 | .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb) |
| 947 | .setContentTitle(title) |
| 948 | .setTicker(title) |
| 949 | .setContentText(context.getString(R.string.bugreport_updating_wait)) |
| 950 | .setLocalOnly(true) |
| 951 | .setColor(context.getColor( |
| 952 | com.android.internal.R.color.system_notification_accent_color)); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 953 | Log.v(TAG, "Sending 'Updating zip' notification for ID " + id + ": " + title); |
| 954 | NotificationManager.from(context).notify(TAG, id, builder.build()); |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /** |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 958 | * Sends a zipped bugreport notification. |
| 959 | */ |
| 960 | private static void sendZippedBugreportNotification(final Context context, |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 961 | final BugreportInfo info) { |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 962 | new AsyncTask<Void, Void, Void>() { |
| 963 | @Override |
| 964 | protected Void doInBackground(Void... params) { |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 965 | zipBugreport(info); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 966 | sendBugreportNotification(context, info); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 967 | return null; |
| 968 | } |
| 969 | }.execute(); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * Zips a bugreport file, returning the path to the new file (or to the |
| 974 | * original in case of failure). |
| 975 | */ |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 976 | private static void zipBugreport(BugreportInfo info) { |
| 977 | final String bugreportPath = info.bugreportFile.getAbsolutePath(); |
| 978 | final String zippedPath = bugreportPath.replace(".txt", ".zip"); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 979 | Log.v(TAG, "zipping " + bugreportPath + " as " + zippedPath); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 980 | final File bugreportZippedFile = new File(zippedPath); |
| 981 | try (InputStream is = new FileInputStream(info.bugreportFile); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 982 | ZipOutputStream zos = new ZipOutputStream( |
| 983 | new BufferedOutputStream(new FileOutputStream(bugreportZippedFile)))) { |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 984 | addEntry(zos, info.bugreportFile.getName(), is); |
| 985 | // Delete old file |
| 986 | final boolean deleted = info.bugreportFile.delete(); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 987 | if (deleted) { |
| 988 | Log.v(TAG, "deleted original bugreport (" + bugreportPath + ")"); |
| 989 | } else { |
| 990 | Log.e(TAG, "could not delete original bugreport (" + bugreportPath + ")"); |
| 991 | } |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 992 | info.bugreportFile = bugreportZippedFile; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 993 | } catch (IOException e) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 994 | Log.e(TAG, "exception zipping file " + zippedPath, e); |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | /** |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 999 | * Adds the user-provided info into the bugreport zip file. |
| 1000 | * <p> |
| 1001 | * If user provided a title, it will be saved into a {@code title.txt} entry; similarly, the |
| 1002 | * description will be saved on {@code description.txt}. |
| 1003 | */ |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1004 | private static void addDetailsToZipFile(Context context, BugreportInfo info) { |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1005 | if (info.bugreportFile == null) { |
| 1006 | // One possible reason is a bug in the Parcelization code. |
Felipe Leme | af6fd40 | 2016-01-29 18:01:49 -0800 | [diff] [blame] | 1007 | Log.wtf(TAG, "addDetailsToZipFile(): no bugreportFile on " + info); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1008 | return; |
| 1009 | } |
Felipe Leme | b9d598c | 2016-01-19 10:31:39 -0800 | [diff] [blame] | 1010 | if (TextUtils.isEmpty(info.title) && TextUtils.isEmpty(info.description)) { |
| 1011 | Log.d(TAG, "Not touching zip file since neither title nor description are set"); |
| 1012 | return; |
| 1013 | } |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1014 | if (info.addedDetailsToZip || info.addingDetailsToZip) { |
| 1015 | Log.d(TAG, "Already added details to zip file for " + info); |
| 1016 | return; |
| 1017 | } |
| 1018 | info.addingDetailsToZip = true; |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 1019 | |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 1020 | // It's not possible to add a new entry into an existing file, so we need to create a new |
| 1021 | // zip, copy all entries, then rename it. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1022 | sendBugreportBeingUpdatedNotification(context, info.id); // ...and that takes time |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 1023 | final File dir = info.bugreportFile.getParentFile(); |
| 1024 | final File tmpZip = new File(dir, "tmp-" + info.bugreportFile.getName()); |
Felipe Leme | 2758d5d | 2016-01-19 10:30:56 -0800 | [diff] [blame] | 1025 | Log.d(TAG, "Writing temporary zip file (" + tmpZip + ") with title and/or description"); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 1026 | try (ZipFile oldZip = new ZipFile(info.bugreportFile); |
| 1027 | ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(tmpZip))) { |
| 1028 | |
| 1029 | // First copy contents from original zip. |
| 1030 | Enumeration<? extends ZipEntry> entries = oldZip.entries(); |
| 1031 | while (entries.hasMoreElements()) { |
| 1032 | final ZipEntry entry = entries.nextElement(); |
| 1033 | final String entryName = entry.getName(); |
| 1034 | if (!entry.isDirectory()) { |
| 1035 | addEntry(zos, entryName, entry.getTime(), oldZip.getInputStream(entry)); |
| 1036 | } else { |
| 1037 | Log.w(TAG, "skipping directory entry: " + entryName); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | // Then add the user-provided info. |
| 1042 | addEntry(zos, "title.txt", info.title); |
| 1043 | addEntry(zos, "description.txt", info.description); |
| 1044 | } catch (IOException e) { |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1045 | info.addingDetailsToZip = false; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 1046 | Log.e(TAG, "exception zipping file " + tmpZip, e); |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | if (!tmpZip.renameTo(info.bugreportFile)) { |
| 1051 | Log.e(TAG, "Could not rename " + tmpZip + " to " + info.bugreportFile); |
| 1052 | } |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1053 | info.addedDetailsToZip = true; |
| 1054 | info.addingDetailsToZip = false; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | private static void addEntry(ZipOutputStream zos, String entry, String text) |
| 1058 | throws IOException { |
| 1059 | if (DEBUG) Log.v(TAG, "adding entry '" + entry + "': " + text); |
| 1060 | if (!TextUtils.isEmpty(text)) { |
| 1061 | addEntry(zos, entry, new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | private static void addEntry(ZipOutputStream zos, String entryName, InputStream is) |
| 1066 | throws IOException { |
| 1067 | addEntry(zos, entryName, System.currentTimeMillis(), is); |
| 1068 | } |
| 1069 | |
| 1070 | private static void addEntry(ZipOutputStream zos, String entryName, long timestamp, |
| 1071 | InputStream is) throws IOException { |
| 1072 | final ZipEntry entry = new ZipEntry(entryName); |
| 1073 | entry.setTime(timestamp); |
| 1074 | zos.putNextEntry(entry); |
| 1075 | final int totalBytes = Streams.copy(is, zos); |
| 1076 | if (DEBUG) Log.v(TAG, "size of '" + entryName + "' entry: " + totalBytes + " bytes"); |
| 1077 | zos.closeEntry(); |
| 1078 | } |
| 1079 | |
| 1080 | /** |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 1081 | * Find the best matching {@link Account} based on build properties. |
| 1082 | */ |
| 1083 | private static Account findSendToAccount(Context context) { |
| 1084 | final AccountManager am = (AccountManager) context.getSystemService( |
| 1085 | Context.ACCOUNT_SERVICE); |
| 1086 | |
| 1087 | String preferredDomain = SystemProperties.get("sendbug.preferred.domain"); |
| 1088 | if (!preferredDomain.startsWith("@")) { |
| 1089 | preferredDomain = "@" + preferredDomain; |
| 1090 | } |
| 1091 | |
| 1092 | final Account[] accounts = am.getAccounts(); |
| 1093 | Account foundAccount = null; |
| 1094 | for (Account account : accounts) { |
| 1095 | if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) { |
| 1096 | if (!preferredDomain.isEmpty()) { |
| 1097 | // if we have a preferred domain and it matches, return; otherwise keep |
| 1098 | // looking |
| 1099 | if (account.name.endsWith(preferredDomain)) { |
| 1100 | return account; |
| 1101 | } else { |
| 1102 | foundAccount = account; |
| 1103 | } |
| 1104 | // if we don't have a preferred domain, just return since it looks like |
| 1105 | // an email address |
| 1106 | } else { |
| 1107 | return account; |
| 1108 | } |
| 1109 | } |
| 1110 | } |
| 1111 | return foundAccount; |
| 1112 | } |
| 1113 | |
Michal Karpinski | 226940e | 2015-12-15 18:14:26 +0000 | [diff] [blame] | 1114 | static Uri getUri(Context context, File file) { |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 1115 | return file != null ? FileProvider.getUriForFile(context, AUTHORITY, file) : null; |
| 1116 | } |
| 1117 | |
| 1118 | static File getFileExtra(Intent intent, String key) { |
| 1119 | final String path = intent.getStringExtra(key); |
| 1120 | if (path != null) { |
| 1121 | return new File(path); |
| 1122 | } else { |
| 1123 | return null; |
| 1124 | } |
| 1125 | } |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1126 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1127 | private static boolean setSystemProperty(String key, String value) { |
| 1128 | try { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 1129 | if (DEBUG) Log.v(TAG, "Setting system property " + key + " to " + value); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1130 | SystemProperties.set(key, value); |
| 1131 | } catch (IllegalArgumentException e) { |
| 1132 | Log.e(TAG, "Could not set property " + key + " to " + value, e); |
| 1133 | return false; |
| 1134 | } |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
| 1138 | /** |
| 1139 | * Updates the system property used by {@code dumpstate} to rename the final bugreport files. |
| 1140 | */ |
| 1141 | private boolean setBugreportNameProperty(int pid, String name) { |
| 1142 | Log.d(TAG, "Updating bugreport name to " + name); |
| 1143 | final String key = DUMPSTATE_PREFIX + pid + NAME_SUFFIX; |
| 1144 | return setSystemProperty(key, name); |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Updates the user-provided details of a bugreport. |
| 1149 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1150 | private void updateBugreportInfo(int id, String name, String title, String description) { |
| 1151 | final BugreportInfo info = getInfo(id); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1152 | if (info == null) { |
| 1153 | return; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1154 | } |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1155 | if (title != null && !title.equals(info.title)) { |
| 1156 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_TITLE_CHANGED); |
| 1157 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1158 | info.title = title; |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1159 | if (description != null && !description.equals(info.description)) { |
| 1160 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_DESCRIPTION_CHANGED); |
| 1161 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1162 | info.description = description; |
Felipe Leme | 1eee199 | 2016-02-16 13:01:38 -0800 | [diff] [blame] | 1163 | if (name != null && !name.equals(info.name)) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1164 | MetricsLogger.action(this, MetricsEvent.ACTION_BUGREPORT_DETAILS_NAME_CHANGED); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1165 | info.name = name; |
| 1166 | updateProgress(info); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private void collapseNotificationBar() { |
| 1171 | sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); |
| 1172 | } |
| 1173 | |
| 1174 | private static Looper newLooper(String name) { |
| 1175 | final HandlerThread thread = new HandlerThread(name, THREAD_PRIORITY_BACKGROUND); |
| 1176 | thread.start(); |
| 1177 | return thread.getLooper(); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Takes a screenshot and save it to the given location. |
| 1182 | */ |
| 1183 | private static boolean takeScreenshot(Context context, String screenshotFile) { |
| 1184 | ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)) |
| 1185 | .vibrate(150); |
| 1186 | final ProcessBuilder screencap = new ProcessBuilder() |
| 1187 | .command("/system/bin/screencap", "-p", screenshotFile); |
| 1188 | Log.d(TAG, "Taking screenshot using " + screencap.command()); |
| 1189 | try { |
| 1190 | final int exitValue = screencap.start().waitFor(); |
| 1191 | if (exitValue == 0) { |
| 1192 | return true; |
| 1193 | } |
| 1194 | Log.e(TAG, "screencap (" + screencap.command() + ") failed: " + exitValue); |
| 1195 | } catch (IOException e) { |
| 1196 | Log.e(TAG, "screencap (" + screencap.command() + ") failed", e); |
| 1197 | } catch (InterruptedException e) { |
| 1198 | Log.w(TAG, "Thread interrupted while screencap still running"); |
| 1199 | Thread.currentThread().interrupt(); |
| 1200 | } |
| 1201 | return false; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Checks whether a character is valid on bugreport names. |
| 1206 | */ |
| 1207 | @VisibleForTesting |
| 1208 | static boolean isValid(char c) { |
| 1209 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') |
| 1210 | || c == '_' || c == '-'; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Helper class encapsulating the UI elements and logic used to display a dialog where user |
| 1215 | * can change the details of a bugreport. |
| 1216 | */ |
| 1217 | private final class BugreportInfoDialog { |
| 1218 | private EditText mInfoName; |
| 1219 | private EditText mInfoTitle; |
| 1220 | private EditText mInfoDescription; |
| 1221 | private AlertDialog mDialog; |
| 1222 | private Button mOkButton; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1223 | private int mId; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1224 | private int mPid; |
| 1225 | |
| 1226 | /** |
| 1227 | * Last "committed" value of the bugreport name. |
| 1228 | * <p> |
| 1229 | * Once initially set, it's only updated when user clicks the OK button. |
| 1230 | */ |
| 1231 | private String mSavedName; |
| 1232 | |
| 1233 | /** |
| 1234 | * Last value of the bugreport name as entered by the user. |
| 1235 | * <p> |
| 1236 | * Every time it's changed the equivalent system property is changed as well, but if the |
| 1237 | * user clicks CANCEL, the old value (stored on {@code mSavedName} is restored. |
| 1238 | * <p> |
| 1239 | * This logic handles the corner-case scenario where {@code dumpstate} finishes after the |
| 1240 | * user changed the name but didn't clicked OK yet (for example, because the user is typing |
| 1241 | * the description). The only drawback is that if the user changes the name while |
| 1242 | * {@code dumpstate} is running but clicks CANCEL after it finishes, then the final name |
| 1243 | * will be the one that has been canceled. But when {@code dumpstate} finishes the {code |
| 1244 | * name} UI is disabled and the old name restored anyways, so the user will be "alerted" of |
| 1245 | * such drawback. |
| 1246 | */ |
| 1247 | private String mTempName; |
| 1248 | |
| 1249 | /** |
| 1250 | * Sets its internal state and displays the dialog. |
| 1251 | */ |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1252 | private void initialize(final Context context, BugreportInfo info) { |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1253 | final String dialogTitle = |
| 1254 | context.getString(R.string.bugreport_info_dialog_title, info.id); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1255 | // First initializes singleton. |
| 1256 | if (mDialog == null) { |
| 1257 | @SuppressLint("InflateParams") |
| 1258 | // It's ok pass null ViewRoot on AlertDialogs. |
| 1259 | final View view = View.inflate(context, R.layout.dialog_bugreport_info, null); |
| 1260 | |
| 1261 | mInfoName = (EditText) view.findViewById(R.id.name); |
| 1262 | mInfoTitle = (EditText) view.findViewById(R.id.title); |
| 1263 | mInfoDescription = (EditText) view.findViewById(R.id.description); |
| 1264 | |
| 1265 | mInfoName.setOnFocusChangeListener(new OnFocusChangeListener() { |
| 1266 | |
| 1267 | @Override |
| 1268 | public void onFocusChange(View v, boolean hasFocus) { |
| 1269 | if (hasFocus) { |
| 1270 | return; |
| 1271 | } |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 1272 | // Select-all is useful just initially, since the date-based filename is |
| 1273 | // full of hyphens. |
| 1274 | mInfoName.setSelectAllOnFocus(false); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1275 | sanitizeName(); |
| 1276 | } |
| 1277 | }); |
| 1278 | |
| 1279 | mDialog = new AlertDialog.Builder(context) |
| 1280 | .setView(view) |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1281 | .setTitle(dialogTitle) |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1282 | .setCancelable(false) |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 1283 | .setPositiveButton(context.getString(R.string.save), |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1284 | null) |
| 1285 | .setNegativeButton(context.getString(com.android.internal.R.string.cancel), |
| 1286 | new DialogInterface.OnClickListener() |
| 1287 | { |
| 1288 | @Override |
| 1289 | public void onClick(DialogInterface dialog, int id) |
| 1290 | { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1291 | MetricsLogger.action(context, |
| 1292 | MetricsEvent.ACTION_BUGREPORT_DETAILS_CANCELED); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1293 | if (!mTempName.equals(mSavedName)) { |
| 1294 | // Must restore dumpstate's name since it was changed |
| 1295 | // before user clicked OK. |
| 1296 | setBugreportNameProperty(mPid, mSavedName); |
| 1297 | } |
| 1298 | } |
| 1299 | }) |
| 1300 | .create(); |
| 1301 | |
| 1302 | mDialog.getWindow().setAttributes( |
| 1303 | new WindowManager.LayoutParams( |
| 1304 | WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG)); |
| 1305 | |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1306 | } else { |
| 1307 | // Re-use view, but reset fields first. |
| 1308 | mDialog.setTitle(dialogTitle); |
| 1309 | mInfoName.setText(null); |
| 1310 | mInfoTitle.setText(null); |
| 1311 | mInfoDescription.setText(null); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | // Then set fields. |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1315 | mSavedName = mTempName = info.name; |
| 1316 | mId = info.id; |
| 1317 | mPid = info.pid; |
| 1318 | if (!TextUtils.isEmpty(info.name)) { |
| 1319 | mInfoName.setText(info.name); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1320 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1321 | if (!TextUtils.isEmpty(info.title)) { |
| 1322 | mInfoTitle.setText(info.title); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1323 | } |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1324 | if (!TextUtils.isEmpty(info.description)) { |
| 1325 | mInfoDescription.setText(info.description); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | // And finally display it. |
| 1329 | mDialog.show(); |
| 1330 | |
| 1331 | // TODO: in a traditional AlertDialog, when the positive button is clicked the |
| 1332 | // dialog is always closed, but we need to validate the name first, so we need to |
| 1333 | // get a reference to it, which is only available after it's displayed. |
| 1334 | // It would be cleaner to use a regular dialog instead, but let's keep this |
| 1335 | // workaround for now and change it later, when we add another button to take |
| 1336 | // extra screenshots. |
| 1337 | if (mOkButton == null) { |
| 1338 | mOkButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE); |
| 1339 | mOkButton.setOnClickListener(new View.OnClickListener() { |
| 1340 | |
| 1341 | @Override |
| 1342 | public void onClick(View view) { |
Felipe Leme | 6605bd8 | 2016-02-22 15:22:20 -0800 | [diff] [blame] | 1343 | MetricsLogger.action(context, MetricsEvent.ACTION_BUGREPORT_DETAILS_SAVED); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1344 | sanitizeName(); |
| 1345 | final String name = mInfoName.getText().toString(); |
| 1346 | final String title = mInfoTitle.getText().toString(); |
| 1347 | final String description = mInfoDescription.getText().toString(); |
| 1348 | |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1349 | updateBugreportInfo(mId, name, title, description); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1350 | mDialog.dismiss(); |
| 1351 | } |
| 1352 | }); |
| 1353 | } |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * Sanitizes the user-provided value for the {@code name} field, automatically replacing |
| 1358 | * invalid characters if necessary. |
| 1359 | */ |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1360 | private void sanitizeName() { |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1361 | String name = mInfoName.getText().toString(); |
| 1362 | if (name.equals(mTempName)) { |
| 1363 | if (DEBUG) Log.v(TAG, "name didn't change, no need to sanitize: " + name); |
| 1364 | return; |
| 1365 | } |
| 1366 | final StringBuilder safeName = new StringBuilder(name.length()); |
| 1367 | boolean changed = false; |
| 1368 | for (int i = 0; i < name.length(); i++) { |
| 1369 | final char c = name.charAt(i); |
| 1370 | if (isValid(c)) { |
| 1371 | safeName.append(c); |
| 1372 | } else { |
| 1373 | changed = true; |
| 1374 | safeName.append('_'); |
| 1375 | } |
| 1376 | } |
| 1377 | if (changed) { |
| 1378 | Log.v(TAG, "changed invalid name '" + name + "' to '" + safeName + "'"); |
| 1379 | name = safeName.toString(); |
| 1380 | mInfoName.setText(name); |
| 1381 | } |
| 1382 | mTempName = name; |
| 1383 | |
| 1384 | // Must update system property for the cases where dumpstate finishes |
| 1385 | // while the user is still entering other fields (like title or |
| 1386 | // description) |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 1387 | setBugreportNameProperty(mPid, name); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * Notifies the dialog that the bugreport has finished so it disables the {@code name} |
| 1392 | * field. |
| 1393 | * <p>Once the bugreport is finished dumpstate has already generated the final files, so |
| 1394 | * changing the name would have no effect. |
| 1395 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1396 | private void onBugreportFinished(int id) { |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1397 | if (mInfoName != null) { |
| 1398 | mInfoName.setEnabled(false); |
| 1399 | mInfoName.setText(mSavedName); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | } |
| 1404 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1405 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1406 | * Information about a bugreport process while its in progress. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1407 | */ |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1408 | private static final class BugreportInfo implements Parcelable { |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 1409 | private final Context context; |
| 1410 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1411 | /** |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1412 | * Sequential, user-friendly id used to identify the bugreport. |
| 1413 | */ |
| 1414 | final int id; |
| 1415 | |
| 1416 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1417 | * {@code pid} of the {@code dumpstate} process generating the bugreport. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1418 | */ |
| 1419 | final int pid; |
| 1420 | |
| 1421 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1422 | * Name of the bugreport, will be used to rename the final files. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1423 | * <p> |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1424 | * Initial value is the bugreport filename reported by {@code dumpstate}, but user can |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1425 | * change it later to a more meaningful name. |
| 1426 | */ |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 1427 | String name; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1428 | |
| 1429 | /** |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1430 | * User-provided, one-line summary of the bug; when set, will be used as the subject |
| 1431 | * of the {@link Intent#ACTION_SEND_MULTIPLE} intent. |
| 1432 | */ |
| 1433 | String title; |
| 1434 | |
| 1435 | /** |
| 1436 | * User-provided, detailed description of the bugreport; when set, will be added to the body |
| 1437 | * of the {@link Intent#ACTION_SEND_MULTIPLE} intent. |
| 1438 | */ |
| 1439 | String description; |
| 1440 | |
| 1441 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1442 | * Maximum progress of the bugreport generation. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1443 | */ |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 1444 | int max; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1445 | |
| 1446 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1447 | * Current progress of the bugreport generation. |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1448 | */ |
| 1449 | int progress; |
| 1450 | |
| 1451 | /** |
| 1452 | * Time of the last progress update. |
| 1453 | */ |
| 1454 | long lastUpdate = System.currentTimeMillis(); |
| 1455 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1456 | /** |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1457 | * Time of the last progress update when Parcel was created. |
| 1458 | */ |
| 1459 | String formattedLastUpdate; |
| 1460 | |
| 1461 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1462 | * Path of the main bugreport file. |
| 1463 | */ |
| 1464 | File bugreportFile; |
| 1465 | |
| 1466 | /** |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1467 | * Path of the screenshot files. |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1468 | */ |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1469 | List<File> screenshotFiles = new ArrayList<>(1); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1470 | |
| 1471 | /** |
| 1472 | * Whether dumpstate sent an intent informing it has finished. |
| 1473 | */ |
| 1474 | boolean finished; |
| 1475 | |
| 1476 | /** |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1477 | * Whether the details entries have been added to the bugreport yet. |
| 1478 | */ |
| 1479 | boolean addingDetailsToZip; |
| 1480 | boolean addedDetailsToZip; |
| 1481 | |
| 1482 | /** |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1483 | * Internal counter used to name screenshot files. |
| 1484 | */ |
| 1485 | int screenshotCounter; |
| 1486 | |
| 1487 | /** |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1488 | * Constructor for tracked bugreports - typically called upon receiving BUGREPORT_STARTED. |
| 1489 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1490 | BugreportInfo(Context context, int id, int pid, String name, int max) { |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 1491 | this.context = context; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1492 | this.id = id; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1493 | this.pid = pid; |
| 1494 | this.name = name; |
| 1495 | this.max = max; |
| 1496 | } |
| 1497 | |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1498 | /** |
| 1499 | * Constructor for untracked bugreports - typically called upon receiving BUGREPORT_FINISHED |
| 1500 | * without a previous call to BUGREPORT_STARTED. |
| 1501 | */ |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1502 | BugreportInfo(Context context, int id) { |
| 1503 | this(context, id, id, null, 0); |
Felipe Leme | 46d4791 | 2015-12-09 13:03:09 -0800 | [diff] [blame] | 1504 | this.finished = true; |
| 1505 | } |
| 1506 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1507 | /** |
| 1508 | * Gets the name for next screenshot file. |
| 1509 | */ |
| 1510 | String getPathNextScreenshot() { |
| 1511 | screenshotCounter ++; |
| 1512 | return "screenshot-" + pid + "-" + screenshotCounter + ".png"; |
| 1513 | } |
| 1514 | |
| 1515 | /** |
| 1516 | * Saves the location of a taken screenshot so it can be sent out at the end. |
| 1517 | */ |
| 1518 | void addScreenshot(File screenshot) { |
| 1519 | screenshotFiles.add(screenshot); |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Rename all screenshots files so that they contain the user-generated name instead of pid. |
| 1524 | */ |
| 1525 | void renameScreenshots(File screenshotDir) { |
| 1526 | if (TextUtils.isEmpty(name)) { |
| 1527 | return; |
| 1528 | } |
| 1529 | final List<File> renamedFiles = new ArrayList<>(screenshotFiles.size()); |
| 1530 | for (File oldFile : screenshotFiles) { |
| 1531 | final String oldName = oldFile.getName(); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 1532 | final String newName = oldName.replaceFirst(Integer.toString(pid), name); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1533 | final File newFile; |
| 1534 | if (!newName.equals(oldName)) { |
| 1535 | final File renamedFile = new File(screenshotDir, newName); |
Felipe Leme | 4f663f6 | 2016-03-08 11:41:18 -0800 | [diff] [blame] | 1536 | Log.d(TAG, "Renaming screenshot file " + oldFile + " to " + renamedFile); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1537 | newFile = oldFile.renameTo(renamedFile) ? renamedFile : oldFile; |
| 1538 | } else { |
| 1539 | Log.w(TAG, "Name didn't change: " + oldName); // Shouldn't happen. |
| 1540 | newFile = oldFile; |
| 1541 | } |
| 1542 | renamedFiles.add(newFile); |
| 1543 | } |
| 1544 | screenshotFiles = renamedFiles; |
| 1545 | } |
| 1546 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1547 | String getFormattedLastUpdate() { |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1548 | if (context == null) { |
| 1549 | // Restored from Parcel |
| 1550 | return formattedLastUpdate == null ? |
| 1551 | Long.toString(lastUpdate) : formattedLastUpdate; |
| 1552 | } |
Felipe Leme | 719aaae | 2015-11-30 15:41:11 -0800 | [diff] [blame] | 1553 | return DateUtils.formatDateTime(context, lastUpdate, |
| 1554 | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | @Override |
| 1558 | public String toString() { |
| 1559 | final float percent = ((float) progress * 100 / max); |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1560 | return "id: " + id + ", pid: " + pid + ", name: " + name + ", finished: " + finished |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1561 | + "\n\ttitle: " + title + "\n\tdescription: " + description |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1562 | + "\n\tfile: " + bugreportFile + "\n\tscreenshots: " + screenshotFiles |
Felipe Leme | 510e922 | 2016-02-22 18:07:49 -0800 | [diff] [blame] | 1563 | + "\n\tprogress: " + progress + "/" + max + " (" + percent + ")" |
Felipe Leme | 18b5892 | 2016-01-29 12:24:25 -0800 | [diff] [blame] | 1564 | + "\n\tlast_update: " + getFormattedLastUpdate() |
| 1565 | + "\naddingDetailsToZip: " + addingDetailsToZip |
| 1566 | + " addedDetailsToZip: " + addedDetailsToZip; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1567 | } |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1568 | |
| 1569 | // Parcelable contract |
| 1570 | protected BugreportInfo(Parcel in) { |
| 1571 | context = null; |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1572 | id = in.readInt(); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1573 | pid = in.readInt(); |
| 1574 | name = in.readString(); |
| 1575 | title = in.readString(); |
| 1576 | description = in.readString(); |
| 1577 | max = in.readInt(); |
| 1578 | progress = in.readInt(); |
| 1579 | lastUpdate = in.readLong(); |
| 1580 | formattedLastUpdate = in.readString(); |
| 1581 | bugreportFile = readFile(in); |
| 1582 | |
| 1583 | int screenshotSize = in.readInt(); |
| 1584 | for (int i = 1; i <= screenshotSize; i++) { |
| 1585 | screenshotFiles.add(readFile(in)); |
| 1586 | } |
| 1587 | |
| 1588 | finished = in.readInt() == 1; |
| 1589 | screenshotCounter = in.readInt(); |
| 1590 | } |
| 1591 | |
| 1592 | @Override |
| 1593 | public void writeToParcel(Parcel dest, int flags) { |
Felipe Leme | fd8ea07 | 2016-02-09 10:13:47 -0800 | [diff] [blame] | 1594 | dest.writeInt(id); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1595 | dest.writeInt(pid); |
| 1596 | dest.writeString(name); |
| 1597 | dest.writeString(title); |
| 1598 | dest.writeString(description); |
| 1599 | dest.writeInt(max); |
| 1600 | dest.writeInt(progress); |
| 1601 | dest.writeLong(lastUpdate); |
| 1602 | dest.writeString(getFormattedLastUpdate()); |
| 1603 | writeFile(dest, bugreportFile); |
| 1604 | |
| 1605 | dest.writeInt(screenshotFiles.size()); |
| 1606 | for (File screenshotFile : screenshotFiles) { |
| 1607 | writeFile(dest, screenshotFile); |
| 1608 | } |
| 1609 | |
| 1610 | dest.writeInt(finished ? 1 : 0); |
| 1611 | dest.writeInt(screenshotCounter); |
| 1612 | } |
| 1613 | |
| 1614 | @Override |
| 1615 | public int describeContents() { |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
| 1619 | private void writeFile(Parcel dest, File file) { |
| 1620 | dest.writeString(file == null ? null : file.getPath()); |
| 1621 | } |
| 1622 | |
| 1623 | private File readFile(Parcel in) { |
| 1624 | final String path = in.readString(); |
| 1625 | return path == null ? null : new File(path); |
| 1626 | } |
| 1627 | |
| 1628 | public static final Parcelable.Creator<BugreportInfo> CREATOR = |
| 1629 | new Parcelable.Creator<BugreportInfo>() { |
| 1630 | public BugreportInfo createFromParcel(Parcel source) { |
| 1631 | return new BugreportInfo(source); |
| 1632 | } |
| 1633 | |
| 1634 | public BugreportInfo[] newArray(int size) { |
| 1635 | return new BugreportInfo[size]; |
| 1636 | } |
| 1637 | }; |
| 1638 | |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 1639 | } |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 1640 | } |