1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package android.support.v4.content;
18
19import android.content.SharedPreferences;
20import android.support.annotation.NonNull;
21
22public final class SharedPreferencesCompat {
23
24    public final static class EditorCompat {
25
26        private static EditorCompat sInstance;
27
28        private static class Helper {
29            Helper() {
30            }
31
32            public void apply(@NonNull SharedPreferences.Editor editor) {
33                try {
34                    editor.apply();
35                } catch (AbstractMethodError unused) {
36                    // The app injected its own pre-Gingerbread
37                    // SharedPreferences.Editor implementation without
38                    // an apply method.
39                    editor.commit();
40                }
41            }
42        }
43
44        private final Helper mHelper;
45
46        private EditorCompat() {
47            mHelper = new Helper();
48        }
49
50        public static EditorCompat getInstance() {
51            if (sInstance == null) {
52                sInstance = new EditorCompat();
53            }
54            return sInstance;
55        }
56
57        public void apply(@NonNull SharedPreferences.Editor editor) {
58            // Note that this redirection is needed to not break the public API chain
59            // of getInstance().apply() calls. Otherwise this method could (and should)
60            // be static.
61            mHelper.apply(editor);
62        }
63    }
64
65    private SharedPreferencesCompat() {}
66
67}
68