Skip to content

Commit

Permalink
Add sdome improvements for files storage. Add context menu in transfe…
Browse files Browse the repository at this point in the history
…rs to show the absolute file path for a transfer
  • Loading branch information
a-pavlov committed Sep 3, 2022
1 parent c57442c commit e00a1cd
Show file tree
Hide file tree
Showing 15 changed files with 213 additions and 42 deletions.
12 changes: 7 additions & 5 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="34"
android:versionName="34">
android:versionCode="35"
android:versionName="35">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Expand All @@ -27,7 +27,8 @@
<activity
android:name="org.dkf.jmule.activities.SplashActivity"
android:label="@string/app_name"
android:theme="@style/SplashTheme">
android:theme="@style/SplashTheme"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand All @@ -36,7 +37,8 @@

<activity
android:name="org.dkf.jmule.activities.MainActivity"
android:label="@string/app_name" >
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="org.dkf.jmule.android.ACTION_SHOW_TRANSFERS" />
<action android:name="org.dkf.jmule.android.ACTION_REQUEST_SHUTDOWN" />
Expand Down Expand Up @@ -96,7 +98,7 @@
android:resource="@xml/provider_paths" />
</provider>

<service android:name="org.dkf.jmule.ED2KService">
<service android:name="org.dkf.jmule.ED2KService" android:exported="false">
<intent-filter>
<action android:name="org.dkf.jmule.INTENT_OPEN"/>
<action android:name="org.dkf.jmule.INTENT_CLOSE"/>
Expand Down
18 changes: 9 additions & 9 deletions android/jdonkey/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.android.tools.build:gradle:7.2.1'
}
}

