| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.blimp.preferences; | |
| 6 | |
| 7 import android.content.Context; | |
| 8 | |
| 9 import org.chromium.base.ContextUtils; | |
| 10 import org.chromium.blimp.R; | |
| 11 | |
| 12 /** | |
| 13 * Provides helper methods for storing and retrieving values in android shared p
references. | |
| 14 */ | |
| 15 public class PreferencesUtil { | |
| 16 /** | |
| 17 * Preference that stores the last used assigner URL. | |
| 18 */ | |
| 19 private static final String PREF_LAST_USED_ASSIGNER = "last_known_assigner"; | |
| 20 private static final String DEFAULT_EMPTY_STRING = ""; | |
| 21 | |
| 22 /** | |
| 23 * Finds the assigner to be used from user's last preference. If the app is
being used for the | |
| 24 * first time, the first entry from the assigner array would be used. | |
| 25 * @return assigner to use. | |
| 26 */ | |
| 27 public static String findAssignerUrl(Context context) { | |
| 28 String lastAssigner = getLastUsedAssigner(context); | |
| 29 if (lastAssigner.isEmpty()) { | |
| 30 String[] assignerUrls = context.getResources().getStringArray( | |
| 31 R.array.blimp_assigner_urls); | |
| 32 assert assignerUrls != null && assignerUrls.length > 0; | |
| 33 lastAssigner = assignerUrls[0]; | |
| 34 setLastUsedAssigner(context, lastAssigner); | |
| 35 } | |
| 36 return lastAssigner; | |
| 37 } | |
| 38 | |
| 39 /** | |
| 40 * Reads the last used assigner from shared preference. | |
| 41 * @param context The current Android context | |
| 42 * @return The saved value of assigner preference | |
| 43 */ | |
| 44 public static String getLastUsedAssigner(Context context) { | |
| 45 return readString(context, PREF_LAST_USED_ASSIGNER); | |
| 46 } | |
| 47 | |
| 48 /** | |
| 49 * Sets the last used assigner. | |
| 50 * @param context The current Android context | |
| 51 * @param assigner The new value of assigner preference | |
| 52 */ | |
| 53 public static void setLastUsedAssigner(Context context, String assigner) { | |
| 54 writeString(context, PREF_LAST_USED_ASSIGNER, assigner); | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Reads a string value from shared preference. | |
| 59 * @param context The current Android context | |
| 60 * @param key The name of the preference to read | |
| 61 * @return The current value of the preference or a default value | |
| 62 */ | |
| 63 private static String readString(Context context, String key) { | |
| 64 return ContextUtils.getAppSharedPreferences().getString(key, DEFAULT_EMP
TY_STRING); | |
| 65 } | |
| 66 | |
| 67 /** | |
| 68 * Writes the given string into shared preference. | |
| 69 * @param context The current Android context | |
| 70 * @param key The name of the preference to modify | |
| 71 * @param value The new value for the preference | |
| 72 */ | |
| 73 private static void writeString(Context context, String key, String value) { | |
| 74 ContextUtils.getAppSharedPreferences().edit().putString(key, value).appl
y(); | |
| 75 } | |
| 76 } | |
| OLD | NEW |