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