Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
SharedPreferencesHelper.java
/* * Copyright 2015, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.testing.unittesting.BasicSample; import android.content.SharedPreferences; import java.util.Calendar; /** * Helper class to manage access to {@link SharedPreferences}. */ public class SharedPreferencesHelper { // Keys for saving values in SharedPreferences. static final String KEY_NAME = "key_name"; static final String KEY_DOB = "key_dob_millis"; static final String KEY_EMAIL = "key_email"; // The injected SharedPreferences implementation to use for persistence. private final SharedPreferences mSharedPreferences; /** * Constructor with dependency injection. * * @param sharedPreferences The {@link SharedPreferences} that will be used in this DAO. */ public SharedPreferencesHelper(SharedPreferences sharedPreferences) { mSharedPreferences = sharedPreferences; } /** * Saves the given {@link SharedPreferenceEntry} that contains the user's settings to * {@link SharedPreferences}. * * @param sharedPreferenceEntry contains data to save to {@link SharedPreferences}. * @return {@code true} if writing to {@link SharedPreferences} succeeded. {@code false} * otherwise. */ public boolean savePersonalInfo(SharedPreferenceEntry sharedPreferenceEntry){ // Start a SharedPreferences transaction. SharedPreferences.Editor editor = mSharedPreferences.edit(); editor.putString(KEY_NAME, sharedPreferenceEntry.getName()); editor.putLong(KEY_DOB, sharedPreferenceEntry.getDateOfBirth().getTimeInMillis()); editor.putString(KEY_EMAIL, sharedPreferenceEntry.getEmail()); // Commit changes to SharedPreferences. return editor.commit(); } /** * Retrieves the {@link SharedPreferenceEntry} containing the user's personal information from * {@link SharedPreferences}. * * @return the Retrieved {@link SharedPreferenceEntry}. */ public SharedPreferenceEntry getPersonalInfo() { // Get data from the SharedPreferences. String name = mSharedPreferences.getString(KEY_NAME, ""); Long dobMillis = mSharedPreferences.getLong(KEY_DOB, Calendar.getInstance().getTimeInMillis()); Calendar dateOfBirth = Calendar.getInstance(); dateOfBirth.setTimeInMillis(dobMillis); String email = mSharedPreferences.getString(KEY_EMAIL, ""); // Create and fill a SharedPreferenceEntry model object. return new SharedPreferenceEntry(name, dateOfBirth, email); } }