Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 android.os; |
| 18 | |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 20 | import android.annotation.FloatRange; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
| 24 | import android.annotation.RequiresPermission; |
Nandana Dutt | 2083e8c | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Nandana Dutt | d7e8d5a | 2019-04-11 17:27:45 +0100 | [diff] [blame] | 27 | import android.annotation.TestApi; |
Paul Chang | 9082c39 | 2019-11-25 16:43:27 +0800 | [diff] [blame] | 28 | import android.app.ActivityManager; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 29 | import android.content.Context; |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 30 | import android.util.Log; |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 31 | |
| 32 | import com.android.internal.util.Preconditions; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 33 | |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 34 | import libcore.io.IoUtils; |
| 35 | |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 36 | import java.io.File; |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 37 | import java.io.FileNotFoundException; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 38 | import java.lang.annotation.Retention; |
| 39 | import java.lang.annotation.RetentionPolicy; |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 40 | import java.util.concurrent.Executor; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Class that provides a privileged API to capture and consume bugreports. |
| 44 | * |
| 45 | * @hide |
| 46 | */ |
Nandana Dutt | 2083e8c | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 47 | @SystemApi |
Nandana Dutt | d7e8d5a | 2019-04-11 17:27:45 +0100 | [diff] [blame] | 48 | @TestApi |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 49 | @SystemService(Context.BUGREPORT_SERVICE) |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 50 | public final class BugreportManager { |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 51 | |
| 52 | private static final String TAG = "BugreportManager"; |
| 53 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 54 | private final Context mContext; |
| 55 | private final IDumpstate mBinder; |
| 56 | |
| 57 | /** @hide */ |
| 58 | public BugreportManager(@NonNull Context context, IDumpstate binder) { |
| 59 | mContext = context; |
| 60 | mBinder = binder; |
| 61 | } |
| 62 | |
| 63 | /** |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 64 | * An interface describing the callback for bugreport progress and status. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 65 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 66 | public abstract static class BugreportCallback { |
| 67 | /** @hide */ |
| 68 | @Retention(RetentionPolicy.SOURCE) |
| 69 | @IntDef(prefix = { "BUGREPORT_ERROR_" }, value = { |
| 70 | BUGREPORT_ERROR_INVALID_INPUT, |
| 71 | BUGREPORT_ERROR_RUNTIME, |
| 72 | BUGREPORT_ERROR_USER_DENIED_CONSENT, |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 73 | BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT, |
| 74 | BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 75 | }) |
| 76 | |
| 77 | /** Possible error codes taking a bugreport can encounter */ |
| 78 | public @interface BugreportErrorCode {} |
| 79 | |
| 80 | /** The input options were invalid */ |
| 81 | public static final int BUGREPORT_ERROR_INVALID_INPUT = |
| 82 | IDumpstateListener.BUGREPORT_ERROR_INVALID_INPUT; |
| 83 | |
| 84 | /** A runtime error occured */ |
| 85 | public static final int BUGREPORT_ERROR_RUNTIME = |
| 86 | IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR; |
| 87 | |
| 88 | /** User denied consent to share the bugreport */ |
| 89 | public static final int BUGREPORT_ERROR_USER_DENIED_CONSENT = |
| 90 | IDumpstateListener.BUGREPORT_ERROR_USER_DENIED_CONSENT; |
| 91 | |
| 92 | /** The request to get user consent timed out. */ |
| 93 | public static final int BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT = |
| 94 | IDumpstateListener.BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT; |
| 95 | |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 96 | /** There is currently a bugreport running. The caller should try again later. */ |
| 97 | public static final int BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS = |
| 98 | IDumpstateListener.BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS; |
| 99 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Called when there is a progress update. |
| 102 | * @param progress the progress in [0.0, 100.0] |
| 103 | */ |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 104 | public void onProgress(@FloatRange(from = 0f, to = 100f) float progress) {} |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 105 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 106 | /** |
| 107 | * Called when taking bugreport resulted in an error. |
| 108 | * |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 109 | * <p>If {@code BUGREPORT_ERROR_USER_DENIED_CONSENT} is passed, then the user did not |
| 110 | * consent to sharing the bugreport with the calling app. |
| 111 | * |
| 112 | * <p>If {@code BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT} is passed, then the consent timed |
| 113 | * out, but the bugreport could be available in the internal directory of dumpstate for |
| 114 | * manual retrieval. |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 115 | * |
| 116 | * <p> If {@code BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS} is passed, then the |
| 117 | * caller should try later, as only one bugreport can be in progress at a time. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 118 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 119 | public void onError(@BugreportErrorCode int errorCode) {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 122 | * Called when taking bugreport finishes successfully. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 123 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 124 | public void onFinished() {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 128 | * Starts a bugreport. |
| 129 | * |
| 130 | * <p>This starts a bugreport in the background. However the call itself can take several |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 131 | * seconds to return in the worst case. {@code callback} will receive progress and status |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 132 | * updates. |
| 133 | * |
| 134 | * <p>The bugreport artifacts will be copied over to the given file descriptors only if the |
| 135 | * user consents to sharing with the calling app. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 136 | * |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 137 | * <p>{@link BugreportManager} takes ownership of {@code bugreportFd} and {@code screenshotFd}. |
| 138 | * |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 139 | * @param bugreportFd file to write the bugreport. This should be opened in write-only, |
| 140 | * append mode. |
| 141 | * @param screenshotFd file to write the screenshot, if necessary. This should be opened |
| 142 | * in write-only, append mode. |
| 143 | * @param params options that specify what kind of a bugreport should be taken |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 144 | * @param callback callback for progress and status updates |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 145 | */ |
| 146 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 147 | public void startBugreport(@NonNull ParcelFileDescriptor bugreportFd, |
| 148 | @Nullable ParcelFileDescriptor screenshotFd, |
| 149 | @NonNull BugreportParams params, |
| 150 | @NonNull @CallbackExecutor Executor executor, |
| 151 | @NonNull BugreportCallback callback) { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 152 | try { |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 153 | Preconditions.checkNotNull(bugreportFd); |
| 154 | Preconditions.checkNotNull(params); |
| 155 | Preconditions.checkNotNull(executor); |
| 156 | Preconditions.checkNotNull(callback); |
| 157 | |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 158 | if (screenshotFd == null) { |
| 159 | // Binder needs a valid File Descriptor to be passed |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 160 | screenshotFd = ParcelFileDescriptor.open(new File("/dev/null"), |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 161 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 162 | } |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 163 | DumpstateListener dsListener = new DumpstateListener(executor, callback); |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 164 | // Note: mBinder can get callingUid from the binder transaction. |
| 165 | mBinder.startBugreport(-1 /* callingUid */, |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 166 | mContext.getOpPackageName(), |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 167 | bugreportFd.getFileDescriptor(), |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 168 | screenshotFd.getFileDescriptor(), |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 169 | params.getMode(), dsListener); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 170 | } catch (RemoteException e) { |
| 171 | throw e.rethrowFromSystemServer(); |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 172 | } catch (FileNotFoundException e) { |
| 173 | Log.wtf(TAG, "Not able to find /dev/null file: ", e); |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 174 | } finally { |
| 175 | // We can close the file descriptors here because binder would have duped them. |
| 176 | IoUtils.closeQuietly(bugreportFd); |
| 177 | if (screenshotFd != null) { |
| 178 | IoUtils.closeQuietly(screenshotFd); |
| 179 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 183 | /* |
| 184 | * Cancels a currently running bugreport. |
| 185 | */ |
| 186 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 187 | public void cancelBugreport() { |
| 188 | try { |
| 189 | mBinder.cancelBugreport(); |
| 190 | } catch (RemoteException e) { |
| 191 | throw e.rethrowFromSystemServer(); |
| 192 | } |
| 193 | } |
| 194 | |
Paul Chang | 9082c39 | 2019-11-25 16:43:27 +0800 | [diff] [blame] | 195 | /** |
| 196 | * Requests a bugreport. |
| 197 | * |
| 198 | * <p>This requests the platform/system to take a bugreport and makes the final bugreport |
| 199 | * available to the user. The user may choose to share it with another app, but the bugreport |
| 200 | * is never given back directly to the app that requested it. |
| 201 | * |
| 202 | * @param params {@link BugreportParams} that specify what kind of a bugreport should |
| 203 | * be taken, please note that not all kinds of bugreport allow for a |
| 204 | * progress notification |
| 205 | * @param shareTitle title on the final share notification |
| 206 | * @param shareDescription description on the final share notification |
| 207 | */ |
| 208 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 209 | public void requestBugreport(@NonNull BugreportParams params, @Nullable CharSequence shareTitle, |
| 210 | @Nullable CharSequence shareDescription) { |
| 211 | try { |
| 212 | String title = shareTitle == null ? null : shareTitle.toString(); |
| 213 | String description = shareDescription == null ? null : shareDescription.toString(); |
| 214 | ActivityManager.getService().requestBugReportWithDescription(title, description, |
| 215 | params.getMode()); |
| 216 | } catch (RemoteException e) { |
| 217 | throw e.rethrowFromSystemServer(); |
| 218 | } |
| 219 | } |
| 220 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 221 | private final class DumpstateListener extends IDumpstateListener.Stub { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 222 | private final Executor mExecutor; |
| 223 | private final BugreportCallback mCallback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 224 | |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 225 | DumpstateListener(Executor executor, BugreportCallback callback) { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 226 | mExecutor = executor; |
| 227 | mCallback = callback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Override |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 231 | public void onProgress(int progress) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 232 | final long identity = Binder.clearCallingIdentity(); |
| 233 | try { |
| 234 | mExecutor.execute(() -> { |
| 235 | mCallback.onProgress(progress); |
| 236 | }); |
| 237 | } finally { |
| 238 | Binder.restoreCallingIdentity(identity); |
| 239 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public void onError(int errorCode) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 244 | final long identity = Binder.clearCallingIdentity(); |
| 245 | try { |
| 246 | mExecutor.execute(() -> { |
| 247 | mCallback.onError(errorCode); |
| 248 | }); |
| 249 | } finally { |
| 250 | Binder.restoreCallingIdentity(identity); |
| 251 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 255 | public void onFinished() throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 256 | final long identity = Binder.clearCallingIdentity(); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 257 | try { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 258 | mExecutor.execute(() -> { |
| 259 | mCallback.onFinished(); |
| 260 | }); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 261 | } finally { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 262 | Binder.restoreCallingIdentity(identity); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 263 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 264 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 265 | } |
| 266 | } |