فهرست منبع

moved the preference accessing to the oC Preference Manager

Andy Scherzinger 9 سال پیش
والد
کامیت
94a6bebb60

+ 69 - 14
src/com/owncloud/android/db/PreferenceManager.java

@@ -22,42 +22,48 @@ package com.owncloud.android.db;
 import android.content.Context;
 import android.content.SharedPreferences;
 
+import com.owncloud.android.utils.FileStorageUtils;
+
 /**
  * Helper to simplify reading of Preferences all around the app
  */
-
-public class PreferenceManager {
-
+public abstract class PreferenceManager {
     /**
      * Constant to access value of last path selected by the user to upload a file shared from other app.
      * 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__SORT_ORDER = "sortOrder";
+    private static final String AUTO_PREF__SORT_ASCENDING = "sortAscending";
+    private static final String PREF__INSTANT_UPLOADING = "instant_uploading";
+    private static final String PREF__INSTANT_VIDEO_UPLOADING = "instant_video_uploading";
+    private static final String PREF__INSTANT_UPLOAD_ON_WIFI = "instant_upload_on_wifi";
+    private static final String PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI = "instant_video_upload_on_wifi";
 
     public static boolean instantPictureUploadEnabled(Context context) {
         return android.preference.PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
-                "instant_uploading",
+                PREF__INSTANT_UPLOADING,
                 false
         );
     }
 
     public static boolean instantVideoUploadEnabled(Context context) {
         return android.preference.PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
-                "instant_video_uploading",
+                PREF__INSTANT_VIDEO_UPLOADING,
                 false
         );
     }
 
     public static boolean instantPictureUploadViaWiFiOnly(Context context) {
         return android.preference.PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
-                "instant_upload_on_wifi",
+                PREF__INSTANT_UPLOAD_ON_WIFI,
                 false
         );
     }
 
     public static boolean instantVideoUploadViaWiFiOnly(Context context) {
         return android.preference.PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
-                "instant_video_upload_on_wifi",
+                PREF__INSTANT_VIDEO_UPLOAD_ON_WIFI,
                 false
         );
     }
@@ -65,27 +71,76 @@ public class PreferenceManager {
     /**
      * 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(String, Context)},
-     *                  or empty String if never saved before.
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @return path     iAbsolute path to a folder, as previously stored by {@link #setLastUploadPath(String, Context)},
+     * or empty String if never saved before.
      */
     public static String getLastUploadPath(Context context) {
         SharedPreferences appPreferences = android.preference.PreferenceManager
-            .getDefaultSharedPreferences(context.getApplicationContext());
+                .getDefaultSharedPreferences(context.getApplicationContext());
         return appPreferences.getString(AUTO_PREF__LAST_UPLOAD_PATH, "");
     }
 
     /**
      * 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.
-     * @param context   Caller {@link Context}, used to access to shared preferences manager.
+     * @param path    Absolute path to a folder.
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
      */
     public static void setLastUploadPath(String path, Context context) {
         SharedPreferences.Editor appPrefs = android.preference.PreferenceManager
-            .getDefaultSharedPreferences(context.getApplicationContext()).edit();
+                .getDefaultSharedPreferences(context.getApplicationContext()).edit();
         appPrefs.putString(AUTO_PREF__LAST_UPLOAD_PATH, path);
         appPrefs.apply();
     }
 
+    /**
+     * Gets the sort order which the user has set last.
+     *
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @return sort order     the sort order, default is {@link FileStorageUtils#SORT_NAME} (sort by name)
+     */
+    public static int getSortOrder(Context context) {
+        SharedPreferences appPreferences = android.preference.PreferenceManager
+                .getDefaultSharedPreferences(context.getApplicationContext());
+        return appPreferences.getInt(AUTO_PREF__SORT_ORDER, FileStorageUtils.SORT_NAME);
+    }
+
+    /**
+     * Save the sort order which the user has set last.
+     *
+     * @param order   the sort order
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     */
+    public static void setSortOrder(int order, Context context) {
+        SharedPreferences.Editor appPreferences = android.preference.PreferenceManager
+                .getDefaultSharedPreferences(context.getApplicationContext()).edit();
+        appPreferences.putInt(AUTO_PREF__SORT_ORDER, order);
+        appPreferences.apply();
+    }
+
+    /**
+     * Gets the ascending order flag which the user has set last.
+     *
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @return ascending order     the ascending order, default is true
+     */
+    public static boolean getSortAscending(Context context) {
+        SharedPreferences appPreferences = android.preference.PreferenceManager
+                .getDefaultSharedPreferences(context.getApplicationContext());
+        return appPreferences.getBoolean(AUTO_PREF__SORT_ASCENDING, true);
+    }
+
+    /**
+     * Saves the ascending order flag which the user has set last.
+     *
+     * @param ascending flag if sorting is ascending or descending
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     */
+    public static void setSortAscending(boolean ascending, Context context) {
+        SharedPreferences.Editor appPreferences = android.preference.PreferenceManager
+                .getDefaultSharedPreferences(context.getApplicationContext()).edit();
+        appPreferences.putBoolean(AUTO_PREF__SORT_ASCENDING, true);
+        appPreferences.apply();
+    }
 }

+ 2 - 8
src/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -33,7 +33,6 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
-import android.content.SharedPreferences;
 import android.content.SyncRequest;
 import android.content.pm.PackageManager;
 import android.content.res.Resources.NotFoundException;
@@ -60,6 +59,7 @@ import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.db.PreferenceManager;
 import com.owncloud.android.files.services.FileDownloader;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader;
@@ -90,7 +90,6 @@ import com.owncloud.android.ui.preview.PreviewTextFragment;
 import com.owncloud.android.ui.preview.PreviewVideoActivity;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.ErrorMessageAdapter;
-import com.owncloud.android.utils.FileStorageUtils;
 import com.owncloud.android.utils.PermissionUtil;
 
 import java.io.File;
@@ -564,12 +563,7 @@ public class FileDisplayActivity extends HookActivity
                 break;
             }
             case R.id.action_sort: {
-                SharedPreferences appPreferences = PreferenceManager
-                        .getDefaultSharedPreferences(this);
-
-                // Read sorting order, default to sort by name ascending
-                Integer sortOrder = appPreferences
-                        .getInt("sortOrder", FileStorageUtils.SORT_NAME);
+                Integer sortOrder = PreferenceManager.getSortOrder(this);
 
                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 builder.setTitle(R.string.actionbar_sort_title)

+ 2 - 2
src/com/owncloud/android/ui/activity/ReceiveExternalFilesActivity.java

@@ -461,8 +461,8 @@ public class ReceiveExternalFilesActivity extends FileActivity
                 .getDefaultSharedPreferences(this);
 
         // Read sorting order, default to sort by name ascending
-        FileStorageUtils.mSortOrder = sharedPreferences.getInt("sortOrder", 0);
-        FileStorageUtils.mSortAscending = sharedPreferences.getBoolean("sortAscending", true);
+        FileStorageUtils.mSortOrder = PreferenceManager.getSortOrder(this);
+        FileStorageUtils.mSortAscending = PreferenceManager.getSortAscending(this);
 
         files = FileStorageUtils.sortFolder(files);
         return files;

+ 5 - 17
src/com/owncloud/android/ui/adapter/FileListListAdapter.java

@@ -26,11 +26,8 @@ package com.owncloud.android.ui.adapter;
 
 import android.accounts.Account;
 import android.content.Context;
-import android.content.SharedPreferences;
 import android.graphics.Bitmap;
 import android.os.Build;
-import android.preference.PreferenceManager;
-import android.text.format.DateUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -46,6 +43,7 @@ import com.owncloud.android.authentication.AccountUtils;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.datamodel.ThumbnailsCacheManager;
+import com.owncloud.android.db.PreferenceManager;
 import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
 import com.owncloud.android.files.services.FileUploader.FileUploaderBinder;
 import com.owncloud.android.services.OperationsService.OperationsServiceBinder;
@@ -75,8 +73,6 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
     private boolean mGridMode;
 
     private enum ViewType {LIST_ITEM, GRID_IMAGE, GRID_ITEM };
-
-    private SharedPreferences mAppPreferences;
     
     public FileListListAdapter(
             boolean justFolders, 
@@ -89,14 +85,10 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
         mAccount = AccountUtils.getCurrentOwnCloudAccount(mContext);
 
         mTransferServiceGetter = transferServiceGetter;
-
-        mAppPreferences = PreferenceManager
-                .getDefaultSharedPreferences(mContext);
         
         // Read sorting order, default to sort by name ascending
-
-        FileStorageUtils.mSortOrder = mAppPreferences.getInt("sortOrder", 0);
-        FileStorageUtils.mSortAscending = mAppPreferences.getBoolean("sortAscending", true);
+        FileStorageUtils.mSortOrder = PreferenceManager.getSortOrder(mContext);
+        FileStorageUtils.mSortAscending = PreferenceManager.getSortAscending(mContext);
         
         // initialise thumbnails cache on background thread
         new ThumbnailsCacheManager.InitDiskCacheTask().execute();
@@ -430,18 +422,14 @@ public class FileListListAdapter extends BaseAdapter implements ListAdapter {
     
     
     public void setSortOrder(Integer order, boolean ascending) {
-        SharedPreferences.Editor editor = mAppPreferences.edit();
-        editor.putInt("sortOrder", order);
-        editor.putBoolean("sortAscending", ascending);
-        editor.commit();
+        PreferenceManager.setSortOrder(order, mContext);
+        PreferenceManager.setSortAscending(ascending, mContext);
         
         FileStorageUtils.mSortOrder = order;
         FileStorageUtils.mSortAscending = ascending;
-        
 
         mFiles = FileStorageUtils.sortFolder(mFiles);
         notifyDataSetChanged();
-
     }
 
     public void setGridMode(boolean gridMode) {