Add logic to create screenshot file

* Creating screenshot file for all types of bugreport
* The logic to populate the screenshot file is in dumpstate (few
bugreport types that do not need screenshots will leave the file
untouched/empty)
* Delete the empty screenshot file and set the file to null so that
empty file does not get shared
* This TODO was previously left as binder did not allow to send null
screenshotfd, which was recently fixed. While fixing and testing the
optional screenshot fd CL with these changes, left the value "null" by
default. Fixed now.

* Not adding any logic here on the basis of the type of bugreport as
all that is already in dumpstate

* Making the names of bugreport (.zip file) and bugreport screenshot
(.png file) consistent. In the old flow also both have the same names.

Bug: 123617758
Test: * Build and flash to the device
      * Turn on use bugreport API feature flag from Settings
      * Take interactive bugreport
      Expectation: On clicking the notification to "Share the bugreport"
      only 1 file (the bugreport zip file) gets shared. Also, there is
      no screenshot file in /bugreports/ in the device
      * Take full bugreport
      Expectation: On clicking the notification to "Share the bugreport"
      2 files (bugreport zip and screenshot png file) get shared. Also
      we can see the screenshot file in /bugreports/ in the device

Change-Id: I39cb6913033b6e3c61bd0c6655a76524c12d2428
diff --git a/packages/Shell/src/com/android/shell/BugreportProgressService.java b/packages/Shell/src/com/android/shell/BugreportProgressService.java
index ea87870..7173c98 100644
--- a/packages/Shell/src/com/android/shell/BugreportProgressService.java
+++ b/packages/Shell/src/com/android/shell/BugreportProgressService.java
@@ -50,7 +50,6 @@
 import android.os.BugreportManager.BugreportCallback;
 import android.os.BugreportManager.BugreportCallback.BugreportErrorCode;
 import android.os.BugreportParams;
-import android.os.BugreportParams.BugreportMode;
 import android.os.Bundle;
 import android.os.FileUtils;
 import android.os.Handler;
@@ -354,7 +353,7 @@
         private final int mId;
         private final BugreportInfo mInfo;
 
-        BugreportCallbackImpl(String name, int id, @BugreportMode int bugreportType) {
+        BugreportCallbackImpl(String name, int id) {
             mId = id;
             // pid not used in this workflow, so setting default = 0
             mInfo = new BugreportInfo(mContext, mId, 0 /* pid */, name,
@@ -544,21 +543,21 @@
 
     private void startBugreportAPI(Intent intent) {
         mUsingBugreportApi = true;
-        String currentTimeStamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(
+        String bugreportName = "bugreport-" + new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(
                 new Date());
 
         // TODO(b/126862297): Make file naming same as dumpstate triggered bugreports
         ParcelFileDescriptor bugreportFd = createReadWriteFile(BUGREPORT_DIR,
-                "bugreport-" + currentTimeStamp + ".zip");
+                bugreportName + ".zip");
         if (bugreportFd == null) {
             Log.e(TAG, "Bugreport parcel file descriptor is null.");
             return;
         }
+        int bugreportType = intent.getIntExtra(EXTRA_BUGREPORT_TYPE,
+                BugreportParams.BUGREPORT_MODE_INTERACTIVE);
 
-        // TODO(b/126862297): Screenshot file is not needed for INTERACTIVE_BUGREPORTS
-        // Add logic to pass screenshot file only for specific bugreports.
         ParcelFileDescriptor screenshotFd = createReadWriteFile(BUGREPORT_DIR,
-                "screenshot-" + currentTimeStamp + ".png");
+                bugreportName + ".png");
         if (screenshotFd == null) {
             Log.e(TAG, "Screenshot parcel file descriptor is null.");
             // TODO(b/123617758): Delete bugreport file created above
@@ -572,16 +571,13 @@
         // Dumpstate increments PROPERTY_LAST_ID, may be racy if multiple calls
         // to dumpstate are made simultaneously.
         final int id = SystemProperties.getInt(PROPERTY_LAST_ID, 0) + 1;
-        int bugreportType = intent.getIntExtra(EXTRA_BUGREPORT_TYPE,
-                BugreportParams.BUGREPORT_MODE_INTERACTIVE);
         Log.i(TAG, "bugreport type = " + bugreportType
                 + " bugreport file fd: " + bugreportFd
                 + " screenshot file fd: " + screenshotFd);
 
-        BugreportCallbackImpl bugreportCallback = new BugreportCallbackImpl("bugreport-"
-                + currentTimeStamp, id, bugreportType);
+        BugreportCallbackImpl bugreportCallback = new BugreportCallbackImpl(bugreportName, id);
         try {
-            mBugreportManager.startBugreport(bugreportFd, null,
+            mBugreportManager.startBugreport(bugreportFd, screenshotFd,
                     new BugreportParams(bugreportType), executor, bugreportCallback);
             mBugreportInfos.put(bugreportCallback.mInfo.id, bugreportCallback.mInfo);
         } catch (RuntimeException e) {
@@ -963,14 +959,20 @@
             return;
         }
         final int max = -1; // this is to log metrics for dumpstate duration.
-        final File screenshotFile = new File(BUGREPORT_DIR, info.name + ".png");
-        // TODO(b/126862297): Screenshot file is not needed for INTERACTIVE_BUGREPORTS
-        // Add logic to null check screenshot file only for specific bugreports.
+        File screenshotFile = new File(BUGREPORT_DIR, info.name + ".png");
         if (screenshotFile == null) {
             // Should never happen, an id always has a file linked to it.
             Log.wtf(TAG, "Missing file " + screenshotFile.getPath() + " does not exist.");
             return;
         }
+        // If the screenshot file did not get populated implies this type of bugreport does not
+        // need the screenshot file; setting the file to null so that empty file doesnt get shared
+        if (screenshotFile.length() == 0) {
+            if (screenshotFile.delete()) {
+                Log.d(TAG, "screenshot file deleted successfully.");
+            }
+            screenshotFile = null;
+        }
         onBugreportFinished(id, bugreportFile, screenshotFile, info.title, info.description, max);
     }