add internal preference for custom storage locations

This commit is contained in:
dkanada 2021-05-13 19:19:39 +09:00
commit d2afefe595
5 changed files with 23 additions and 26 deletions

View file

@ -23,7 +23,7 @@ import java.io.File;
public class CustomGlideModule extends AppGlideModule { public class CustomGlideModule extends AppGlideModule {
@Override @Override
public void applyOptions(@NonNull Context context, GlideBuilder builder) { public void applyOptions(@NonNull Context context, GlideBuilder builder) {
File cacheDir = new File(context.getCacheDir(), "glide"); File cacheDir = new File(PreferenceUtil.getInstance(context).getLocationCache(), "glide");
int size = PreferenceUtil.getInstance(context).getImageCacheSize(); int size = PreferenceUtil.getInstance(context).getImageCacheSize();
builder.setDiskCache(new DiskLruCacheFactory(() -> cacheDir, size)); builder.setDiskCache(new DiskLruCacheFactory(() -> cacheDir, size));

View file

@ -11,6 +11,7 @@ import com.dkanada.gramophone.BuildConfig;
import com.dkanada.gramophone.database.Cache; import com.dkanada.gramophone.database.Cache;
import com.dkanada.gramophone.model.Song; import com.dkanada.gramophone.model.Song;
import com.dkanada.gramophone.util.MusicUtil; import com.dkanada.gramophone.util.MusicUtil;
import com.dkanada.gramophone.util.PreferenceUtil;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -51,7 +52,9 @@ public class DownloadService extends Service {
try { try {
URL url = new URL(MusicUtil.getDownloadUri(song)); URL url = new URL(MusicUtil.getDownloadUri(song));
URLConnection connection = url.openConnection(); URLConnection connection = url.openConnection();
File download = new File(getCacheDir(), "download/" + song.id);
String cache = PreferenceUtil.getInstance(App.getInstance()).getLocationCache();
File download = new File(cache, "download/" + song.id);
File audio = new File(MusicUtil.getFileUri(song)); File audio = new File(MusicUtil.getFileUri(song));
download.getParentFile().mkdirs(); download.getParentFile().mkdirs();

View file

@ -90,7 +90,7 @@ public class LocalPlayer implements Playback {
LeastRecentlyUsedCacheEvictor recentlyUsedCache = new LeastRecentlyUsedCacheEvictor(cacheSize); LeastRecentlyUsedCacheEvictor recentlyUsedCache = new LeastRecentlyUsedCacheEvictor(cacheSize);
ExoDatabaseProvider databaseProvider = new ExoDatabaseProvider(context); ExoDatabaseProvider databaseProvider = new ExoDatabaseProvider(context);
File cacheDirectory = new File(context.getCacheDir(), "exoplayer"); File cacheDirectory = new File(PreferenceUtil.getInstance(context).getLocationCache(), "exoplayer");
simpleCache = new SimpleCache(cacheDirectory, recentlyUsedCache, databaseProvider); simpleCache = new SimpleCache(cacheDirectory, recentlyUsedCache, databaseProvider);
} }

View file

@ -84,7 +84,7 @@ public class MusicUtil {
} }
public static String getFileUri(Song song) { public static String getFileUri(Song song) {
File root = new File(App.getInstance().getCacheDir(), "music"); File root = new File(PreferenceUtil.getInstance(App.getInstance()).getLocationDownload(), "music");
String path = "/" + song.artistName + "/" + song.albumName + "/"; String path = "/" + song.artistName + "/" + song.albumName + "/";
String name = song.discNumber + "." + song.trackNumber + " - " + song.title + "." + song.container; String name = song.discNumber + "." + song.trackNumber + " - " + song.title + "." + song.container;

View file

@ -81,23 +81,27 @@ public final class PreferenceUtil {
public static final String REMEMBER_SHUFFLE = "remember_shuffle"; public static final String REMEMBER_SHUFFLE = "remember_shuffle";
public static final String REMEMBER_QUEUE = "remember_queue"; public static final String REMEMBER_QUEUE = "remember_queue";
public static final String LOCATION_DOWNLOAD = "location_download";
public static final String LOCATION_CACHE = "location_cache";
public static final String IMAGE_CACHE_SIZE = "image_cache_size"; public static final String IMAGE_CACHE_SIZE = "image_cache_size";
public static final String MEDIA_CACHE_SIZE = "media_cache_size"; public static final String MEDIA_CACHE_SIZE = "media_cache_size";
private static PreferenceUtil sInstance; private static PreferenceUtil instance;
private final SharedPreferences mPreferences; private final SharedPreferences mPreferences;
private final Context mContext;
private PreferenceUtil(final Context context) { private PreferenceUtil(final Context context) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(context); mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
mContext = context;
} }
public static PreferenceUtil getInstance(final Context context) { public static PreferenceUtil getInstance(final Context context) {
if (sInstance == null) { if (instance == null) {
sInstance = new PreferenceUtil(context.getApplicationContext()); instance = new PreferenceUtil(context.getApplicationContext());
} }
return sInstance; return instance;
} }
public void registerOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) { public void registerOnSharedPreferenceChangedListener(SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener) {
@ -163,32 +167,14 @@ public final class PreferenceUtil {
return mPreferences.getBoolean(COLORED_NOTIFICATION, true); return mPreferences.getBoolean(COLORED_NOTIFICATION, true);
} }
public void setColoredNotification(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(COLORED_NOTIFICATION, value);
editor.apply();
}
public final boolean getClassicNotification() { public final boolean getClassicNotification() {
return mPreferences.getBoolean(CLASSIC_NOTIFICATION, Build.VERSION.SDK_INT <= Build.VERSION_CODES.O); return mPreferences.getBoolean(CLASSIC_NOTIFICATION, Build.VERSION.SDK_INT <= Build.VERSION_CODES.O);
} }
public void setClassicNotification(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(CLASSIC_NOTIFICATION, value);
editor.apply();
}
public final boolean getColoredShortcuts() { public final boolean getColoredShortcuts() {
return mPreferences.getBoolean(COLORED_SHORTCUTS, true); return mPreferences.getBoolean(COLORED_SHORTCUTS, true);
} }
public void setColoredShortcuts(final boolean value) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(COLORED_SHORTCUTS, value);
editor.apply();
}
public final String getTranscodeCodec() { public final String getTranscodeCodec() {
return mPreferences.getString(TRANSCODE_CODEC, "aac"); return mPreferences.getString(TRANSCODE_CODEC, "aac");
} }
@ -387,6 +373,14 @@ public final class PreferenceUtil {
editor.apply(); editor.apply();
} }
public final String getLocationDownload() {
return mPreferences.getString(LOCATION_DOWNLOAD, mContext.getCacheDir().toString());
}
public final String getLocationCache() {
return mPreferences.getString(LOCATION_CACHE, mContext.getCacheDir().toString());
}
public final int getImageCacheSize() { public final int getImageCacheSize() {
return Integer.parseInt(mPreferences.getString(IMAGE_CACHE_SIZE, "400000000")); return Integer.parseInt(mPreferences.getString(IMAGE_CACHE_SIZE, "400000000"));
} }