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.ClipDescription;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.text.TextUtils;
26 
27 import com.android.systemui.res.R;
28 
29 class IntentCreator {
30     private static final String EXTRA_EDIT_SOURCE = "edit_source";
31     private static final String EDIT_SOURCE_CLIPBOARD = "clipboard";
32     private static final String REMOTE_COPY_ACTION = "android.intent.action.REMOTE_COPY";
33 
getTextEditorIntent(Context context)34     static Intent getTextEditorIntent(Context context) {
35         Intent intent = new Intent(context, EditTextActivity.class);
36         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
37         return intent;
38     }
39 
getShareIntent(ClipData clipData, Context context)40     static Intent getShareIntent(ClipData clipData, Context context) {
41         Intent shareIntent = new Intent(Intent.ACTION_SEND);
42 
43         // From the ACTION_SEND docs:
44         //   "If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the
45         //    MIME type of the data in EXTRA_STREAM"
46         Uri uri = clipData.getItemAt(0).getUri();
47         if (uri != null) {
48             // We don't use setData here because some apps interpret this as "to:".
49             shareIntent.setType(clipData.getDescription().getMimeType(0));
50             // Include URI in ClipData also, so that grantPermission picks it up.
51             shareIntent.setClipData(new ClipData(
52                     new ClipDescription(
53                             "content", new String[]{clipData.getDescription().getMimeType(0)}),
54                     new ClipData.Item(uri)));
55             shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
56             shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
57         } else {
58             shareIntent.putExtra(
59                     Intent.EXTRA_TEXT, clipData.getItemAt(0).coerceToText(context).toString());
60             shareIntent.setType("text/plain");
61         }
62         Intent chooserIntent = Intent.createChooser(shareIntent, null)
63                 .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK)
64                 .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
65 
66         return chooserIntent;
67     }
68 
getImageEditIntent(Uri uri, Context context)69     static Intent getImageEditIntent(Uri uri, Context context) {
70         String editorPackage = context.getString(R.string.config_screenshotEditor);
71         Intent editIntent = new Intent(Intent.ACTION_EDIT);
72         if (!TextUtils.isEmpty(editorPackage)) {
73             editIntent.setComponent(ComponentName.unflattenFromString(editorPackage));
74         }
75         editIntent.setDataAndType(uri, "image/*");
76         editIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
77         editIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
78         editIntent.putExtra(EXTRA_EDIT_SOURCE, EDIT_SOURCE_CLIPBOARD);
79         return editIntent;
80     }
81 
getRemoteCopyIntent(ClipData clipData, Context context)82     static Intent getRemoteCopyIntent(ClipData clipData, Context context) {
83         Intent nearbyIntent = new Intent(REMOTE_COPY_ACTION);
84 
85         String remoteCopyPackage = context.getString(R.string.config_remoteCopyPackage);
86         if (!TextUtils.isEmpty(remoteCopyPackage)) {
87             nearbyIntent.setComponent(ComponentName.unflattenFromString(remoteCopyPackage));
88         }
89 
90         nearbyIntent.setClipData(clipData);
91         nearbyIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
92         nearbyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
93         return nearbyIntent;
94     }
95 }
96