| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.FloatRange; |
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.annotation.RequiresPermission; |
| 25 |
import android.annotation.SuppressAutoDoc; |
| 26 |
import android.annotation.SystemApi; |
| 27 |
import android.annotation.SystemService; |
| 28 |
import android.annotation.WorkerThread; |
| 29 |
import android.app.ActivityManager; |
| 30 |
import android.content.Context; |
| 31 |
import android.util.Log; |
| 32 |
import android.widget.Toast; |
| 33 |
|
| 34 |
import com.android.internal.R; |
| 35 |
import com.android.internal.util.Preconditions; |
| 36 |
|
| 37 |
import libcore.io.IoUtils; |
| 38 |
|
| 39 |
import java.io.File; |
| 40 |
import java.io.FileNotFoundException; |
| 41 |
import java.lang.annotation.Retention; |
| 42 |
import java.lang.annotation.RetentionPolicy; |
| 43 |
import java.util.concurrent.Executor; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@SystemService(Context.BUGREPORT_SERVICE) |
| 55 |
public final class BugreportManager { |
| 56 |
|
| 57 |
private static final String TAG = "BugreportManager"; |
| 58 |
|
| 59 |
private final Context mContext; |
| 60 |
private final IDumpstate mBinder; |
| 61 |
|
| 62 |
|
| 63 |
public BugreportManager(@NonNull Context context, IDumpstate binder) { |
| 64 |
mContext = context; |
| 65 |
mBinder = binder; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public abstract static class BugreportCallback { |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@Retention(RetentionPolicy.SOURCE) |
| 84 |
@IntDef( |
| 85 |
prefix = {"BUGREPORT_ERROR_"}, |
| 86 |
value = { |
| 87 |
BUGREPORT_ERROR_INVALID_INPUT, |
| 88 |
BUGREPORT_ERROR_RUNTIME, |
| 89 |
BUGREPORT_ERROR_USER_DENIED_CONSENT, |
| 90 |
BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT, |
| 91 |
BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS |
| 92 |
}) |
| 93 |
public @interface BugreportErrorCode {} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final int BUGREPORT_ERROR_INVALID_INPUT = |
| 100 |
IDumpstateListener.BUGREPORT_ERROR_INVALID_INPUT; |
| 101 |
|
| 102 |
|
| 103 |
public static final int BUGREPORT_ERROR_RUNTIME = |
| 104 |
IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR; |
| 105 |
|
| 106 |
|
| 107 |
public static final int BUGREPORT_ERROR_USER_DENIED_CONSENT = |
| 108 |
IDumpstateListener.BUGREPORT_ERROR_USER_DENIED_CONSENT; |
| 109 |
|
| 110 |
|
| 111 |
public static final int BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT = |
| 112 |
IDumpstateListener.BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT; |
| 113 |
|
| 114 |
|
| 115 |
public static final int BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS = |
| 116 |
IDumpstateListener.BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS; |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public void onProgress(@FloatRange(from = 0f, to = 100f) float progress) {} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void onError(@BugreportErrorCode int errorCode) {} |
| 139 |
|
| 140 |
|
| 141 |
public void onFinished() {} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public void onEarlyReportFinished() {} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
@SystemApi |
| 171 |
@RequiresPermission(android.Manifest.permission.DUMP) |
| 172 |
@WorkerThread |
| 173 |
public void startBugreport( |
| 174 |
@NonNull ParcelFileDescriptor bugreportFd, |
| 175 |
@Nullable ParcelFileDescriptor screenshotFd, |
| 176 |
@NonNull BugreportParams params, |
| 177 |
@NonNull @CallbackExecutor Executor executor, |
| 178 |
@NonNull BugreportCallback callback) { |
| 179 |
try { |
| 180 |
Preconditions.checkNotNull(bugreportFd); |
| 181 |
Preconditions.checkNotNull(params); |
| 182 |
Preconditions.checkNotNull(executor); |
| 183 |
Preconditions.checkNotNull(callback); |
| 184 |
|
| 185 |
boolean isScreenshotRequested = screenshotFd != null; |
| 186 |
if (screenshotFd == null) { |
| 187 |
|
| 188 |
screenshotFd = |
| 189 |
ParcelFileDescriptor.open( |
| 190 |
new File("/dev/null"), ParcelFileDescriptor.MODE_READ_ONLY); |
| 191 |
} |
| 192 |
DumpstateListener dsListener = |
| 193 |
new DumpstateListener(executor, callback, isScreenshotRequested); |
| 194 |
|
| 195 |
mBinder.startBugreport( |
| 196 |
-1 , |
| 197 |
mContext.getOpPackageName(), |
| 198 |
bugreportFd.getFileDescriptor(), |
| 199 |
screenshotFd.getFileDescriptor(), |
| 200 |
params.getMode(), |
| 201 |
dsListener, |
| 202 |
isScreenshotRequested); |
| 203 |
} catch (RemoteException e) { |
| 204 |
throw e.rethrowFromSystemServer(); |
| 205 |
} catch (FileNotFoundException e) { |
| 206 |
Log.wtf(TAG, "Not able to find /dev/null file: ", e); |
| 207 |
} finally { |
| 208 |
|
| 209 |
IoUtils.closeQuietly(bugreportFd); |
| 210 |
if (screenshotFd != null) { |
| 211 |
IoUtils.closeQuietly(screenshotFd); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
@SuppressAutoDoc |
| 246 |
@WorkerThread |
| 247 |
public void startConnectivityBugreport( |
| 248 |
@NonNull ParcelFileDescriptor bugreportFd, |
| 249 |
@NonNull @CallbackExecutor Executor executor, |
| 250 |
@NonNull BugreportCallback callback) { |
| 251 |
startBugreport( |
| 252 |
bugreportFd, |
| 253 |
null , |
| 254 |
new BugreportParams(BugreportParams.BUGREPORT_MODE_TELEPHONY), |
| 255 |
executor, |
| 256 |
callback); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
@SuppressAutoDoc |
| 272 |
@WorkerThread |
| 273 |
public void cancelBugreport() { |
| 274 |
try { |
| 275 |
mBinder.cancelBugreport(-1 , mContext.getOpPackageName()); |
| 276 |
} catch (RemoteException e) { |
| 277 |
throw e.rethrowFromSystemServer(); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
@SystemApi |
| 295 |
@RequiresPermission(android.Manifest.permission.DUMP) |
| 296 |
public void requestBugreport( |
| 297 |
@NonNull BugreportParams params, |
| 298 |
@Nullable CharSequence shareTitle, |
| 299 |
@Nullable CharSequence shareDescription) { |
| 300 |
try { |
| 301 |
String title = shareTitle == null ? null : shareTitle.toString(); |
| 302 |
String description = shareDescription == null ? null : shareDescription.toString(); |
| 303 |
ActivityManager.getService() |
| 304 |
.requestBugReportWithDescription(title, description, params.getMode()); |
| 305 |
} catch (RemoteException e) { |
| 306 |
throw e.rethrowFromSystemServer(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
private final class DumpstateListener extends IDumpstateListener.Stub { |
| 311 |
private final Executor mExecutor; |
| 312 |
private final BugreportCallback mCallback; |
| 313 |
private final boolean mIsScreenshotRequested; |
| 314 |
|
| 315 |
DumpstateListener( |
| 316 |
Executor executor, BugreportCallback callback, boolean isScreenshotRequested) { |
| 317 |
mExecutor = executor; |
| 318 |
mCallback = callback; |
| 319 |
mIsScreenshotRequested = isScreenshotRequested; |
| 320 |
} |
| 321 |
|
| 322 |
@Override |
| 323 |
public void onProgress(int progress) throws RemoteException { |
| 324 |
final long identity = Binder.clearCallingIdentity(); |
| 325 |
try { |
| 326 |
mExecutor.execute(() -> mCallback.onProgress(progress)); |
| 327 |
} finally { |
| 328 |
Binder.restoreCallingIdentity(identity); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
@Override |
| 333 |
public void onError(int errorCode) throws RemoteException { |
| 334 |
final long identity = Binder.clearCallingIdentity(); |
| 335 |
try { |
| 336 |
mExecutor.execute(() -> mCallback.onError(errorCode)); |
| 337 |
} finally { |
| 338 |
Binder.restoreCallingIdentity(identity); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
@Override |
| 343 |
public void onFinished() throws RemoteException { |
| 344 |
final long identity = Binder.clearCallingIdentity(); |
| 345 |
try { |
| 346 |
mExecutor.execute(() -> mCallback.onFinished()); |
| 347 |
} finally { |
| 348 |
Binder.restoreCallingIdentity(identity); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
public void onScreenshotTaken(boolean success) throws RemoteException { |
| 354 |
if (!mIsScreenshotRequested) { |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
Handler mainThreadHandler = new Handler(Looper.getMainLooper()); |
| 359 |
mainThreadHandler.post( |
| 360 |
() -> { |
| 361 |
int message = |
| 362 |
success |
| 363 |
? R.string.bugreport_screenshot_success_toast |
| 364 |
: R.string.bugreport_screenshot_failure_toast; |
| 365 |
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show(); |
| 366 |
}); |
| 367 |
} |
| 368 |
|
| 369 |
@Override |
| 370 |
public void onUiIntensiveBugreportDumpsFinished() throws RemoteException { |
| 371 |
final long identity = Binder.clearCallingIdentity(); |
| 372 |
try { |
| 373 |
mExecutor.execute(() -> mCallback.onEarlyReportFinished()); |
| 374 |
} finally { |
| 375 |
Binder.restoreCallingIdentity(identity); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |