1 /* 2 * Copyright (C) 2013 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 com.example.android.cardemulation; 18 19 import android.content.Context; 20 import android.content.SharedPreferences; 21 import android.preference.PreferenceManager; 22 import android.util.Log; 23 24 /** 25 * Utility class for persisting account numbers to disk. 26 * 27 * <p>The default SharedPreferences instance is used as the backing storage. Values are cached 28 * in memory for performance. 29 * 30 * <p>This class is thread-safe. 31 */ 32 public class AccountStorage { 33 private static final String PREF_ACCOUNT_NUMBER = "account_number"; 34 private static final String DEFAULT_ACCOUNT_NUMBER = "00000000"; 35 private static final String TAG = "AccountStorage"; 36 private static String sAccount = null; 37 private static final Object sAccountLock = new Object(); 38 SetAccount(Context c, String s)39 public static void SetAccount(Context c, String s) { 40 synchronized(sAccountLock) { 41 Log.i(TAG, "Setting account number: " + s); 42 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); 43 prefs.edit().putString(PREF_ACCOUNT_NUMBER, s).commit(); 44 sAccount = s; 45 } 46 } 47 GetAccount(Context c)48 public static String GetAccount(Context c) { 49 synchronized (sAccountLock) { 50 if (sAccount == null) { 51 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c); 52 String account = prefs.getString(PREF_ACCOUNT_NUMBER, DEFAULT_ACCOUNT_NUMBER); 53 sAccount = account; 54 } 55 return sAccount; 56 } 57 } 58 } 59