diff options
author | Alon Albert <aalbert@google.com> | 2011-09-13 11:11:47 -0700 |
---|---|---|
committer | Alon Albert <aalbert@google.com> | 2011-09-13 11:11:47 -0700 |
commit | 01cef7d875c77ad5efed9e73d0e3d60a258f79de (patch) | |
tree | 6ceedc92bc0f29717b576d0fd54a445d79b095b3 | |
parent | d4cd3249e37e5689fc4a8c2858351ec9bebb467c (diff) | |
download | extras-01cef7d875c77ad5efed9e73d0e3d60a258f79de.tar.gz |
Add screenshot to bugreport email if available
Only works for email bugreport for now. Might add BugReported support later - requiers either
changes to BugReporter tool or zipping the file with is not great.
Change-Id: I1941a8436f6b5ba31b04334a89cad075f3bc836e
-rw-r--r-- | bugmailer/src/com/android/commands/sendbug/SendBug.java | 112 |
1 files changed, 98 insertions, 14 deletions
diff --git a/bugmailer/src/com/android/commands/sendbug/SendBug.java b/bugmailer/src/com/android/commands/sendbug/SendBug.java index 9ae2da85..8e0fd89a 100644 --- a/bugmailer/src/com/android/commands/sendbug/SendBug.java +++ b/bugmailer/src/com/android/commands/sendbug/SendBug.java @@ -24,12 +24,20 @@ import android.content.Intent; import android.content.pm.IPackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; +import android.os.Environment; import android.os.RemoteException; import android.os.ServiceManager; import android.os.SystemProperties; import java.io.File; +import java.io.FilenameFilter; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class SendBug { @@ -37,6 +45,12 @@ public class SendBug { private static final String EMAIL_ACCOUNT_TYPE = "com.android.email"; private static final String SEND_BUG_INTENT_ACTION = "android.testing.SEND_BUG"; + private static final Pattern datePattern = Pattern.compile( + ".*(\\d\\d\\d\\d[-_.]\\d\\d[-_.]\\d\\d[-_.]\\d\\d[-_.]\\d\\d[-_.]\\d\\d).*"); + private static final File screenshotDir = new File( + Environment.getExternalStorageDirectory() + "/Pictures/Screenshots"); + private static final long MAX_SCREENSHOT_AGE_MS = 5 * 50 * 1000; + public static void main(String[] args) { if (args.length >= 1) { new SendBug().run(args[0]); @@ -44,36 +58,41 @@ public class SendBug { } private void run(String bugreportPath) { - File bugreport = new File(bugreportPath); + final File bugreport = new File(bugreportPath); if (bugreport.exists()) { - Uri bugreportUri = Uri.fromFile(bugreport); + final Uri bugreportUri = Uri.fromFile(bugreport); + // todo (aalbert): investigate adding a screenshot to BugReporter Intent intent = tryBugReporter(bugreportUri); if (intent == null) { - intent = getSendMailIntent(bugreportUri); + final File screenshotFile = findScreenshotFile(bugreportPath); + final Uri screenshotUri = screenshotFile != null + ? Uri.fromFile(screenshotFile) : null; + intent = getSendMailIntent(bugreportUri, screenshotUri); } - IActivityManager mAm = ActivityManagerNative.getDefault(); + final IActivityManager mAm = ActivityManagerNative.getDefault(); try { mAm.startActivity(null, intent, intent.getType(), null, 0, null, null, 0, false, false, null, null, false); } catch (RemoteException e) { + // ignore } } } private Intent tryBugReporter(Uri bugreportUri) { - Intent intent = new Intent(SEND_BUG_INTENT_ACTION); + final Intent intent = new Intent(SEND_BUG_INTENT_ACTION); intent.setData(bugreportUri); - IPackageManager mPm = IPackageManager.Stub.asInterface( + final IPackageManager mPm = IPackageManager.Stub.asInterface( ServiceManager.getService("package")); if (mPm != null) { - List<ResolveInfo> results = null; + final List<ResolveInfo> results; try { results = mPm.queryIntentActivities(intent, null, 0); } catch (RemoteException e) { return null; } if (results != null && results.size() > 0) { - ResolveInfo info = results.get(0); + final ResolveInfo info = results.get(0); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClassName(info.activityInfo.applicationInfo.packageName, info.activityInfo.name); @@ -85,17 +104,25 @@ public class SendBug { return null; } - private Intent getSendMailIntent(Uri bugreportUri) { - Account sendToAccount = findSendToAccount(); - Intent intent = new Intent(Intent.ACTION_SEND); + private Intent getSendMailIntent(Uri bugreportUri, Uri screenshotUri) { + final Account sendToAccount = findSendToAccount(); + final Intent intent = new Intent(Intent.ACTION_SEND); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setType("application/octet-stream"); intent.putExtra("subject", bugreportUri.getLastPathSegment()); - StringBuilder sb = new StringBuilder(); + final StringBuilder sb = new StringBuilder(); sb.append(SystemProperties.get("ro.build.description")); sb.append("\n(Sent from BugMailer)"); intent.putExtra("body", sb.toString()); - intent.putExtra(Intent.EXTRA_STREAM, bugreportUri); + if (screenshotUri != null) { + final ArrayList<Uri> attachments = new ArrayList<Uri>(); + attachments.add(bugreportUri); + attachments.add(screenshotUri); + intent.setAction(Intent.ACTION_SEND_MULTIPLE); + intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments); + } else { + intent.putExtra(Intent.EXTRA_STREAM, bugreportUri); + } if (sendToAccount != null) { intent.putExtra("to", sendToAccount.name); } @@ -103,13 +130,14 @@ public class SendBug { } private Account findSendToAccount() { - IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager + final IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager .getService(Context.ACCOUNT_SERVICE)); Account[] accounts = null; Account foundAccount = null; try { accounts = accountManager.getAccounts(null); } catch (RemoteException e) { + // ignore } if (accounts != null) { for (Account account : accounts) { @@ -125,4 +153,60 @@ public class SendBug { } return foundAccount; } + + // Try to find a screenshot that was taken shortly before this bugreport was. + private File findScreenshotFile(String bugreportPath) { + final Date bugreportDate = getDate(bugreportPath); + if (bugreportDate == null) { + return null; + } + + final String[] screenshotFiles = screenshotDir.list( + new FilenameFilter() { + private final Pattern pattern = Pattern.compile("[Ss]creenshot.*\\.png"); + + public boolean accept(File dir, String filename) { + return pattern.matcher(filename).matches(); + } + }); + long minDiff = Long.MAX_VALUE; + String bestMatch = null; + for (String screenshotFile : screenshotFiles) { + final Date date = getDate(screenshotFile); + if (date == null) { + continue; + } + final long diff = bugreportDate.getTime() - date.getTime(); + if (diff < minDiff) { + minDiff = diff; + bestMatch = screenshotFile; + } + } + + if (minDiff < MAX_SCREENSHOT_AGE_MS) { + return new File(screenshotDir, bestMatch); + } + + return null; + } + + private static Date getDate(final String string) { + final Matcher matcher = datePattern.matcher(string); + if (!matcher.matches()) { + return null; + } + final String dateString = matcher.group(1); + final char sep1 = dateString.charAt(4); + final char sep2 = dateString.charAt(7); + final char sep3 = dateString.charAt(10); + final char sep4 = dateString.charAt(13); + final char sep5 = dateString.charAt(16); + final SimpleDateFormat format = new SimpleDateFormat( + "yyyy" + sep1 + "MM" + sep2 + "dd" + sep3 + "HH" + sep4 + "mm" + sep5 + "ss"); + try { + return format.parse(dateString); + } catch (ParseException e) { + return null; + } + } } |