Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit efd5b065 authored by Rhed Jao's avatar Rhed Jao Committed by Android (Google) Code Review
Browse files

Merge changes I5e1802c5,I4542568f,I99a052d7

* changes:
  Returns immediately if the bugreport file already exists
  Fixes an error handling in BugreportProgressService
  Fixes BugreportReceiverTest failed
parents 4528031e 1893959c
Loading
Loading
Loading
Loading
+16 −8
Original line number Diff line number Diff line
@@ -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);
        }

+31 −4
Original line number Diff line number Diff line
@@ -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;
@@ -512,6 +513,17 @@ public class BugreportReceiverTest {
        assertEquals("Didn't change state", STATE_HIDE, newState);
    }

    @Test
    public void testBugreportFinished_withEmptyBugreportFile() throws Exception {
        sendBugreportStarted();

        IoUtils.closeQuietly(mBugreportFd);
        mBugreportFd = null;
        sendBugreportFinished();

        assertServiceNotRunning();
    }

    @Test
    public void testShareBugreportAfterServiceDies() throws Exception {
        sendBugreportStarted();
@@ -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 {
        if (mBugreportFd != null) {
            writeZipFile(mBugreportFd, BUGREPORT_FILE, BUGREPORT_CONTENT);
        }
        if (mScreenshotFd != null) {
            writeScreenshotFile(mScreenshotFd, SCREENSHOT_CONTENT);
        }