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