1 /*
2  * Copyright (C) 2015 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 
17 package com.android.traceur;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.app.Notification;
22 import android.app.NotificationManager;
23 import android.app.PendingIntent;
24 import android.content.ClipData;
25 import android.content.Context;
26 import androidx.core.content.FileProvider;
27 import android.content.Intent;
28 import android.content.pm.PackageManager;
29 import android.net.Uri;
30 import android.os.Build;
31 import android.os.SystemProperties;
32 import android.util.Patterns;
33 
34 import java.io.File;
35 
36 /**
37  * Sends bugreport-y files, adapted from fw/base/packages/Shell's BugreportReceiver.
38  */
39 public class FileSender {
40 
41     private static final String AUTHORITY = "com.android.traceur.files";
42     private static final String MIME_TYPE = "application/vnd.android.systrace";
43 
postNotification(Context context, File file)44     public static void postNotification(Context context, File file) {
45         // Files are kept on private storage, so turn into Uris that we can
46         // grant temporary permissions for.
47         final Uri traceUri = getUriForFile(context, file);
48 
49         // Intent to send the file
50         Intent sendIntent = buildSendIntent(context, traceUri);
51         sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
52 
53         // This dialog will show to warn the user about sharing traces, then will execute
54         // the above file-sharing intent.
55         final Intent intent = new Intent(context, UserConsentActivityDialog.class);
56         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_RECEIVER_FOREGROUND);
57         intent.putExtra(Intent.EXTRA_INTENT, sendIntent);
58 
59         final Notification.Builder builder =
60             new Notification.Builder(context, Receiver.NOTIFICATION_CHANNEL_OTHER)
61                 .setSmallIcon(R.drawable.bugfood_icon)
62                 .setContentTitle(context.getString(R.string.trace_saved))
63                 .setTicker(context.getString(R.string.trace_saved))
64                 .setContentText(context.getString(R.string.tap_to_share))
65                 .setContentIntent(PendingIntent.getActivity(
66                         context, traceUri.hashCode(), intent, PendingIntent.FLAG_ONE_SHOT
67                                 | PendingIntent.FLAG_CANCEL_CURRENT
68                                 | PendingIntent.FLAG_IMMUTABLE))
69                 .setAutoCancel(true)
70                 .setLocalOnly(true)
71                 .setColor(context.getColor(
72                         com.android.internal.R.color.system_notification_accent_color));
73 
74         if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
75             builder.extend(new Notification.TvExtender());
76         }
77 
78         NotificationManager.from(context).notify(file.getName(), 0, builder.build());
79     }
80 
send(Context context, File file)81     public static void send(Context context, File file) {
82         // Files are kept on private storage, so turn into Uris that we can
83         // grant temporary permissions for.
84         final Uri traceUri = getUriForFile(context, file);
85 
86         Intent sendIntent = buildSendIntent(context, traceUri);
87         sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
88 
89         context.startActivity(sendIntent);
90     }
91 
getUriForFile(Context context, File file)92     private static Uri getUriForFile(Context context, File file) {
93         return FileProvider.getUriForFile(context, AUTHORITY, file);
94     }
95 
96     /**
97      * Build {@link Intent} that can be used to share the given bugreport.
98      */
buildSendIntent(Context context, Uri traceUri)99     private static Intent buildSendIntent(Context context, Uri traceUri) {
100         final CharSequence description = Build.FINGERPRINT;
101 
102         final Intent intent = new Intent(Intent.ACTION_SEND);
103         intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
104         intent.addCategory(Intent.CATEGORY_DEFAULT);
105         intent.setType(MIME_TYPE);
106 
107         intent.putExtra(Intent.EXTRA_SUBJECT, traceUri.getLastPathSegment());
108         intent.putExtra(Intent.EXTRA_TEXT, description);
109         intent.putExtra(Intent.EXTRA_STREAM, traceUri);
110 
111         // Explicitly set the clip data; see b/119399115
112         intent.setClipData(new ClipData(null, new String[] { MIME_TYPE },
113             new ClipData.Item(description, null, traceUri)));
114 
115         final Account sendToAccount = findSendToAccount(context);
116         if (sendToAccount != null) {
117             intent.putExtra(Intent.EXTRA_EMAIL, new String[] { sendToAccount.name });
118         }
119 
120         return intent;
121     }
122 
123     /**
124      * Find the best matching {@link Account} based on build properties.
125      */
findSendToAccount(Context context)126     private static Account findSendToAccount(Context context) {
127         final AccountManager am = (AccountManager) context.getSystemService(
128                 Context.ACCOUNT_SERVICE);
129 
130         String preferredDomain = SystemProperties.get("sendbug.preferred.domain");
131         if (!preferredDomain.startsWith("@")) {
132             preferredDomain = "@" + preferredDomain;
133         }
134 
135         final Account[] accounts = am.getAccounts();
136         Account foundAccount = null;
137         for (Account account : accounts) {
138             if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) {
139                 if (!preferredDomain.isEmpty()) {
140                     // if we have a preferred domain and it matches, return; otherwise keep
141                     // looking
142                     if (account.name.endsWith(preferredDomain)) {
143                         return account;
144                     } else {
145                         foundAccount = account;
146                     }
147                     // if we don't have a preferred domain, just return since it looks like
148                     // an email address
149                 } else {
150                     return account;
151                 }
152             }
153         }
154         return foundAccount;
155     }
156 }
157