2022-02-08 14:58:50 +05:30
|
|
|
package com.github.libretube
|
|
|
|
|
2022-03-16 19:02:42 +05:30
|
|
|
import android.Manifest
|
2022-03-31 23:04:19 +05:30
|
|
|
import android.content.ContentResolver
|
2022-02-13 11:12:40 +05:30
|
|
|
import android.content.Context
|
2022-03-18 00:46:09 +05:30
|
|
|
import android.content.pm.PackageManager
|
2022-03-16 19:02:42 +05:30
|
|
|
import android.net.Uri
|
2022-03-18 00:46:09 +05:30
|
|
|
import android.os.Build
|
2022-02-08 14:58:50 +05:30
|
|
|
import android.os.Bundle
|
2022-02-10 17:09:34 +05:30
|
|
|
import android.text.TextUtils
|
2022-02-08 19:57:13 +05:30
|
|
|
import android.util.Log
|
2022-02-13 11:12:40 +05:30
|
|
|
import android.widget.Toast
|
2022-03-16 19:02:42 +05:30
|
|
|
import androidx.activity.result.ActivityResultLauncher
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
2022-03-18 01:18:43 +05:30
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2022-03-18 00:46:09 +05:30
|
|
|
import androidx.core.app.ActivityCompat
|
2022-03-31 23:04:19 +05:30
|
|
|
import androidx.core.content.ContextCompat
|
2022-02-10 17:02:22 +05:30
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
import androidx.lifecycle.lifecycleScope
|
2022-02-08 19:57:13 +05:30
|
|
|
import androidx.preference.ListPreference
|
|
|
|
import androidx.preference.Preference
|
2022-02-08 14:58:50 +05:30
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2022-03-16 19:02:42 +05:30
|
|
|
import com.blankj.utilcode.util.UriUtils
|
2022-02-10 17:02:22 +05:30
|
|
|
import retrofit2.HttpException
|
2022-03-31 23:04:19 +05:30
|
|
|
import java.io.*
|
2022-03-16 19:02:42 +05:30
|
|
|
import java.util.zip.ZipFile
|
2022-02-08 14:58:50 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
|
2022-02-08 14:58:50 +05:30
|
|
|
class Settings : PreferenceFragmentCompat() {
|
2022-03-29 22:32:34 +05:30
|
|
|
val TAG = "Settings"
|
2022-03-16 19:02:42 +05:30
|
|
|
companion object {
|
|
|
|
lateinit var getContent: ActivityResultLauncher<String>
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
2022-03-18 00:40:18 +05:30
|
|
|
getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-18 00:46:09 +05:30
|
|
|
if (uri != null) {
|
2022-03-29 22:32:34 +05:30
|
|
|
try{
|
2022-03-31 23:04:19 +05:30
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
// Open a specific media item using ParcelFileDescriptor.
|
|
|
|
val resolver: ContentResolver =
|
|
|
|
requireActivity()
|
|
|
|
.contentResolver
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
// "rw" for read-and-write;
|
|
|
|
// "rwt" for truncating or overwriting existing file contents.
|
|
|
|
val readOnlyMode = "r"
|
|
|
|
// uri - I have got from onActivityResult
|
|
|
|
//uri = data.getData();
|
|
|
|
val parcelFile = resolver.openFileDescriptor(uri, readOnlyMode)
|
|
|
|
val fileReader = FileReader(parcelFile!!.fileDescriptor)
|
|
|
|
val reader = BufferedReader(fileReader)
|
|
|
|
var line: String?
|
|
|
|
while (reader.readLine().also { line = it } != null) {
|
|
|
|
Log.d(TAG,reader.readLine())
|
|
|
|
}
|
|
|
|
reader.close()
|
|
|
|
fileReader.close()
|
|
|
|
}else{
|
|
|
|
Log.d(TAG,UriUtils.uri2File(uri).toString())
|
|
|
|
val file = UriUtils.uri2File(uri)
|
|
|
|
var inputStream: InputStream? = null
|
|
|
|
if (file.extension == "zip") {
|
|
|
|
var zipfile = ZipFile(file)
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
var zipentry =
|
|
|
|
zipfile.getEntry("Takeout/YouTube and YouTube Music/subscriptions/subscriptions.csv")
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
inputStream = zipfile.getInputStream(zipentry)
|
|
|
|
}else if(file.extension == "csv"){
|
|
|
|
inputStream = file.inputStream()
|
2022-03-29 22:32:34 +05:30
|
|
|
}
|
2022-03-31 23:04:19 +05:30
|
|
|
val baos = ByteArrayOutputStream()
|
|
|
|
|
|
|
|
inputStream?.use { it.copyTo(baos) }
|
2022-03-18 00:40:18 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
var subscriptions = baos.toByteArray().decodeToString()
|
|
|
|
var channels: MutableList<String> = emptyList<String>().toMutableList()
|
|
|
|
var subscribedCount = 0
|
|
|
|
for (text in subscriptions.lines().subList(1,subscriptions.lines().size)) {
|
|
|
|
if (text.replace(" ","") != "") {
|
|
|
|
val channel = text.split(",")[0]
|
|
|
|
channels.add(channel)
|
|
|
|
subscribedCount++
|
|
|
|
Log.d(TAG, "subscribed: " + text + " total: " + subscribedCount)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
subscribe(channels)
|
|
|
|
}
|
2022-03-29 22:32:34 +05:30
|
|
|
}catch (e: Exception){
|
2022-03-31 23:04:19 +05:30
|
|
|
Log.e(TAG,e.toString())
|
2022-03-29 22:32:34 +05:30
|
|
|
Toast.makeText(
|
|
|
|
context,
|
|
|
|
R.string.error,
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
2022-03-16 19:02:42 +05:30
|
|
|
}
|
2022-03-29 22:32:34 +05:30
|
|
|
}
|
|
|
|
|
2022-03-18 00:40:18 +05:30
|
|
|
|
2022-03-16 19:02:42 +05:30
|
|
|
}
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
}
|
|
|
|
|
2022-02-08 14:58:50 +05:30
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
setPreferencesFromResource(R.xml.settings, rootKey)
|
2022-02-08 19:57:13 +05:30
|
|
|
val instance = findPreference<ListPreference>("instance")
|
2022-02-10 17:02:22 +05:30
|
|
|
fetchInstance()
|
2022-02-26 22:49:42 +05:30
|
|
|
instance?.setOnPreferenceChangeListener { _, newValue ->
|
2022-02-10 17:02:22 +05:30
|
|
|
RetrofitInstance.url = newValue.toString()
|
2022-02-09 23:40:39 +05:30
|
|
|
RetrofitInstance.lazyMgr.reset()
|
2022-02-13 11:12:40 +05:30
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-03-18 00:46:09 +05:30
|
|
|
if (sharedPref?.getString("token", "") != "") {
|
2022-02-13 11:12:40 +05:30
|
|
|
with(sharedPref!!.edit()) {
|
|
|
|
putString("token", "")
|
|
|
|
apply()
|
|
|
|
}
|
|
|
|
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
2022-02-08 19:57:13 +05:30
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:46:09 +05:30
|
|
|
val login = findPreference<Preference>("login_register")
|
|
|
|
login?.setOnPreferenceClickListener {
|
|
|
|
val newFragment = LoginDialog()
|
|
|
|
newFragment.show(childFragmentManager, "Login")
|
|
|
|
true
|
|
|
|
}
|
2022-02-10 17:09:34 +05:30
|
|
|
|
2022-03-16 19:02:42 +05:30
|
|
|
val importFromYt = findPreference<Preference>("import_from_yt")
|
|
|
|
importFromYt?.setOnPreferenceClickListener {
|
2022-03-30 00:16:34 +05:30
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
|
|
|
val token = sharedPref?.getString("token","")!!
|
2022-03-16 19:02:42 +05:30
|
|
|
//check StorageAccess
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
|
|
|
Log.d("myz", "" + Build.VERSION.SDK_INT)
|
2022-03-31 23:04:19 +05:30
|
|
|
if (ContextCompat.checkSelfPermission(this.requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
|
|
|
|
!= PackageManager.PERMISSION_GRANTED
|
|
|
|
) {
|
2022-03-16 19:02:42 +05:30
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
this.requireActivity(), arrayOf(
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
|
Manifest.permission.MANAGE_EXTERNAL_STORAGE
|
|
|
|
), 1
|
|
|
|
) //permission request code is just an int
|
2022-03-30 00:16:34 +05:30
|
|
|
}else if (token != ""){
|
2022-03-29 22:32:34 +05:30
|
|
|
getContent.launch("*/*")
|
2022-03-30 00:16:34 +05:30
|
|
|
}else{
|
|
|
|
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
2022-03-16 19:02:42 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (ActivityCompat.checkSelfPermission(
|
|
|
|
requireContext(),
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
|
|
|
) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(
|
|
|
|
requireContext(),
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
|
|
) != PackageManager.PERMISSION_GRANTED
|
|
|
|
) {
|
|
|
|
ActivityCompat.requestPermissions(
|
|
|
|
this.requireActivity(),
|
|
|
|
arrayOf(
|
|
|
|
Manifest.permission.READ_EXTERNAL_STORAGE,
|
|
|
|
Manifest.permission.WRITE_EXTERNAL_STORAGE
|
|
|
|
),
|
|
|
|
1
|
|
|
|
)
|
2022-03-30 00:16:34 +05:30
|
|
|
}else if (token != ""){
|
2022-03-29 22:32:34 +05:30
|
|
|
getContent.launch("*/*")
|
2022-03-30 00:16:34 +05:30
|
|
|
}else{
|
|
|
|
Toast.makeText(context, R.string.login_first, Toast.LENGTH_SHORT).show()
|
2022-03-16 19:02:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-18 01:18:43 +05:30
|
|
|
val themeToggle = findPreference<ListPreference>("theme_togglee")
|
|
|
|
themeToggle?.setOnPreferenceChangeListener { _, newValue ->
|
|
|
|
when (newValue.toString()) {
|
|
|
|
"A" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
|
|
|
"L" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
|
|
|
"D" -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2022-03-18 00:46:09 +05:30
|
|
|
}
|
2022-02-10 17:02:22 +05:30
|
|
|
|
|
|
|
private fun fetchInstance() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
2022-02-25 12:33:56 +05:30
|
|
|
RetrofitInstance.api.getInstances("https://instances.tokhmi.xyz/")
|
2022-02-10 17:02:22 +05:30
|
|
|
} catch (e: IOException) {
|
|
|
|
println(e)
|
|
|
|
Log.e("settings", "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
2022-03-18 00:46:09 +05:30
|
|
|
Log.e("settings", "HttpException, unexpected response $e")
|
2022-02-10 17:02:22 +05:30
|
|
|
return@launchWhenCreated
|
2022-03-18 00:46:09 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e("settings", e.toString())
|
2022-02-10 17:02:22 +05:30
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
|
|
|
val listEntries: MutableList<String> = ArrayList()
|
|
|
|
val listEntryValues: MutableList<String> = ArrayList()
|
2022-03-18 00:46:09 +05:30
|
|
|
for (item in response) {
|
2022-02-25 12:33:56 +05:30
|
|
|
listEntries.add(item.name!!)
|
|
|
|
listEntryValues.add(item.api_url!!)
|
2022-02-10 17:02:22 +05:30
|
|
|
}
|
|
|
|
val entries = listEntries.toTypedArray<CharSequence>()
|
|
|
|
val entryValues = listEntryValues.toTypedArray<CharSequence>()
|
|
|
|
runOnUiThread {
|
|
|
|
val instance = findPreference<ListPreference>("instance")
|
|
|
|
instance?.entries = entries
|
|
|
|
instance?.entryValues = entryValues
|
2022-03-18 00:46:09 +05:30
|
|
|
instance?.summaryProvider =
|
|
|
|
Preference.SummaryProvider<ListPreference> { preference ->
|
|
|
|
val text = preference.entry
|
|
|
|
if (TextUtils.isEmpty(text)) {
|
|
|
|
"kavin.rocks (Official)"
|
|
|
|
} else {
|
|
|
|
text
|
|
|
|
}
|
2022-02-10 17:09:34 +05:30
|
|
|
}
|
2022-02-10 17:02:22 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-18 00:46:09 +05:30
|
|
|
|
2022-02-10 17:02:22 +05:30
|
|
|
private fun Fragment?.runOnUiThread(action: () -> Unit) {
|
|
|
|
this ?: return
|
|
|
|
if (!isAdded) return // Fragment not attached to an Activity
|
|
|
|
activity?.runOnUiThread(action)
|
|
|
|
}
|
2022-03-16 19:02:42 +05:30
|
|
|
|
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
private fun subscribe(channels: List<String>) {
|
2022-03-16 19:02:42 +05:30
|
|
|
fun run() {
|
|
|
|
lifecycleScope.launchWhenCreated {
|
|
|
|
val response = try {
|
|
|
|
val sharedPref = context?.getSharedPreferences("token", Context.MODE_PRIVATE)
|
2022-03-31 23:04:19 +05:30
|
|
|
RetrofitInstance.api.importSubscriptions(sharedPref?.getString("token", "")!!,channels)
|
2022-03-18 00:46:09 +05:30
|
|
|
} catch (e: IOException) {
|
2022-03-16 19:02:42 +05:30
|
|
|
Log.e(TAG, "IOException, you might not have internet connection")
|
|
|
|
return@launchWhenCreated
|
|
|
|
} catch (e: HttpException) {
|
|
|
|
Log.e(TAG, "HttpException, unexpected response$e")
|
|
|
|
return@launchWhenCreated
|
|
|
|
}
|
2022-03-31 23:04:19 +05:30
|
|
|
if(response.message == "ok"){
|
|
|
|
Toast.makeText(
|
|
|
|
context,
|
|
|
|
R.string.importsuccess,
|
|
|
|
Toast.LENGTH_SHORT
|
|
|
|
).show()
|
|
|
|
}
|
2022-03-16 19:02:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
run()
|
|
|
|
}
|
2022-02-08 14:58:50 +05:30
|
|
|
}
|
2022-03-16 19:02:42 +05:30
|
|
|
|
2022-03-31 23:04:19 +05:30
|
|
|
|