fixed logs screen crashing on older apis

This commit is contained in:
Denis Çerri 2023-03-02 20:32:32 +01:00
parent 24f04ec2ba
commit 42fbf78ea2
No known key found for this signature in database
GPG key ID: 95C43D517D830350
6 changed files with 68 additions and 51 deletions

View file

@ -125,7 +125,7 @@ dependencies {
implementation "androidx.appcompat:appcompat:$appCompatVer" implementation "androidx.appcompat:appcompat:$appCompatVer"
implementation "androidx.constraintlayout:constraintlayout:2.1.4" implementation "androidx.constraintlayout:constraintlayout:2.1.4"
implementation 'com.google.android.material:material:1.7.0' implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.core:core:1.9.0' implementation 'androidx.core:core:1.9.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1' implementation 'androidx.recyclerview:recyclerview:1.2.1'

View file

@ -1,6 +1,7 @@
package com.deniscerri.ytdlnis.ui.downloadLogs package com.deniscerri.ytdlnis.ui.downloadLogs
import android.content.Context import android.content.Context
import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.FileObserver import android.os.FileObserver
import android.util.Log import android.util.Log
@ -43,7 +44,7 @@ class DownloadLogActivity : AppCompatActivity() {
topAppBar.title = file.name topAppBar.title = file.name
content.text = file.readText() content.text = file.readText()
observer = object : FileObserver(file, MODIFY) { observer = object : FileObserver(file.absolutePath, MODIFY) {
override fun onEvent(event: Int, p: String?) { override fun onEvent(event: Int, p: String?) {
runOnUiThread{ runOnUiThread{
content.text = File(path).readText() content.text = File(path).readText()

View file

@ -2,10 +2,7 @@ package com.deniscerri.ytdlnis.ui.downloadLogs
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.*
import android.os.FileObserver
import android.os.Handler
import android.os.Looper
import android.text.InputType import android.text.InputType
import android.util.Log import android.util.Log
import android.view.LayoutInflater import android.view.LayoutInflater
@ -57,7 +54,7 @@ class DownloadLogListActivity : AppCompatActivity(), DownloadLogsAdapter.OnItemC
val logFolder = File(filesDir.absolutePath + "/logs") val logFolder = File(filesDir.absolutePath + "/logs")
updateList(logFolder) updateList(logFolder)
val observer: FileObserver = object : FileObserver(logFolder) { val observer: FileObserver = object : FileObserver(logFolder.absolutePath) {
override fun onEvent(event: Int, path: String?) { override fun onEvent(event: Int, path: String?) {
when(event) { when(event) {
CREATE, DELETE -> updateList(logFolder) CREATE, DELETE -> updateList(logFolder)

View file

@ -89,7 +89,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
version!!.summary = BuildConfig.VERSION_NAME version!!.summary = BuildConfig.VERSION_NAME
val values = resources.getStringArray(R.array.language_values) val values = resources.getStringArray(R.array.language_values)
if (Build.VERSION.SDK_INT >= 33){ if (Build.VERSION.SDK_INT >= 33 || Build.VERSION.SDK_INT < 24){
language!!.isVisible = false language!!.isVisible = false
}else{ }else{
val entries = mutableListOf<String>() val entries = mutableListOf<String>()
@ -299,7 +299,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
} }
audioQuality!!.onPreferenceChangeListener = audioQuality!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any -> Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
editor.putInt("audio_format", newValue.toString().toInt()) editor.putInt("audio_quality", newValue.toString().toInt())
editor.apply() editor.apply()
true true
} }

View file

@ -1,25 +1,23 @@
package com.deniscerri.ytdlnis.util package com.deniscerri.ytdlnis.util
import android.R.attr.src
import android.content.Context import android.content.Context
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Build import android.os.Build
import android.provider.DocumentsContract
import android.util.Log
import android.webkit.MimeTypeMap
import android.widget.Toast
import com.deniscerri.ytdlnis.R import com.deniscerri.ytdlnis.R
import com.deniscerri.ytdlnis.database.models.DownloadItem
import java.io.File import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.net.URLDecoder import java.net.URLDecoder
import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.StandardCopyOption import java.nio.file.StandardCopyOption
import java.text.DecimalFormat import java.text.DecimalFormat
import kotlin.math.log10 import kotlin.math.log10
import kotlin.math.max
import kotlin.math.pow import kotlin.math.pow
class FileUtil() { class FileUtil() {
fun deleteFile(path: String){ fun deleteFile(path: String){
val file = File(path) val file = File(path)
@ -97,43 +95,54 @@ class FileUtil() {
Files.move(it.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING) Files.move(it.toPath(), f.toPath(), StandardCopyOption.REPLACE_EXISTING)
progress(100) progress(100)
}else{ }else{
val mimeType = val f = File(formatPath(destDir)+"/"+it.name)
MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
val destination = Uri.parse(destDir).run {
DocumentsContract.buildChildDocumentsUriUsingTree(this, DocumentsContract.getTreeDocumentId(this))
}
val destUri = DocumentsContract.createDocument(
context.contentResolver,
destination,
mimeType,
it.name
) ?: return@forEach
val inputStream = it.inputStream()
val outputStream = context.contentResolver.openOutputStream(destUri) ?: return@forEach
val fileLength = it.length()
var bytesCopied: Long = 0
val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
var bytes = inputStream.read(buffer)
val inChannel: FileChannel = FileInputStream(it).channel
val outChannel: FileChannel = FileOutputStream(f).channel
try { try {
while (bytes >= 0) { inChannel.transferTo(0, inChannel.size(), outChannel)
outputStream.write(buffer, 0, bytes) } finally {
bytesCopied += bytes inChannel.close()
progress((bytesCopied * 100 / fileLength).toInt()) outChannel.close()
bytes = inputStream.read(buffer)
}
}catch (e : Exception){
e.printStackTrace()
Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
} }
//
inputStream.close() // val mimeType =
outputStream.close() // MimeTypeMap.getSingleton().getMimeTypeFromExtension(it.extension) ?: "*/*"
//
it.delete() // val destination = Uri.parse(destDir).run {
// DocumentsContract.buildChildDocumentsUriUsingTree(this, DocumentsContract.getTreeDocumentId(this))
// }
//
// val destUri = DocumentsContract.createDocument(
// context.contentResolver,
// destination,
// mimeType,
// it.name
// ) ?: return@forEach
//
// val inputStream = it.inputStream()
// val outputStream = context.contentResolver.openOutputStream(destUri) ?: return@forEach
// val fileLength = it.length()
// var bytesCopied: Long = 0
// val buffer = ByteArray(DEFAULT_BUFFER_SIZE)
// var bytes = inputStream.read(buffer)
//
// try {
// while (bytes >= 0) {
// outputStream.write(buffer, 0, bytes)
// bytesCopied += bytes
// progress((bytesCopied * 100 / fileLength).toInt())
// bytes = inputStream.read(buffer)
// }
// }catch (e : Exception){
// e.printStackTrace()
// Toast.makeText(context, e.message, Toast.LENGTH_LONG).show()
// }
//
// inputStream.close()
// outputStream.close()
//
// it.delete()
} }
} }
originDir.delete() originDir.delete()

View file

@ -96,6 +96,12 @@
android:paddingStart="10dp" android:paddingStart="10dp"
android:paddingEnd="0dp" android:paddingEnd="0dp"
android:textSize="15sp" android:textSize="15sp"
android:shadowColor="@color/black"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"
android:textColor="@color/white"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/output" app:layout_constraintBottom_toTopOf="@+id/output"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />
@ -153,9 +159,13 @@
android:paddingTop="10dp" android:paddingTop="10dp"
android:paddingRight="10dp" android:paddingRight="10dp"
android:paddingBottom="10dp" android:paddingBottom="10dp"
android:shadowRadius="1.5"
android:textSize="12sp" android:textSize="12sp"
android:textStyle="bold" android:textStyle="bold"
android:shadowColor="@color/black"
android:shadowDx="4"
android:shadowDy="4"
android:shadowRadius="2"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" /> app:layout_constraintStart_toStartOf="parent" />