• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.screenshot
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 com.android.systemui.R
26 
27 object ActionIntentCreator {
28     /** @return a chooser intent to share the given URI. */
createShareIntentnull29     fun createShareIntent(uri: Uri) = createShareIntent(uri, null, null)
30 
31     /** @return a chooser intent to share the given URI with the optional provided subject. */
32     fun createShareIntentWithSubject(uri: Uri, subject: String?) =
33         createShareIntent(uri, subject = subject)
34 
35     /** @return a chooser intent to share the given URI with the optional provided extra text. */
36     fun createShareIntentWithExtraText(uri: Uri, extraText: String?) =
37         createShareIntent(uri, extraText = extraText)
38 
39     private fun createShareIntent(
40         uri: Uri,
41         subject: String? = null,
42         extraText: String? = null
43     ): Intent {
44         // Create a share intent, this will always go through the chooser activity first
45         // which should not trigger auto-enter PiP
46         val sharingIntent =
47             Intent(Intent.ACTION_SEND).apply {
48                 setDataAndType(uri, "image/png")
49                 putExtra(Intent.EXTRA_STREAM, uri)
50 
51                 // Include URI in ClipData also, so that grantPermission picks it up.
52                 // We don't use setData here because some apps interpret this as "to:".
53                 clipData =
54                     ClipData(
55                         ClipDescription("content", arrayOf(ClipDescription.MIMETYPE_TEXT_PLAIN)),
56                         ClipData.Item(uri)
57                     )
58 
59                 putExtra(Intent.EXTRA_SUBJECT, subject)
60                 putExtra(Intent.EXTRA_TEXT, extraText)
61                 addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
62                 addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
63             }
64 
65         return Intent.createChooser(sharingIntent, null)
66             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
67             .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
68             .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
69     }
70 
71     /**
72      * @return an ACTION_EDIT intent for the given URI, directed to config_screenshotEditor if
73      *   available.
74      */
createEditIntentnull75     fun createEditIntent(uri: Uri, context: Context): Intent {
76         val editIntent = Intent(Intent.ACTION_EDIT)
77 
78         context.getString(R.string.config_screenshotEditor)?.let {
79             if (it.isNotEmpty()) {
80                 editIntent.component = ComponentName.unflattenFromString(it)
81             }
82         }
83 
84         return editIntent
85             .setDataAndType(uri, "image/png")
86             .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
87             .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
88             .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
89             .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
90     }
91 }
92