Move legacy free pref to AppPreferences

Finally, the beast is dead...

Signed-off-by: Chris Narkiewicz <hello@ezaquarii.com>
This commit is contained in:
Chris Narkiewicz 2019-03-11 18:07:20 +00:00
parent 77ccba1ada
commit ee99611546
No known key found for this signature in database
GPG key ID: 98BF5BA2F3BB4D28
5 changed files with 124 additions and 110 deletions

View file

@ -381,8 +381,8 @@ public final class PushUtils {
private static void migratePushKeys() {
Context context = MainApp.getAppContext();
if (!PreferenceManager.getKeysMigration(context)) {
AppPreferences preferences = PreferenceManager.fromContext(context);
if (!preferences.isKeysMigrationEnabled()) {
String oldKeyPath = MainApp.getStoragePath() + File.separator + MainApp.getDataFolder()
+ File.separator + "nc-keypair";
File oldPrivateKeyFile = new File(oldKeyPath, "push_key.priv");
@ -394,7 +394,7 @@ public final class PushUtils {
if ((privateKeyFile.exists() && publicKeyFile.exists()) ||
(!oldPrivateKeyFile.exists() && !oldPublicKeyFile.exists())) {
PreferenceManager.setKeysMigration(context, true);
preferences.setKeysMigrationEnabled(true);
} else {
if (oldPrivateKeyFile.exists()) {
FileStorageUtils.moveFile(oldPrivateKeyFile, privateKeyFile);
@ -405,7 +405,7 @@ public final class PushUtils {
}
if (privateKeyFile.exists() && publicKeyFile.exists()) {
PreferenceManager.setKeysMigration(context, true);
preferences.setKeysMigrationEnabled(true);
}
}
}

View file

@ -205,6 +205,27 @@ public interface AppPreferences {
FileSortOrder getSortOrderByType(FileSortOrder.Type type, FileSortOrder defaultOrder);
FileSortOrder getSortOrderByType(FileSortOrder.Type type);
/**
* Gets the legacy cleaning flag last set.
*
* @return ascending order the legacy cleaning flag, default is false
*/
boolean isLegacyClean();
/**
* Saves the legacy cleaning flag which the user has set last.
*
* @param legacyClean flag if it is a legacy cleaning
*/
void setLegacyClean(boolean legacyClean);
boolean isKeysMigrationEnabled();
void setKeysMigrationEnabled(boolean enabled);
boolean isStoragePathFixEnabled();
void setStoragePathFixEnabled(boolean enabled);
boolean isShowDetailedTimestampEnabled();
void setShowDetailedTimestampEnabled(boolean showDetailedTimestamp);

View file

@ -72,9 +72,14 @@ public final class PreferenceManager implements AppPreferences {
public static AppPreferences fromContext(Context context) {
Context appContext = context.getApplicationContext();
SharedPreferences prefs = getDefaultSharedPreferences(appContext);
return new PreferenceManager(appContext, prefs);
}
public static SharedPreferences getDefaultSharedPreferences(Context context) {
return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
}
PreferenceManager(Context appContext, SharedPreferences preferences) {
this.context = appContext;
this.preferences = preferences;
@ -191,6 +196,7 @@ public final class PreferenceManager implements AppPreferences {
};
}
@Override
public boolean isFingerprintUnlockEnabled() {
return preferences.getBoolean(SettingsActivity.PREFERENCE_USE_FINGERPRINT, false);
}
@ -243,72 +249,34 @@ public final class PreferenceManager implements AppPreferences {
dataProvider.storeOrUpdateKeyValue(account.name, PREF__FOLDER_SORT_ORDER + "_" + type, sortOrder.name);
}
/**
* Get preference value for a folder.
* If folder is not set itself, it finds an ancestor that is set.
*
* @param context Context object.
* @param preferenceName Name of the preference to lookup.
* @param folder Folder.
* @param defaultValue Fallback value in case no ancestor is set.
* @return Preference value
*/
public static String getFolderPreference(Context context, String preferenceName, OCFile folder,
String defaultValue) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
if (account == null) {
return defaultValue;
}
ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
while (folder != null && value.isEmpty()) {
folder = storageManager.getFileById(folder.getParentId());
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
}
return value.isEmpty() ? defaultValue : value;
@Override
public boolean isLegacyClean() {
return preferences.getBoolean(PREF__LEGACY_CLEAN, false);
}
/**
* Set preference value for a folder.
*
* @param context Context object.
* @param preferenceName Name of the preference to set.
* @param folder Folder.
* @param value Preference value to set.
*/
public static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
@Override
public void setLegacyClean(boolean isLegacyClean) {
preferences.edit().putBoolean(PREF__LEGACY_CLEAN, isLegacyClean);
}
private static String getKeyFromFolder(String preferenceName, OCFile folder) {
final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
FileDataStorageManager.ROOT_PARENT_ID);
return preferenceName + "_" + folderIdString;
@Override
public boolean isKeysMigrationEnabled() {
return preferences.getBoolean(PREF__KEYS_MIGRATION, false);
}
/**
* Gets the legacy cleaning flag last set.
*
* @param context Caller {@link Context}, used to access to shared preferences manager.
* @return ascending order the legacy cleaning flag, default is false
*/
public static boolean getLegacyClean(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__LEGACY_CLEAN, false);
@Override
public void setKeysMigrationEnabled(boolean keysMigration) {
preferences.edit().putBoolean(PREF__KEYS_MIGRATION, keysMigration).apply();
}
public static boolean getKeysMigration(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__KEYS_MIGRATION, false);
@Override
public boolean isStoragePathFixEnabled() {
return preferences.getBoolean(PREF__FIX_STORAGE_PATH, false);
}
public static boolean getStoragePathFix(Context context) {
return getDefaultSharedPreferences(context).getBoolean(PREF__FIX_STORAGE_PATH, false);
@Override
public void setStoragePathFixEnabled(boolean storagePathFixEnabled) {
preferences.edit().putBoolean(PREF__FIX_STORAGE_PATH, storagePathFixEnabled).apply();
}
@Override
@ -341,24 +309,6 @@ public final class PreferenceManager implements AppPreferences {
preferences.edit().putBoolean(PREF__AUTO_UPLOAD_INIT, autoUploadInit).apply();
}
/**
* Saves the legacy cleaning flag which the user has set last.
*
* @param context Caller {@link Context}, used to access to shared preferences manager.
* @param legacyClean flag if it is a legacy cleaning
*/
public static void setLegacyClean(Context context, boolean legacyClean) {
saveBooleanPreference(context, PREF__LEGACY_CLEAN, legacyClean);
}
public static void setKeysMigration(Context context, boolean keysMigration) {
saveBooleanPreference(context, PREF__KEYS_MIGRATION, keysMigration);
}
public static void setStoragePathFix(Context context, boolean storagePathFix) {
saveBooleanPreference(context, PREF__FIX_STORAGE_PATH, storagePathFix);
}
@Override
public int getUploaderBehaviour() {
return preferences.getInt(AUTO_PREF__UPLOADER_BEHAVIOR, 1);
@ -399,10 +349,12 @@ public final class PreferenceManager implements AppPreferences {
preferences.edit().putInt(AUTO_PREF__LAST_SEEN_VERSION_CODE, versionCode).apply();
}
@Override
public long getLockTimestamp() {
return preferences.getLong(PREF__LOCK_TIMESTAMP, 0);
}
@Override
public void setLockTimestamp(long timestamp) {
preferences.edit().putLong(PREF__LOCK_TIMESTAMP, timestamp).apply();
}
@ -427,21 +379,6 @@ public final class PreferenceManager implements AppPreferences {
preferences.edit().putBoolean(PREF__SHOW_MEDIA_SCAN_NOTIFICATIONS, value).apply();
}
private static void saveBooleanPreference(Context context, String key, boolean value) {
SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
appPreferences.putBoolean(key, value).apply();
}
private static void saveStringPreferenceNow(Context context, String key, String value) {
SharedPreferences.Editor appPreferences = getDefaultSharedPreferences(context.getApplicationContext()).edit();
appPreferences.putString(key, value);
appPreferences.apply();
}
public static SharedPreferences getDefaultSharedPreferences(Context context) {
return android.preference.PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
}
@Override
public void removeLegacyPreferences() {
preferences.edit()
@ -464,4 +401,54 @@ public final class PreferenceManager implements AppPreferences {
public void clear() {
preferences.edit().clear().apply();
}
/**
* Get preference value for a folder.
* If folder is not set itself, it finds an ancestor that is set.
*
* @param context Context object.
* @param preferenceName Name of the preference to lookup.
* @param folder Folder.
* @param defaultValue Fallback value in case no ancestor is set.
* @return Preference value
*/
private static String getFolderPreference(Context context, String preferenceName, OCFile folder,
String defaultValue) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
if (account == null) {
return defaultValue;
}
ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
while (folder != null && value.isEmpty()) {
folder = storageManager.getFileById(folder.getParentId());
value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
}
return value.isEmpty() ? defaultValue : value;
}
/**
* Set preference value for a folder.
*
* @param context Context object.
* @param preferenceName Name of the preference to set.
* @param folder Folder.
* @param value Preference value to set.
*/
private static void setFolderPreference(Context context, String preferenceName, OCFile folder, String value) {
Account account = AccountUtils.getCurrentOwnCloudAccount(context);
ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
dataProvider.storeOrUpdateKeyValue(account.name, getKeyFromFolder(preferenceName, folder), value);
}
private static String getKeyFromFolder(String preferenceName, OCFile folder) {
final String folderIdString = String.valueOf(folder != null ? folder.getFileId() :
FileDataStorageManager.ROOT_PARENT_ID);
return preferenceName + "_" + folderIdString;
}
}

View file

@ -243,7 +243,7 @@ public class MainApp extends MultiDexApplication {
@SuppressLint("ApplySharedPref") // commit is done on purpose to write immediately
private void fixStoragePath() {
if (!PreferenceManager.getStoragePathFix(this)) {
if (!preferences.isStoragePathFixEnabled()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
StoragePoint[] storagePoints = DataStorageProvider.getInstance().getAvailableStoragePoints();
String storagePath = sharedPreferences.getString(SettingsActivity.PreferenceKeys.STORAGE_PATH, "");
@ -280,10 +280,10 @@ public class MainApp extends MultiDexApplication {
}
}
PreferenceManager.setStoragePathFix(this, true);
preferences.setStoragePathFixEnabled(true);
} else {
sharedPreferences.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true);
preferences.setStoragePathFixEnabled(true);
}
} else {
if (TextUtils.isEmpty(storagePath)) {
@ -291,7 +291,7 @@ public class MainApp extends MultiDexApplication {
Environment.getExternalStorageDirectory().getAbsolutePath()).commit();
}
sharedPreferences.edit().remove(PreferenceManager.PREF__KEYS_MIGRATION).commit();
PreferenceManager.setStoragePathFix(this, true);
preferences.setStoragePathFixEnabled(true);
}
}
}
@ -543,7 +543,7 @@ public class MainApp extends MultiDexApplication {
AppPreferences preferences = PreferenceManager.fromContext(context);
if (!preferences.isAutoUploadPathsUpdateEnabled()) {
SyncedFolderProvider syncedFolderProvider =
new SyncedFolderProvider(MainApp.getAppContext().getContentResolver());
new SyncedFolderProvider(MainApp.getAppContext().getContentResolver(), preferences);
syncedFolderProvider.updateAutoUploadPaths(mContext);
}
}
@ -557,7 +557,7 @@ public class MainApp extends MultiDexApplication {
Log_OC.i(TAG, "Migrate synced_folders records for image/video split");
ContentResolver contentResolver = context.getContentResolver();
SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver, preferences);
final List<MediaFolder> imageMediaFolders = MediaProvider.getImageFolders(contentResolver, 1, null, true);
final List<MediaFolder> videoMediaFolders = MediaProvider.getVideoFolders(contentResolver, 1, null, true);
@ -608,7 +608,7 @@ public class MainApp extends MultiDexApplication {
AppPreferences preferences = PreferenceManager.fromContext(getAppContext());
if (!preferences.isAutoUploadInitialized()) {
SyncedFolderProvider syncedFolderProvider =
new SyncedFolderProvider(MainApp.getAppContext().getContentResolver());
new SyncedFolderProvider(MainApp.getAppContext().getContentResolver(), preferences);
for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
if (syncedFolder.isEnabled()) {
@ -627,10 +627,11 @@ public class MainApp extends MultiDexApplication {
// database, and this cleans all that and leaves 1 (newest) entry per synced folder
Context context = getAppContext();
AppPreferences preferences = PreferenceManager.fromContext(context);
if (!PreferenceManager.getLegacyClean(context)) {
if (!preferences.isLegacyClean()) {
SyncedFolderProvider syncedFolderProvider =
new SyncedFolderProvider(context.getContentResolver());
new SyncedFolderProvider(context.getContentResolver(), preferences);
List<SyncedFolder> syncedFolderList = syncedFolderProvider.getSyncedFolders();
Map<Pair<String, String>, Long> syncedFolders = new HashMap<>();
@ -649,9 +650,12 @@ public class MainApp extends MultiDexApplication {
ids.addAll(syncedFolders.values());
if (ids.size() > 0) {
syncedFolderProvider.deleteSyncedFoldersNotInList(mContext, ids);
int deletedCount = syncedFolderProvider.deleteSyncedFoldersNotInList(ids);
if(deletedCount > 0) {
preferences.setLegacyClean(true);
}
} else {
PreferenceManager.setLegacyClean(context, true);
preferences.setLegacyClean(true);
}
}
}

View file

@ -29,6 +29,7 @@ import android.net.Uri;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.client.preferences.PreferenceManager;
import com.owncloud.android.MainApp;
import com.owncloud.android.db.ProviderMeta;
import com.owncloud.android.lib.common.utils.Log_OC;
@ -46,17 +47,19 @@ public class SyncedFolderProvider extends Observable {
static private final String TAG = SyncedFolderProvider.class.getSimpleName();
private ContentResolver mContentResolver;
private AppPreferences preferences;
/**
* constructor.
*
* @param contentResolver the ContentResolver to work with.
*/
public SyncedFolderProvider(ContentResolver contentResolver) {
public SyncedFolderProvider(ContentResolver contentResolver, AppPreferences preferences) {
if (contentResolver == null) {
throw new IllegalArgumentException("Cannot create an instance with a NULL contentResolver");
}
mContentResolver = contentResolver;
this.preferences = preferences;
}
/**
@ -271,19 +274,18 @@ public class SyncedFolderProvider extends Observable {
/**
* delete any records of synchronized folders that are not within the given list of ids.
*
* @param context the context.
* @param ids the list of ids to be excluded from deletion.
* @param ids the list of ids to be excluded from deletion.
* @return number of deleted records.
*/
public int deleteSyncedFoldersNotInList(Context context, List<Long> ids) {
public int deleteSyncedFoldersNotInList(List<Long> ids) {
int result = mContentResolver.delete(
ProviderMeta.ProviderTableMeta.CONTENT_URI_SYNCED_FOLDERS,
ProviderMeta.ProviderTableMeta._ID + " NOT IN (?)",
new String[]{String.valueOf(ids)}
);
if (result > 0 && context != null) {
PreferenceManager.setLegacyClean(context, true);
if(result > 0) {
preferences.setLegacyClean(true);
}
return result;