Expand Down Expand Up @@ -66,13 +66,13 @@ def changeApkOutput(variant) {
boolean isAssembleRelease = gradle.startParameter.taskNames.contains("assembleRelease") || gradle.startParameter.taskNames.contains("assembleBasicRelease")

android {
compileSdkVersion 31
compileSdkVersion 32

defaultConfig {
applicationId 'org.dkf.jmule'
versionName project.ext.versionName
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 32
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -100,9 +100,6 @@ android {
}
}

lintOptions {
abortOnError false
}

signingConfigs {
release {
Expand Down Expand Up @@ -154,6 +151,9 @@ android {
resValue "string", "application_label", "Mule"
}
}
lint {
abortOnError false
}

applicationVariants.all { variant ->
changeApkOutput(variant)
Expand All @@ -170,9 +170,9 @@ dependencies {
implementation 'androidx.documentfile:documentfile:1.0.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.preference:preference:1.1.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
//releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.9.1'
//testImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.9.1'
testImplementation 'org.robolectric:robolectric:4.6'
androidTestImplementation 'androidx.test:core:' + project.ext.coreVersion
androidTestImplementation 'androidx.test.ext:junit:' + project.ext.extJUnitVersion
Expand Down
27 changes: 19 additions & 8 deletions android/jdonkey/src/main/java/org/dkf/jmule/AndroidPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,34 @@
*/
public final class AndroidPaths {
private static final boolean USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 = true;
private final Context context;
private final Application app;

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

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

public File data() {
if (SystemUtils.hasAndroid10OrNewer()) {
File externalDir = context.getExternalFilesDir(null);
return USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 ? externalDir : context.getFilesDir();
if (SystemUtils.hasAndroid10()) {
return app.getExternalFilesDir(null);
}

// On Android 11 and up, they finally let us use File objects in the public download directory as long as we have permission from the user
return android11AndUpStorage();
}

if (SystemUtils.hasAndroid10OrNewer()) {
File externalDir = app.getExternalFilesDir(null);
return USE_EXTERNAL_STORAGE_DIR_ON_OR_AFTER_ANDROID_10 ? externalDir : app.getFilesDir();
}

/* For Older versions of Android where we used to have access to write to external storage
* <externalStoragePath>
* <externalStoragePath>/Download/FrostWire/
*/
String path = ConfigurationManager.instance().getStoragePath();
return new File(ConfigurationManager.instance().getStoragePath());
return new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
}

public static byte getFileType(String filePath, boolean returnTorrentsAsDocument) {
Expand All @@ -79,6 +86,10 @@ public static byte getFileType(String filePath, boolean returnTorrentsAsDocument
return result;
}

public static File android11AndUpStorage() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
}


/**
* FILE_TYPE_AUDIO -> "Music"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.dkf.jmule;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.os.Looper;
Expand Down Expand Up @@ -88,7 +89,7 @@ enum NetworkType {

private final int sdk;

public AndroidPlatform(Context app) {
public AndroidPlatform(Application app) {
fileSystem = buildFileSystem(app);
systemPaths = new AndroidPaths(app);
appSettings = new AndroidSettings();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.dkf.jmule;

import android.app.Application;
import com.squareup.leakcanary.LeakCanary;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -40,14 +39,6 @@ public void onTerminate() {
public void onCreate() {
super.onCreate();

if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}

LeakCanary.install(this);

try {

Platforms.set(new AndroidPlatform(this));
Expand Down
7 changes: 6 additions & 1 deletion android/jdonkey/src/main/java/org/dkf/jmule/Platforms.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
package org.dkf.jmule;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author gubatron
* @author aldenml
*/
public final class Platforms {
private static final Logger LOG = LoggerFactory.getLogger(Platforms.class);

private static AndroidPlatform platform;

Expand Down Expand Up @@ -64,6 +67,8 @@ public static AndroidSettings appSettings() {
}

public static File data() {
return get().systemPaths().data();
File f = get().systemPaths().data();
LOG.warn("PLATFORM FILE " + f.getAbsolutePath());
return f;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ private String populateTransferDownloadMenuAction(Transfer download, List<MenuAc
R.string.transfers_context_menu_copy_link_copied, download.toLink()));

items.add(new CancelMenuAction(context.get(), download, !download.isComplete()));
items.add(new ShowThePathMenuAction(context.get(), download));
return title;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.dkf.jmule.adapters.menu;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.dkf.jmule.Platforms;
import org.dkf.jmule.R;
import org.dkf.jmule.transfers.Transfer;
import org.dkf.jmule.util.Ref;
import org.dkf.jmule.views.AbstractDialog;
import org.dkf.jmule.views.MenuAction;

import java.lang.ref.WeakReference;

public class ShowThePathMenuAction extends MenuAction {
private final Transfer transfer;

@SuppressWarnings("WeakerAccess")
public final static class ShowThePathDialog extends AbstractDialog {
Context context;
String filename;

public static ShowThePathDialog newInstance(Context context, String filename) {
return new ShowThePathDialog(context, filename);
}

// Important to keep this guy 'public', even if IntelliJ thinks you shouldn't.
// otherwise, the app crashes when you turn the screen and the dialog can't
public ShowThePathDialog(Context contex, String filename) {
super(R.layout.dialog_default_info);
this.context = contex;
this.filename = filename;
}

@Override
protected void initComponents(Dialog dlg, Bundle savedInstanceState) {
TextView title = findView(dlg, R.id.dialog_default_info_title);
title.setText(R.string.transfers_context_menu_show_path_title);
TextView text = findView(dlg, R.id.dialog_default_info_text);
String pathsTemplate = context.getResources().getString(R.string.transfers_context_menu_show_path_body);
String pathsStr = String.format(pathsTemplate, Platforms.data().getAbsolutePath(), filename);
text.setText(pathsStr);
Button okButton = findView(dlg, R.id.dialog_default_info_button_ok);
okButton.setText(android.R.string.ok);
okButton.setOnClickListener(new OkButtonOnClickListener(dlg));
}
}

private final static class OkButtonOnClickListener implements View.OnClickListener {
private final WeakReference<Dialog> dialogPtr;

OkButtonOnClickListener(Dialog newNoWifiInformationDialog) {
this.dialogPtr = Ref.weak(newNoWifiInformationDialog);
}

@Override
public void onClick(View view) {
if (Ref.alive(dialogPtr)) {
dialogPtr.get().dismiss();
}
}
}

public ShowThePathMenuAction(Context context
, Transfer transfer) {
super(context, R.drawable.ic_search_black_24dp, R.string.transfers_context_menu_show_path_title);
this.transfer = transfer;
}

@Override
protected void onClick(Context context) {
if (transfer != null) {
ShowThePathDialog dialog = ShowThePathDialog.newInstance(context, transfer.getFilePath());
dialog.show(((Activity)context).getFragmentManager());
}
}
}

27 changes: 21 additions & 6 deletions android/jdonkey/src/main/java/org/dkf/jmule/util/SystemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public final class SystemUtils {

private static final int VERSION_CODE_KITKAT = 19;
private static final int VERSION_SDK_NOUGAT_7_0 = 24;
private static final int VERSION_SDK_NOUGAT_7_0_N = 24;
private static final int VERSION_SDK_ANDROID_10_Q = 29;
private static final int VERSION_SDK_ANDROID_11_R = 30;
private static final int VERSION_SDK_ANDROID_12_S = 31;

private SystemUtils() {
}
Expand Down Expand Up @@ -195,6 +199,23 @@ private static boolean hasSdkOrNewer(int versionCode) {
return Build.VERSION.SDK_INT >= versionCode;
}

@SuppressWarnings("SameParameterValue")
private static boolean hasSdk(int versionCode) {
return Build.VERSION.SDK_INT == versionCode;
}


public static boolean hasAndroid10() {
return hasSdk(VERSION_SDK_ANDROID_10_Q);
}

/**
* Used to determine if the device is running Android11 or greater
*/
public static boolean hasAndroid11OrNewer() {
return hasSdkOrNewer(VERSION_SDK_ANDROID_11_R);
}

/**
* Used to determine if the device is running
* KitKat (Android 4.4) or greater
Expand Down Expand Up @@ -226,12 +247,6 @@ public static boolean hasNougatOrNewer() {
return hasSdkOrNewer(VERSION_SDK_NOUGAT_7_0);
}

/**
* Used to determine if the device is running Android11 or greater
*/
public static boolean hasAndroid11OrNewer() {
return hasSdkOrNewer(30); //Build.VERSION_CODES.R
}

/**
* We call it "safe" because if any exceptions are thrown,
Expand Down
Loading

0 comments on commit e00a1cd

Please sign in to comment.