OSDN Git Service

UTF8
[evermemo/source.git] / workspace / EverMemo / src / com / yuji / em / utility / PrefUtilImpl.java
1 package com.yuji.em.utility;
2
3 import android.content.Context;
4 import android.content.SharedPreferences;
5 import android.content.SharedPreferences.Editor;
6 import android.content.pm.PackageManager.NameNotFoundException;
7
8 public class PrefUtilImpl implements PrefUtil {
9         private static final String EVER_MEMO_ACTIVITY = "EverMemoActivity";
10         private static final String PACKAGE_COM_YUJI_EM = "com.yuji.em";
11         private Context context;
12
13         public PrefUtilImpl(Context context) {
14                 this.context = context;
15         }
16
17         public void put(String key, String value) {
18                 synchronized (this) {
19                         SharedPreferences pref = null;
20                         try {
21                                 Context ctxt = context.createPackageContext(PACKAGE_COM_YUJI_EM,
22                                                 Context.CONTEXT_RESTRICTED);
23                                 pref = ctxt.getSharedPreferences(EVER_MEMO_ACTIVITY,
24                                                 Context.MODE_PRIVATE);
25                         } catch (NameNotFoundException e) {
26                                 Debug.d(this, null, e);
27                                 return;
28                         }
29
30                         Editor e = pref.edit();
31                         e.putString(key, value);
32                         e.commit();
33                 }
34         }
35
36         public void put(String key, int value) {
37                 put(key, String.valueOf(value));
38         }
39
40         public String get(String key) {
41                 String val;
42                 
43                 synchronized (this) {
44                         SharedPreferences pref = null;
45                         try {
46                                 Context ctxt = context.createPackageContext(PACKAGE_COM_YUJI_EM,
47                                                 Context.CONTEXT_RESTRICTED);
48                                 pref = ctxt.getSharedPreferences(EVER_MEMO_ACTIVITY,
49                                                 Context.MODE_WORLD_READABLE);
50                         } catch (NameNotFoundException e) {
51                                 Debug.d(this, null, e);
52                                 return null;
53                         }
54                         val = pref.getString(key, null);
55                 }
56
57                 return val;
58         }
59
60         public int getInt(String key) {
61                 return Integer.parseInt(get(key));
62         }
63
64         public String get(String key, String initValue) {
65                 String value = get(key);
66                 if (value != null) {
67                         return value;
68                 }
69                 put(key, initValue);
70                 return initValue;
71         }
72
73         public int getInt(String key, int initValue) {
74                 return Integer.valueOf(get(key, String.valueOf(initValue)));
75         }
76
77         public void remove(String key) {
78                 put(key, null);
79         }
80 }