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

Commit 1893959c authored by Rhed Jao's avatar Rhed Jao
Browse files

Returns immediately if the bugreport file already exists

There's a case that BugreportProgressService is invoked twice quickly,
and both services create the same bugreport file name. The later one
may delete current running bugreport file in its clean function,
when it detects another bugreport is running.

Bug: 174314124
Test: atest BugreportReceiverTest
Change-Id: I5e1802c5912f4414f1ad3b8bdaf7c7420332b9d6
parent 8ba3c84c
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -618,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();
@@ -1920,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);
        }

+13 −0
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;
@@ -534,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.