Merge branch 'dev'
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / CheckVersionInfoTask.java
diff --git a/app/src/main/java/ai/suanzi/rtmpclient/CheckVersionInfoTask.java b/app/src/main/java/ai/suanzi/rtmpclient/CheckVersionInfoTask.java
new file mode 100644 (file)
index 0000000..d17b85f
--- /dev/null
@@ -0,0 +1,119 @@
+package ai.suanzi.rtmpclient;
+
+import android.content.Context;
+import android.os.AsyncTask;
+import org.apache.log4j.Logger;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import android.content.Intent;
+
+
+public class CheckVersionInfoTask extends AsyncTask<Void, Void, String> {
+
+    private static Logger gLogger = Logger.getLogger("CheckVersionInfoTask");
+    private Context mContext;
+
+    private static final String VERSION_INFO_URL = "http://downloads.suanzi.ai/RtmpClient/update.json";
+
+    public CheckVersionInfoTask(Context context){
+        this.mContext = context;
+    }
+
+    @Override
+    protected String doInBackground(Void... params){
+        gLogger.debug("doInBackground");
+        return getVersionInfo(VERSION_INFO_URL);
+    }
+
+    @Override
+    protected void onPreExecute(){
+        gLogger.debug("onPreExecute");
+    }
+
+    @Override
+    protected void onPostExecute(String result) {
+        gLogger.debug("onPostExecute, update.json " + result);
+        parseJson(result);
+    }
+
+    private String getVersionInfo(String urlStr) {
+        HttpURLConnection urlConnection = null;
+        InputStream is = null;
+        BufferedReader buffer = null;
+        String result = null;
+        try {
+            URL url = new URL(urlStr);
+            urlConnection = (HttpURLConnection) url.openConnection();
+            is = urlConnection.getInputStream();
+            buffer = new BufferedReader(new InputStreamReader(is));
+            StringBuilder strBuilder = new StringBuilder();
+            String line;
+            while ((line = buffer.readLine()) != null) {
+                strBuilder.append(line);
+            }
+            result = strBuilder.toString();
+        } catch (FileNotFoundException e){
+            gLogger.error("getVersionInfo - File not found " + e.getMessage());
+            e.printStackTrace();
+
+        } catch (Exception e){
+            gLogger.error("getVersionInfo - error: " + e.getMessage());
+            e.printStackTrace();
+        } finally {
+            if (buffer != null) {
+                try {
+                    buffer.close();
+                } catch (IOException e){
+                    e.printStackTrace();
+                }
+            }
+            if (urlConnection != null){
+                urlConnection.disconnect();
+            }
+        }
+        return result;
+    }
+
+    private void parseJson(String result) {
+        if(result == null || result.length() == 0) {
+            gLogger.error("parseJson - " + result);
+            return;
+        }
+        try {
+            JSONObject obj = new JSONObject(result);
+            String apkUrl = obj.getString("url");
+            String updateMessage = obj.getString("updateMessage");
+            int apkCode = obj.getInt("versionCode");
+            int versionCode = getCurrentVersionCode();
+            gLogger.error("apkUrl: " + apkUrl);
+            gLogger.error("UpdateMessage: " + updateMessage);
+            gLogger.error("apkCode: " + apkCode);
+            gLogger.error("versionCode: " + versionCode);
+            if(apkCode > versionCode){
+                gLogger.debug("Found new version, current is " + versionCode + ", will update to " + apkCode);
+                goToDownloadApk(apkUrl);
+            }
+        } catch (JSONException e){
+            gLogger.error("parseJson - error " + e.getMessage());
+            e.printStackTrace();
+        }
+    }
+
+    private int getCurrentVersionCode(){
+        return BuildConfig.VERSION_CODE;
+    }
+
+    private void goToDownloadApk(String downloadUrl){
+        Intent intent = new Intent(mContext, DownloadApkService.class);
+        intent.putExtra("apkUrl", downloadUrl);
+        mContext.startService(intent);
+    }
+}