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

Move upload preferences to AppPreferences interface

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
Chris Narkiewicz 6 жил өмнө
parent
commit
5dabec67bf

+ 63 - 1
src/main/java/com/nextcloud/client/preferences/AppPreferences.java

@@ -24,8 +24,70 @@ public interface AppPreferences {
     boolean instantPictureUploadEnabled();
     boolean instantVideoUploadEnabled();
 
-    void setShowDetailedTimestampEnabled(boolean showDetailedTimestamp);
+    /**
+     * Gets the selected file extension position the user selected to do the
+     * last upload of a url file shared from other app.
+     *
+     * @return selectedPos     the selected file extension position.
+     */
+    int getUploadUrlFileExtensionUrlSelectedPos();
+
+    /**
+     * Saves the selected file extension position the user selected to do the
+     * last upload of a url file shared from other app.
+     *
+     * @param selectedPos the selected file extension position.
+     */
+    void setUploadUrlFileExtensionUrlSelectedPos(int selectedPos);
+
+    /**
+     * Gets the selected map file extension position the user selected to
+     * do the last upload of a url file shared from other app.
+     *
+     * @return selectedPos     the selected file extension position.
+     */
+    int getUploadMapFileExtensionUrlSelectedPos();
+
+    /**
+     * Saves the selected map file extension position the user selected to
+     * do the last upload of a url file shared from other app.
+     *
+     * @param selectedPos the selected file extension position.
+     */
+    void setUploadMapFileExtensionUrlSelectedPos(int selectedPos);
+
+    /**
+     * Gets the last local path where the user selected to do an upload from.
+     *
+     * @return path     Absolute path to a folder, as previously stored by
+     * {@link #setUploadFromLocalLastPath(String)}, or empty String if never saved before.
+     */
+    String getUploadFromLocalLastPath();
+
+    /**
+     * Saves the path where the user selected to do the last local upload of a file from.
+     *
+     * @param path    Absolute path to a folder.
+     */
+    void setUploadFromLocalLastPath(String path);
+
+    /**
+     * Gets the path where the user selected to do the last upload of a file shared from other app.
+     *
+     * @return path     Absolute path to a folder, as previously stored by {@link #setLastUploadPath(String)},
+     * or empty String if never saved before.
+     */
+    String getLastUploadPath();
+
+    /**
+     * Saves the path where the user selected to do the last upload of a file shared from other app.
+     *
+     * @param path    Absolute path to a folder.
+     */
+    void setLastUploadPath(String path);
+
     boolean isShowDetailedTimestampEnabled();
+    void setShowDetailedTimestampEnabled(boolean showDetailedTimestamp);
 
     boolean isShowMediaScanNotifications();
     void setShowMediaScanNotifications(boolean showMediaScanNotification);

+ 24 - 70
src/main/java/com/nextcloud/client/preferences/PreferenceManager.java

@@ -135,90 +135,44 @@ public final class PreferenceManager implements AppPreferences {
         return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("show_hidden_files_pref", false);
     }
 
-    /**
-     * Gets the selected file extension position the user selected to do the last upload of a url file shared from other
-     * app.
-     *
-     * @param context Caller {@link Context}, used to access to shared preferences manager.
-     * @return selectedPos     the selected file extension position.
-     */
-    public static int getUploadUrlFileExtensionUrlSelectedPos(Context context) {
-        return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
+    @Override
+    public int getUploadUrlFileExtensionUrlSelectedPos() {
+        return preferences.getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, 0);
     }
 
-    /**
-     * Saves the selected file extension position the user selected to do the last upload of a url file shared from
-     * other app.
-     *
-     * @param context     Caller {@link Context}, used to access to shared preferences manager.
-     * @param selectedPos the selected file extension position.
-     */
-    public static void setUploadUrlFileExtensionUrlSelectedPos(Context context, int selectedPos) {
-        saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos);
+    @Override
+    public void setUploadUrlFileExtensionUrlSelectedPos(int selectedPos) {
+        preferences.edit().putInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_URL, selectedPos).apply();
     }
 
