2323import android .content .Context ;
2424import android .os .Environment ;
2525
26+ import org .apache .commons .io .FilenameUtils ;
2627import org .dkf .jmule .fragments .TransfersFragment ;
2728import org .dkf .jmule .util .SystemUtils ;
2829import org .slf4j .Logger ;
2930import org .slf4j .LoggerFactory ;
3031
3132import java .io .File ;
33+ import java .util .HashMap ;
34+ import java .util .Map ;
3235
3336/**
3437 * @author gubatron
@@ -39,6 +42,9 @@ public final class AndroidPaths {
3942 private static final boolean USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 = true ;
4043 private final Context context ;
4144
45+ private static final Map <Byte , String > fileTypeFolders = new HashMap <>();
46+ private static final Object fileTypeFoldersLock = new Object ();
47+
4248 public AndroidPaths (Context app ) {
4349 this .context = app ;
4450 }
@@ -56,4 +62,74 @@ public File data() {
5662 String path = ConfigurationManager .instance ().getStoragePath ();
5763 return new File (ConfigurationManager .instance ().getStoragePath ());
5864 }
65+
66+ public static byte getFileType (String filePath , boolean returnTorrentsAsDocument ) {
67+ byte result = Constants .FILE_TYPE_UNKNOWN ;
68+
69+ MediaType mt = MediaType .getMediaTypeForExtension (FilenameUtils .getExtension (filePath ));
70+
71+ if (mt != null ) {
72+ result = (byte ) mt .getId ();
73+ }
74+
75+ if (returnTorrentsAsDocument && result == Constants .FILE_TYPE_TORRENTS ) {
76+ result = Constants .FILE_TYPE_DOCUMENTS ;
77+ }
78+
79+ return result ;
80+ }
81+
82+
83+ /**
84+ * FILE_TYPE_AUDIO -> "Music"
85+ * FILE_TYPE_VIDEOS -> "Movies"
86+ * ...
87+ * Based on Android's Environment.DIRECTORY_XXX constants
88+ * We'll use these for MediaStore relative path prefixes concatenated to "/FrostWire" so the user
89+ * can easily find what's been downloaded with FrostWire in external folders.
90+ */
91+ private static String getFileTypeExternalRelativeFolderName (byte fileType ) {
92+ synchronized (fileTypeFoldersLock ) {
93+ // thread safe lazy load check
94+ if (fileTypeFolders .size () == 0 ) {
95+ fileTypeFolders .put (Constants .FILE_TYPE_AUDIO , Environment .DIRECTORY_MUSIC );
96+ fileTypeFolders .put (Constants .FILE_TYPE_VIDEOS , Environment .DIRECTORY_MOVIES );
97+ fileTypeFolders .put (Constants .FILE_TYPE_RINGTONES , Environment .DIRECTORY_RINGTONES );
98+ fileTypeFolders .put (Constants .FILE_TYPE_PICTURES , Environment .DIRECTORY_PICTURES );
99+ fileTypeFolders .put (Constants .FILE_TYPE_TORRENTS , Environment .DIRECTORY_DOWNLOADS );
100+ fileTypeFolders .put (Constants .FILE_TYPE_DOCUMENTS , Environment .DIRECTORY_DOCUMENTS );
101+ }
102+ }
103+ return fileTypeFolders .get (fileType );
104+ }
105+
106+ public static String getRelativeFolderPath (File f ) {
107+ //if (BTEngine.ctx.dataDir == null) {
108+ // throw new RuntimeException("AndroidPaths.getRelativeFolderPath() BTEngine.ctx.dataDir is null, check your logic");
109+ //}
110+
111+ byte fileType = AndroidPaths .getFileType (f .getAbsolutePath (), true );
112+
113+ // "Music","Movies","Pictures","Download"
114+ String fileTypeSubfolder = AndroidPaths .getFileTypeExternalRelativeFolderName (fileType );
115+
116+ // "Music/FrostWire"
117+ String mediaStoreFolderPrefix = fileTypeSubfolder + "/FrostWire" ;
118+ mediaStoreFolderPrefix = mediaStoreFolderPrefix .replace ("//" ,"/" );
119+
120+ String fullOriginalFilePath = f .getAbsolutePath ();
121+
122+ // BTEngine.ctx.dataDir -> /storage/emulated/0/Android/data/com.frostwire.android/files/FrostWire/TorrentData
123+ // Let's remove this from the fullOriginalFilePath and we should now have only either the file name by itself
124+ // or the torrent folders and sub-folders containing it
125+ String removedDataPathFromFilePath = fullOriginalFilePath .replace (Platforms .data ().getAbsolutePath () + "/" , "" );
126+
127+ // Single file download, not contained by folders or sub-folders
128+ if (removedDataPathFromFilePath .equals (f .getName ())) {
129+ return mediaStoreFolderPrefix ;
130+ }
131+
132+ String fileFoldersWithoutDataPath = removedDataPathFromFilePath .replace (f .getName (), "" );
133+ return (mediaStoreFolderPrefix + "/" + fileFoldersWithoutDataPath ).replace ("//" ,"/" );
134+ }
59135}
0 commit comments