2022-06-07 16:12:59 +05:30
|
|
|
package com.github.libretube.preferences
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.DocumentsContract
|
|
|
|
import androidx.core.net.toUri
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import androidx.preference.PreferenceManager
|
|
|
|
import com.github.libretube.R
|
|
|
|
|
2022-06-07 20:22:06 +05:30
|
|
|
class AdvancedSettings : PreferenceFragmentCompat() {
|
|
|
|
val TAG = "AdvancedSettings"
|
2022-06-07 16:12:59 +05:30
|
|
|
private val directoryRequestCode = 9999
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
2022-06-07 20:22:06 +05:30
|
|
|
setPreferencesFromResource(R.xml.advanced_settings, rootKey)
|
|
|
|
|
|
|
|
val clearHistory = findPreference<Preference>("clear_history")
|
|
|
|
clearHistory?.setOnPreferenceClickListener {
|
|
|
|
val sharedPreferences =
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
sharedPreferences.edit().remove("search_history").commit()
|
|
|
|
true
|
|
|
|
}
|
2022-06-07 16:12:59 +05:30
|
|
|
|
|
|
|
// doesn't work yet
|
|
|
|
val directory = findPreference<Preference>("download_directory")
|
|
|
|
directory?.setOnPreferenceClickListener {
|
2022-06-07 16:14:22 +05:30
|
|
|
val sharedPreferences = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(requireContext())
|
|
|
|
val pickerInitialUri = sharedPreferences
|
|
|
|
.getString("download_directory_path", "")?.toUri()
|
2022-06-07 16:12:59 +05:30
|
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
|
|
|
intent.addCategory(Intent.CATEGORY_DEFAULT)
|
|
|
|
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
|
2022-06-07 16:14:22 +05:30
|
|
|
startActivityForResult(
|
|
|
|
Intent.createChooser(intent, "Choose directory"),
|
|
|
|
directoryRequestCode
|
|
|
|
)
|
2022-06-07 16:12:59 +05:30
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
when (requestCode) {
|
|
|
|
directoryRequestCode -> {
|
|
|
|
val directoryUri = data?.data
|
|
|
|
// save selected download directory to the shared preferences
|
2022-06-07 16:14:22 +05:30
|
|
|
val sharedPref = PreferenceManager
|
2022-06-07 16:12:59 +05:30
|
|
|
.getDefaultSharedPreferences(requireContext())
|
2022-06-07 16:14:22 +05:30
|
|
|
sharedPref.edit().putString("download_directory_path", directoryUri.toString())
|
2022-06-07 16:12:59 +05:30
|
|
|
.apply()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 16:14:22 +05:30
|
|
|
}
|