1 /* 2 * Copyright (C) 2022 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.systemui.clipboardoverlay; 18 19 import android.content.ClipData; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.net.Uri; 24 import android.text.TextUtils; 25 26 import com.android.systemui.R; 27 28 class IntentCreator { 29 private static final String EXTRA_EDIT_SOURCE_CLIPBOARD = "edit_source_clipboard"; 30 private static final String REMOTE_COPY_ACTION = "android.intent.action.REMOTE_COPY"; 31 getTextEditorIntent(Context context)32 static Intent getTextEditorIntent(Context context) { 33 Intent intent = new Intent(context, EditTextActivity.class); 34 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 35 return intent; 36 } 37 getShareIntent(ClipData clipData, Context context)38 static Intent getShareIntent(ClipData clipData, Context context) { 39 Intent shareIntent = new Intent(Intent.ACTION_SEND); 40 41 // From the ACTION_SEND docs: 42 // "If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the 43 // MIME type of the data in EXTRA_STREAM" 44 if (clipData.getItemAt(0).getUri() != null) { 45 shareIntent.setDataAndType( 46 clipData.getItemAt(0).getUri(), clipData.getDescription().getMimeType(0)); 47 shareIntent.putExtra(Intent.EXTRA_STREAM, clipData.getItemAt(0).getUri()); 48 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 49 } else { 50 shareIntent.putExtra( 51 Intent.EXTRA_TEXT, clipData.getItemAt(0).coerceToText(context).toString()); 52 shareIntent.setType("text/plain"); 53 } 54 Intent chooserIntent = Intent.createChooser(shareIntent, null) 55 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK) 56 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 57 58 return chooserIntent; 59 } 60 getImageEditIntent(Uri uri, Context context)61 static Intent getImageEditIntent(Uri uri, Context context) { 62 String editorPackage = context.getString(R.string.config_screenshotEditor); 63 Intent editIntent = new Intent(Intent.ACTION_EDIT); 64 if (!TextUtils.isEmpty(editorPackage)) { 65 editIntent.setComponent(ComponentName.unflattenFromString(editorPackage)); 66 } 67 editIntent.setDataAndType(uri, "image/*"); 68 editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 69 editIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 70 editIntent.putExtra(EXTRA_EDIT_SOURCE_CLIPBOARD, true); 71 return editIntent; 72 } 73 getRemoteCopyIntent(ClipData clipData, Context context)74 static Intent getRemoteCopyIntent(ClipData clipData, Context context) { 75 Intent nearbyIntent = new Intent(REMOTE_COPY_ACTION); 76 77 String remoteCopyPackage = context.getString(R.string.config_remoteCopyPackage); 78 if (!TextUtils.isEmpty(remoteCopyPackage)) { 79 nearbyIntent.setComponent(ComponentName.unflattenFromString(remoteCopyPackage)); 80 } 81 82 nearbyIntent.setClipData(clipData); 83 nearbyIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 84 nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 85 return nearbyIntent; 86 } 87 } 88