Navigation voice changer app for Android
Revision | 4bb598634e48fd04c20f32d517d2ac38440e1c03 (tree) |
---|---|
Time | 2013-07-30 23:51:31 |
Author | ![]() |
Commiter | HMML |
Add config class.
@@ -0,0 +1,51 @@ | ||
1 | +package jp.nekoteki.android.navivoicechanger; | |
2 | + | |
3 | +import java.io.File; | |
4 | +import java.io.FileInputStream; | |
5 | +import java.io.FileOutputStream; | |
6 | +import java.io.InputStreamReader; | |
7 | +import java.io.OutputStreamWriter; | |
8 | +import java.util.Properties; | |
9 | + | |
10 | +import android.content.Context; | |
11 | + | |
12 | +public class Config { | |
13 | + final static public String CONF_FILE = "config.ini"; | |
14 | + | |
15 | + public static String get(Context context, String name) { | |
16 | + return (String) getProp(context).get(name); | |
17 | + } | |
18 | + | |
19 | + public static void set(Context context, String name, String value) { | |
20 | + Properties prop = getProp(context); | |
21 | + prop.setProperty(name, value); | |
22 | + File conf = getConfFile(context); | |
23 | + if (conf == null) return; | |
24 | + try { | |
25 | + prop.store(new OutputStreamWriter(new FileOutputStream(conf), "UTF-8"), "NaviVoiceChanger Config"); | |
26 | + } catch (Exception e) { | |
27 | + e.printStackTrace(); | |
28 | + } | |
29 | + } | |
30 | + | |
31 | + public static Properties getProp(Context context) { | |
32 | + Properties prop = new Properties(); | |
33 | + File conf = getConfFile(context); | |
34 | + if (conf == null) return prop; | |
35 | + if (!conf.exists()) return prop; | |
36 | + try { | |
37 | + prop.load(new InputStreamReader(new FileInputStream(conf), "UTF-8")); | |
38 | + } catch (Exception e) { | |
39 | + e.printStackTrace(); | |
40 | + } | |
41 | + return prop; | |
42 | + } | |
43 | + | |
44 | + public static File getConfFile(Context context) { | |
45 | + File dir = context.getExternalFilesDir(null); | |
46 | + if (dir == null) return null; | |
47 | + if (!dir.exists()) dir.mkdirs(); | |
48 | + return new File(dir, CONF_FILE); | |
49 | + } | |
50 | +} | |
51 | + |