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 .setAutoCancel(true) 69 .setLocalOnly(true) 70 .setColor(context.getColor( 71 com.android.internal.R.color.system_notification_accent_color)); 72 73 if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) { 74 builder.extend(new Notification.TvExtender()); 75 } 76 77 NotificationManager.from(context).notify(file.getName(), 0, builder.build()); 78 } 79 send(Context context, File file)80 public static void send(Context context, File file) { 81 // Files are kept on private storage, so turn into Uris that we can 82 // grant temporary permissions for. 83 final Uri traceUri = getUriForFile(context, file); 84 85 Intent sendIntent = buildSendIntent(context, traceUri); 86 sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 87 88 context.startActivity(sendIntent); 89 } 90 getUriForFile(Context context, File file)91 private static Uri getUriForFile(Context context, File file) { 92 return FileProvider.getUriForFile(context, AUTHORITY, file); 93 } 94 95 /** 96 * Build {@link Intent} that can be used to share the given bugreport. 97 */ buildSendIntent(Context context, Uri traceUri)98 private static Intent buildSendIntent(Context context, Uri traceUri) { 99 final CharSequence description = Build.FINGERPRINT; 100 101 final Intent intent = new Intent(Intent.ACTION_SEND); 102 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 103 intent.addCategory(Intent.CATEGORY_DEFAULT); 104 intent.setType(MIME_TYPE); 105 106 intent.putExtra(Intent.EXTRA_SUBJECT, traceUri.getLastPathSegment()); 107 intent.putExtra(Intent.EXTRA_TEXT, description); 108 intent.putExtra(Intent.EXTRA_STREAM, traceUri); 109 110 // Explicitly set the clip data; see b/119399115 111 intent.setClipData(new ClipData(null, new String[] { MIME_TYPE }, 112 new ClipData.Item(description, null, traceUri))); 113 114 final Account sendToAccount = findSendToAccount(context); 115 if (sendToAccount != null) { 116 intent.putExtra(Intent.EXTRA_EMAIL, new String[] { sendToAccount.name }); 117 } 118 119 return intent; 120 } 121 122 /** 123 * Find the best matching {@link Account} based on build properties. 124 */ findSendToAccount(Context context)125 private static Account findSendToAccount(Context context) { 126 final AccountManager am = (AccountManager) context.getSystemService( 127 Context.ACCOUNT_SERVICE); 128 129 String preferredDomain = SystemProperties.get("sendbug.preferred.domain"); 130 if (!preferredDomain.startsWith("@")) { 131 preferredDomain = "@" + preferredDomain; 132 } 133 134 final Account[] accounts = am.getAccounts(); 135 Account foundAccount = null; 136 for (Account account : accounts) { 137 if (Patterns.EMAIL_ADDRESS.matcher(account.name).matches()) { 138 if (!preferredDomain.isEmpty()) { 139 // if we have a preferred domain and it matches, return; otherwise keep 140 // looking 141 if (account.name.endsWith(preferredDomain)) { 142 return account; 143 } else { 144 foundAccount = account; 145 } 146 // if we don't have a preferred domain, just return since it looks like 147 // an email address 148 } else { 149 return account; 150 } 151 } 152 } 153 return foundAccount; 154 } 155 } 156