Fix bug when update.json file not exist
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / CheckVersionInfoTask.java
1 package ai.suanzi.rtmpclient;
2
3 import android.content.Context;
4 import android.os.AsyncTask;
5 import org.apache.log4j.Logger;
6 import org.json.JSONException;
7 import org.json.JSONObject;
8
9 import java.io.BufferedReader;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.net.HttpURLConnection;
15 import java.net.URL;
16 import android.content.Intent;
17
18
19 public class CheckVersionInfoTask extends AsyncTask<Void, Void, String> {
20
21     private static Logger gLogger = Logger.getLogger("CheckVersionInfoTask");
22     private Context mContext;
23
24     private static final String VERSION_INFO_URL = "http://downloads.suanzi.ai/RtmpClient/update.json";
25
26     public CheckVersionInfoTask(Context context){
27         this.mContext = context;
28     }
29
30     @Override
31     protected String doInBackground(Void... params){
32         gLogger.debug("doInBackground");
33         return getVersionInfo(VERSION_INFO_URL);
34     }
35
36     @Override
37     protected void onPreExecute(){
38         gLogger.debug("onPreExecute");
39     }
40
41     @Override
42     protected void onPostExecute(String result) {
43         gLogger.debug("onPostExecute, update.json " + result);
44         parseJson(result);
45     }
46
47     private String getVersionInfo(String urlStr) {
48         HttpURLConnection urlConnection = null;
49         InputStream is = null;
50         BufferedReader buffer = null;
51         String result = null;
52         try {
53             URL url = new URL(urlStr);
54             urlConnection = (HttpURLConnection) url.openConnection();
55             is = urlConnection.getInputStream();
56             buffer = new BufferedReader(new InputStreamReader(is));
57             StringBuilder strBuilder = new StringBuilder();
58             String line;
59             while ((line = buffer.readLine()) != null) {
60                 strBuilder.append(line);
61             }
62             result = strBuilder.toString();
63         } catch (FileNotFoundException e){
64             gLogger.error("getVersionInfo - File not found " + e.getMessage());
65             e.printStackTrace();
66
67         } catch (Exception e){
68             gLogger.error("getVersionInfo - error: " + e.getMessage());
69             e.printStackTrace();
70         } finally {
71             if (buffer != null) {
72                 try {
73                     buffer.close();
74                 } catch (IOException e){
75                     e.printStackTrace();
76                 }
77             }
78             if (urlConnection != null){
79                 urlConnection.disconnect();
80             }
81         }
82         return result;
83     }
84
85     private void parseJson(String result) {
86         if(result == null || result.length() == 0) {
87             gLogger.error("parseJson - " + result);
88             return;
89         }
90         try {
91             JSONObject obj = new JSONObject(result);
92             String apkUrl = obj.getString("url");
93             String updateMessage = obj.getString("updateMessage");
94             int apkCode = obj.getInt("versionCode");
95             int versionCode = getCurrentVersionCode();
96             gLogger.error("apkUrl: " + apkUrl);
97             gLogger.error("UpdateMessage: " + updateMessage);
98             gLogger.error("apkCode: " + apkCode);
99             gLogger.error("versionCode: " + versionCode);
100             if(apkCode > versionCode){
101                 gLogger.debug("Found new version, current is " + versionCode + ", will update to " + apkCode);
102                 goToDownloadApk(apkUrl);
103             }
104         } catch (JSONException e){
105             gLogger.error("parseJson - error " + e.getMessage());
106             e.printStackTrace();
107         }
108     }
109
110     private int getCurrentVersionCode(){
111         return BuildConfig.VERSION_CODE;
112     }
113
114     private void goToDownloadApk(String downloadUrl){
115         Intent intent = new Intent(mContext, DownloadApkService.class);
116         intent.putExtra("apkUrl", downloadUrl);
117         mContext.startService(intent);
118     }
119 }