add user input
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / UserInfo.java
1 package ai.suanzi.rtmpclient;
2
3 import android.content.Context;
4 import android.util.Log;
5 import android.widget.Toast;
6
7 import java.io.BufferedReader;
8 import java.io.BufferedWriter;
9 import java.io.File;
10 import java.io.FileReader;
11 import java.io.FileWriter;
12 import java.io.IOException;
13 import java.io.InputStream;
14
15 import org.json.JSONException;
16 import org.json.JSONObject;
17
18 public class UserInfo {
19
20     public String server = "";
21     public String user = "suanzi";
22     public String macAddr = "ac83f34ead90";
23     public String cameraId = "cameraId";
24     private static String configPath;
25
26     private static UserInfo instance = null;
27     private UserInfo () {}
28
29     public static void setConfigPath(String fname){
30         configPath = fname;
31     }
32
33     public static UserInfo getConfig() {
34         if (instance != null) return instance;
35         File file = new File(configPath);
36         StringBuilder text = new StringBuilder();
37         try {
38             BufferedReader br = new BufferedReader(new FileReader(file));
39             String line;
40             while((line = br.readLine()) != null){
41                 text.append(line);
42             }
43             br.close();
44         }catch (IOException e){
45             e.printStackTrace();
46         }
47
48         String json = text.toString();
49
50         UserInfo info = new UserInfo();
51
52         try {
53             JSONObject jobj = new JSONObject(json);
54             info.server = jobj.getString("server");
55             info.user = jobj.getString("user");
56             info.macAddr = jobj.getString("macAddr");
57             info.cameraId = jobj.getString("cameraId");
58         } catch (JSONException e){
59             e.printStackTrace();
60         }
61         instance = info;
62         return info;
63     }
64
65     public boolean saveConfig() {
66
67         String jstring = toString();
68
69         Log.e("Config", "xxxxxxxxx "  + jstring);
70
71         File file = new File(configPath);
72         try{
73             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
74             bw.write(jstring);
75             bw.close();
76         } catch (IOException e){
77             e.printStackTrace();
78             return false;
79         }
80         return true;
81     }
82
83     public void update(String server, String user, String macAddr, String cameraId) {
84         this.server = server;
85         this.user = user;
86         this.macAddr = macAddr;
87         this.cameraId = cameraId;
88     }
89
90     public String toString () {
91         JSONObject obj = toJsonObj();
92         if (obj.equals(null)) return "";
93         return obj.toString();
94     }
95
96     private JSONObject toJsonObj () {
97         try {
98             JSONObject obj = new JSONObject();
99             obj.put("server", this.server);
100             obj.put("user", this.user);
101             obj.put("macAddr", this.macAddr);
102             obj.put("cameraId", this.cameraId);
103             return obj;
104         } catch (JSONException e) {
105             e.printStackTrace();
106             return null;
107         }
108     }
109 }