1 /* 2 * Copyright (C) 2018 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 17 package android.perftests.utils; 18 19 import android.content.Context; 20 import android.provider.Settings; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 25 /** 26 * Manages the state of a preference backed by {@link Settings}. 27 */ 28 public class SettingsStateManager implements StateManager<String> { 29 30 private final Context mContext; 31 private final String mNamespace; 32 private final String mKey; 33 34 /** 35 * Default constructor. 36 * 37 * @param context context used to retrieve the {@link Settings} provider. 38 * @param namespace settings namespace. 39 * @param key prefence key. 40 */ SettingsStateManager(@onNull Context context, @NonNull String namespace, @NonNull String key)41 public SettingsStateManager(@NonNull Context context, @NonNull String namespace, 42 @NonNull String key) { 43 mContext = context; 44 mNamespace = namespace; 45 mKey = key; 46 } 47 48 @Override set(@ullable String value)49 public void set(@Nullable String value) { 50 SettingsHelper.syncSet(mContext, mNamespace, mKey, value); 51 } 52 53 @Override 54 @Nullable get()55 public String get() { 56 return SettingsHelper.get(mNamespace, mKey); 57 } 58 59 @Override toString()60 public String toString() { 61 return "SettingsStateManager[namespace=" + mNamespace + ", key=" + mKey + "]"; 62 } 63 } 64