Skip to content

Commit 9a67e6f

Browse files
committed
Minor fixes
1 parent d7e6874 commit 9a67e6f

3 files changed

Lines changed: 22 additions & 26 deletions

File tree

app/src/main/java/com/gianlu/aria2android/FileUtil.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,39 @@
1111
import java.lang.reflect.Array;
1212
import java.lang.reflect.Method;
1313

14+
import androidx.annotation.NonNull;
1415
import androidx.annotation.Nullable;
1516

1617
public final class FileUtil {
1718
private static final String PRIMARY_VOLUME_NAME = "primary";
1819

1920
@Nullable
20-
public static String getFullPathFromTreeUri(@Nullable final Uri treeUri, Context con) {
21-
if (treeUri == null) {
21+
public static String getFullPathFromTreeUri(@Nullable Uri treeUri, @NonNull Context con) {
22+
if (treeUri == null)
2223
return null;
23-
}
24+
2425
String volumePath = FileUtil.getVolumePath(FileUtil.getVolumeIdFromTreeUri(treeUri), con);
25-
if (volumePath == null) {
26+
if (volumePath == null)
2627
return File.separator;
27-
}
28-
if (volumePath.endsWith(File.separator)) {
28+
29+
if (volumePath.endsWith(File.separator))
2930
volumePath = volumePath.substring(0, volumePath.length() - 1);
30-
}
3131

3232
String documentPath = FileUtil.getDocumentPathFromTreeUri(treeUri);
33-
if (documentPath.endsWith(File.separator)) {
33+
if (documentPath != null && documentPath.endsWith(File.separator))
3434
documentPath = documentPath.substring(0, documentPath.length() - 1);
35-
}
3635

37-
if (documentPath.length() > 0) {
38-
if (documentPath.startsWith(File.separator)) {
39-
return volumePath + documentPath;
40-
} else {
41-
return volumePath + File.separator + documentPath;
42-
}
36+
if (documentPath != null && documentPath.length() > 0) {
37+
if (documentPath.startsWith(File.separator)) return volumePath + documentPath;
38+
else return volumePath + File.separator + documentPath;
4339
} else {
4440
return volumePath;
4541
}
4642
}
4743

4844
@Nullable
4945
@SuppressWarnings({"ConstantConditions", "JavaReflectionMemberAccess"})
50-
private static String getVolumePath(final String volumeId, Context con) {
46+
private static String getVolumePath(@NonNull String volumeId, @NonNull Context con) {
5147
try {
5248
StorageManager mStorageManager = (StorageManager) con.getSystemService(Context.STORAGE_SERVICE);
5349
Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
@@ -65,15 +61,13 @@ private static String getVolumePath(final String volumeId, Context con) {
6561
Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement);
6662

6763
// primary volume?
68-
if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) {
64+
if (primary && PRIMARY_VOLUME_NAME.equals(volumeId))
6965
return (String) getPath.invoke(storageVolumeElement);
70-
}
7166

7267
// other volumes?
7368
if (uuid != null) {
74-
if (uuid.equals(volumeId)) {
69+
if (uuid.equals(volumeId))
7570
return (String) getPath.invoke(storageVolumeElement);
76-
}
7771
}
7872
}
7973

@@ -84,16 +78,18 @@ private static String getVolumePath(final String volumeId, Context con) {
8478
}
8579
}
8680

81+
@Nullable
8782
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
88-
private static String getVolumeIdFromTreeUri(final Uri treeUri) {
83+
private static String getVolumeIdFromTreeUri(@NonNull Uri treeUri) {
8984
final String docId = DocumentsContract.getTreeDocumentId(treeUri);
9085
final String[] split = docId.split(":");
9186
if (split.length > 0) return split[0];
9287
else return null;
9388
}
9489

90+
@Nullable
9591
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
96-
private static String getDocumentPathFromTreeUri(final Uri treeUri) {
92+
private static String getDocumentPathFromTreeUri(@NonNull Uri treeUri) {
9793
final String docId = DocumentsContract.getTreeDocumentId(treeUri);
9894
final String[] split = docId.split(":");
9995
if ((split.length >= 2) && (split[1] != null)) return split[1];

app/src/main/java/com/gianlu/aria2android/MainActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,11 @@ protected void onCreate(Bundle savedInstanceState) {
170170
outputPath.setOverrideClickListener(v -> {
171171
try {
172172
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
173+
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
173174
startActivityForResult(intent, STORAGE_ACCESS_CODE);
174175
return true;
175176
} catch (ActivityNotFoundException ex) {
176-
Toaster.with(MainActivity.this).message(R.string.noOpenTree).ex(ex).show();
177+
Toaster.with(this).message(R.string.noOpenTree).ex(ex).show();
177178
return false;
178179
}
179180
});
@@ -301,13 +302,12 @@ protected void onCreate(Bundle savedInstanceState) {
301302
}
302303
}
303304

304-
private boolean toggleService(boolean on) {
305+
private void toggleService(boolean on) {
305306
boolean successful;
306307
if (on) successful = startService();
307308
else successful = stopService();
308309

309310
if (successful) updateUiStatus(on);
310-
return successful;
311311
}
312312

313313
private void updateUiStatus(boolean on) {

0 commit comments

Comments
 (0)