1 | package org.rapidandroid; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.FileInputStream; |
---|
5 | import java.io.FileNotFoundException; |
---|
6 | import java.io.FileOutputStream; |
---|
7 | import java.io.IOException; |
---|
8 | import java.io.InputStreamReader; |
---|
9 | |
---|
10 | import org.json.JSONException; |
---|
11 | import org.json.JSONObject; |
---|
12 | import org.rapidandroid.receiver.SmsParseReceiver; |
---|
13 | |
---|
14 | import android.content.Context; |
---|
15 | import android.util.Log; |
---|
16 | |
---|
17 | /** |
---|
18 | * @author Daniel Myung [email protected] |
---|
19 | * @created Feb 10, 2009 |
---|
20 | * Summary: |
---|
21 | */ |
---|
22 | public class ApplicationGlobals { |
---|
23 | |
---|
24 | |
---|
25 | public static void checkGlobals(Context context) { |
---|
26 | File f = context.getFileStreamPath(SETTINGS_FILE); |
---|
27 | if (!f.exists()) { |
---|
28 | try { |
---|
29 | f.createNewFile(); |
---|
30 | } catch (IOException e) { |
---|
31 | // TODO Auto-generated catch block |
---|
32 | e.printStackTrace(); |
---|
33 | } |
---|
34 | saveGlobalSettings(context, true, "Message parsed successfully, thank you", true, "Unable to understand your message, please try again"); |
---|
35 | } |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * |
---|
40 | */ |
---|
41 | public static final String KEY_FAILED_REPLY_TEXT = "FailedReplyText"; |
---|
42 | /** |
---|
43 | * |
---|
44 | */ |
---|
45 | public static final String KEY_FAILED_REPLY = "FailedReply"; |
---|
46 | /** |
---|
47 | * |
---|
48 | */ |
---|
49 | public static final String KEY_PARSE_REPLY_TEXT = "ParseReplyText"; |
---|
50 | /** |
---|
51 | * |
---|
52 | */ |
---|
53 | public static final String KEY_PARSE_REPLY = "ParseReply"; |
---|
54 | |
---|
55 | /** |
---|
56 | * |
---|
57 | */ |
---|
58 | public static final String SETTINGS_FILE = "GlobalSettings.json"; |
---|
59 | |
---|
60 | |
---|
61 | public static final String LOG_DEBUG_KEY = "ApplicationGlobals"; |
---|
62 | /** |
---|
63 | * |
---|
64 | */ |
---|
65 | public static JSONObject loadSettingsFromFile(Context context) { |
---|
66 | FileInputStream fin = null; |
---|
67 | InputStreamReader irdr = null; |
---|
68 | JSONObject readobject = null; |
---|
69 | try { |
---|
70 | |
---|
71 | fin = context.openFileInput(SETTINGS_FILE); |
---|
72 | |
---|
73 | irdr = new InputStreamReader(fin); // promote |
---|
74 | |
---|
75 | int size = (int) fin.getChannel().size(); |
---|
76 | char[] data = new char[size]; // allocate char array of right |
---|
77 | // size |
---|
78 | irdr.read(data, 0, size); // read into char array |
---|
79 | irdr.close(); |
---|
80 | |
---|
81 | String contents = new String(data); |
---|
82 | readobject = new JSONObject(contents); |
---|
83 | // mParseCheckbox.setChecked(readobject.getBoolean(KEY_PARSE_REPLY)); |
---|
84 | // mParseReplyText.setText(readobject.getString(KEY_PARSE_REPLY_TEXT)); |
---|
85 | // mNoparseCheckBox.setChecked(readobject.getBoolean(KEY_FAILED_REPLY)); |
---|
86 | // mNoparseReplyText.setText(readobject.getString(KEY_FAILED_REPLY_TEXT)); |
---|
87 | |
---|
88 | } catch (FileNotFoundException e) { |
---|
89 | // TODO Auto-generated catch block |
---|
90 | e.printStackTrace(); |
---|
91 | } catch (IOException e) { |
---|
92 | // TODO Auto-generated catch block |
---|
93 | e.printStackTrace(); |
---|
94 | } catch (JSONException e) { |
---|
95 | // TODO Auto-generated catch block |
---|
96 | e.printStackTrace(); |
---|
97 | } finally { |
---|
98 | try { |
---|
99 | if (irdr != null) { |
---|
100 | irdr.close(); |
---|
101 | } |
---|
102 | if (fin != null) { |
---|
103 | fin.close(); |
---|
104 | } |
---|
105 | } catch (IOException e) { |
---|
106 | // TODO Auto-generated catch block |
---|
107 | e.printStackTrace(); |
---|
108 | } |
---|
109 | return readobject; |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * |
---|
115 | */ |
---|
116 | public static void saveGlobalSettings(Context context, boolean parseReply, String parseReplyText, boolean failedReply, String failedReplyText) { |
---|
117 | JSONObject settingsObj = new JSONObject(); |
---|
118 | FileOutputStream fos = null; |
---|
119 | try { |
---|
120 | settingsObj.put(KEY_PARSE_REPLY, parseReply); |
---|
121 | settingsObj.put(KEY_PARSE_REPLY_TEXT, parseReplyText); |
---|
122 | settingsObj.put(KEY_FAILED_REPLY, failedReply); |
---|
123 | settingsObj.put(KEY_FAILED_REPLY_TEXT, failedReplyText); |
---|
124 | } catch (JSONException e1) { |
---|
125 | e1.printStackTrace(); |
---|
126 | } |
---|
127 | |
---|
128 | try { |
---|
129 | fos = context.openFileOutput(SETTINGS_FILE, Context.MODE_PRIVATE); |
---|
130 | fos.write(settingsObj.toString().getBytes()); |
---|
131 | } catch (FileNotFoundException e) { |
---|
132 | // TODO Auto-generated catch block |
---|
133 | e.printStackTrace(); |
---|
134 | Log.d(LOG_DEBUG_KEY, e.getMessage()); |
---|
135 | } catch (IOException e) { |
---|
136 | // TODO Auto-generated catch block |
---|
137 | e.printStackTrace(); |
---|
138 | Log.d(LOG_DEBUG_KEY, e.getMessage()); |
---|
139 | } |
---|
140 | finally { |
---|
141 | if(fos != null) { |
---|
142 | try { |
---|
143 | fos.close(); |
---|
144 | } catch (IOException e) { |
---|
145 | // TODO Auto-generated catch block |
---|
146 | e.printStackTrace(); |
---|
147 | } |
---|
148 | } |
---|
149 | SmsParseReceiver.initGlobals(context); |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|