clean the code, and fix bug service stopped when back pressed
[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.net.wifi.WifiInfo;
5 import android.net.wifi.WifiManager;
6 import android.widget.Toast;
7
8 import java.io.BufferedReader;
9 import java.io.BufferedWriter;
10 import java.io.File;
11 import java.io.FileReader;
12 import java.io.FileWriter;
13 import java.io.IOException;
14
15
16 import org.json.JSONException;
17 import org.json.JSONObject;
18 import org.apache.log4j.Logger;
19
20 public class UserInfo {
21
22     public String server = "";
23     public String user = "";
24     public String macAddr = "";
25     public String cameraId = "";
26     private static String configPath;
27     private static Logger gLogger = Logger.getLogger("UserInfo");
28
29     private static String DEFAULT_SERVER = "rtmp://gpussh.suanzi.ai:1935/myapp";
30     private static String DEFAULT_USER = "yunzhi";
31
32     private static UserInfo instance = null;
33     private UserInfo () {}
34
35     public static void setConfigPath(String fname){
36         configPath = fname;
37         File file = new File(configPath);
38         if(!file.exists()) {
39             gLogger.error("Config file: " + configPath + " not exists! Create it");
40             try {
41                 file.createNewFile();
42             }catch (IOException e){
43                 gLogger.error("Create file error: " + e.getMessage());
44                 e.printStackTrace();
45             }
46         }
47     }
48
49     public static UserInfo getConfig() {
50         if (instance != null) return instance;
51
52         File file = new File(configPath);
53         if (!file.exists()){
54             gLogger.error("getConfig - file configpath: " + configPath + " not exists");
55             return new UserInfo();
56         }
57
58         StringBuilder text = new StringBuilder();
59         try {
60             BufferedReader br = new BufferedReader(new FileReader(file));
61             String line;
62             while((line = br.readLine()) != null){
63                 text.append(line);
64             }
65             br.close();
66         }catch (IOException e){
67             e.printStackTrace();
68         }
69
70         String json = text.toString();
71
72         UserInfo info = new UserInfo();
73
74         try {
75             JSONObject jobj = new JSONObject(json);
76             info.server = jobj.getString("server");
77             info.user = jobj.getString("user");
78             info.macAddr = jobj.getString("macAddr");
79             info.cameraId = jobj.getString("cameraId");
80         } catch (JSONException e){
81             gLogger.error("getConfig - error: " + e.getMessage());
82             e.printStackTrace();
83         }
84         instance = info;
85         return info;
86     }
87
88     public boolean saveConfig() {
89
90         String jstring = toString();
91
92         File file = new File(configPath);
93         try{
94             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
95             bw.write(jstring);
96             bw.close();
97         } catch (IOException e){
98             gLogger.error("saveConfig - error: " + e.getMessage());
99             e.printStackTrace();
100             return false;
101         }
102         return true;
103     }
104
105     public void update(String server, String user, String macAddr, String cameraId) {
106         this.server = server;
107         this.user = user;
108         this.macAddr = macAddr;
109         this.cameraId = cameraId;
110     }
111
112     public String toString () {
113         JSONObject obj = toJsonObj();
114         String str = "";
115         if (!obj.equals(null)) {
116             try {
117                 str = obj.toString(4);
118             }catch (JSONException e){
119                 gLogger.error("toString - error: " + e.getMessage());
120                 e.printStackTrace();
121             }
122         }
123         return str;
124     }
125
126     private JSONObject toJsonObj () {
127         try {
128             JSONObject obj = new JSONObject();
129             obj.put("server", this.server);
130             obj.put("user", this.user);
131             obj.put("macAddr", this.macAddr);
132             obj.put("cameraId", this.cameraId);
133             return obj;
134         } catch (JSONException e) {
135             e.printStackTrace();
136             return null;
137         }
138     }
139
140     public String toUrl (String mac) {
141         //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
142         //return server + "/" + user + "_" + macAddr + "_" + cameraId;
143         return (server.equals("") ? DEFAULT_SERVER : server) + "/" + (user.equals("") ? DEFAULT_USER : user) + "_" + mac;
144
145     }
146
147     public String getValue(String key){
148         if(key.equals("server")) return server;
149         if(key.equals("user")) return user;
150         if(key.equals("macAddr")) return macAddr;
151         if(key.equals("cameraId")) return cameraId;
152         return "";
153     }
154 }