Fix bug when update.json file not exist
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / MainActivity.java
index 685d85d..33ac4a6 100644 (file)
@@ -1,6 +1,8 @@
 package ai.suanzi.rtmpclient;
 
 import android.content.IntentFilter;
+import android.hardware.Camera;
+import android.net.Uri;
 import android.support.design.widget.TextInputEditText;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AppCompatActivity;
@@ -29,15 +31,11 @@ import android.content.ComponentName;
 import ai.suanzi.rtmpclient.MyService.LocalBinder;
 import android.os.IBinder;
 import android.net.ConnectivityManager;
-
-//"rtmp://gpussh.suanzi.ai:1935/myapp/suanzi_ac83f34ead90";
+import android.view.ViewGroup;
 
 public class MainActivity extends AppCompatActivity implements MyService.MyServiceEventListener, CameraView.Callback {
 
-    private static final String TAG = "MainActivity";
     private Logger gLogger;
-
-    private String mMacAddr = "";
     private NetworkMonitor networkMonitor;
     private UsbMonitor mUsbMonitor;
     private ServiceHealthMonitor mServiceHealthMonitor;
@@ -49,29 +47,7 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
 
     private CameraView mCameraView;
 
-    private void configLog(){
-        try {
-            final LogConfigurator logConfigurator = new LogConfigurator();
-            String fname = getExternalFilesDir(null) + File.separator + "log.txt";
-            logConfigurator.setFileName(fname);
-            Log.e(TAG, "Log file is located at: " + fname);
-            logConfigurator.setRootLevel(Level.DEBUG);
-            logConfigurator.setLevel("org.apache", Level.ERROR);
-            logConfigurator.setMaxFileSize(1024 * 1024 * 10);
-            logConfigurator.configure();
-            gLogger = Logger.getLogger(getClass());
-        } catch (Exception e){
-            e.printStackTrace();
-        }
-    }
-
     private void init(){
-        configLog();
-        gLogger.debug("#######################################");
-        // set config file
-        UserInfo.setConfigPath(getExternalFilesDir(null) + File.separator + "config");
-
-        this.mMacAddr = getMacAddr();
 
         mIntent = new Intent(this, MyService.class);
         mUsbMonitor = new UsbMonitor(new UsbMonitor.UsbListener() {
@@ -129,7 +105,7 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
                 doBindService();
             }
         });
-        mServiceHealthMonitor.setInterval(INTERVAL); // 5 minutes
+        mServiceHealthMonitor.setInterval(INTERVAL);
     }
 
 
@@ -150,9 +126,10 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
             LocalBinder mLocalBinder = (LocalBinder)service;
             mServer = mLocalBinder.getServiceInstance();
             mServer.setServiceEventListener(MainActivity.this);
-            if(mServer.setRtmpUrl(UserInfo.getConfig().toUrl(mMacAddr))){
-                //mServer.startPreview(mHolder);
+            if(mServer.setRtmpUrl(UserInfo.toUrl())){
                 mServer.startPreview(mCameraView.getHolder());
+                Camera.Size cs = mServer.getBestPictureSize();
+                mCameraView.setLayout(cs.width, cs.height);
             }
 
 
@@ -182,31 +159,36 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
         getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
         ///setContentView(R.layout.activity_main);
 
-
-        DisplayMetrics outMetrics = new DisplayMetrics();
-        this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
-
         ActionBar actionBar = getSupportActionBar();
         actionBar.hide();
 
+        DisplayMetrics outMetrics = new DisplayMetrics();
         this.getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
 
         CameraView.SCREEN_WIDTH = outMetrics.widthPixels;
         CameraView.SCREEN_HEIGHT = outMetrics.heightPixels;
+        gLogger.debug("Screen size is w: " + CameraView.SCREEN_WIDTH  + ", h: " + CameraView.SCREEN_HEIGHT);
+
+
+
         mCameraView = new CameraView(getApplicationContext(), this);
-        setContentView(mCameraView);
+        setContentView(R.layout.activity_main);
+
+        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(640, 480);
+        addContentView(mCameraView, lp);
+        //setContentView(mCameraView);
     }
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        LogUtil.config(this);
+        gLogger = Logger.getLogger(getClass());
+        gLogger.error("onCreate ---------> ");
+        UserInfo.readConfig(getExternalFilesDir(null) + File.separator + "config", getMacAddr());
         initCameraView();
-
-
         init();
-        loadConfig();
 
-
-        if(NetworkMonitor.isNetworkAvailable(this) && mUsbMonitor.hasUsbCamera()){
+        if(canStartService()){
             gLogger.error("Current network is available");
             doBindService();
         } else {
@@ -217,6 +199,10 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
             gLogger.debug("mServiceHealthMonitor start, interval " + INTERVAL);
             mServiceHealthMonitor.start();
         }
+        ////////// LogUtil.sendLogs();  /// Log test
+        //LogUtil.sendLogs();
+
+        new CheckVersionInfoTask(MainActivity.this).execute();
     }
 
     @Override
@@ -270,30 +256,15 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
     private String getMacAddr() {
         WifiManager manager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
         WifiInfo info = manager.getConnectionInfo();
+        gLogger.debug("Mac Address is " + info.getMacAddress());
         return info.getMacAddress().replace(":", ""); //02:00:00:00:00:00 - 020000000000
     }
 
-    private void loadConfig() {
-//        UserInfo info = UserInfo.getConfig();
-//        gLogger.error("server is:" + info.server);
-//        mTextServer.setText(info.server.equals("") ? "rtmp://gpussh.suanzi.ai:1935/myapp" : info.server);
-//        mTextUser.setText(info.user);
-//        //mMacAddr = info.macAddr.equals("") ?  this.mMacAddr : info.macAddr;
-//        gLogger.error("loadConfig - url is :" + info.toUrl(mMacAddr));
-    }
+    private boolean canStartService(){
 
-    private void saveConfig() {
-        UserInfo info = UserInfo.getConfig();
-        //info.update(mTextServer.getText().toString(), mTextUser.getText().toString(), mMacAddr, mTextCamera.getText().toString());
-        if(info.saveConfig()) {
-            Toast.makeText(getApplicationContext(), "Config saved", Toast.LENGTH_LONG).show();
-        } else {
-            Toast.makeText(getApplicationContext(), "Error: config saved", Toast.LENGTH_LONG).show();
+        if(System.getProperty("os.arch").equals("i686")){
+            return  true;
         }
-        gLogger.error("saveConfig - url: " + info.toUrl(mMacAddr));
-    }
-
-    private boolean canStartService(){
         return mUsbMonitor.hasUsbCamera() && NetworkMonitor.isNetworkAvailable(this);
     }
 
@@ -301,9 +272,9 @@ public class MainActivity extends AppCompatActivity implements MyService.MyServi
     @Override
     public void onCameraError(String msg){
         gLogger.error("onCameraEvent " + msg);
-        if(mUsbMonitor.hasUsbCamera()){
+        //if(mUsbMonitor.hasUsbCamera()){
             //mServer.reopenCamera();
-        }
+        //}
     }
 
     @Override