Issue 548, delete() before close() on FileConnection
[zxing.git] / rim / src / com / google / zxing / client / rim / persistence / AppSettings.java
1 /*\r
2  * Copyright 2008 ZXing authors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package com.google.zxing.client.rim.persistence;\r
18 \r
19 import net.rim.device.api.system.PersistentObject;\r
20 import net.rim.device.api.system.PersistentStore;\r
21 \r
22 import java.util.Hashtable;\r
23 \r
24 /**\r
25  * Singleton object that represents the persistent application settings data.\r
26  *\r
27  * This code was contributed by LifeMarks.\r
28  * \r
29  * @author Matt York (matt@lifemarks.mobi)\r
30  */\r
31 public final class AppSettings {\r
32 \r
33   public static final String SETTING_CAM_RES_MSG = "setting_cam_res_msg";\r
34   private static final long ID_LONG = 0x92ac4e8ac35b8aa0L;\r
35 \r
36   private static AppSettings instance;\r
37 \r
38   private final PersistentObject store;\r
39   private final Hashtable settingsItems;\r
40 \r
41   private AppSettings() {\r
42     store = PersistentStore.getPersistentObject(ID_LONG);\r
43     Hashtable temp = (Hashtable) store.getContents();\r
44     settingsItems = temp == null ? new Hashtable() : temp;\r
45   }\r
46 \r
47   public static AppSettings getInstance() {\r
48     if (instance == null) {\r
49       instance = new AppSettings();\r
50     }\r
51     return instance;\r
52   }\r
53 \r
54   /**\r
55    * Adds a setting object.\r
56    */\r
57   public void addItem(String itemName, Object itemValue) {\r
58     settingsItems.put(itemName, itemValue);\r
59   }\r
60 \r
61   /**\r
62    * Returns all settings objects.\r
63    */\r
64   public Hashtable getItems() {\r
65     return settingsItems;\r
66   }\r
67 \r
68   /**\r
69    * Gets a particular settings object by name.\r
70    */\r
71   Object getItem(String itemName) {\r
72     return settingsItems.get(itemName);\r
73   }\r
74 \r
75   /**\r
76    * Gets a particular boolean type settings object by name.\r
77    */\r
78   public Boolean getBooleanItem(String itemName) {\r
79     Object value = getItem(itemName);\r
80     return value instanceof Boolean ? (Boolean) value : Boolean.FALSE;\r
81   }\r
82 \r
83   /**\r
84    * Returns the number of settings.\r
85    */\r
86   public int getNumItems() {\r
87     return settingsItems.size();\r
88   }\r
89 \r
90   /**\r
91    * Persists the settings to the device.\r
92    */\r
93   public void persist() {\r
94     synchronized (store) {\r
95       store.setContents(settingsItems);\r
96       store.commit();\r
97     }\r
98   }\r
99 \r
100 }\r