1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.egg.neko; 16 17 import android.content.Context; 18 import android.content.SharedPreferences; 19 import android.content.SharedPreferences.OnSharedPreferenceChangeListener; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 import java.util.Map; 24 25 public class PrefState implements OnSharedPreferenceChangeListener { 26 27 private static final String FILE_NAME = "mPrefs"; 28 29 private static final String FOOD_STATE = "food"; 30 31 private static final String CAT_KEY_PREFIX = "cat:"; 32 33 private final Context mContext; 34 private final SharedPreferences mPrefs; 35 private PrefsListener mListener; 36 PrefState(Context context)37 public PrefState(Context context) { 38 mContext = context; 39 mPrefs = mContext.getSharedPreferences(FILE_NAME, 0); 40 } 41 42 // Can also be used for renaming. addCat(Cat cat)43 public void addCat(Cat cat) { 44 mPrefs.edit() 45 .putString(CAT_KEY_PREFIX + String.valueOf(cat.getSeed()), cat.getName()) 46 .apply(); 47 } 48 removeCat(Cat cat)49 public void removeCat(Cat cat) { 50 mPrefs.edit().remove(CAT_KEY_PREFIX + String.valueOf(cat.getSeed())).apply(); 51 } 52 getCats()53 public List<Cat> getCats() { 54 ArrayList<Cat> cats = new ArrayList<>(); 55 Map<String, ?> map = mPrefs.getAll(); 56 for (String key : map.keySet()) { 57 if (key.startsWith(CAT_KEY_PREFIX)) { 58 long seed = Long.parseLong(key.substring(CAT_KEY_PREFIX.length())); 59 Cat cat = new Cat(mContext, seed); 60 cat.setName(String.valueOf(map.get(key))); 61 cats.add(cat); 62 } 63 } 64 return cats; 65 } 66 getFoodState()67 public int getFoodState() { 68 return mPrefs.getInt(FOOD_STATE, 0); 69 } 70 setFoodState(int foodState)71 public void setFoodState(int foodState) { 72 mPrefs.edit().putInt(FOOD_STATE, foodState).apply(); 73 } 74 setListener(PrefsListener listener)75 public void setListener(PrefsListener listener) { 76 mListener = listener; 77 if (mListener != null) { 78 mPrefs.registerOnSharedPreferenceChangeListener(this); 79 } else { 80 mPrefs.unregisterOnSharedPreferenceChangeListener(this); 81 } 82 } 83 84 @Override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)85 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 86 mListener.onPrefsChanged(); 87 } 88 89 public interface PrefsListener { onPrefsChanged()90 void onPrefsChanged(); 91 } 92 } 93