Skip to content

Commit

Permalink
Fast fix for storing files since Android 10 and before.
Browse files Browse the repository at this point in the history
  • Loading branch information
a-pavlov committed Feb 12, 2022
1 parent f574c17 commit 318c18c
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 25 deletions.
4 changes: 2 additions & 2 deletions android/jdonkey/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
xmlns:tools="http://schemas.android.com/tools"
package="org.dkf.jmule"
android:installLocation="auto"
android:versionCode="31"
android:versionName="31">
android:versionCode="32"
android:versionName="32">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Expand Down
84 changes: 75 additions & 9 deletions android/jdonkey/src/main/java/org/dkf/jmule/AndroidPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@
package org.dkf.jmule;


import android.app.Application;
import android.content.Context;
import android.os.Environment;

import org.dkf.jmule.fragments.TransfersFragment;
import org.dkf.jmule.util.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

Expand All @@ -46,33 +53,92 @@ public final class AndroidPaths {

private static final String TEMP_PATH = "temp";

private static final boolean USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 = true;

private final Context context;
//private final Application app;

public AndroidPaths(Context app) {
this.context = app;
}

// public File data(Context ctx) {
//return new File(storage(), DOWNLOADS_PATH);
// return storage(ctx);
//}

public File data() {
//return new File(storage(), DOWNLOADS_PATH);
return storage();
}

public File metadata() {
return new File(storage(), METADATA_PATH);
}
//public File metadata(Context ctx) {
// return new File(storage(ctx), METADATA_PATH);
//}

public File temp() {
return new File(context.getExternalFilesDir(null), TEMP_PATH);
}
//public File temp() {
// return new File(context.getExternalFilesDir(null), TEMP_PATH);
//}

private static final Logger LOG = LoggerFactory.getLogger(AndroidPaths.class);
/*
private static File storage(Context ctx) {
if (SystemUtils.hasAndroid10OrNewer()) {
File externalDir = ctx.getExternalFilesDir(null);
return new File(USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 ?
externalDir : ctx.getFilesDir(),
STORAGE_PATH);
}
*/
/* For Older versions of Android where we used to have access to write to external storage
* <externalStoragePath>/FrostWire/
*/
/* String path = ConfigurationManager.instance().getString(Constants.PREF_KEY_STORAGE_PATH, Environment.getExternalStorageDirectory().getAbsolutePath());
if (path.toLowerCase().endsWith("/" + STORAGE_PATH.toLowerCase())) {
return new File(path);
} else {
return new File(path, STORAGE_PATH);
}
*/
//String path = ConfigurationManager.instance().getString(Constants.PREF_KEY_STORAGE_PATH);
/*if (path.toLowerCase().endsWith("/" + STORAGE_PATH.toLowerCase())) {
return new File(path);
} else {
return new File(path, STORAGE_PATH);
}
*/
//return new File(path);
//}

private File storage() {
if (SystemUtils.hasAndroid10OrNewer()) {
File externalDir = context.getExternalFilesDir(null);
LOG.info("storage: external path {}", externalDir);
return USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 ? externalDir : context.getFilesDir();

}

/* For Older versions of Android where we used to have access to write to external storage
* <externalStoragePath>/FrostWire/
*/
String path = ConfigurationManager.instance().getStoragePath();
//ConfigurationManager.instance().getString(Constants.PREF_KEY_STORAGE_PATH, Environment.getExternalStorageDirectory().getAbsolutePath());
LOG.info("storage: internal path {}", path);
return new File(ConfigurationManager.instance().getStoragePath());
/*
if (path.toLowerCase().endsWith("/" + STORAGE_PATH.toLowerCase())) {
return new File(path);
} else {
return new File(path);
}*/

private static File storage() {
String path = ConfigurationManager.instance().getString(Constants.PREF_KEY_STORAGE_PATH);
//String path = ConfigurationManager.instance().getString(Constants.PREF_KEY_STORAGE_PATH);
/*if (path.toLowerCase().endsWith("/" + STORAGE_PATH.toLowerCase())) {
return new File(path);
} else {
return new File(path, STORAGE_PATH);
}
*/
return new File(path);
//return new File(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public String getString(String key) {
return preferences.getString(key, "");
}

public String getString(String key, String defValue) {
if (preferences == null) {
throw new IllegalStateException("getString(key=" + key + ") failed, preferences:SharedPreferences is null");
}
return preferences.getString(key, defValue);
}

public void setString(String key, String value) {
editor.putString(key, value);
editor.commit();
Expand Down
4 changes: 2 additions & 2 deletions android/jdonkey/src/main/java/org/dkf/jmule/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public Transfer downloadLink(final EMuleLink link) {
try {
return new ED2KTransfer(service.addTransfer(link.getHash()
, link.getNumberValue()
, new File(ConfigurationManager.instance().getStoragePath(), link.getStringValue())));
, new File(Platforms.data(), link.getStringValue())));
} catch(JED2KException e) {
log.error("download link error {}", e);
} catch(Exception e) {
Expand All @@ -393,7 +393,7 @@ public Transfer startDownload(final String slink) {
if (link.getType().equals(EMuleLink.LinkType.FILE)) {
return new ED2KTransfer(service.addTransfer(link.getHash()
, link.getNumberValue()
, new File(ConfigurationManager.instance().getStoragePath(), link.getStringValue())));
, new File(Platforms.data(), link.getStringValue())));
} else {
// message to userv link is incorrect type
}
Expand Down
18 changes: 12 additions & 6 deletions android/jdonkey/src/main/java/org/dkf/jmule/Platforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package org.dkf.jmule;

import android.content.Context;

import java.io.File;

/**
Expand Down Expand Up @@ -68,6 +70,10 @@ public static AndroidSettings appSettings() {
*
* @return
*/
//public static File data(Context ctx) {
// return get().systemPaths().data(ctx);
//}

public static File data() {
return get().systemPaths().data();
}
Expand All @@ -77,16 +83,16 @@ public static File data() {
*
* @return
*/
public static File metadata() {
return get().systemPaths().metadata();
}
//public static File metadata(Context ctx) {
// return get().systemPaths().metadata(ctx);
//}

/**
* Shortcut to current platform file system temp method.
*
* @return
*/
public static File temp() {
return get().systemPaths().temp();
}
//public static File temp() {
// return get().systemPaths().temp();
//}
}
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
}

// TODO: refactor and move this method for a common place when needed
private static String saveViewContent(Context context, Uri uri, String name) {
/*private static String saveViewContent(Context context, Uri uri, String name) {
InputStream inStream = null;
OutputStream outStream = null;
if (!Platforms.temp().exists()) {
Expand All @@ -851,7 +851,7 @@ private static String saveViewContent(Context context, Uri uri, String name) {
}
return "file://" + target.getAbsolutePath();
}
}*/

public Fragment getFragmentByNavMenuId(int id) {
if (id == R.id.menu_main_search) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.dkf.jed2k.protocol.Hash;
import org.dkf.jed2k.protocol.server.SharedFileEntry;
import org.dkf.jmule.Engine;
import org.dkf.jmule.Platforms;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -102,7 +103,7 @@ public List<Transfer> getTransfers() {
}

public Transfer download(final Hash hash, long size, final String fileName) throws JED2KException {
return Engine.instance().startDownload(hash, size, new File(ConfigurationManager.instance().getStoragePath(), fileName));
return Engine.instance().startDownload(hash, size, new File(Platforms.data(), fileName));
/*
os = null;
FileChannel channel = null;
Expand Down
7 changes: 5 additions & 2 deletions android/jdonkey/src/main/java/org/dkf/jmule/util/UIUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,11 @@ public static String getFileTypeAsString(Resources resources, byte fileType) {
public static void openFile(Context context, String filePath, String mime, boolean useFileProvider) {
try {
if (filePath != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(getFileUri(context, filePath, useFileProvider), Intent.normalizeMimeType(mime));
Intent i = new Intent(Constants.MIME_TYPE_ANDROID_PACKAGE_ARCHIVE.equals(mime) ?
Intent.ACTION_INSTALL_PACKAGE : Intent.ACTION_VIEW);
Uri fileUri = getFileUri(context, filePath, useFileProvider);
LOG.info("openFile(...) -> fileUri=" + fileUri.toString(), true);
i.setDataAndType(fileUri, Intent.normalizeMimeType(mime));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(i);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
*/
public class StoragePreference extends DialogPreference {

private final Logger log = LoggerFactory.getLogger(StoragePreference.class);
private static final Logger log = LoggerFactory.getLogger(StoragePreference.class);
private AlertDialog confirmDlg;
private String selectedPath;

Expand Down

0 comments on commit 318c18c

Please sign in to comment.