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