Using preferences' apply() instead of commit() on Gingerbread.

This commit is contained in:
Rodrigo Damazio
2010-12-24 05:41:03 -02:00
parent 913d30f2de
commit df2af0185c
8 changed files with 38 additions and 7 deletions
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.Activity; import android.app.Activity;
@@ -82,7 +83,8 @@ class CheckUnits {
} }
private static void recordCheckPerformed(SharedPreferences preferences) { private static void recordCheckPerformed(SharedPreferences preferences) {
preferences.edit().putBoolean(PREFERENCE_UNITS_CHECKED, true).commit(); ApiFeatures.getInstance().getApiPlatformAdapter().applyPreferenceChanges(
preferences.edit().putBoolean(PREFERENCE_UNITS_CHECKED, true));
} }
private CheckUnits() { private CheckUnits() {
@@ -15,6 +15,7 @@
*/ */
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.apps.mytracks.util.ResourceUtils; import com.google.android.apps.mytracks.util.ResourceUtils;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -77,7 +78,8 @@ class Eula {
} }
private static void accept(Activity activity, SharedPreferences preferences) { private static void accept(Activity activity, SharedPreferences preferences) {
preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true).commit(); ApiFeatures.getInstance().getApiPlatformAdapter().applyPreferenceChanges(
preferences.edit().putBoolean(PREFERENCE_EULA_ACCEPTED, true));
Intent startIntent = new Intent(activity, WelcomeActivity.class); Intent startIntent = new Intent(activity, WelcomeActivity.class);
activity.startActivityForResult(startIntent, MyTracksConstants.WELCOME); activity.startActivityForResult(startIntent, MyTracksConstants.WELCOME);
} }
@@ -1064,10 +1064,10 @@ public class MyTracks extends TabActivity implements OnTouchListener,
* @param trackId the id of the track * @param trackId the id of the track
*/ */
public void setSelectedTrackId(final long trackId) { public void setSelectedTrackId(final long trackId) {
sharedPreferences ApiFeatures.getInstance().getApiPlatformAdapter().applyPreferenceChanges(
.edit() sharedPreferences
.putLong(getString(R.string.selected_track_key), trackId) .edit()
.commit(); .putLong(getString(R.string.selected_track_key), trackId));
} }
long getSelectedTrackId() { long getSelectedTrackId() {
@@ -16,6 +16,7 @@
package com.google.android.apps.mytracks; package com.google.android.apps.mytracks;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.util.ApiFeatures;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
import android.app.AlertDialog; import android.app.AlertDialog;
@@ -63,7 +64,7 @@ public class MyTracksDeleteAllTracks extends Handler {
context.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0); context.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
SharedPreferences.Editor editor = prefs.edit(); SharedPreferences.Editor editor = prefs.edit();
editor.putLong(context.getString(R.string.selected_track_key), -1); editor.putLong(context.getString(R.string.selected_track_key), -1);
editor.commit(); ApiFeatures.getInstance().getApiPlatformAdapter().applyPreferenceChanges(editor);
if (done != null) { if (done != null) {
Handler h = new Handler(); Handler h = new Handler();
h.post(done); h.post(done);
@@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.util;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.SharedPreferences;
import android.os.HandlerThread; import android.os.HandlerThread;
/** /**
@@ -58,6 +59,12 @@ public interface ApiPlatformAdapter {
*/ */
boolean stopHandlerThread(HandlerThread handlerThread); boolean stopHandlerThread(HandlerThread handlerThread);
/**
* Applies all changes done to the given preferences editor.
* Changes may or may not be applied immediately.
*/
void applyPreferenceChanges(SharedPreferences.Editor editor);
/** /**
* Enables strict mode where supported, only if this is a development build. * Enables strict mode where supported, only if this is a development build.
*/ */
@@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.util;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.SharedPreferences.Editor;
import android.os.HandlerThread; import android.os.HandlerThread;
/** /**
@@ -51,6 +52,11 @@ public class CupcakePlatformAdapter implements ApiPlatformAdapter {
return false; return false;
} }
@Override
public void applyPreferenceChanges(Editor editor) {
editor.commit();
}
@Override @Override
public void enableStrictMode() { public void enableStrictMode() {
// Not supported // Not supported
@@ -18,6 +18,7 @@ package com.google.android.apps.mytracks.util;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.Service; import android.app.Service;
import android.content.SharedPreferences.Editor;
import android.os.HandlerThread; import android.os.HandlerThread;
/** /**
@@ -46,6 +47,11 @@ public class EclairPlatformAdapter implements ApiPlatformAdapter {
return handlerThread.quit(); return handlerThread.quit();
} }
@Override
public void applyPreferenceChanges(Editor editor) {
editor.commit();
}
@Override @Override
public void enableStrictMode() { public void enableStrictMode() {
// Not supported // Not supported
@@ -2,6 +2,7 @@ package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.MyTracksConstants; import com.google.android.apps.mytracks.MyTracksConstants;
import android.content.SharedPreferences.Editor;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.Log; import android.util.Log;
@@ -12,6 +13,12 @@ import android.util.Log;
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class GingerbreadPlatformAdapter extends EclairPlatformAdapter { public class GingerbreadPlatformAdapter extends EclairPlatformAdapter {
@Override
public void applyPreferenceChanges(Editor editor) {
// Apply asynchronously
editor.apply();
}
@Override @Override
public void enableStrictMode() { public void enableStrictMode() {
Log.d(MyTracksConstants.TAG, "Enabling strict mode"); Log.d(MyTracksConstants.TAG, "Enabling strict mode");