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