Browse Source

Merge pull request #3086 from nextcloud/rememberLocalUploadPath

Remember last path which was used on local file selection
Andy Scherzinger 6 years ago
parent
commit
726cb1a3eb

+ 22 - 0
src/main/java/com/owncloud/android/db/PreferenceManager.java

@@ -42,6 +42,7 @@ public final class PreferenceManager {
      * Value handled by the app without direct access in the UI.
      */
     private static final String AUTO_PREF__LAST_UPLOAD_PATH = "last_upload_path";
+    private static final String AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH = "upload_from_local_last_path";
     private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL = "prefs_upload_file_extension_map_url";
     private static final String AUTO_PREF__UPLOAD_FILE_EXTENSION_URL = "prefs_upload_file_extension_url";
     private static final String AUTO_PREF__UPLOADER_BEHAVIOR = "prefs_uploader_behaviour";
@@ -186,6 +187,27 @@ public final class PreferenceManager {
         saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
     }
 
+    /**
+     * Gets the last local path where the user selected to do an upload from.
+     *
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @return path     Absolute path to a folder, as previously stored by
+     * {@link #setUploadFromLocalLastPath(Context, String)}, or empty String if never saved before.
+     */
+    public static String getUploadFromLocalLastPath(Context context) {
+        return getDefaultSharedPreferences(context).getString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, "");
+    }
+
+    /**
+     * Saves the path where the user selected to do the last local upload of a file from.
+     *
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @param path    Absolute path to a folder.
+     */
+    public static void setUploadFromLocalLastPath(Context context, String path) {
+        saveStringPreference(context, AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, path);
+    }
+
     /**
      * Gets the lock preference configured by the user.
      *

+ 16 - 4
src/main/java/com/owncloud/android/ui/activity/UploadFilesActivity.java

@@ -126,12 +126,22 @@ public class UploadFilesActivity extends FileActivity implements
             mLocalFolderPickerMode = extras.getBoolean(KEY_LOCAL_FOLDER_PICKER_MODE, false);
         }
 
-        if(savedInstanceState != null) {
+        if (savedInstanceState != null) {
             mCurrentDir = new File(savedInstanceState.getString(UploadFilesActivity.KEY_DIRECTORY_PATH, Environment
                     .getExternalStorageDirectory().getAbsolutePath()));
             mSelectAll = savedInstanceState.getBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
         } else {
-            mCurrentDir = Environment.getExternalStorageDirectory();
+            String lastUploadFrom = PreferenceManager.getUploadFromLocalLastPath(this);
+
+            if (!lastUploadFrom.isEmpty()) {
+                mCurrentDir = new File(lastUploadFrom);
+
+                while (!mCurrentDir.exists()) {
+                    mCurrentDir = mCurrentDir.getParentFile();
+                }
+            } else {
+                mCurrentDir = Environment.getExternalStorageDirectory();
+            }
         }
         
         mAccountOnCreation = getAccount();
@@ -487,9 +497,11 @@ public class UploadFilesActivity extends FileActivity implements
             finish();
 
         } else if (v.getId() == R.id.upload_files_btn_upload) {
-            if(mLocalFolderPickerMode) {
+            PreferenceManager.setUploadFromLocalLastPath(this, mCurrentDir.getAbsolutePath());
+
+            if (mLocalFolderPickerMode) {
                 Intent data = new Intent();
-                if(mCurrentDir != null) {
+                if (mCurrentDir != null) {
                     data.putExtra(EXTRA_CHOSEN_FILES, mCurrentDir.getAbsolutePath());
                 }
                 setResult(RESULT_OK, data);