add file select preference for media downloads

This commit is contained in:
dkanada 2021-05-30 11:43:46 +09:00
commit dd599c6979
5 changed files with 71 additions and 0 deletions

View file

@ -130,6 +130,10 @@ public final class PreferenceUtil {
return instance;
}
public SharedPreferences getPreferences() {
return mPreferences;
}
public void registerOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) {
mPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
}

View file

@ -0,0 +1,59 @@
package com.dkanada.gramophone.views.settings;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.AttributeSet;
import android.view.View;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import com.codekidlabs.storagechooser.StorageChooser;
import com.dkanada.gramophone.util.PreferenceUtil;
public class FilePreference extends Preference implements View.OnClickListener, StorageChooser.OnSelectListener {
private final SharedPreferences preferences;
private final String defaultLocation;
public FilePreference(Context context, AttributeSet attrs) {
super(context, attrs);
preferences = PreferenceUtil.getInstance(getContext()).getPreferences();
defaultLocation = getContext().getCacheDir().toString();
setSummary(preferences.getString(getKey(), defaultLocation));
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
holder.itemView.setOnClickListener(this);
}
@Override
public void onSelect(String path) {
preferences.edit().putString(getKey(), path).apply();
setSummary(path);
}
@Override
public void onClick(View v) {
Activity activity = (Activity) getContext();
FragmentManager fragmentManager = activity.getFragmentManager();
StorageChooser chooser = new StorageChooser.Builder()
.withActivity(activity)
.withFragmentManager(fragmentManager)
.withPredefinedPath(preferences.getString(getKey(), defaultLocation))
.allowCustomPath(true)
.allowAddFolder(true)
.setType(StorageChooser.DIRECTORY_CHOOSER)
.build();
chooser.setOnSelectListener(this);
chooser.show();
}
}

View file

@ -126,6 +126,7 @@
<string name="pref_title_remember_shuffle">Remember Shuffle</string>
<string name="pref_title_remember_queue">Remember Queue</string>
<string name="pref_title_page_size">Page Size</string>
<string name="pref_title_download_location">Download Location</string>
<string name="pref_title_image_cache_size">Images</string>
<string name="pref_title_media_cache_size">Media</string>

View file

@ -4,6 +4,11 @@
<com.dkanada.gramophone.views.settings.JellyPreferenceCategory android:title="@string/pref_header_cache">
<com.dkanada.gramophone.views.settings.FilePreference
app:iconSpaceReserved="false"
android:key="location_download"
android:title="@string/pref_title_download_location" />
<com.kabouzeid.appthemehelper.common.prefs.supportv7.ATEListPreference
app:iconSpaceReserved="false"
android:defaultValue="400"