SendBug.java revision 8c658e058446ad69fb056b2160340d708582b9ee
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 */
16package com.android.commands.sendbug;
17
18import android.accounts.Account;
19import android.accounts.IAccountManager;
20import android.app.ActivityManagerNative;
21import android.app.IActivityManager;
22import android.content.Context;
23import android.content.Intent;
24import android.net.Uri;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.os.SystemProperties;
28
29import java.io.File;
30
31public class SendBug {
32
33    private static final String GOOGLE_ACCOUNT_TYPE = "com.google";
34    private static final String EMAIL_ACCOUNT_TYPE = "com.android.email";
35
36    public static void main(String[] args) {
37        if (args.length >= 1) {
38            new SendBug().run(args[0]);
39        }
40    }
41
42    private void run(String bugreportPath) {
43        File bugreport = new File(bugreportPath);
44        if (bugreport.exists()) {
45            Account sendToAccount = findSendToAccount();
46            Intent intent = new Intent(Intent.ACTION_SEND);
47            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
48            intent.setType("application/octet-stream");
49            intent.putExtra("subject", bugreport.getName());
50            StringBuilder sb = new StringBuilder();
51            sb.append(SystemProperties.get("ro.build.description"));
52            sb.append("\n(Sent from BugMailer)");
53            intent.putExtra("body", sb.toString());
54            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(bugreport));
55            if (sendToAccount != null) {
56                intent.putExtra("to", sendToAccount.name);
57            }
58            IActivityManager mAm = ActivityManagerNative.getDefault();
59            try {
60                mAm.startActivity(null, intent, intent.getType(), null, 0, null, null, 0, false,
61                        false);
62            } catch (RemoteException e) {
63            }
64        }
65    }
66
67    private Account findSendToAccount() {
68        IAccountManager accountManager = IAccountManager.Stub.asInterface(ServiceManager
69                .getService(Context.ACCOUNT_SERVICE));
70        Account[] accounts = null;
71        Account foundAccount = null;
72        try {
73            accounts = accountManager.getAccounts(null);
74        } catch (RemoteException e) {
75        }
76        if (accounts != null) {
77            for (Account account : accounts) {
78                if (GOOGLE_ACCOUNT_TYPE.equals(account.type)) {
79                    // return first gmail account found
80                    return account;
81                } else if (EMAIL_ACCOUNT_TYPE.equals(account.type)) {
82                    // keep regular email account for now in case there are gmail accounts
83                    // found later
84                    foundAccount = account;
85                }
86            }
87        }
88        return foundAccount;
89    }
90}
91