2019-09-05 17:42:39 -03:00
|
|
|
package com.stevesoltys.backup.ui
|
2019-07-08 16:02:53 +02:00
|
|
|
|
|
|
|
import android.content.ActivityNotFoundException
|
|
|
|
import android.content.Intent
|
|
|
|
import android.content.Intent.*
|
|
|
|
import android.os.Bundle
|
2019-07-09 19:22:24 +02:00
|
|
|
import android.provider.DocumentsContract.EXTRA_PROMPT
|
2019-07-08 16:02:53 +02:00
|
|
|
import android.widget.Toast
|
|
|
|
import android.widget.Toast.LENGTH_LONG
|
|
|
|
import androidx.preference.Preference
|
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
|
|
|
import com.stevesoltys.backup.R
|
|
|
|
|
|
|
|
class BackupLocationFragment : PreferenceFragmentCompat() {
|
|
|
|
|
|
|
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
|
|
|
setPreferencesFromResource(R.xml.backup_location, rootKey)
|
|
|
|
|
|
|
|
requireActivity().setTitle(R.string.settings_backup_location_title)
|
|
|
|
|
|
|
|
val externalStorage = Preference(requireContext()).apply {
|
|
|
|
setIcon(R.drawable.ic_storage)
|
|
|
|
setTitle(R.string.settings_backup_external_storage)
|
|
|
|
setOnPreferenceClickListener {
|
|
|
|
showChooseFolderActivity()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
preferenceScreen.addPreference(externalStorage)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun showChooseFolderActivity() {
|
|
|
|
val openTreeIntent = Intent(ACTION_OPEN_DOCUMENT_TREE)
|
2019-07-09 19:22:24 +02:00
|
|
|
openTreeIntent.putExtra(EXTRA_PROMPT, getString(R.string.settings_backup_location_picker))
|
2019-07-08 16:02:53 +02:00
|
|
|
openTreeIntent.addFlags(FLAG_GRANT_PERSISTABLE_URI_PERMISSION or
|
|
|
|
FLAG_GRANT_READ_URI_PERMISSION or FLAG_GRANT_WRITE_URI_PERMISSION)
|
|
|
|
try {
|
|
|
|
val documentChooser = createChooser(openTreeIntent, null)
|
2019-09-06 12:36:51 -03:00
|
|
|
// start from the activity context, so we can receive and handle the result there
|
|
|
|
requireActivity().startActivityForResult(documentChooser, REQUEST_CODE_OPEN_DOCUMENT_TREE)
|
2019-07-08 16:02:53 +02:00
|
|
|
} catch (ex: ActivityNotFoundException) {
|
|
|
|
Toast.makeText(requireContext(), "Please install a file manager.", LENGTH_LONG).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|