X-Git-Url: http://47.100.26.94:8080/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FCheckVersionInfoTask.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fai%2Fsuanzi%2Frtmpclient%2FCheckVersionInfoTask.java;h=d17b85fde467ae94dbdde228b1d60cacaa3dacd4;hb=dd394951609bc6647111ca8246429543c378ba7b;hp=0000000000000000000000000000000000000000;hpb=d0d496554f96cebf84db3e9b3cf507577272ef8c;p=rtmpclient.git 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 index 0000000..d17b85f --- /dev/null +++ b/app/src/main/java/ai/suanzi/rtmpclient/CheckVersionInfoTask.java @@ -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 { + + 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); + } +}