-    /**
-     * Gets the selected map file extension position the user selected to do the last upload of a url file shared
-     * from other app.
-     *
-     * @param context Caller {@link Context}, used to access to shared preferences manager.
-     * @return selectedPos     the selected file extension position.
-     */
-    public static int getUploadMapFileExtensionUrlSelectedPos(Context context) {
-        return getDefaultSharedPreferences(context).getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
+    @Override
+    public int getUploadMapFileExtensionUrlSelectedPos() {
+        return preferences.getInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, 0);
     }
 
-    /**
-     * Saves the selected map file extension position the user selected to do the last upload of a url file shared from
-     * other app.
-     *
-     * @param context     Caller {@link Context}, used to access to shared preferences manager.
-     * @param selectedPos the selected file extension position.
-     */
-    public static void setUploadMapFileExtensionUrlSelectedPos(Context context, int selectedPos) {
-        saveIntPreference(context, AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos);
+    @Override
+    public void setUploadMapFileExtensionUrlSelectedPos(int selectedPos) {
+        preferences.edit().putInt(AUTO_PREF__UPLOAD_FILE_EXTENSION_MAP_URL, selectedPos).apply();
     }
 
-    /**
-     * Gets the path where the user selected to do the last upload of a file shared from other app.
-     *
-     * @param context Caller {@link Context}, used to access to shared preferences manager.
-     * @return path     Absolute path to a folder, as previously stored by {@link #setLastUploadPath(Context, String)},
-     * or empty String if never saved before.
-     */
-    public static String getLastUploadPath(Context context) {
-        return getDefaultSharedPreferences(context).getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
+    @Override
+    public String getUploadFromLocalLastPath() {
+        return preferences.getString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, "");
     }
 
-    /**
-     * Saves the path where the user selected to do the last upload of a file shared from other app.
-     *
-     * @param context Caller {@link Context}, used to access to shared preferences manager.
-     * @param path    Absolute path to a folder.
-     */
-    public static void setLastUploadPath(Context context, String path) {
-        saveStringPreference(context, AUTO_PREF__LAST_UPLOAD_PATH, path);
+    @Override
+    public void setUploadFromLocalLastPath(String path) {
+        preferences.edit().putString(AUTO_PREF__UPLOAD_FROM_LOCAL_LAST_PATH, path).apply();
     }
 
-    /**
-     * 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, "");
+    @Override
+    public String getLastUploadPath() {
+        return preferences.getString(AUTO_PREF__LAST_UPLOAD_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);
+    @Override
+    public void setLastUploadPath(String path) {
+        preferences.edit().putString(AUTO_PREF__LAST_UPLOAD_PATH, path).apply();
     }
 
     /**

+ 16 - 6
src/main/java/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

@@ -60,6 +60,7 @@ import android.widget.ProgressBar;
 import android.widget.Spinner;
 import android.widget.TextView;
 
+import com.nextcloud.client.preferences.AppPreferences;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
@@ -158,6 +159,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
     private TextView mEmptyListHeadline;
     private ImageView mEmptyListIcon;
     private ProgressBar mEmptyListProgress;
+    private AppPreferences preferences;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -175,6 +177,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
         mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
 
         super.onCreate(savedInstanceState);
+        preferences = PreferenceManager.fromContext(this);
 
         // Listen for sync messages
         IntentFilter syncIntentFilter = new IntentFilter(RefreshFolderOperation.
@@ -340,6 +343,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
         private int mFileCategory;
 
         private Spinner mSpinner;
+        private AppPreferences preferences;
 
         public static DialogInputUploadFilename newInstance(String subjectText, String extraText) {
             DialogInputUploadFilename dialog = new DialogInputUploadFilename();
@@ -350,6 +354,12 @@ public class ReceiveExternalFilesActivity extends FileActivity
             return dialog;
         }
 
+        @Override
+        public void onAttach(Context context) {
+            super.onAttach(context);
+            preferences = PreferenceManager.fromContext(context);
+        }
+
         @NonNull
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
@@ -401,7 +411,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
                 mFilenameSuffix.add(DESKTOP_FILE_SUFFIX);
                 adapter.add(String.format(str,DESKTOP_FILE_SUFFIX));
 
-                selectPos = PreferenceManager.getUploadUrlFileExtensionUrlSelectedPos(getActivity());
+                selectPos = preferences.getUploadUrlFileExtensionUrlSelectedPos();
                 mFileCategory = CATEGORY_URL;
             } else if (isIntentFromGoogleMap(subjectText, extraText)) {
                 String str = getString(R.string.upload_file_dialog_filetype_googlemap_shortcut);
@@ -421,7 +431,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
                 mFilenameSuffix.add(DESKTOP_FILE_SUFFIX);
                 adapter.add(String.format(str,DESKTOP_FILE_SUFFIX));
 
-                selectPos = PreferenceManager.getUploadMapFileExtensionUrlSelectedPos(getActivity());
+                selectPos = preferences.getUploadMapFileExtensionUrlSelectedPos();
                 mFileCategory = CATEGORY_MAPS_URL;
             }
 
@@ -501,10 +511,10 @@ public class ReceiveExternalFilesActivity extends FileActivity
         private void saveSelection(int selectPos) {
             switch (mFileCategory) {
                 case CATEGORY_URL:
-                    PreferenceManager.setUploadUrlFileExtensionUrlSelectedPos(getActivity(), selectPos);
+                    preferences.setUploadUrlFileExtensionUrlSelectedPos(selectPos);
                     break;
                 case CATEGORY_MAPS_URL:
-                    PreferenceManager.setUploadMapFileExtensionUrlSelectedPos(getActivity(), selectPos);
+                    preferences.setUploadMapFileExtensionUrlSelectedPos(selectPos);
                     break;
                 default:
                     Log_OC.d(TAG, "Simple text snippet only: no selection to be persisted");
@@ -948,7 +958,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
         UriUploader.UriUploaderResultCode resultCode = uploader.uploadUris();
 
         // Save the path to shared preferences; even if upload is not possible, user chose the folder
-        PreferenceManager.setLastUploadPath(this, mUploadPath);
+        preferences.setLastUploadPath(mUploadPath);
 
         if (resultCode == UriUploader.UriUploaderResultCode.OK) {
             finish();
@@ -1022,7 +1032,7 @@ public class ReceiveExternalFilesActivity extends FileActivity
         }
 
         if (mParents.empty()) {
-            String lastPath = PreferenceManager.getLastUploadPath(this);
+            String lastPath = preferences.getLastUploadPath();
             // "/" equals root-directory
             if ("/".equals(lastPath)) {
                 mParents.add("");

+ 7 - 3
src/main/java/com/owncloud/android/ui/activity/UploadFilesActivity.java

@@ -42,6 +42,7 @@ import android.widget.Spinner;
 import android.widget.TextView;
 
 import com.google.android.material.button.MaterialButton;
+import com.nextcloud.client.preferences.AppPreferences;
 import com.owncloud.android.R;
 import com.nextcloud.client.preferences.PreferenceManager;
 import com.owncloud.android.files.services.FileUploader;
@@ -119,11 +120,13 @@ public class UploadFilesActivity extends FileActivity implements
     public static final String REQUEST_CODE_KEY = "requestCode";
     private int requestCode;
     private LocalStoragePathPickerDialogFragment dialog;
+    private AppPreferences preferences;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         Log_OC.d(TAG, "onCreate() start");
         super.onCreate(savedInstanceState);
+        preferences = PreferenceManager.fromContext(this);
 
         Bundle extras = getIntent().getExtras();
         if (extras != null) {
@@ -136,7 +139,7 @@ public class UploadFilesActivity extends FileActivity implements
                     .getExternalStorageDirectory().getAbsolutePath()));
             mSelectAll = savedInstanceState.getBoolean(UploadFilesActivity.KEY_ALL_SELECTED, false);
         } else {
-            String lastUploadFrom = PreferenceManager.getUploadFromLocalLastPath(this);
+            String lastUploadFrom = preferences.getUploadFromLocalLastPath();
 
             if (!lastUploadFrom.isEmpty()) {
                 mCurrentDir = new File(lastUploadFrom);
@@ -631,8 +634,9 @@ public class UploadFilesActivity extends FileActivity implements
             finish();
 
         } else if (v.getId() == R.id.upload_files_btn_upload) {
-            PreferenceManager.setUploadFromLocalLastPath(this, mCurrentDir.getAbsolutePath());
-
+            if(mCurrentDir != null) {
+                preferences.setUploadFromLocalLastPath(mCurrentDir.getAbsolutePath());
+            }
             if (mLocalFolderPickerMode) {
                 Intent data = new Intent();
                 if (mCurrentDir != null) {