Эх сурвалжийг харах

Fix crash when showing default storage friendly name

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky 4 жил өмнө
parent
commit
60b1f3702e

+ 42 - 0
src/androidTest/java/com/owncloud/android/utils/FileStorageUtilsTest.kt

@@ -0,0 +1,42 @@
+/*
+ *
+ * Nextcloud Android client application
+ *
+ * @author Tobias Kaminsky
+ * Copyright (C) 2020 Tobias Kaminsky
+ * Copyright (C) 2020 Nextcloud GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+package com.owncloud.android.utils
+
+import com.owncloud.android.AbstractIT
+import com.owncloud.android.utils.FileStorageUtils.pathToUserFriendlyDisplay
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class FileStorageUtilsTest : AbstractIT() {
+    @Test
+    fun testPathToUserFriendlyDisplay() {
+        assertEquals("/", pathToUserFriendlyDisplay("/"))
+        assertEquals("/sdcard/", pathToUserFriendlyDisplay("/sdcard/"))
+        assertEquals("/sdcard/test/1/", pathToUserFriendlyDisplay("/sdcard/test/1/"))
+        assertEquals("Internal storage/Movies/", pathToUserFriendlyDisplay("/storage/emulated/0/Movies/"))
+        assertEquals("Internal storage/", pathToUserFriendlyDisplay("/storage/emulated/0/"))
+    }
+
+    private fun pathToUserFriendlyDisplay(path: String): String {
+        return pathToUserFriendlyDisplay(path, targetContext, targetContext.resources)
+    }
+}

+ 18 - 14
src/main/java/com/owncloud/android/utils/FileStorageUtils.java

@@ -22,7 +22,6 @@ package com.owncloud.android.utils;
 import android.Manifest;
 import android.accounts.Account;
 import android.annotation.TargetApi;
-import android.app.Activity;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.content.res.Resources;
@@ -473,7 +472,7 @@ public final class FileStorageUtils {
      */
     @SuppressFBWarnings(value = "DMI_HARDCODED_ABSOLUTE_FILENAME",
         justification = "Default Android fallback storage path")
-    public static List<String> getStorageDirectories(Activity activity) {
+    public static List<String> getStorageDirectories(Context context) {
         // Final set of paths
         final List<String> rv = new ArrayList<>();
         // Primary physical SD-CARD (not emulated)
@@ -524,11 +523,11 @@ public final class FileStorageUtils {
             final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
             Collections.addAll(rv, rawSecondaryStorages);
         }
-        if (SDK_INT >= Build.VERSION_CODES.M && checkStoragePermission(activity)) {
+        if (SDK_INT >= Build.VERSION_CODES.M && checkStoragePermission(context)) {
             rv.clear();
         }
         if (SDK_INT >= Build.VERSION_CODES.KITKAT) {
-            String strings[] = getExtSdCardPathsForActivity(activity);
+            String strings[] = getExtSdCardPathsForActivity(context);
             File f;
             for (String s : strings) {
                 f = new File(s);
@@ -543,17 +542,17 @@ public final class FileStorageUtils {
 
     /**
      * Update the local path summary display. If a special directory is recognized, it is replaced by its name.
-     *
-     * Example: /storage/emulated/0/Movies -> Internal Storage / Movies
-     * Example: /storage/ABC/non/standard/directory -> ABC /non/standard/directory
+     * <p>
+     * Example: /storage/emulated/0/Movies -> Internal Storage / Movies Example: /storage/ABC/non/standard/directory ->
+     * ABC /non/standard/directory
      *
      * @param path the path to display
      * @return a user friendly path as defined in examples, or {@param path} if the storage device isn't recognized.
      */
-    public static String pathToUserFriendlyDisplay(String path, Activity activity, Resources resources) {
+    public static String pathToUserFriendlyDisplay(String path, Context context, Resources resources) {
         // Determine storage device (external, sdcard...)
         String storageDevice = null;
-        for (String storageDirectory : FileStorageUtils.getStorageDirectories(activity)) {
+        for (String storageDirectory : FileStorageUtils.getStorageDirectories(context)) {
             if (path.startsWith(storageDirectory)) {
                 storageDevice = storageDirectory;
                 break;
@@ -566,7 +565,13 @@ public final class FileStorageUtils {
         }
 
         // Default to full path without storage device path
-        String storageFolder = path.substring(storageDevice.length() + 1);
+        String storageFolder;
+        try {
+            storageFolder = path.substring(storageDevice.length() + 1);
+        } catch (StringIndexOutOfBoundsException e) {
+            storageFolder = "";
+        }
+
         FileStorageUtils.StandardDirectory standardDirectory = FileStorageUtils.StandardDirectory.fromPath(storageFolder);
         if (standardDirectory != null) { // Friendly name of standard directory
             storageFolder = " " + resources.getString(standardDirectory.getDisplayName());
@@ -584,12 +589,11 @@ public final class FileStorageUtils {
 
     /**
      * Taken from https://github.com/TeamAmaze/AmazeFileManager/blob/d11e0d2874c6067910e58e059859431a31ad6aee/app/src
-     * /main/java/com/amaze/filemanager/activities/superclasses/PermissionsActivity.java#L47 on
-     * 14.02.2019
+     * /main/java/com/amaze/filemanager/activities/superclasses/PermissionsActivity.java#L47 on 14.02.2019
      */
-    private static boolean checkStoragePermission(Activity activity) {
+    private static boolean checkStoragePermission(Context context) {
         // Verify that all required contact permissions have been granted.
-        return ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
+        return ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
             == PackageManager.PERMISSION_GRANTED;
     }