2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.commands.sendbug;
18 import android.accounts.Account;
19 import android.accounts.IAccountManager;
20 import android.app.ActivityManagerNative;
21 import android.app.IActivityManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.IPackageManager;
25 import android.content.pm.ResolveInfo;
26 import android.net.Uri;
27 import android.os.RemoteException;
28 import android.os.ServiceManager;
29 import android.os.SystemProperties;
32 import java.util.List;
34 public class SendBug {
36 private static final String GOOGLE_ACCOUNT_TYPE = "com.google";
37 private static final String EMAIL_ACCOUNT_TYPE = "com.android.email";
38 private static final String SEND_BUG_INTENT_ACTION = "android.testing.SEND_BUG";
40 public static void main(String[] args) {
41 if (args.length >= 1) {
42 new SendBug().run(args[0]);
46 private void run(String bugreportPath) {
47 File bugreport = new File(bugreportPath);
48 if (bugreport.exists()) {
49 Uri bugreportUri = Uri.fromFile(bugreport);
50 Intent intent = tryBugReporter(bugreportUri);
52 intent = getSendMailIntent(bugreportUri);
54 IActivityManager mAm = ActivityManagerNative.getDefault();
56 mAm.startActivity(null, intent, intent.getType(), null, 0, null, null, 0, false,
57 false, null, null, false);
58 } catch (RemoteException e) {
63 private Intent tryBugReporter(Uri bugreportUri) {
64 Intent intent = new Intent(SEND_BUG_INTENT_ACTION);
65 intent.setData(bugreportUri);
66 IPackageManager mPm = IPackageManager.Stub.asInterface(
67 ServiceManager.getService("package"));
69 List<ResolveInfo> results = null;
71 results = mPm.queryIntentActivities(intent, null, 0);
72 } catch (RemoteException e) {
75 if (results != null && results.size() > 0) {
76 ResolveInfo info = results.get(0);
77 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
78 intent.setClassName(info.activityInfo.applicationInfo.packageName,
79 info.activityInfo.name);
88 private Intent getSendMailIntent(Uri bugreportUri) {
89 Account sendToAccount = findSendToAccount();
90 Intent intent = new Intent(Intent.ACTION_SEND);
91 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
92 intent.setType("application/octet-stream");
93 intent.putExtra("subject", bugreportUri.getLastPathSegment());
94 StringBuilder sb = new StringBuilder();
95 sb.append(SystemProperties.get("ro.build.description"));
96 sb.append("\n(Sent from BugMailer)");
97 intent.putExtra("body", sb.toString());
98 intent.putExtra(Intent.EXTRA_STREAM, bugreportUri);
99 if (sendToAccount != null) {
100 intent.putExtra("to", sendToAccount.name);
105 private Account findSendToAccount() {
106 IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager
107 .getService(Context.ACCOUNT_SERVICE));
108 Account[] accounts = null;
109 Account foundAccount = null;
111 accounts = accountManager.getAccounts(null);
112 } catch (RemoteException e) {
114 if (accounts != null) {
115 for (Account account : accounts) {
116 if (GOOGLE_ACCOUNT_TYPE.equals(account.type)) {
117 // return first gmail account found
119 } else if (EMAIL_ACCOUNT_TYPE.equals(account.type)) {
120 // keep regular email account for now in case there are gmail accounts
122 foundAccount = account;