Call preference editor apply() for efficiency

When it's available (API level >=9) call SharedPreferences.Editor apply()
instead of commit for efficiency.
This commit is contained in:
Brian Pellin committed 2012-09-11 23:53:02 -05:00
1 parent c99b053101
commit 4fd38dfd9f
5 files changed
+63 -6

No files matched your search

+2 -1
View File
@@ -41,6 +41,7 @@ import com.android.keepass.KeePass;
import com.android.keepass.R;
import com.keepassdroid.app.App;
import com.keepassdroid.compat.ActivityCompat;
import com.keepassdroid.compat.EditorCompat;
import com.keepassdroid.database.PwGroup;
import com.keepassdroid.database.edit.OnFinish;
import com.keepassdroid.settings.AppSettingsActivity;
@@ -216,7 +217,7 @@ public abstract class GroupBaseActivity extends LockCloseListActivity {
boolean sortByName = prefs.getBoolean(sortKey, getResources().getBoolean(R.bool.sort_default));
Editor editor = prefs.edit();
editor.putBoolean(sortKey, ! sortByName);
editor.commit();
EditorCompat.apply(editor);
// Refresh menu titles
ActivityCompat.invalidateOptionsMenu(this);
+2 -1
View File
@@ -49,6 +49,7 @@ import com.android.keepass.KeePass;
import com.android.keepass.R;
import com.keepassdroid.app.App;
import com.keepassdroid.compat.BackupManagerCompat;
import com.keepassdroid.compat.EditorCompat;
import com.keepassdroid.database.edit.LoadDB;
import com.keepassdroid.database.edit.OnFinish;
import com.keepassdroid.fileselect.BrowserDialog;
@@ -299,7 +300,7 @@ public class PasswordActivity extends LockingActivity {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY_DEFAULT_FILENAME, newDefaultFileName);
editor.commit();
EditorCompat.apply(editor);
BackupManagerCompat backupManager = new BackupManagerCompat(PasswordActivity.this);
backupManager.dataChanged();
@@ -0,0 +1,53 @@
/*
* Copyright 2012 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.compat;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.SharedPreferences;
public class EditorCompat {
private static Method apply;
static {
try {
apply = Activity.class.getMethod("apply", (Class<SharedPreferences.Editor>[]) null);
} catch (Exception e) {
// Substitute commit for apply when not available (API level < 9)
try {
apply = Activity.class.getMethod("commit", (Class<SharedPreferences.Editor>[]) null);
} catch (Exception f) {
// Should be impossible, but leave apply null in this case
}
}
}
public static void apply(SharedPreferences.Editor edit) {
try {
apply.invoke(edit, (Object[]) null);
} catch (Exception e) {
// Shouldn't be possible, but call commit directly if this happens
edit.commit();
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2009 Brian Pellin.
* Copyright 2009-2012 Brian Pellin.
*
* This file is part of KeePassDroid.
*
@@ -19,6 +19,8 @@
*/
package com.keepassdroid.fileselect;
import com.keepassdroid.compat.EditorCompat;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
@@ -97,7 +99,7 @@ public class FileDbHelper {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(LAST_FILENAME);
editor.remove(LAST_KEYFILE);
editor.commit();
EditorCompat.apply(editor);
} catch (Exception e) {
assert(true);
}
@@ -26,6 +26,7 @@ import android.preference.PreferenceManager;
import com.android.keepass.KeePass;
import com.android.keepass.R;
import com.keepassdroid.app.App;
import com.keepassdroid.compat.EditorCompat;
public class TimeoutHelper {
@@ -39,8 +40,7 @@ public class TimeoutHelper {
SharedPreferences.Editor edit = prefs.edit();
edit.putLong(act.getString(R.string.timeout_key), time);
// TODO: Replace with compat wrapper to call .apply when available
edit.commit();
EditorCompat.apply(edit);
}
public static void resume(Activity act) {