summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/Shell/src/com/android/shell/BugreportProgressService.java24
-rw-r--r--packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java35
2 files changed, 47 insertions, 12 deletions
diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java
index d50f08df947a..a2608a0a3057 100644
--- a/packages/Shell/src/com/android/shell/BugreportProgressService.java
+++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java
@@ -380,6 +380,11 @@ public class BugreportProgressService extends Service {
public void onFinished() {
mInfo.renameBugreportFile();
mInfo.renameScreenshots();
+ if (mInfo.bugreportFile.length() == 0) {
+ Log.e(TAG, "Bugreport file empty. File path = " + mInfo.bugreportFile);
+ onError(BUGREPORT_ERROR_RUNTIME);
+ return;
+ }
synchronized (mLock) {
sendBugreportFinishedBroadcastLocked();
mMainThreadHandler.post(() -> mInfoDialog.onBugreportFinished(mInfo));
@@ -408,10 +413,6 @@ public class BugreportProgressService extends Service {
@GuardedBy("mLock")
private void sendBugreportFinishedBroadcastLocked() {
final String bugreportFilePath = mInfo.bugreportFile.getAbsolutePath();
- if (mInfo.bugreportFile.length() == 0) {
- Log.e(TAG, "Bugreport file empty. File path = " + bugreportFilePath);
- return;
- }
if (mInfo.type == BugreportParams.BUGREPORT_MODE_REMOTE) {
sendRemoteBugreportFinishedBroadcast(mContext, bugreportFilePath,
mInfo.bugreportFile);
@@ -617,12 +618,21 @@ public class BugreportProgressService extends Service {
BugreportInfo info = new BugreportInfo(mContext, baseName, name,
shareTitle, shareDescription, bugreportType, mBugreportsDir);
+ synchronized (mLock) {
+ if (info.bugreportFile.exists()) {
+ Log.e(TAG, "Failed to start bugreport generation, the requested bugreport file "
+ + info.bugreportFile + " already exists");
+ return;
+ }
+ info.createBugreportFile();
+ }
ParcelFileDescriptor bugreportFd = info.getBugreportFd();
if (bugreportFd == null) {
Log.e(TAG, "Failed to start bugreport generation as "
+ " bugreport parcel file descriptor is null.");
return;
}
+ info.createScreenshotFile(mBugreportsDir);
ParcelFileDescriptor screenshotFd = null;
if (isDefaultScreenshotRequired(bugreportType, /* hasScreenshotButton= */ !mIsTv)) {
screenshotFd = info.getDefaultScreenshotFd();
@@ -1919,12 +1929,10 @@ public class BugreportProgressService extends Service {
this.shareDescription = shareDescription == null ? "" : shareDescription;
this.type = type;
this.baseName = baseName;
- createBugreportFile(bugreportsDir);
- createScreenshotFile(bugreportsDir);
+ this.bugreportFile = new File(bugreportsDir, getFileName(this, ".zip"));
}
- void createBugreportFile(File bugreportsDir) {
- bugreportFile = new File(bugreportsDir, getFileName(this, ".zip"));
+ void createBugreportFile() {
createReadWriteFile(bugreportFile);
}
diff --git a/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java b/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java
index 3b02e3b46557..947691206741 100644
--- a/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java
+++ b/packages/Shell/tests/src/com/android/shell/BugreportReceiverTest.java
@@ -40,6 +40,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.timeout;
+import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import android.app.ActivityManager;
@@ -513,6 +514,17 @@ public class BugreportReceiverTest {
}
@Test
+ public void testBugreportFinished_withEmptyBugreportFile() throws Exception {
+ sendBugreportStarted();
+
+ IoUtils.closeQuietly(mBugreportFd);
+ mBugreportFd = null;
+ sendBugreportFinished();
+
+ assertServiceNotRunning();
+ }
+
+ @Test
public void testShareBugreportAfterServiceDies() throws Exception {
sendBugreportStarted();
waitForScreenshotButtonEnabled(true);
@@ -523,6 +535,18 @@ public class BugreportReceiverTest {
assertActionSendMultiple(extras);
}
+ @Test
+ public void testBugreportRequestTwice_oneStartBugreportInvoked() throws Exception {
+ sendBugreportStarted();
+ new BugreportRequestedReceiver().onReceive(mContext,
+ new Intent(INTENT_BUGREPORT_REQUESTED));
+ getInstrumentation().waitForIdleSync();
+
+ verify(mMockIDumpstate, times(1)).startBugreport(anyInt(), any(), any(), any(),
+ anyInt(), any(), anyBoolean());
+ sendBugreportFinished();
+ }
+
private void cancelExistingNotifications() {
// Must kill service first, because notifications from a foreground service cannot be
// canceled.
@@ -576,9 +600,10 @@ public class BugreportReceiverTest {
*/
private void sendBugreportStarted() throws Exception {
Intent intent = new Intent(INTENT_BUGREPORT_REQUESTED);
- intent.setPackage("com.android.shell");
- intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
- mContext.sendBroadcast(intent);
+ // Ideally, we should invoke BugreportRequestedReceiver by sending
+ // INTENT_BUGREPORT_REQUESTED. But the intent has been protected broadcast by the system
+ // starting from S.
+ new BugreportRequestedReceiver().onReceive(mContext, intent);
ArgumentCaptor<IDumpstateListener> listenerCap = ArgumentCaptor.forClass(
IDumpstateListener.class);
@@ -646,7 +671,9 @@ public class BugreportReceiverTest {
* Callbacks to service to finish the bugreport.
*/
private void sendBugreportFinished() throws Exception {
- writeZipFile(mBugreportFd, BUGREPORT_FILE, BUGREPORT_CONTENT);
+ if (mBugreportFd != null) {
+ writeZipFile(mBugreportFd, BUGREPORT_FILE, BUGREPORT_CONTENT);
+ }
if (mScreenshotFd != null) {
writeScreenshotFile(mScreenshotFd, SCREENSHOT_CONTENT);
}