From 10a4dd8f8f5530d11051c7d07c3846724c9870dc Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 4 Aug 2024 10:53:26 +0200 Subject: [PATCH] move some things to opener --- .../java/eu/m724/vastapp/activity/Opener.kt | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/app/src/main/java/eu/m724/vastapp/activity/Opener.kt b/app/src/main/java/eu/m724/vastapp/activity/Opener.kt index 2b73c55..0b512e7 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/Opener.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/Opener.kt @@ -1,28 +1,93 @@ package eu.m724.vastapp.activity +import android.app.PendingIntent import android.content.ClipData import android.content.ClipboardManager import android.content.Context import android.content.Intent import android.net.Uri +import android.os.Build +import android.widget.Toast import androidx.activity.ComponentActivity +import eu.m724.vastapp.activity.termux.TermuxSshActivity class Opener { 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) { val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) 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) { val intent = activity.packageManager.getLaunchIntentForPackage(packageName) 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) { + 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 clipData = ClipData.newPlainText(label, text) 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) { + 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) + } } } \ No newline at end of file