From 226940ed8550c02875a987f7e46699e6003ec1c0 Mon Sep 17 00:00:00 2001 From: Michal Karpinski Date: Tue, 15 Dec 2015 18:14:26 +0000 Subject: [PATCH] Modifying Shell to accomodate remote bugreports After receiving android.intent.action.REMOTE_BUGREPORT_FINISHED in newly created RemoteBugreportReceiver, Shell will generate URI to the bugreport zip file and send the broadcast android.intent.action.REMOTE_BUGREPORT_DISPATCH. Bug: 26152603 Change-Id: I058d626e021b488c9347b45467a4e3505134e79c --- core/res/AndroidManifest.xml | 4 +- packages/Shell/AndroidManifest.xml | 8 +++ .../shell/BugreportProgressService.java | 6 +- .../com/android/shell/BugreportReceiver.java | 12 ++-- .../shell/RemoteBugreportReceiver.java | 63 +++++++++++++++++++ 5 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 packages/Shell/src/com/android/shell/RemoteBugreportReceiver.java diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml index 58dfd81ff0dce..7d9fd9300dafd 100644 --- a/core/res/AndroidManifest.xml +++ b/core/res/AndroidManifest.xml @@ -277,8 +277,10 @@ - + + + diff --git a/packages/Shell/AndroidManifest.xml b/packages/Shell/AndroidManifest.xml index c6d9e98774d07..7416fb564e27a 100644 --- a/packages/Shell/AndroidManifest.xml +++ b/packages/Shell/AndroidManifest.xml @@ -151,6 +151,14 @@ + + + + + + diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java index 00a6cbd4da732..5c807e1bd652c 100644 --- a/packages/Shell/src/com/android/shell/BugreportProgressService.java +++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java @@ -116,6 +116,10 @@ public class BugreportProgressService extends Service { // External intents sent by dumpstate. static final String INTENT_BUGREPORT_STARTED = "android.intent.action.BUGREPORT_STARTED"; static final String INTENT_BUGREPORT_FINISHED = "android.intent.action.BUGREPORT_FINISHED"; + static final String INTENT_REMOTE_BUGREPORT_FINISHED = + "android.intent.action.REMOTE_BUGREPORT_FINISHED"; + static final String INTENT_REMOTE_BUGREPORT_DISPATCH = + "android.intent.action.REMOTE_BUGREPORT_DISPATCH"; // Internal intents used on notification actions. static final String INTENT_BUGREPORT_CANCEL = "android.intent.action.BUGREPORT_CANCEL"; @@ -1005,7 +1009,7 @@ public class BugreportProgressService extends Service { return foundAccount; } - private static Uri getUri(Context context, File file) { + static Uri getUri(Context context, File file) { return file != null ? FileProvider.getUriForFile(context, AUTHORITY, file) : null; } diff --git a/packages/Shell/src/com/android/shell/BugreportReceiver.java b/packages/Shell/src/com/android/shell/BugreportReceiver.java index b818343b6ce03..c8898b97d8785 100644 --- a/packages/Shell/src/com/android/shell/BugreportReceiver.java +++ b/packages/Shell/src/com/android/shell/BugreportReceiver.java @@ -52,7 +52,7 @@ public class BugreportReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Clean up older bugreports in background - cleanupOldFiles(intent); + cleanupOldFiles(this, intent, INTENT_BUGREPORT_FINISHED, MIN_KEEP_COUNT, MIN_KEEP_AGE); // Delegate intent handling to service. Intent serviceIntent = new Intent(context, BugreportProgressService.class); @@ -60,8 +60,9 @@ public class BugreportReceiver extends BroadcastReceiver { context.startService(serviceIntent); } - private void cleanupOldFiles(Intent intent) { - if (!INTENT_BUGREPORT_FINISHED.equals(intent.getAction())) { + static void cleanupOldFiles(BroadcastReceiver br, Intent intent, String expectedAction, + final int minCount, final long minAge) { + if (!expectedAction.equals(intent.getAction())) { return; } final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT); @@ -69,12 +70,11 @@ public class BugreportReceiver extends BroadcastReceiver { Log.e(TAG, "Not deleting old files because file " + bugreportFile + " doesn't exist"); return; } - final PendingResult result = goAsync(); + final PendingResult result = br.goAsync(); new AsyncTask() { @Override protected Void doInBackground(Void... params) { - FileUtils.deleteOlderFiles( - bugreportFile.getParentFile(), MIN_KEEP_COUNT, MIN_KEEP_AGE); + FileUtils.deleteOlderFiles(bugreportFile.getParentFile(), minCount, minAge); result.finish(); return null; } diff --git a/packages/Shell/src/com/android/shell/RemoteBugreportReceiver.java b/packages/Shell/src/com/android/shell/RemoteBugreportReceiver.java new file mode 100644 index 0000000000000..6f783a1fbabfe --- /dev/null +++ b/packages/Shell/src/com/android/shell/RemoteBugreportReceiver.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.shell; + +import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT; +import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_FINISHED; +import static com.android.shell.BugreportProgressService.INTENT_REMOTE_BUGREPORT_DISPATCH; +import static com.android.shell.BugreportProgressService.getFileExtra; +import static com.android.shell.BugreportProgressService.getUri; +import static com.android.shell.BugreportReceiver.cleanupOldFiles; + +import java.io.File; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; +import android.os.UserHandle; + +/** + * Receiver that handles finished remote bugreports, by re-sending + * the intent with appended bugreport zip file URI. + * + *

Remote bugreport never contains a screenshot. + */ +public class RemoteBugreportReceiver extends BroadcastReceiver { + + private static final String BUGREPORT_MIMETYPE = "application/vnd.android.bugreport"; + private static final String EXTRA_REMOTE_BUGREPORT_HASH = + "android.intent.extra.REMOTE_BUGREPORT_HASH"; + + /** Always keep just the last remote bugreport zip file */ + private static final int MIN_KEEP_COUNT = 1; + + @Override + public void onReceive(Context context, Intent intent) { + cleanupOldFiles(this, intent, INTENT_REMOTE_BUGREPORT_FINISHED, MIN_KEEP_COUNT, 0); + + final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT); + final Uri bugreportUri = getUri(context, bugreportFile); + final String bugreportHash = intent.getStringExtra(EXTRA_REMOTE_BUGREPORT_HASH); + + final Intent newIntent = new Intent(INTENT_REMOTE_BUGREPORT_DISPATCH); + newIntent.setDataAndType(bugreportUri, BUGREPORT_MIMETYPE); + newIntent.putExtra(EXTRA_REMOTE_BUGREPORT_HASH, bugreportHash); + context.sendBroadcastAsUser(newIntent, UserHandle.SYSTEM, + android.Manifest.permission.DUMP); + } +}