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