OSDN Git Service

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