Browse Source

Merge pull request #947 from nextcloud/externalStorage

Enhance external Sdcard usage
Tobias Kaminsky 8 năm trước cách đây
mục cha
commit
d1a68cc834

+ 3 - 3
src/main/java/com/owncloud/android/datastorage/DataStorageProvider.java

@@ -24,7 +24,6 @@ package com.owncloud.android.datastorage;
 import android.os.Build;
 
 import com.owncloud.android.MainApp;
-import com.owncloud.android.R;
 import com.owncloud.android.datastorage.providers.EnvironmentStoragePointProvider;
 import com.owncloud.android.datastorage.providers.HardcodedStoragePointProvider;
 import com.owncloud.android.datastorage.providers.IStoragePointProvider;
@@ -65,7 +64,7 @@ public class DataStorageProvider {
         }
 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            for (File f : MainApp.getAppContext().getExternalFilesDirs(null)) {
+            for (File f : MainApp.getAppContext().getExternalMediaDirs()) {
                 if (f != null) {
                     mCachedStoragePoints.add(new StoragePoint(f.getAbsolutePath(), f.getAbsolutePath()));
                 }
@@ -87,7 +86,8 @@ public class DataStorageProvider {
                 return s.getDescription();
             }
         }
-        return MainApp.getAppContext().getString(R.string.storage_description_unknown);
+        // Fallback to just display complete path
+        return path;
     }
 
     public void addStoragePointProvider(IStoragePointProvider provider) {

+ 14 - 6
src/main/java/com/owncloud/android/datastorage/providers/SystemDefaultStoragePointProvider.java

@@ -21,14 +21,15 @@
 
 package com.owncloud.android.datastorage.providers;
 
-import android.os.Environment;
-
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datastorage.StoragePoint;
 
+import java.io.File;
 import java.util.Vector;
 
+import static android.os.Environment.getExternalStorageDirectory;
+
 /**
  * @author Bartosz Przybylski
  */
@@ -42,10 +43,17 @@ public class SystemDefaultStoragePointProvider extends AbstractStoragePointProvi
     public Vector<StoragePoint> getAvailableStoragePoint() {
         Vector<StoragePoint> result = new Vector<>();
 
-        final String defaultStringDesc =
-                MainApp.getAppContext().getString(R.string.storage_description_default);
-        final String path = Environment.getExternalStorageDirectory().getAbsolutePath();
-        result.add(new StoragePoint(defaultStringDesc, path));
+        final String defaultStringDesc = MainApp.getAppContext().getString(R.string.storage_description_default);
+        File path;
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
+            path = MainApp.getAppContext().getExternalMediaDirs()[0];
+        } else {
+            path = getExternalStorageDirectory();
+        }
+
+        if (path != null && path.canWrite()) {
+            result.add(new StoragePoint(defaultStringDesc, path.getAbsolutePath()));
+        }
 
         return result;
     }

+ 5 - 3
src/main/java/com/owncloud/android/operations/DownloadFileOperation.java

@@ -81,9 +81,11 @@ public class DownloadFileOperation extends RemoteOperation {
     }
 
     public String getSavePath() {
-        String path = mFile.getStoragePath();  // re-downloads should be done over the original file
-        if (path != null && path.length() > 0) {
-            return path;
+        if (mFile.getStoragePath() != null) {
+            File path = new File(mFile.getStoragePath());  // re-downloads should be done over the original file
+            if (path.canWrite()) {
+                return path.getAbsolutePath();
+            }
         }
         return FileStorageUtils.getDefaultSavePathFor(mAccount.name, mFile);
     }

+ 30 - 2
src/main/java/com/owncloud/android/ui/activity/StorageMigration.java

@@ -205,8 +205,14 @@ public class StorageMigration {
             if (succeed) {
                 mProgressDialog.hide();
             } else {
-                mProgressDialog.getButton(ProgressDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
-                mProgressDialog.setIndeterminateDrawable(mContext.getResources().getDrawable(R.drawable.image_fail));
+
+                if (code == R.string.file_migration_failed_not_readable) {
+                    mProgressDialog.hide();
+                    askToStillMove();
+                } else {
+                    mProgressDialog.getButton(ProgressDialog.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
+                    mProgressDialog.setIndeterminateDrawable(mContext.getResources().getDrawable(R.drawable.image_fail));
+                }
             }
 
             if (mListener != null) {
@@ -214,6 +220,28 @@ public class StorageMigration {
             }
         }
 
+        private void askToStillMove() {
+            new AlertDialog.Builder(mContext)
+                    .setTitle(R.string.file_migration_source_not_readable_title)
+                    .setMessage(mContext.getString(R.string.file_migration_source_not_readable, mStorageTarget))
+                    .setNegativeButton(R.string.common_no, new OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialogInterface, int i) {
+                            dialogInterface.dismiss();
+                        }
+                    })
+                    .setPositiveButton(R.string.common_yes, new OnClickListener() {
+                        @Override
+                        public void onClick(DialogInterface dialogInterface, int i) {
+                            if (mListener != null) {
+                                mListener.onStorageMigrationFinished(mStorageTarget, true);
+                            }
+                        }
+                    })
+                    .create()
+                    .show();
+        }
+
         protected boolean[] saveAccountsSyncStatus() {
             boolean[] syncs = new boolean[mOcAccounts.length];
             for (int i = 0; i < mOcAccounts.length; ++i) {

+ 5 - 2
src/main/res/values/strings.xml

@@ -412,8 +412,8 @@
     <string name="file_migration_restoring_accounts_configuration">Restoring configuration of accounts&#8230;</string>
     <string name="file_migration_ok_finished">Finished</string>
     <string name="file_migration_failed_not_enough_space">ERROR: Insufficient space</string>
-    <string name="file_migration_failed_not_writable">ERROR: File not writable</string>
-    <string name="file_migration_failed_not_readable">ERROR: File not readable</string>
+    <string name="file_migration_failed_not_writable">ERROR: Destination file not writable</string>
+    <string name="file_migration_failed_not_readable">ERROR: Source file not readable</string>
     <string name="file_migration_failed_dir_already_exists">ERROR: Nextcloud directory already exists</string>
     <string name="file_migration_failed_while_coping">ERROR: Failed during migration</string>
     <string name="file_migration_failed_while_updating_index">ERROR: Failed to updating index</string>
@@ -422,6 +422,9 @@
     <string name="file_migration_override_data_folder">Replace</string>
     <string name="file_migration_use_data_folder">Use</string>
 
+    <string name="file_migration_source_not_readable_title">Source directory not readable!</string>
+    <string name="file_migration_source_not_readable">Do you still want to change the storage path to %1$s?\n\nNote: all data will have to be downloaded again.</string>
+
     <string name="prefs_category_accounts">Accounts</string>
     <string name="prefs_add_account">Add account</string>
     <string name="drawer_manage_accounts">Manage accounts</string>