OSDN Git Service

BugReporter integration
[android-x86/system-extras.git] / bugmailer / src / com / android / commands / sendbug / SendBug.java
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.commands.sendbug;
17
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;
30
31 import java.io.File;
32 import java.util.List;
33
34 public class SendBug {
35
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";
39
40     public static void main(String[] args) {
41         if (args.length >= 1) {
42             new SendBug().run(args[0]);
43         }
44     }
45
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);
51             if (intent == null) {
52                 intent = getSendMailIntent(bugreportUri);
53             }
54             IActivityManager mAm = ActivityManagerNative.getDefault();
55             try {
56                 mAm.startActivity(null, intent, intent.getType(), null, 0, null, null, 0, false,
57                         false, null, null, false);
58             } catch (RemoteException e) {
59             }
60         }
61     }
62
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"));
68         if (mPm != null) {
69             List<ResolveInfo> results = null;
70             try {
71                 results = mPm.queryIntentActivities(intent, null, 0);
72             } catch (RemoteException e) {
73                 return null;
74             }
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);
80                 return intent;
81             } else {
82                 return null;
83             }
84         }
85         return null;
86     }
87
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);
101         }
102         return intent;
103     }
104
105     private Account findSendToAccount() {
106         IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager
107                 .getService(Context.ACCOUNT_SERVICE));
108         Account[] accounts = null;
109         Account foundAccount = null;
110         try {
111             accounts = accountManager.getAccounts(null);
112         } catch (RemoteException e) {
113         }
114         if (accounts != null) {
115             for (Account account : accounts) {
116                 if (GOOGLE_ACCOUNT_TYPE.equals(account.type)) {
117                     // return first gmail account found
118                     return account;
119                 } else if (EMAIL_ACCOUNT_TYPE.equals(account.type)) {
120                     // keep regular email account for now in case there are gmail accounts
121                     // found later
122                     foundAccount = account;
123                 }
124             }
125         }
126         return foundAccount;
127     }
128 }