am 4228ed68: Merge "Support custom path when saving bugreports." into jb-mr1-dev

* commit '4228ed68c2cb70a2c79620619b4ef01cfcdc8a3d':
  Support custom path when saving bugreports.
diff --git a/bugmailer/bugmailer.sh b/bugmailer/bugmailer.sh
index 93d1c8a..225b18d 100755
--- a/bugmailer/bugmailer.sh
+++ b/bugmailer/bugmailer.sh
@@ -1,5 +1,7 @@
 #!/system/bin/sh
 
+# TODO: restructure this to keep bugreports entirely on internal storage
+
 # Do not allow bugreports on user builds unless USB debugging
 # is enabled.
 if [ "x$(getprop ro.build.type)" = "xuser" -a \
@@ -7,29 +9,44 @@
   exit 0
 fi
 
-timestamp=`date +'%Y-%m-%d-%H-%M-%S'`
-storagePath="$EXTERNAL_STORAGE/bugreports"
-bugreport=$storagePath/bugreport-$timestamp
-screenshotPath="$EXTERNAL_STORAGE/Pictures/Screenshots"
-screenshot=$screenshotPath/Screenshot_$timestamp.png
-
-# check screen shot folder
-if [ ! -e $screenshotPath ]; then
-  mkdir $screenshotPath
+# Use bugreport-specific paths if defined
+if [ -n "$BUGREPORT_WRITE_PATH" ]; then
+  writePath="$BUGREPORT_WRITE_PATH"
+else
+  writePath="$EXTERNAL_STORAGE"
 fi
+if [ -n "$BUGREPORT_READ_PATH" ]; then
+  readPath="$BUGREPORT_READ_PATH"
+else
+  readPath="$EXTERNAL_STORAGE"
+fi
+
+tmpPath="/data/local/tmp"
+bugreportPath="bugreports"
+screenshotPath="Pictures/Screenshots"
+
+# Create directories if needed
+if [ ! -e "$writePath/$bugreportPath" ]; then
+  mkdir "$writePath/$bugreportPath"
+fi
+if [ ! -e "$writePath/$screenshotPath" ]; then
+  mkdir "$writePath/$screenshotPath"
+fi
+
+timestamp=`date +'%Y-%m-%d-%H-%M-%S'`
 
 # take screen shot
 # we run this as a bg job in case screencap is stuck
-/system/bin/screencap -p $screenshot &
+/system/bin/screencap -p "$writePath/$screenshotPath/Screenshot_$timestamp.png" &
 
 # run bugreport
-/system/bin/dumpstate -o $bugreport $@
+/system/bin/dumpstate -o "$tmpPath/bugreport-$timestamp" $@
 
-
-# make files readable
-chown root.sdcard_rw $bugreport.txt
-chown root.sdcard_rw $screenshot
+# copy finished bugreport into place for sending
+cp "$tmpPath/bugreport-$timestamp.txt" "$writePath/$bugreportPath/bugreport-$timestamp.txt"
+# clean up any remaining files
+rm $tmpPath/bugreport*
 
 # invoke send_bug to look up email accounts and fire intents
 # make it convenient to send bugreport to oneself
-/system/bin/send_bug $bugreport.txt $screenshot
+/system/bin/send_bug "$readPath/$bugreportPath/bugreport-$timestamp.txt" "$readPath/$screenshotPath/Screenshot_$timestamp.png"
diff --git a/bugmailer/src/com/android/commands/sendbug/SendBug.java b/bugmailer/src/com/android/commands/sendbug/SendBug.java
index 1b8d669..f6d0db7 100644
--- a/bugmailer/src/com/android/commands/sendbug/SendBug.java
+++ b/bugmailer/src/com/android/commands/sendbug/SendBug.java
@@ -58,32 +58,26 @@
         File screenShot = null;
         if (screenShotPath != null) {
             screenShot = new File(screenShotPath);
-            if (!screenShot.exists()) {
-              // screen shot probably failed
-              screenShot = null;
-            }
         }
-        if (bugreport.exists()) {
-            final Uri bugreportUri = Uri.fromFile(bugreport);
-            // todo (aalbert): investigate adding a screenshot to BugReporter
-            Intent intent = tryBugReporter(bugreportUri);
-            if (intent == null) {
-                final Uri screenshotUri = screenShot != null
-                        ? Uri.fromFile(screenShot) : null;
-                intent = getSendMailIntent(bugreportUri, screenshotUri);
+        final Uri bugreportUri = Uri.fromFile(bugreport);
+        // todo (aalbert): investigate adding a screenshot to BugReporter
+        Intent intent = tryBugReporter(bugreportUri);
+        if (intent == null) {
+            final Uri screenshotUri = screenShot != null
+                    ? Uri.fromFile(screenShot) : null;
+            intent = getSendMailIntent(bugreportUri, screenshotUri);
+        }
+        if (intent != null) {
+            final IActivityManager mAm = ActivityManagerNative.getDefault();
+            try {
+                mAm.startActivity(null, intent, intent.getType(), null, null, 0, 0,
+                        null, null, null);
+            } catch (RemoteException e) {
+                // ignore
             }
-            if (intent != null) {
-                final IActivityManager mAm = ActivityManagerNative.getDefault();
-                try {
-                    mAm.startActivity(null, intent, intent.getType(), null, null, 0, 0,
-                            null, null, null);
-                } catch (RemoteException e) {
-                    // ignore
-                }
-            } else {
-                Log.w(LOG_TAG, "Cannot find account to send bugreport, local path: "
-                        + bugreportPath);
-            }
+        } else {
+            Log.w(LOG_TAG, "Cannot find account to send bugreport, local path: "
+                    + bugreportPath);
         }
     }