SendBug.java revision 9ebc639a313d143ade1293cb5932b446d5e623d5
18c658e058446ad69fb056b2160340d708582b9eeGuang Zhu/* 28c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * Copyright (C) 2011 The Android Open Source Project 38c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * 48c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * Licensed under the Apache License, Version 2.0 (the "License"); 58c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * you may not use this file except in compliance with the License. 68c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * You may obtain a copy of the License at 78c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * 88c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * http://www.apache.org/licenses/LICENSE-2.0 98c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * 108c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * Unless required by applicable law or agreed to in writing, software 118c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * distributed under the License is distributed on an "AS IS" BASIS, 128c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * See the License for the specific language governing permissions and 148c658e058446ad69fb056b2160340d708582b9eeGuang Zhu * limitations under the License. 158c658e058446ad69fb056b2160340d708582b9eeGuang Zhu */ 168c658e058446ad69fb056b2160340d708582b9eeGuang Zhupackage com.android.commands.sendbug; 178c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 188c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.accounts.Account; 198c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.accounts.IAccountManager; 208c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.app.ActivityManagerNative; 218c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.app.IActivityManager; 228c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.content.Context; 238c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.content.Intent; 24d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhuimport android.content.pm.IPackageManager; 25d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhuimport android.content.pm.ResolveInfo; 268c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.net.Uri; 278c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.os.RemoteException; 288c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.os.ServiceManager; 298c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport android.os.SystemProperties; 309ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhuimport android.util.Log; 318c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 328c658e058446ad69fb056b2160340d708582b9eeGuang Zhuimport java.io.File; 3301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albertimport java.util.ArrayList; 34d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhuimport java.util.List; 359ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhuimport java.util.regex.Pattern; 368c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 378c658e058446ad69fb056b2160340d708582b9eeGuang Zhupublic class SendBug { 388c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 399ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu private static final String LOG_TAG = SendBug.class.getSimpleName(); 409ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu private static final Pattern EMAIL_REGEX = Pattern.compile( 419ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu "^[\\w.%+-]+@[\\w.-]+\\.[a-zA-Z]{2,}$"); 42d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu private static final String SEND_BUG_INTENT_ACTION = "android.testing.SEND_BUG"; 438c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 448c658e058446ad69fb056b2160340d708582b9eeGuang Zhu public static void main(String[] args) { 458beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu if (args.length == 1) { 468c658e058446ad69fb056b2160340d708582b9eeGuang Zhu new SendBug().run(args[0]); 478beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu } else if (args.length == 2) { 488beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu new SendBug().run(args[0], args[1]); 498c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 508c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 518c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 528c658e058446ad69fb056b2160340d708582b9eeGuang Zhu private void run(String bugreportPath) { 538beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu run(bugreportPath, null); 548beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu } 558beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu 568beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu private void run(String bugreportPath, String screenShotPath) { 5701cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final File bugreport = new File(bugreportPath); 588beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu File screenShot = null; 598beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu if (screenShotPath != null) { 608beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu screenShot = new File(screenShotPath); 618beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu if (!screenShot.exists()) { 628beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu // screen shot probably failed 638beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu screenShot = null; 648beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu } 658beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu } 668c658e058446ad69fb056b2160340d708582b9eeGuang Zhu if (bugreport.exists()) { 6701cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final Uri bugreportUri = Uri.fromFile(bugreport); 6801cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert // todo (aalbert): investigate adding a screenshot to BugReporter 69d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu Intent intent = tryBugReporter(bugreportUri); 70d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu if (intent == null) { 718beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu final Uri screenshotUri = screenShot != null 728beb39758fe046fc19ebee7317949b9ba7ee97ddGuang Zhu ? Uri.fromFile(screenShot) : null; 7301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert intent = getSendMailIntent(bugreportUri, screenshotUri); 748c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 759ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu if (intent != null) { 769ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu final IActivityManager mAm = ActivityManagerNative.getDefault(); 779ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu try { 789ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu mAm.startActivity(null, intent, intent.getType(), null, null, 0, 0, 799ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu null, null, null); 809ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } catch (RemoteException e) { 819ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu // ignore 829ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } 839ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } else { 849ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu Log.w(LOG_TAG, "Cannot find account to send bugreport, local path: " 859ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu + bugreportPath); 868c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 878c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 888c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 898c658e058446ad69fb056b2160340d708582b9eeGuang Zhu 90d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu private Intent tryBugReporter(Uri bugreportUri) { 9101cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final Intent intent = new Intent(SEND_BUG_INTENT_ACTION); 92d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu intent.setData(bugreportUri); 9301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final IPackageManager mPm = IPackageManager.Stub.asInterface( 94d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu ServiceManager.getService("package")); 95d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu if (mPm != null) { 9601cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final List<ResolveInfo> results; 97d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu try { 98f591abb39f0634094909af6684a874341fb4f0a4Amith Yamasani results = mPm.queryIntentActivities(intent, null, 0, 0); 99d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } catch (RemoteException e) { 100d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu return null; 101d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 102d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu if (results != null && results.size() > 0) { 10301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final ResolveInfo info = results.get(0); 104d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 105d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu intent.setClassName(info.activityInfo.applicationInfo.packageName, 106d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu info.activityInfo.name); 107d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu return intent; 108d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } else { 109d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu return null; 110d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 111d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 112d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu return null; 113d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 114d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu 11501cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert private Intent getSendMailIntent(Uri bugreportUri, Uri screenshotUri) { 11601cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final Account sendToAccount = findSendToAccount(); 11701cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final Intent intent = new Intent(Intent.ACTION_SEND); 118d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 119d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu intent.setType("application/octet-stream"); 1209ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu intent.putExtra(Intent.EXTRA_SUBJECT, bugreportUri.getLastPathSegment()); 12101cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final StringBuilder sb = new StringBuilder(); 122d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu sb.append(SystemProperties.get("ro.build.description")); 123d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu sb.append("\n(Sent from BugMailer)"); 1249ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu intent.putExtra(Intent.EXTRA_TEXT, (CharSequence)sb); 12501cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert if (screenshotUri != null) { 12601cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final ArrayList<Uri> attachments = new ArrayList<Uri>(); 12701cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert attachments.add(bugreportUri); 12801cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert attachments.add(screenshotUri); 12901cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert intent.setAction(Intent.ACTION_SEND_MULTIPLE); 13001cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments); 13101cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert } else { 13201cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert intent.putExtra(Intent.EXTRA_STREAM, bugreportUri); 13301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert } 134d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu if (sendToAccount != null) { 1359ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu intent.putExtra(Intent.EXTRA_EMAIL, new String[]{sendToAccount.name}); 1369ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu return intent; 137d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 1389ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu return null; 139d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu } 140d4cd3249e37e5689fc4a8c2858351ec9bebb467cGuang Zhu 1418c658e058446ad69fb056b2160340d708582b9eeGuang Zhu private Account findSendToAccount() { 14201cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert final IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager 1438c658e058446ad69fb056b2160340d708582b9eeGuang Zhu .getService(Context.ACCOUNT_SERVICE)); 1448c658e058446ad69fb056b2160340d708582b9eeGuang Zhu Account[] accounts = null; 1458c658e058446ad69fb056b2160340d708582b9eeGuang Zhu Account foundAccount = null; 1469ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu String preferredDomain = SystemProperties.get("sendbug.preferred.domain"); 1479ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu if (!preferredDomain.startsWith("@")) { 1489ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu preferredDomain = "@" + preferredDomain; 1499ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } 1508c658e058446ad69fb056b2160340d708582b9eeGuang Zhu try { 1518c658e058446ad69fb056b2160340d708582b9eeGuang Zhu accounts = accountManager.getAccounts(null); 1528c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } catch (RemoteException e) { 15301cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert // ignore 1548c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 1558c658e058446ad69fb056b2160340d708582b9eeGuang Zhu if (accounts != null) { 1568c658e058446ad69fb056b2160340d708582b9eeGuang Zhu for (Account account : accounts) { 1579ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu if (EMAIL_REGEX.matcher(account.name).matches()) { 1589ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu if (!preferredDomain.isEmpty()) { 1599ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu // if we have a preferred domain and it matches, return; otherwise keep 1609ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu // looking 1619ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu if (account.name.endsWith(preferredDomain)) { 1629ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu return account; 1639ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } else { 1649ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu foundAccount = account; 1659ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } 1669ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu // if we don't have a preferred domain, just return since it looks like 1679ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu // an email address 1689ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } else { 1699ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu return account; 1709ebc639a313d143ade1293cb5932b446d5e623d5Guang Zhu } 1718c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 1728c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 1738c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 1748c658e058446ad69fb056b2160340d708582b9eeGuang Zhu return foundAccount; 1758c658e058446ad69fb056b2160340d708582b9eeGuang Zhu } 17601cef7d875c77ad5efed9e73d0e3d60a258f79deAlon Albert 1778c658e058446ad69fb056b2160340d708582b9eeGuang Zhu} 178