Fix memory leak, and all work well except startup and jni log
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / UserInfo.java
index 1d655da..ebac937 100644 (file)
@@ -1,7 +1,5 @@
 package ai.suanzi.rtmpclient;
 
-import android.content.Context;
-import android.util.Log;
 import android.widget.Toast;
 
 import java.io.BufferedReader;
@@ -10,10 +8,11 @@ import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
-import java.io.InputStream;
+
 
 import org.json.JSONException;
 import org.json.JSONObject;
+import org.apache.log4j.Logger;
 
 public class UserInfo {
 
@@ -22,19 +21,34 @@ public class UserInfo {
     public String macAddr = "";
     public String cameraId = "";
     private static String configPath;
+    private static Logger gLogger = Logger.getLogger("UserInfo");
+
 
     private static UserInfo instance = null;
     private UserInfo () {}
 
     public static void setConfigPath(String fname){
         configPath = fname;
+        File file = new File(configPath);
+        if(!file.exists()) {
+            gLogger.error("Config file: " + configPath + " not exists! Create it");
+            try {
+                file.createNewFile();
+            }catch (IOException e){
+                gLogger.error("Create file error: " + e.getMessage());
+                e.printStackTrace();
+            }
+        }
     }
 
     public static UserInfo getConfig() {
         if (instance != null) return instance;
 
         File file = new File(configPath);
-        if (!file.exists()) return new UserInfo();
+        if (!file.exists()){
+            gLogger.error("getConfig - file configpath: " + configPath + " not exists");
+            return new UserInfo();
+        }
 
         StringBuilder text = new StringBuilder();
         try {
@@ -59,6 +73,7 @@ public class UserInfo {
             info.macAddr = jobj.getString("macAddr");
             info.cameraId = jobj.getString("cameraId");
         } catch (JSONException e){
+            gLogger.error("getConfig - error: " + e.getMessage());
             e.printStackTrace();
         }
         instance = info;
@@ -69,14 +84,13 @@ public class UserInfo {
 
         String jstring = toString();
 
-        Log.e("Config", "xxxxxxxxx "  + jstring);
-
         File file = new File(configPath);
         try{
             BufferedWriter bw = new BufferedWriter(new FileWriter(file));
             bw.write(jstring);
             bw.close();
         } catch (IOException e){
+            gLogger.error("saveConfig - error: " + e.getMessage());
             e.printStackTrace();
             return false;
         }
@@ -92,8 +106,16 @@ public class UserInfo {
 
     public String toString () {
         JSONObject obj = toJsonObj();
-        if (obj.equals(null)) return "";
-        return obj.toString();
+        String str = "";
+        if (!obj.equals(null)) {
+            try {
+                str = obj.toString(4);
+            }catch (JSONException e){
+                gLogger.error("toString - error: " + e.getMessage());
+                e.printStackTrace();
+            }
+        }
+        return str;
     }
 
     private JSONObject toJsonObj () {
@@ -109,4 +131,9 @@ public class UserInfo {
             return null;
         }
     }
+
+    public String toUrl () {
+        //rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90_cameraid
+        return server + "/" + user + "_" + macAddr + "_" + cameraId;
+    }
 }