Java tutorial
/** * Copyright 2012 Scott Weeden-Moody * * 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.lillicoder.newsblurry.feeds; import java.util.HashMap; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import com.lillicoder.newsblurry.R; /** * Repository that stores and manages {@link Feed} in persistent storage. * @author lillicoder */ @SuppressLint("UseSparseArrays") public class FeedRepository { private static final String TAG = FeedRepository.class.getSimpleName(); private static final String EXCEPTION_INVALID_CONSTRUCTOR_CONTEXT = "The given context must not be null."; private static final String WARNING_FAILED_TO_RESTORE_FEEDS_JSON = "Failed to restore feeds JSON from JSON string, will not load feeds into memory."; private static final String WARNING_FAILED_TO_SAVE_FEED_TO_JSON = "Failed to write feed %d to feeds JSON, feed will not be saved."; private Context _context; public FeedRepository(Context context) { if (context == null) throw new IllegalArgumentException(EXCEPTION_INVALID_CONSTRUCTOR_CONTEXT); this.setContext(context); } /** * Gets the application default {@link SharedPreferences}. * @return Application default {@link SharedPreferences}. */ private SharedPreferences getSharedPreferences() { Context context = this.getContext(); String appPreferencesName = context.getString(R.string.default_preferencesName); return context.getSharedPreferences(appPreferencesName, Context.MODE_PRIVATE); } /** * Gets the {@link Context} for this repository. * @return {@link Context} for this repository. */ private Context getContext() { return this._context; } /** * Sets the {@link Context} for this repository. * @param context {@link Context} for this repository. */ private void setContext(Context context) { this._context = context; } /** * Restores feeds saved in storage and returns * a map of the restored feeds keyed by feed ID. * @return {@link Map} of restored {@link Feed} keyed by feed ID. */ public Map<Integer, Feed> restoreFeeds() { Map<Integer, Feed> feedsById = new HashMap<Integer, Feed>(); Context context = this.getContext(); SharedPreferences preferences = this.getSharedPreferences(); String feedsJsonText = preferences.getString(context.getString(R.string.preference_persistedFeeds), null); if (feedsJsonText != null) { try { JSONArray feedsArrayJson = new JSONArray(feedsJsonText); FeedParser parser = new FeedParser(context); feedsById = parser.parseFeedsById(feedsArrayJson); } catch (JSONException e) { Log.w(TAG, WARNING_FAILED_TO_RESTORE_FEEDS_JSON, e); } } return feedsById; } /** * Saves the given map of feeds to storage. */ public void saveFeeds(Map<Integer, Feed> feedsById) { // Create a feeds JSON object we can write to storage. JSONArray feedsJson = new JSONArray(); for (Feed feed : feedsById.values()) { try { feedsJson.put(feed.toJson()); } catch (JSONException e) { Log.w(TAG, String.format(WARNING_FAILED_TO_SAVE_FEED_TO_JSON, feed.getId()), e); } } // Write data to storage. Context context = this.getContext(); SharedPreferences preferences = this.getSharedPreferences(); Editor editor = preferences.edit(); editor.putString(context.getString(R.string.preference_persistedFeeds), feedsJson.toString()); editor.commit(); } }