Implemented custom preferences for the download directory.

This commit is contained in:
Erik Tews 2018-01-03 03:59:10 +01:00
parent c5d7ddb6a7
commit 0cdd7169ab
5 changed files with 80 additions and 7 deletions

View file

@ -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()
}

View file

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:name=".touch.ChaosflixApplication"
@ -15,7 +16,8 @@
android:icon="@drawable/icon_notext"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
tools:replace="android:theme">
<activity
android:name=".touch.SplashActivity"
android:label="@string/app_name"
@ -53,6 +55,8 @@
android:name=".touch.settings.SettingsActivity"
android:parentActivityName=".touch.browse.BrowseActivity"/>
<activity android:name="net.rdrei.android.dirchooser.DirectoryChooserActivity" />
<service
android:name=".touch.sync.DownloadJobService"
android:permission="android.permission.BIND_JOB_SERVICE"/>

View file

@ -94,9 +94,12 @@ class OfflineItemManager(downloadRefs: List<Long>? = 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<Long>? = 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 {

View file

@ -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 {

View file

@ -7,4 +7,9 @@
android:title="Allow downloads over metered networks"
android:summary="Enable downloads over mobile or payed networks"/>
<Preference
android:id="@+id/download_folder"
android:key="download_folder"
android:title="Download folder"
android:focusable="false" />
</PreferenceScreen>