Skip to content

Commit

Permalink
Port Librarian and related code. Fix compilation.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-pavlov committed Feb 16, 2022
1 parent 926aa2a commit 41bc919
Show file tree
Hide file tree
Showing 60 changed files with 1,987 additions and 79 deletions.
76 changes: 76 additions & 0 deletions android/jdonkey/src/main/java/org/dkf/jmule/AndroidPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import android.content.Context;
import android.os.Environment;

import org.apache.commons.io.FilenameUtils;
import org.dkf.jmule.fragments.TransfersFragment;
import org.dkf.jmule.util.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* @author gubatron
Expand All @@ -39,6 +42,9 @@ public final class AndroidPaths {
private static final boolean USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 = true;
private final Context context;

private static final Map<Byte, String> fileTypeFolders = new HashMap<>();
private static final Object fileTypeFoldersLock = new Object();

public AndroidPaths(Context app) {
this.context = app;
}
Expand All @@ -56,4 +62,74 @@ public File data() {
String path = ConfigurationManager.instance().getStoragePath();
return new File(ConfigurationManager.instance().getStoragePath());
}

public static byte getFileType(String filePath, boolean returnTorrentsAsDocument) {
byte result = Constants.FILE_TYPE_UNKNOWN;

MediaType mt = MediaType.getMediaTypeForExtension(FilenameUtils.getExtension(filePath));

if (mt != null) {
result = (byte) mt.getId();
}

if (returnTorrentsAsDocument && result == Constants.FILE_TYPE_TORRENTS) {
result = Constants.FILE_TYPE_DOCUMENTS;
}

return result;
}


/**
* FILE_TYPE_AUDIO -> "Music"
* FILE_TYPE_VIDEOS -> "Movies"
* ...
* Based on Android's Environment.DIRECTORY_XXX constants
* We'll use these for MediaStore relative path prefixes concatenated to "/FrostWire" so the user
* can easily find what's been downloaded with FrostWire in external folders.
*/
private static String getFileTypeExternalRelativeFolderName(byte fileType) {
synchronized (fileTypeFoldersLock) {
// thread safe lazy load check
if (fileTypeFolders.size() == 0) {
fileTypeFolders.put(Constants.FILE_TYPE_AUDIO, Environment.DIRECTORY_MUSIC);
fileTypeFolders.put(Constants.FILE_TYPE_VIDEOS, Environment.DIRECTORY_MOVIES);
fileTypeFolders.put(Constants.FILE_TYPE_RINGTONES, Environment.DIRECTORY_RINGTONES);
fileTypeFolders.put(Constants.FILE_TYPE_PICTURES, Environment.DIRECTORY_PICTURES);
fileTypeFolders.put(Constants.FILE_TYPE_TORRENTS, Environment.DIRECTORY_DOWNLOADS);
fileTypeFolders.put(Constants.FILE_TYPE_DOCUMENTS, Environment.DIRECTORY_DOCUMENTS);
}
}
return fileTypeFolders.get(fileType);
}

public static String getRelativeFolderPath(File f) {
//if (BTEngine.ctx.dataDir == null) {
// throw new RuntimeException("AndroidPaths.getRelativeFolderPath() BTEngine.ctx.dataDir is null, check your logic");
//}

byte fileType = AndroidPaths.getFileType(f.getAbsolutePath(), true);

// "Music","Movies","Pictures","Download"
String fileTypeSubfolder = AndroidPaths.getFileTypeExternalRelativeFolderName(fileType);

// "Music/FrostWire"
String mediaStoreFolderPrefix = fileTypeSubfolder + "/FrostWire";
mediaStoreFolderPrefix = mediaStoreFolderPrefix.replace("//","/");

String fullOriginalFilePath = f.getAbsolutePath();

// BTEngine.ctx.dataDir -> /storage/emulated/0/Android/data/com.frostwire.android/files/FrostWire/TorrentData
// Let's remove this from the fullOriginalFilePath and we should now have only either the file name by itself
// or the torrent folders and sub-folders containing it
String removedDataPathFromFilePath = fullOriginalFilePath.replace(Platforms.data().getAbsolutePath() + "/", "");

// Single file download, not contained by folders or sub-folders
if (removedDataPathFromFilePath.equals(f.getName())) {
return mediaStoreFolderPrefix;
}

String fileFoldersWithoutDataPath = removedDataPathFromFilePath.replace(f.getName(), "");
return (mediaStoreFolderPrefix + "/" + fileFoldersWithoutDataPath).replace("//","/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import android.content.Context;
import android.os.Build;
import android.os.Looper;

import org.dkf.jmule.util.SystemUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -173,11 +174,15 @@ private static FileSystem buildFileSystem(Context app) {
fs = new DefaultFileSystem() {
@Override
public void scan(File file) {
// do nothing
Librarian.instance().scan(app, file);
}
};
}

return fs;
}

public boolean isUIThread() {
return Looper.myLooper() == Looper.getMainLooper();
}
}
3 changes: 3 additions & 0 deletions android/jdonkey/src/main/java/org/dkf/jmule/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private Constants() {
public static final String EXTRA_DOWNLOAD_COMPLETE_PATH = "org.dkf.jmule.EXTRA_DOWNLOAD_COMPLETE_PATH";
public static final String EXTRA_REFRESH_FILE_TYPE = "org.dkf.jmule.EXTRA_REFRESH_FILE_TYPE";
public static final String EXTRA_FINISH_MAIN_ACTIVITY = "org.dkf.jmule.EXTRA_FINISH_MAIN_ACTIVITY";
public static final String ACTION_FILE_ADDED_OR_REMOVED = "org.dkf.jmule.ACTION_FILE_ADDED_OR_REMOVED";

public static final String BROWSE_PEER_FRAGMENT_LISTVIEW_FIRST_VISIBLE_POSITION = "org.dkf.jmule.BROWSE_PEER_FRAGMENT_LISTVIEW_FIRST_VISIBLE_POSITION.";

Expand All @@ -128,10 +129,12 @@ private Constants() {
public static final byte FILE_TYPE_VIDEOS = 0x02;
public static final byte FILE_TYPE_DOCUMENTS = 0x03;
public static final byte FILE_TYPE_APPLICATIONS = 0x04;
public static final byte FILE_TYPE_RINGTONES = 0x05;
public static final byte FILE_TYPE_TORRENTS = 0x06;
public static final byte FILE_TYPE_ARCHIVE = 0x7;
public static final byte FILE_TYPE_CD_IMAGE = 0x8;
public static final byte FILE_TYPE_OTHERS = 0x9;
public static final byte FILE_TYPE_UNKNOWN = 0x10;

public static final String MIME_TYPE_ANDROID_PACKAGE_ARCHIVE = "application/vnd.android.package-archive";
public static final String MIME_TYPE_BITTORRENT = "application/x-bittorrent";
Expand Down
Loading

0 comments on commit 41bc919

Please sign in to comment.