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.os.Build; 21import android.support.annotation.NonNull; 22 23public final class SharedPreferencesCompat { 24 25 public final static class EditorCompat { 26 27 private static EditorCompat sInstance; 28 29 private interface Helper { 30 void apply(@NonNull SharedPreferences.Editor editor); 31 } 32 33 private static class EditorHelperBaseImpl implements Helper { 34 35 @Override 36 public void apply(@NonNull SharedPreferences.Editor editor) { 37 editor.commit(); 38 } 39 } 40 41 private static class EditorHelperApi9Impl implements Helper { 42 43 @Override 44 public void apply(@NonNull SharedPreferences.Editor editor) { 45 EditorCompatGingerbread.apply(editor); 46 } 47 } 48 49 private final Helper mHelper; 50 51 private EditorCompat() { 52 if (Build.VERSION.SDK_INT >= 9) { 53 mHelper = new EditorHelperApi9Impl(); 54 } else { 55 mHelper = new EditorHelperBaseImpl(); 56 } 57 } 58 59 public static EditorCompat getInstance() { 60 if (sInstance == null) { 61 sInstance = new EditorCompat(); 62 } 63 return sInstance; 64 } 65 66 public void apply(@NonNull SharedPreferences.Editor editor) { 67 mHelper.apply(editor); 68 } 69 } 70 71 private SharedPreferencesCompat() {} 72 73} 74