Skip to content

Commit

Permalink
Unifile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Blatzar committed Nov 1, 2021
1 parent 70c14d1 commit 4b1e5ab
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 74 deletions.
13 changes: 8 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ dependencies {
testImplementation 'org.json:json:20180813'

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.4.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
testImplementation 'junit:junit:4.13.2'
Expand Down Expand Up @@ -141,10 +141,13 @@ dependencies {
//implementation 'com.github.TorrentStream:TorrentStream-Android:2.7.0'

// Downloading
implementation "androidx.work:work-runtime:2.7.0-rc01"
implementation "androidx.work:work-runtime-ktx:2.7.0-rc01"
implementation "androidx.work:work-runtime:2.7.0"
implementation "androidx.work:work-runtime-ktx:2.7.0"

// Networking
implementation "com.squareup.okhttp3:okhttp:4.9.1"
implementation "com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.1"

// Util to skip the URI file fuckery 🙏
implementation "com.github.tachiyomiorg:unifile:17bec43"
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package com.lagradost.cloudstream3.ui.settings

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import com.hippo.unifile.UniFile
import com.lagradost.cloudstream3.APIHolder.apis
import com.lagradost.cloudstream3.APIHolder.getApiDubstatusSettings
import com.lagradost.cloudstream3.APIHolder.getApiProviderLangSettings
import com.lagradost.cloudstream3.APIHolder.getApiSettings
import com.lagradost.cloudstream3.APIHolder.restrictedApis
import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.DubStatus
import com.lagradost.cloudstream3.MainActivity.Companion.setLocale
import com.lagradost.cloudstream3.MainActivity.Companion.showToast
Expand All @@ -26,12 +32,41 @@ import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showDialog
import com.lagradost.cloudstream3.utils.SingleSelectionHelper.showMultiDialog
import com.lagradost.cloudstream3.utils.SubtitleHelper
import com.lagradost.cloudstream3.utils.UIHelper.hideKeyboard
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getBasePath
import com.lagradost.cloudstream3.utils.VideoDownloadManager.getDownloadDir
import com.lagradost.cloudstream3.utils.VideoDownloadManager.isScopedStorage
import java.io.File
import kotlin.concurrent.thread


class SettingsFragment : PreferenceFragmentCompat() {
private var beneneCount = 0

// Open file picker
private val pathPicker = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { uri ->
val context = AcraApplication.context ?: return@registerForActivityResult
// RW perms for the path
val flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION

context.contentResolver.takePersistableUriPermission(uri, flags)

val file = UniFile.fromUri(context, uri)
println("Selected URI path: $uri - Full path: ${file.filePath}")

// Stores the real URI using download_path_key
// Important that the URI is stored instead of filepath due to permissions.
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putString(getString(R.string.download_path_key), uri.toString()).apply()

// From URI -> File path
// File path here is purely for cosmetic purposes in settings
(file.filePath ?: uri.toString()).let {
PreferenceManager.getDefaultSharedPreferences(context)
.edit().putString(getString(R.string.download_path_pref), it).apply()
}
}

// idk, if you find a way of automating this it would be great
private val languages = arrayListOf(
Triple("\uD83C\uDDEA\uD83C\uDDF8", "Spanish", "es"),
Expand Down Expand Up @@ -61,6 +96,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
val legalPreference = findPreference<Preference>(getString(R.string.legal_notice_key))!!
val subdubPreference = findPreference<Preference>(getString(R.string.display_sub_key))!!
val providerLangPreference = findPreference<Preference>(getString(R.string.provider_lang_key))!!
val downloadPathPreference = findPreference<Preference>(getString(R.string.download_path_key))!!

legalPreference.setOnPreferenceClickListener {
val builder: AlertDialog.Builder = AlertDialog.Builder(it.context)
Expand Down Expand Up @@ -141,6 +177,48 @@ class SettingsFragment : PreferenceFragmentCompat() {
return@setOnPreferenceClickListener true
}

fun getDownloadDirs(): List<String> {
val defaultDir = getDownloadDir()?.filePath

// app_name_download_path = Cloudstream and does not change depending on release.
// DOES NOT WORK ON SCOPED STORAGE.
val secondaryDir = if (isScopedStorage) null else Environment.getExternalStorageDirectory().absolutePath +
File.separator + resources.getString(R.string.app_name_download_path)

val currentDir = context?.getBasePath()?.let { it.first?.filePath ?: it.second }

return (listOf(defaultDir, secondaryDir) +
requireContext().getExternalFilesDirs("").mapNotNull { it.path } +
currentDir).filterNotNull().distinct()
}

downloadPathPreference.setOnPreferenceClickListener {
val dirs = getDownloadDirs()
val settingsManager = PreferenceManager.getDefaultSharedPreferences(context)

val currentDir =
settingsManager.getString(getString(R.string.download_path_pref), null) ?: getDownloadDir().toString()

context?.showBottomDialog(
dirs + listOf("Custom"),
dirs.indexOf(currentDir),
getString(R.string.download_path_pref),
true,
{}) {
// Last = custom
if (it == dirs.size) {
pathPicker.launch(Uri.EMPTY)
} else {
// Sets both visual and actual paths.
// key = used path
// pref = visual path
settingsManager.edit().putString(getString(R.string.download_path_key), dirs[it]).apply()
settingsManager.edit().putString(getString(R.string.download_path_pref), dirs[it]).apply()
}
}
return@setOnPreferenceClickListener true
}

watchQualityPreference.setOnPreferenceClickListener {
val prefNames = resources.getStringArray(R.array.quality_pref)
val prefValues = resources.getIntArray(R.array.quality_pref_values)
Expand Down
Loading

0 comments on commit 4b1e5ab

Please sign in to comment.