From 0cdd7169ab787070f3b7412404a0fe0979274e0d Mon Sep 17 00:00:00 2001 From: Erik Tews Date: Wed, 3 Jan 2018 03:59:10 +0100 Subject: [PATCH] Implemented custom preferences for the download directory. --- touch/build.gradle | 4 ++ touch/src/main/AndroidManifest.xml | 6 ++- .../chaosflix/touch/OfflineItemManager.kt | 21 ++++++-- .../touch/settings/SettingsFragment.kt | 51 ++++++++++++++++++- touch/src/main/res/xml/preferences.xml | 5 ++ 5 files changed, 80 insertions(+), 7 deletions(-) diff --git a/touch/build.gradle b/touch/build.gradle index 1c12e568..485bd6ef 100644 --- a/touch/build.gradle +++ b/touch/build.gradle @@ -93,6 +93,8 @@ dependencies { implementation 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'net.opacapp:multiline-collapsingtoolbar:1.6.0' + implementation 'net.rdrei.android.dirchooser:library:3.2@aar' + implementation 'com.gu:option:1.3' androidTestImplementation('com.android.support.test:rules:0.5') { exclude module: 'support-annotations' @@ -111,6 +113,8 @@ dependencies { } repositories { mavenCentral() + maven { url 'http://guardian.github.com/maven/repo-releases' } + mavenLocal() } diff --git a/touch/src/main/AndroidManifest.xml b/touch/src/main/AndroidManifest.xml index 6d71210c..c5c81f27 100644 --- a/touch/src/main/AndroidManifest.xml +++ b/touch/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ + + android:theme="@style/AppTheme" + tools:replace="android:theme"> + + diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/OfflineItemManager.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/OfflineItemManager.kt index a024e1ac..8a8ba70e 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/OfflineItemManager.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/OfflineItemManager.kt @@ -94,9 +94,12 @@ class OfflineItemManager(downloadRefs: List? = emptyList()) { val request = DownloadManager.Request(Uri.parse(recording.recordingUrl)) request.setTitle(event.title) - request.setDestinationInExternalPublicDir( - Environment.DIRECTORY_MOVIES, DOWNLOAD_DIR + recording.filename) - request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) + + request.setDestinationUri( + Uri.withAppendedPath(Uri.fromFile( + File(getDownloadDir())), recording.filename)) + + request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE) request.setVisibleInDownloadsUi(true) val sharedPref: SharedPreferences = PreferenceManager @@ -177,8 +180,18 @@ class OfflineItemManager(downloadRefs: List? = emptyList()) { } } + private fun getMovieDir(): String { + val sharedPref: SharedPreferences = PreferenceManager + .getDefaultSharedPreferences(ChaosflixApplication.APPLICATION_CONTEXT); + var dir = sharedPref.getString("download_folder", null) + if (dir == null) { + dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).path + } + return dir + } + private fun getDownloadDir(): String { - return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).path + DOWNLOAD_DIR; + return getMovieDir() + DOWNLOAD_DIR; } companion object { diff --git a/touch/src/main/java/de/nicidienase/chaosflix/touch/settings/SettingsFragment.kt b/touch/src/main/java/de/nicidienase/chaosflix/touch/settings/SettingsFragment.kt index 977c2f47..38ce0101 100644 --- a/touch/src/main/java/de/nicidienase/chaosflix/touch/settings/SettingsFragment.kt +++ b/touch/src/main/java/de/nicidienase/chaosflix/touch/settings/SettingsFragment.kt @@ -1,13 +1,60 @@ package de.nicidienase.chaosflix.touch.settings +import android.content.Intent import android.os.Bundle -import android.preference.PreferenceFragment +import android.preference.PreferenceManager import android.support.v7.preference.PreferenceFragmentCompat import de.nicidienase.chaosflix.R +import de.nicidienase.chaosflix.touch.ChaosflixApplication +import net.rdrei.android.dirchooser.DirectoryChooserActivity +import net.rdrei.android.dirchooser.DirectoryChooserConfig + class SettingsFragment : PreferenceFragmentCompat() { - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + private val REQUEST_DIRECTORY: Int = 0 + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (requestCode == REQUEST_DIRECTORY) { + if (resultCode == DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) { + val dir = data!!.getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR) + val sharedPref = PreferenceManager.getDefaultSharedPreferences(ChaosflixApplication.APPLICATION_CONTEXT) + val edit = sharedPref.edit() + edit.putString("download_folder", dir) + edit.apply() + this.updateSummary() + } + } + } + + private fun updateSummary() { + val sharedPref = PreferenceManager.getDefaultSharedPreferences(ChaosflixApplication.APPLICATION_CONTEXT) + val folder = sharedPref.getString("download_folder", "") + val pref = this.findPreference("download_folder") + pref.setSummary(folder) + } + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.preferences,rootKey) + updateSummary() + val pref = this.findPreference("download_folder") + + pref.setOnPreferenceClickListener({ + val chooserIntent = Intent(context, DirectoryChooserActivity::class.java) + + val config = DirectoryChooserConfig.builder() + .newDirectoryName("Download folder") + .allowReadOnlyDirectory(false) + .allowNewDirectoryNameModification(true) + .build() + + chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config) + startActivityForResult(chooserIntent, REQUEST_DIRECTORY) + + return@setOnPreferenceClickListener true + + }) } companion object { diff --git a/touch/src/main/res/xml/preferences.xml b/touch/src/main/res/xml/preferences.xml index 76d5a909..c79d3a23 100644 --- a/touch/src/main/res/xml/preferences.xml +++ b/touch/src/main/res/xml/preferences.xml @@ -7,4 +7,9 @@ android:title="Allow downloads over metered networks" android:summary="Enable downloads over mobile or payed networks"/> + \ No newline at end of file