pushing and previewing work when switch background/foreground
[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 = "";
22     public String macAddr = "";
23     public String 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
36         File file = new File(configPath);
37         if (!file.exists()) return new UserInfo();
38
39         StringBuilder text = new StringBuilder();
40         try {
41             BufferedReader br = new BufferedReader(new FileReader(file));
42             String line;
43             while((line = br.readLine()) != null){
44                 text.append(line);
45             }
46             br.close();
47         }catch (IOException e){
48             e.printStackTrace();
49         }
50
51         String json = text.toString();
52
53         UserInfo info = new UserInfo();
54
55         try {
56             JSONObject jobj = new JSONObject(json);
57             info.server = jobj.getString("server");
58             info.user = jobj.getString("user");
59             info.macAddr = jobj.getString("macAddr");
60             info.cameraId = jobj.getString("cameraId");
61         } catch (JSONException e){
62             e.printStackTrace();
63         }
64         instance = info;
65         return info;
66     }
67
68     public boolean saveConfig() {
69
70         String jstring = toString();
71
72         Log.e("Config", "xxxxxxxxx "  + jstring);
73
74         File file = new File(configPath);
75         try{
76             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
77             bw.write(jstring);
78             bw.close();
79         } catch (IOException e){
80             e.printStackTrace();
81             return false;
82         }
83         return true;
84     }
85
86     public void update(String server, String user, String macAddr, String cameraId) {
87         this.server = server;
88         this.user = user;
89         this.macAddr = macAddr;
90         this.cameraId = cameraId;
91     }
92
93     public String toString () {
94         JSONObject obj = toJsonObj();
95         if (obj.equals(null)) return "";
96         return obj.toString();
97     }
98
99     private JSONObject toJsonObj () {
100         try {
101             JSONObject obj = new JSONObject();
102             obj.put("server", this.server);
103             obj.put("user", this.user);
104             obj.put("macAddr", this.macAddr);
105             obj.put("cameraId", this.cameraId);
106             return obj;
107         } catch (JSONException e) {
108             e.printStackTrace();
109             return null;
110         }
111     }
112 }