more folders picker

This commit is contained in:
gdlbo
2022-08-05 20:39:24 +03:00
parent 9dd3a26bab
commit efa28aa3d6
5 changed files with 93 additions and 23 deletions
@@ -1,11 +1,16 @@
package ru.vtosters.lite.music;
import static ru.vtosters.lite.utils.Preferences.getMusicDir;
import android.app.DownloadManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.vk.dto.music.MusicTrack;
import org.json.JSONException;
@@ -53,12 +58,10 @@ public class MP3Downloader {
if (responseNode.has("url")) {
var url = responseNode.optString("url");
Log.d("MP3Downloader", url);
if (url != null) {
if (url.endsWith(".mp3")) {
downloadMP3(track.toString(), url);
} else if (VKM3UParser.isM3U8(url)) {
AudioDownloader.downloadM3U8(track);
}
if (url.endsWith(".mp3")) {
downloadMP3(track.toString(), url);
} else if (VKM3UParser.isM3U8(url)) {
AudioDownloader.downloadM3U8(track);
}
}
}
@@ -69,13 +72,14 @@ public class MP3Downloader {
});
}
@RequiresApi(api = Build.VERSION_CODES.M)
private static void downloadMP3(String title, String url) {
var downloadManager = AndroidUtils.getGlobalContext().getSystemService(DownloadManager.class);
var request = new DownloadManager.Request(Uri.parse(url))
.setAllowedOverRoaming(true)
.setTitle("Downloading " + title)
.setDestinationInExternalFilesDir(AndroidUtils.getGlobalContext(),
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(),
getMusicDir().getAbsolutePath(),
title + ".mp3")
.setVisibleInDownloadsUi(true);
downloadManager.enqueue(request);
@@ -94,6 +94,19 @@ public class MediaFragment extends MaterialPreferenceToolbarFragment {
findPreference("downloads_directory").setSummary(
"Текущая папка: " + Preferences.getDownloadsDir()
);
findPreference("photos_directory").setSummary(
"Текущая папка: " + Preferences.getPhotosDir()
);
findPreference("videos_directory").setSummary(
"Текущая папка: " + Preferences.getVideosDir()
);
findPreference("music_directory").setSummary(
"Текущая папка: " + Preferences.getMusicDir()
);
findPreference("downloads_directory").setOnPreferenceClickListener(preference -> {
var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
@@ -101,6 +114,27 @@ public class MediaFragment extends MaterialPreferenceToolbarFragment {
return true;
});
findPreference("photos_directory").setOnPreferenceClickListener(preference -> {
var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, REQUEST_CODE_SET_DOWNLOAD_DIRECTORY);
return true;
});
findPreference("videos_directory").setOnPreferenceClickListener(preference -> {
var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, REQUEST_CODE_SET_DOWNLOAD_DIRECTORY);
return true;
});
findPreference("music_directory").setOnPreferenceClickListener(preference -> {
var intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(intent, REQUEST_CODE_SET_DOWNLOAD_DIRECTORY);
return true;
});
if (isLoggedIn()) {
findPreference("lastfm_auth").setSummary("Вы авторизованы как " + Scrobbler.getUserName());
} else {
@@ -392,4 +392,22 @@ public class Preferences {
"downloads_directory",
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()));
}
public static File getPhotosDir() {
return new File(getPreferences().getString(
"photos_directory",
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()));
}
public static File getVideosDir() {
return new File(getPreferences().getString(
"videos_directory",
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()));
}
public static File getMusicDir() {
return new File(getPreferences().getString(
"videos_directory",
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath()));
}
}