Sfoglia il codice sorgente

cleanup preference handling

AndyScherzinger 6 anni fa
parent
commit
4c117baa2b

+ 2 - 8
src/main/java/com/owncloud/android/MainApp.java

@@ -244,7 +244,7 @@ public class MainApp extends MultiDexApplication {
                 StoragePoint[] storagePoints = DataStorageProvider.getInstance().getAvailableStoragePoints();
                 String storagePath = appPrefs.getString(Preferences.PreferenceKeys.STORAGE_PATH, "");
                 if (TextUtils.isEmpty(storagePath)) {
-                    if (appPrefs.getInt(WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE, 0) != 0) {
+                    if (PreferenceManager.getLastSeenVersionCode(this) != 0) {
                         // We already used the app, but no storage is set - fix that!
                         appPrefs.edit().putString(Preferences.PreferenceKeys.STORAGE_PATH,
                                 Environment.getExternalStorageDirectory().getAbsolutePath()).commit();
@@ -494,9 +494,8 @@ public class MainApp extends MultiDexApplication {
         String packageName = getAppContext().getPackageName();
         String version = "";
 
-        PackageInfo pInfo = null;
         try {
-            pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
+            PackageInfo pInfo = getAppContext().getPackageManager().getPackageInfo(packageName, 0);
             if (pInfo != null) {
                 version = pInfo.versionName;
             }
@@ -662,9 +661,4 @@ public class MainApp extends MultiDexApplication {
             }
         }
     }
-
-    static public int getLastSeenVersionCode(Context context) {
-        SharedPreferences pref = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
-        return pref.getInt(WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE, 0);
-    }
 }

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

@@ -46,6 +46,7 @@ public abstract class PreferenceManager {
     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";
     private static final String AUTO_PREF__GRID_COLUMNS = "grid_columns";
+    public static final String AUTO_PREF__LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
     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_PATH_USE_SUBFOLDERS = "instant_upload_path_use_subfolders";
@@ -425,6 +426,26 @@ public abstract class PreferenceManager {
         saveFloatPreference(context, AUTO_PREF__GRID_COLUMNS, gridColumns);
     }
 
+    /**
+     * Gets the last seen version code right before updating.
+     *
+     * @param context Caller {@link Context}, used to access to shared preferences manager.
+     * @return grid columns     grid columns
+     */
+    public static int getLastSeenVersionCode(Context context) {
+        return getDefaultSharedPreferences(context).getInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, 0);
+    }
+
+    /**
+     * Saves the version code as the last seen version code.
+     *
+     * @param context   Caller {@link Context}, used to access to shared preferences manager.
+     * @param versionCode the app's version code
+     */
+    public static void setLastSeenVersionCode(Context context, int versionCode) {
+        saveIntPreference(context, AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode);
+    }
+
     private static void saveBooleanPreference(Context context, String key, boolean value) {
         SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
         appPreferences.putBoolean(key, value).apply();

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

@@ -362,7 +362,7 @@ public class FileDisplayActivity extends HookActivity
             ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(getContentResolver());
 
             int lastSeenVersion = arbitraryDataProvider.getIntegerValue(account,
-                    WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE);
+                    PreferenceManager.AUTO_PREF__LAST_SEEN_VERSION_CODE);
 
             if (MainApp.getVersionCode() > lastSeenVersion) {
                 OwnCloudVersion serverVersion = AccountUtils.getServerVersionForAccount(account, this);
@@ -371,7 +371,7 @@ public class FileDisplayActivity extends HookActivity
                     DisplayUtils.showServerOutdatedSnackbar(this);
                 }
 
-                arbitraryDataProvider.storeOrUpdateKeyValue(account.name, WhatsNewActivity.KEY_LAST_SEEN_VERSION_CODE,
+                arbitraryDataProvider.storeOrUpdateKeyValue(account.name, PreferenceManager.AUTO_PREF__LAST_SEEN_VERSION_CODE,
                         String.valueOf(MainApp.getVersionCode()));
             }
         }

+ 5 - 16
src/main/java/com/owncloud/android/ui/activity/WhatsNewActivity.java

@@ -24,10 +24,8 @@ package com.owncloud.android.ui.activity;
 
 import android.content.Context;
 import android.content.Intent;
-import android.content.SharedPreferences;
 import android.os.Build;
 import android.os.Bundle;
-import android.preference.PreferenceManager;
 import android.support.v4.app.FragmentActivity;
 import android.support.v4.view.ViewPager;
 import android.view.View;
@@ -38,6 +36,7 @@ import android.widget.TextView;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.R;
 import com.owncloud.android.authentication.AccountUtils;
+import com.owncloud.android.db.PreferenceManager;
 import com.owncloud.android.features.FeatureItem;
 import com.owncloud.android.ui.adapter.FeaturesViewAdapter;
 import com.owncloud.android.ui.adapter.FeaturesWebViewAdapter;
@@ -49,8 +48,6 @@ import com.owncloud.android.utils.ThemeUtils;
  */
 public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
 
-    public static final String KEY_LAST_SEEN_VERSION_CODE = "lastSeenVersionCode";
-
     private ImageButton mForwardFinishButton;
     private Button mSkipButton;
     private ProgressIndicator mProgress;
@@ -138,18 +135,12 @@ public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPa
     }
 
     private void onFinish() {
-        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
-        SharedPreferences.Editor editor = pref.edit();
-        editor.putInt(KEY_LAST_SEEN_VERSION_CODE, MainApp.getVersionCode());
-        editor.apply();
+        PreferenceManager.setLastSeenVersionCode(this, MainApp.getVersionCode());
     }
 
     static public void runIfNeeded(Context context) {
-        if (!context.getResources().getBoolean(R.bool.show_whats_new)) {
-            return;
-        }
-
-        if (context instanceof WhatsNewActivity) {
+        if (!context.getResources().getBoolean(R.bool.show_whats_new)
+                || context instanceof WhatsNewActivity) {
             return;
         }
 
@@ -185,10 +176,8 @@ public class WhatsNewActivity extends FragmentActivity implements ViewPager.OnPa
     static private FeatureItem[] getWhatsNew(Context context) {
         int itemVersionCode = 30030000;
 
-        int lastSeenVersionCode = MainApp.getLastSeenVersionCode(context);
-
         if (!isFirstRun(context) && MainApp.getVersionCode() >= itemVersionCode
-                && lastSeenVersionCode < itemVersionCode) {
+                && PreferenceManager.getLastSeenVersionCode(context) < itemVersionCode) {
             return new FeatureItem[]{new FeatureItem(R.drawable.whats_new_device_credentials,
                     R.string.whats_new_device_credentials_title, R.string.whats_new_device_credentials_content,
                     false, false)};