added export to clipboard in terminal

This commit is contained in:
deniscerri 2023-04-13 23:23:36 +02:00
parent 55b59d75b1
commit ee6eef64d4
No known key found for this signature in database
GPG key ID: 95C43D517D830350
6 changed files with 88 additions and 31 deletions

View file

@ -56,4 +56,7 @@
#-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept. #-keepnames class <1>$$serializer { # -keepnames suffices; class is kept when serializer() is kept.
# static <1>$$serializer INSTANCE; # static <1>$$serializer INSTANCE;
#} #}
-keepclassmembers enum * { *; } -keepclassmembers enum * { *; }
-keepclassmembers class * extends androidx.work.Worker {
public <init>(android.content.Context,androidx.work.WorkerParameters);
}

View file

@ -1,7 +1,12 @@
package com.deniscerri.ytdlnis.database.models package com.deniscerri.ytdlnis.database.models
import com.google.gson.annotations.SerializedName
data class ChapterItem( data class ChapterItem(
@SerializedName(value = "start_time")
var start_time: Long, var start_time: Long,
@SerializedName(value = "end_time")
var end_time: Long, var end_time: Long,
@SerializedName(value = "title")
var title: String, var title: String,
) )

View file

@ -1,19 +1,20 @@
package com.deniscerri.ytdlnis.ui.more package com.deniscerri.ytdlnis.ui.more
import android.app.Activity import android.app.Activity
import android.content.ClipboardManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.FileObserver
import android.util.Log import android.util.Log
import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.Window import android.view.Window
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.*
import android.widget.LinearLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout import androidx.constraintlayout.widget.ConstraintLayout
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@ -33,6 +34,7 @@ import com.yausername.youtubedl_android.YoutubeDL
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import java.io.File
import kotlin.properties.Delegates import kotlin.properties.Delegates
@ -47,6 +49,8 @@ class TerminalActivity : AppCompatActivity() {
private lateinit var commandTemplateViewModel: CommandTemplateViewModel private lateinit var commandTemplateViewModel: CommandTemplateViewModel
private lateinit var sharedPreferences: SharedPreferences private lateinit var sharedPreferences: SharedPreferences
private var downloadID by Delegates.notNull<Int>() private var downloadID by Delegates.notNull<Int>()
private lateinit var downloadFile : File
private lateinit var observer: FileObserver
var context: Context? = null var context: Context? = null
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
@ -54,6 +58,8 @@ class TerminalActivity : AppCompatActivity() {
setContentView(R.layout.activity_terminal) setContentView(R.layout.activity_terminal)
downloadID = System.currentTimeMillis().toInt() % 100000 downloadID = System.currentTimeMillis().toInt() % 100000
downloadFile = File(cacheDir.absolutePath + "/$downloadID.txt")
if (! downloadFile.exists()) downloadFile.createNewFile()
context = baseContext context = baseContext
scrollView = findViewById(R.id.custom_command_scrollview) scrollView = findViewById(R.id.custom_command_scrollview)
@ -84,6 +90,7 @@ class TerminalActivity : AppCompatActivity() {
input!!.visibility = View.GONE input!!.visibility = View.GONE
output!!.text = "${output!!.text}\n~ $ ${input!!.text}\n" output!!.text = "${output!!.text}\n~ $ ${input!!.text}\n"
showCancelFab() showCancelFab()
downloadFile.appendText("~ $ ${input!!.text}\n")
startDownload( startDownload(
input!!.text.toString() input!!.text.toString()
) )
@ -96,6 +103,36 @@ class TerminalActivity : AppCompatActivity() {
notificationUtil = NotificationUtil(this) notificationUtil = NotificationUtil(this)
handleIntent(intent) handleIntent(intent)
if(Build.VERSION.SDK_INT < 29){
observer = object : FileObserver(downloadFile.absolutePath, MODIFY) {
override fun onEvent(event: Int, p: String?) {
runOnUiThread{
try {
val newText = downloadFile.readText()
output!!.text = newText
output!!.scrollTo(0, output!!.height)
scrollView!!.fullScroll(View.FOCUS_DOWN)
}catch (ignored: Exception) {}
}
}
}
observer.startWatching()
}else{
observer = object : FileObserver(downloadFile, MODIFY) {
override fun onEvent(event: Int, p: String?) {
runOnUiThread{
try {
val newText = downloadFile.readText()
output!!.text = newText
output!!.scrollTo(0, output!!.height)
scrollView!!.fullScroll(View.FOCUS_DOWN)
}catch (ignored: Exception) {}
}
}
}
observer.startWatching()
}
WorkManager.getInstance(this) WorkManager.getInstance(this)
.getWorkInfosForUniqueWorkLiveData(downloadID.toString()) .getWorkInfosForUniqueWorkLiveData(downloadID.toString())
.observe(this){ list -> .observe(this){ list ->
@ -106,16 +143,27 @@ class TerminalActivity : AppCompatActivity() {
input!!.requestFocus() input!!.requestFocus()
hideCancelFab() hideCancelFab()
} }
val line = work.progress.getString("output") ?: (work.outputData.getString("output") ?: return@observe)
runOnUiThread {
try {
output!!.text = "${output!!.text}\n$line"
output!!.scrollTo(0, output!!.height)
scrollView!!.fullScroll(View.FOCUS_DOWN)
}catch (ignored: Exception) {}
}
} }
} }
initMenu()
}
private fun initMenu() {
topAppBar?.setOnMenuItemClickListener { m: MenuItem ->
val itemId = m.itemId
if (itemId == R.id.export_clipboard) {
lifecycleScope.launch(Dispatchers.IO){
val clipboard: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setText(output?.text)
}
}
true
}
}
override fun onDestroy() {
downloadFile.delete()
super.onDestroy()
} }
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {

View file

@ -49,6 +49,9 @@ class TerminalDownloadWorker(
tempFileDir.delete() tempFileDir.delete()
tempFileDir.mkdirs() tempFileDir.mkdirs()
val outputFile = File(context.cacheDir.absolutePath + "/$itemId.txt")
if (! outputFile.exists()) outputFile.createNewFile()
request.addOption( request.addOption(
"--config-locations", "--config-locations",
File(context.cacheDir.absolutePath + "/downloads/config${System.currentTimeMillis()}.txt").apply { File(context.cacheDir.absolutePath + "/downloads/config${System.currentTimeMillis()}.txt").apply {
@ -77,7 +80,7 @@ class TerminalDownloadWorker(
} }
YoutubeDL.getInstance().execute(request, itemId.toString()){ progress, _, line -> YoutubeDL.getInstance().execute(request, itemId.toString()){ progress, _, line ->
setProgressAsync(workDataOf("progress" to progress.toInt(), "output" to line, "id" to itemId, "log" to logDownloads)) outputFile.appendText("${line}\n")
val title: String = command.take(20) val title: String = command.take(20)
notificationUtil.updateDownloadNotification( notificationUtil.updateDownloadNotification(
itemId, itemId,
@ -103,20 +106,15 @@ class TerminalDownloadWorker(
}, 1000) }, 1000)
} }
val chunks = it.out.chunked(5000) outputFile.appendText("${it.out}\n")
val dataBuilder = Data.Builder(); if (logDownloads && logFile.exists()){
dataBuilder.putString("output", chunks[0]) logFile.appendText("${it.out}\n")
}
return Result.success(
dataBuilder.build()
)
}.onFailure { }.onFailure {
if (it is YoutubeDL.CanceledException) { if (it is YoutubeDL.CanceledException) {
return Result.failure() return Result.failure()
} }
outputFile.appendText("${it.message}\n")
if (logDownloads && logFile.exists()){ if (logDownloads && logFile.exists()){
logFile.appendText("${it.message}\n") logFile.appendText("${it.message}\n")
} }
@ -129,13 +127,7 @@ class TerminalDownloadWorker(
NotificationUtil.DOWNLOAD_SERVICE_CHANNEL_ID NotificationUtil.DOWNLOAD_SERVICE_CHANNEL_ID
) )
if (logDownloads && logFile.exists()){ return Result.failure()
logFile.appendText("${it.message}\n")
}
return Result.failure(
Data.Builder().putString("output", it.message.toString()).build()
)
} }
return Result.success() return Result.success()

View file

@ -17,6 +17,7 @@
android:id="@+id/custom_command_toolbar" android:id="@+id/custom_command_toolbar"
android:elevation="0dp" android:elevation="0dp"
app:title="@string/terminal" app:title="@string/terminal"
app:menu="@menu/terminal_top_menu"
android:layout_width="match_parent" android:layout_width="match_parent"
app:navigationIcon="@drawable/ic_back" app:navigationIcon="@drawable/ic_back"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/export_clipboard"
android:title="@string/export_from_clipboard"
app:showAsAction="never" />
</menu>