3
0
Fork 0

Intercept copy to clipboard on chromeos and inject our file contents into the clipboard

This commit is contained in:
Nate Brown 2020-08-31 18:33:25 -05:00
parent eb6ac79eed
commit 0fd4d8d7fc
2 changed files with 26 additions and 5 deletions

View File

@ -31,6 +31,7 @@
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER"/>
</intent-filter> </intent-filter>
</activity> </activity>
<receiver android:name=".ShareReceiver" android:exported="false"/>
<provider <provider
android:name="androidx.core.content.FileProvider" android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider" android:authorities="${applicationId}.provider"

View File

@ -1,6 +1,7 @@
package net.defined.mobile_nebula package net.defined.mobile_nebula
import android.content.Intent import android.app.PendingIntent
import android.content.*
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.content.pm.ResolveInfo import android.content.pm.ResolveInfo
import android.util.Log import android.util.Log
@ -85,16 +86,17 @@ class Share {
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.action = Intent.ACTION_SEND intent.action = Intent.ACTION_SEND
intent.type = "text/plain" intent.type = "text/*"
intent.putExtra(Intent.EXTRA_SUBJECT, title) intent.putExtra(Intent.EXTRA_SUBJECT, title)
intent.putExtra(Intent.EXTRA_STREAM, fileUri) intent.putExtra(Intent.EXTRA_STREAM, fileUri)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
val chooserIntent = Intent.createChooser(intent, title) val receiver = Intent(context, ShareReceiver::class.java)
chooserIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP receiver.putExtra(Intent.EXTRA_TEXT, file)
chooserIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK val pendingIntent = PendingIntent.getBroadcast(context, 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT)
val chooserIntent = Intent.createChooser(intent, title, pendingIntent.intentSender)
val resInfoList: List<ResolveInfo> = context.packageManager.queryIntentActivities(chooserIntent, PackageManager.MATCH_DEFAULT_ONLY) val resInfoList: List<ResolveInfo> = context.packageManager.queryIntentActivities(chooserIntent, PackageManager.MATCH_DEFAULT_ONLY)
for (resolveInfo in resInfoList) { for (resolveInfo in resInfoList) {
val packageName: String = resolveInfo.activityInfo.packageName val packageName: String = resolveInfo.activityInfo.packageName
@ -112,3 +114,21 @@ class Share {
} }
} }
} }
class ShareReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent == null) {
return
}
val res = intent.extras.get(Intent.EXTRA_CHOSEN_COMPONENT) as? ComponentName ?: return
when (res.className) {
"org.chromium.arc.intent_helper.SendTextToClipboardActivity" -> {
val file = intent.extras[Intent.EXTRA_TEXT] as? File ?: return
val clipboard = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.primaryClip = ClipData.newPlainText("", file.readText())
}
}
}
}