1 /*
<lambda>null2 * Copyright (C) 2024 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.sharetest
18
19 import android.app.PendingIntent
20 import android.content.ComponentName
21 import android.content.Context
22 import android.content.Intent
23 import android.graphics.drawable.Icon
24 import android.net.Uri
25 import android.os.Bundle
26 import android.service.chooser.ChooserAction
27 import android.service.chooser.ChooserTarget
28 import android.text.TextUtils
29 import androidx.core.os.bundleOf
30 import kotlin.math.roundToLong
31
32 const val REFINEMENT_ACTION = "com.android.sharetest.REFINEMENT"
33 private const val EXTRA_IS_INITIAL = "isInitial"
34
35 fun createAlternateIntent(intent: Intent): Intent {
36 val text = buildString {
37 append("Shared URIs:")
38 intent.extraStream.forEach {
39 append("\n * $it")
40 }
41 }
42 return Intent(Intent.ACTION_SEND).apply {
43 setText(text)
44 }
45 }
46
Intentnull47 fun Intent.setText(text: CharSequence) {
48 if (TextUtils.isEmpty(type)) {
49 type = "text/plain"
50 }
51 putExtra(Intent.EXTRA_TEXT, text)
52 }
53
setModifyShareActionnull54 fun Intent.setModifyShareAction(context: Context) {
55 val modifyShareAction = createModifyShareAction(context, true, 0)
56 putExtra(Intent.EXTRA_CHOOSER_MODIFY_SHARE_ACTION, modifyShareAction)
57 }
58
Bundlenull59 fun Bundle.setModifyShareAction(context: Context, count: Int) {
60 val modifyShareAction = createModifyShareAction(context, false, count)
61 putParcelable(Intent.EXTRA_CHOOSER_MODIFY_SHARE_ACTION, modifyShareAction)
62 }
63
64 // Provide some gaussian noise around the preferred average latency
getLatencyMsnull65 fun getLatencyMs(avg: Long): Long {
66 // Using avg/4 as the standard deviation.
67 val noise = avg / 4 * random.nextGaussian()
68 return (avg + noise).roundToLong().coerceAtLeast(0)
69 }
70
getRandomFailurenull71 fun getRandomFailure(failureRate: Float): Boolean = random.nextFloat() < failureRate
72
73 private val random by lazy { java.util.Random() }
74
createModifyShareActionnull75 private fun createModifyShareAction(
76 context: Context,
77 isInitial: Boolean,
78 count: Int,
79 ): ChooserAction {
80 val pendingIntent = PendingIntent.getBroadcast(
81 context,
82 1,
83 Intent(CustomActionFactory.BROADCAST_ACTION)
84 .apply {
85 this.isInitial = isInitial
86 },
87 PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
88 )
89 return ChooserAction.Builder(
90 Icon.createWithResource(context, R.drawable.testicon),
91 buildString {
92 append("Modify Share")
93 if (!isInitial) {
94 append(" (items: $count)")
95 }
96 },
97 pendingIntent
98 ).build()
99 }
100
101 val Intent.extraStream: List<Uri>
<lambda>null102 get() = buildList {
103 when (action) {
104 Intent.ACTION_SEND -> getParcelableExtra(
105 Intent.EXTRA_STREAM,
106 Uri::class.java
107 )?.let { add(it) }
108
109 Intent.ACTION_SEND_MULTIPLE -> getParcelableArrayListExtra(
110 Intent.EXTRA_STREAM,
111 Uri::class.java
112 )?.let { uris ->
113 for (uri in uris) {
114 if (uri != null) {
115 add(uri)
116 }
117 }
118 }
119 }
120 }
121
122 var Intent.isInitial: Boolean
123 set(value) {
124 putExtra(EXTRA_IS_INITIAL, value)
125 }
126 get() = getBooleanExtra(EXTRA_IS_INITIAL, true)
127
createCallerTargetnull128 fun createCallerTarget(context: Context, text: String) =
129 ChooserTarget(
130 "Caller Target",
131 Icon.createWithResource(context, R.drawable.launcher_icon),
132 1f,
133 ComponentName(context, CallerDirectTargetActivity::class.java),
134 bundleOf(Intent.EXTRA_TEXT to text)
135 )
136
137 fun createRefinementIntentSender(context: Context, isInitial: Boolean) =
138 PendingIntent.getBroadcast(
139 context,
140 1,
141 Intent(REFINEMENT_ACTION).apply {
142 setPackage(context.packageName)
143 this.isInitial = isInitial
144 },
145 PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_CANCEL_CURRENT or
146 PendingIntent.FLAG_CANCEL_CURRENT
147
148 ).intentSender
149