[Android] [PATCH] Asynchronize preferences saving when possible

Geoffrey Métais geoffrey.metais at gmail.com
Mon Jan 5 17:48:19 CET 2015


---
 vlc-android/src/org/videolan/vlc/gui/MainActivity.java           | 9 ++++-----
 vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java    | 5 +++--
 .../src/org/videolan/vlc/gui/audio/EqualizerFragment.java        | 6 +++---
 .../src/org/videolan/vlc/gui/video/VideoPlayerActivity.java      | 9 +++++----
 vlc-android/src/org/videolan/vlc/util/CustomDirectories.java     | 4 ++--
 5 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 454bd06..594dd81 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -165,7 +165,7 @@ public class MainActivity extends ActionBarActivity {
         if (mFirstRun) {
             Editor editor = mSettings.edit();
             editor.putInt(PREF_FIRST_RUN, mVersionNumber);
-            editor.commit();
+            Util.commitPreferences(editor);
         }
 
         try {
@@ -423,7 +423,7 @@ public class MainActivity extends ActionBarActivity {
         /* Save the tab status in pref */
         SharedPreferences.Editor editor = getSharedPreferences("MainActivity", MODE_PRIVATE).edit();
         editor.putString("fragment", mCurrentFragment);
-        editor.commit();
+        Util.commitPreferences(editor);
 
         mAudioController.removeAudioPlayer(mAudioPlayer);
         AudioServiceController.getInstance().unbindAudioService(this);
@@ -1004,10 +1004,9 @@ public class MainActivity extends ActionBarActivity {
                 @Override
                 public void onClick(View v) {
                     removeTipViewIfDisplayed();
-                    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
-                    Editor editor = settings.edit();
+                    Editor editor = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
                     editor.putBoolean(settingKey, true);
-                    editor.commit();
+                    Util.commitPreferences(editor);
                 }
             });
         }
diff --git a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
index fc7d695..9627902 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PreferencesActivity.java
@@ -31,6 +31,7 @@ import org.videolan.vlc.gui.audio.AudioUtil;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.BitmapCache;
 import org.videolan.vlc.util.Logcat;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.VLCInstance;
 
 import android.app.AlertDialog;
@@ -100,7 +101,7 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
                 final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(PreferencesActivity.this);
                 SharedPreferences.Editor editor = sharedPrefs.edit();
                 editor.putString("screen_orientation_value", (String)newValue);
-                editor.commit();
+                Util.commitPreferences(editor);
                 return true;
             }
         });
@@ -275,7 +276,7 @@ public class PreferencesActivity extends PreferenceActivity implements OnSharedP
                     editor.putInt("network_caching_value", 0);
                     editor.putString("network_caching", "0");
                 }
-                editor.commit();
+                Util.commitPreferences(editor);
                 return true;
             }
         });
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
index 10b20ce..ffd272e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/EqualizerFragment.java
@@ -25,6 +25,7 @@ import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
 import org.videolan.vlc.interfaces.OnEqualizerBarChangeListener;
 import org.videolan.vlc.util.Preferences;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.widget.EqualizerBar;
 
@@ -173,12 +174,11 @@ public class EqualizerFragment extends Fragment {
         preamp.setOnSeekBarChangeListener(null);
         bands_layout.removeAllViews();
 
-        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext());
-        SharedPreferences.Editor editor = preferences.edit();
+        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(VLCApplication.getAppContext()).edit();
         editor.putBoolean("equalizer_enabled", button.isChecked());
         Preferences.putFloatArray(editor, "equalizer_values", equalizer);
         editor.putInt("equalizer_preset", equalizer_presets.getSelectedItemPosition());
-        editor.commit();
+        Util.commitPreferences(editor);
     }
 
     private final OnItemSelectedListener mPresetListener = new OnItemSelectedListener() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 145f5b3..ae03822 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -54,6 +54,7 @@ import org.videolan.vlc.gui.MainActivity;
 import org.videolan.vlc.gui.PreferencesActivity;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.Strings;
+import org.videolan.vlc.util.Util;
 import org.videolan.vlc.util.VLCInstance;
 import org.videolan.vlc.util.WeakHandler;
 
@@ -411,7 +412,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         // only (like desktop VLC). We don't want the customs subtitle file
         // to persist forever with this video.
         editor.putString(PreferencesActivity.VIDEO_SUBTITLE_FILES, null);
-        editor.commit();
+        Util.commitPreferences(editor);
 
         IntentFilter filter = new IntentFilter();
         if (mBattery != null)
@@ -539,7 +540,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         }
         editor.putString(PreferencesActivity.VIDEO_SUBTITLE_FILES, subtitleList_serialized);
 
-        editor.commit();
+        Util.commitPreferences(editor);
         AudioServiceController.getInstance().unbindAudioService(this);
     }
 
@@ -2382,7 +2383,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
                 long rTime = mSettings.getLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
                 Editor editor = mSettings.edit();
                 editor.putLong(PreferencesActivity.VIDEO_RESUME_TIME, -1);
-                editor.commit();
+                Util.commitPreferences(editor);
                 if(rTime > 0)
                     mLibVLC.setTime(rTime);
 
@@ -2639,7 +2640,7 @@ public class VideoPlayerActivity extends ActionBarActivity implements IVideoPlay
         mOverlayTips.setVisibility(View.GONE);
         Editor editor = mSettings.edit();
         editor.putBoolean(PREF_TIPS_SHOWN, true);
-        editor.commit();
+        Util.commitPreferences(editor);
     }
 
     private void updateNavStatus() {
diff --git a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
index 9bffa43..1a1ac1b 100644
--- a/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
+++ b/vlc-android/src/org/videolan/vlc/util/CustomDirectories.java
@@ -44,7 +44,7 @@ public class CustomDirectories {
         }
         SharedPreferences.Editor editor = preferences.edit();
         editor.putString("custom_paths", builder.toString());
-        editor.commit();
+        Util.commitPreferences(editor);
     }
 
     public static void removeCustomDirectory(String path) {
@@ -69,7 +69,7 @@ public class CustomDirectories {
         }
         SharedPreferences.Editor editor = preferences.edit();
         editor.putString("custom_paths", custom_path);
-        editor.commit();
+        Util.commitPreferences(editor);
     }
 
     public static String[] getCustomDirectories() {
-- 
2.1.0



More information about the Android mailing list