Use SharedPreferences$Editor.apply() instead of commit() when possible.

Change-Id: Ic609c6693effcdd16acc7383d9fb4834186dd25e
diff --git a/src/com/android/music/MediaPlaybackService.java b/src/com/android/music/MediaPlaybackService.java
index 10783a4..c4de9fc 100644
--- a/src/com/android/music/MediaPlaybackService.java
+++ b/src/com/android/music/MediaPlaybackService.java
@@ -409,8 +409,8 @@
         }
         ed.putInt("repeatmode", mRepeatMode);
         ed.putInt("shufflemode", mShuffleMode);
-        ed.commit();
-  
+        SharedPreferencesCompat.apply(ed);
+
         //Log.i("@@@@ service", "saved state in " + (System.currentTimeMillis() - start) + " ms");
     }
 
diff --git a/src/com/android/music/MusicUtils.java b/src/com/android/music/MusicUtils.java
index 398f462..b81b24b 100644
--- a/src/com/android/music/MusicUtils.java
+++ b/src/com/android/music/MusicUtils.java
@@ -1079,7 +1079,7 @@
             context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
         Editor ed = prefs.edit();
         ed.putInt(name, value);
-        ed.commit();
+        SharedPreferencesCompat.apply(ed);
     }
 
     static void setRingtone(Context context, long id) {
diff --git a/src/com/android/music/SharedPreferencesCompat.java b/src/com/android/music/SharedPreferencesCompat.java
new file mode 100644
index 0000000..92780a6
--- /dev/null
+++ b/src/com/android/music/SharedPreferencesCompat.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.music;
+
+import android.content.SharedPreferences;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+/**
+ * Reflection utils to call SharedPreferences$Editor.apply when possible,
+ * falling back to commit when apply isn't available.
+ */
+public class SharedPreferencesCompat {
+    private static final Method sApplyMethod = findApplyMethod();
+
+    private static Method findApplyMethod() {
+        try {
+            Class cls = SharedPreferences.Editor.class;
+            return cls.getMethod("apply");
+        } catch (NoSuchMethodException unused) {
+            // fall through
+        }
+        return null;
+    }
+
+    public static void apply(SharedPreferences.Editor editor) {
+        if (sApplyMethod != null) {
+            try {
+                sApplyMethod.invoke(editor);
+                return;
+            } catch (InvocationTargetException unused) {
+                // fall through
+            } catch (IllegalAccessException unused) {
+                // fall through
+            }
+        }
+        editor.commit();
+    }
+}