move some things to opener

This commit is contained in:
Minecon724 2024-08-04 10:53:26 +02:00
parent d43b02b4f2
commit 10a4dd8f8f
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8

View file

@ -1,28 +1,93 @@
package eu.m724.vastapp.activity package eu.m724.vastapp.activity
import android.app.PendingIntent
import android.content.ClipData import android.content.ClipData
import android.content.ClipboardManager import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import eu.m724.vastapp.activity.termux.TermuxSshActivity
class Opener { class Opener {
companion object { companion object {
/**
* opens an url in another app
* usually a browser but can be another app if it's default
* @param url the url
* @param activity the activity that starts the browser activity
*/
fun openUrl(url: String, activity: ComponentActivity) { fun openUrl(url: String, activity: ComponentActivity) {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
activity.startActivity(intent) activity.startActivity(intent)
} }
/**
* opens another app
* @param packageName package name like com.termux or eu.m724.vastapp
* @param activity the activity that starts the package
*/
fun openApp(packageName: String, activity: ComponentActivity) { fun openApp(packageName: String, activity: ComponentActivity) {
val intent = activity.packageManager.getLaunchIntentForPackage(packageName) val intent = activity.packageManager.getLaunchIntentForPackage(packageName)
activity.startActivity(intent) activity.startActivity(intent)
} }
/**
* copies text to clipboard without toast
* @param text the text to copy
* @param label what are you copying
* @param context the context
*/
fun copyToClipboard(text: String, label: String, context: Context) { fun copyToClipboard(text: String, label: String, context: Context) {
copyToClipboard(text, label, null, context)
}
/**
* copies text to clipboard with toast
* @param text the text to copy
* @param label what are you copying
* @param toast text of toast that will appear if android < 12
* @param context the context
*/
fun copyToClipboard(text: String, label: String, toast: String?, context: Context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
Toast.makeText(
context,
toast,
Toast.LENGTH_SHORT
).show()
}
val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager val clipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(label, text) val clipData = ClipData.newPlainText(label, text)
clipboardManager.setPrimaryClip(clipData) clipboardManager.setPrimaryClip(clipData)
} }
/**
* starts termux with command
* it will start [TermuxSshActivity] on finish
* @param context the context
* @param command the command, first entry is the executable and then arguments
*/
fun startTermux(context: Context, command: Array<String>) {
val noSshIntent = Intent(context, TermuxSshActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
context,
0,
noSshIntent,
PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_MUTABLE
)
val intent = Intent()
intent.setClassName("com.termux", "com.termux.app.RunCommandService")
intent.setAction("com.termux.RUN_COMMAND")
intent.putExtra("com.termux.RUN_COMMAND_PATH", command[0])
intent.putExtra("com.termux.RUN_COMMAND_ARGUMENTS", command.drop(1).toTypedArray())
intent.putExtra("com.termux.RUN_COMMAND_PENDING_INTENT", pendingIntent)
context.startForegroundService(intent)
}
} }
} }