Rename the generated package
[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     /* the content of update.json as below
27     peng@[~/web/RtmpClient]>> cat update.json
28     {
29         "url":"http://downloads.suanzi.ai/RtmpClient/RtmpClient-v0.3.3-release.apk",
30         "versionCode":33,
31         "updateMessage":"1. Fix bug<br/>2. Update<br/>3. test"
32     }
33     */
34
35     public CheckVersionInfoTask(Context context){
36         this.mContext = context;
37     }
38
39     @Override
40     protected String doInBackground(Void... params){
41         gLogger.debug("doInBackground");
42         return getVersionInfo(VERSION_INFO_URL);
43     }
44
45     @Override
46     protected void onPreExecute(){
47         gLogger.debug("onPreExecute");
48     }
49
50     @Override
51     protected void onPostExecute(String result) {
52         gLogger.debug("onPostExecute, update.json " + result);
53         parseJson(result);
54     }
55
56     private String getVersionInfo(String urlStr) {
57         HttpURLConnection urlConnection = null;
58         InputStream is = null;
59         BufferedReader buffer = null;
60         String result = null;
61         try {
62             URL url = new URL(urlStr);
63             urlConnection = (HttpURLConnection) url.openConnection();
64             is = urlConnection.getInputStream();
65             buffer = new BufferedReader(new InputStreamReader(is));
66             StringBuilder strBuilder = new StringBuilder();
67             String line;
68             while ((line = buffer.readLine()) != null) {
69                 strBuilder.append(line);
70             }
71             result = strBuilder.toString();
72         } catch (FileNotFoundException e){
73             gLogger.error("getVersionInfo - File not found " + e.getMessage());
74             e.printStackTrace();
75
76         } catch (Exception e){
77             gLogger.error("getVersionInfo - error: " + e.getMessage());
78             e.printStackTrace();
79         } finally {
80             if (buffer != null) {
81                 try {
82                     buffer.close();
83                 } catch (IOException e){
84                     e.printStackTrace();
85                 }
86             }
87             if (urlConnection != null){
88                 urlConnection.disconnect();
89             }
90         }
91         return result;
92     }
93
94     private void parseJson(String result) {
95         if(result == null || result.length() == 0) {
96             gLogger.error("parseJson - " + result);
97             return;
98         }
99         try {
100             JSONObject obj = new JSONObject(result);
101             String apkUrl = obj.getString("url");
102             String updateMessage = obj.getString("updateMessage");
103             int apkCode = obj.getInt("versionCode");
104             int versionCode = getCurrentVersionCode();
105             gLogger.error("apkUrl: " + apkUrl);
106             gLogger.error("UpdateMessage: " + updateMessage);
107             gLogger.error("apkCode: " + apkCode);
108             gLogger.error("versionCode: " + versionCode);
109             if(apkCode > versionCode){
110                 gLogger.debug("Found new version, current is " + versionCode + ", will update to " + apkCode);
111                 goToDownloadApk(apkUrl);
112             }
113         } catch (JSONException e){
114             gLogger.error("parseJson - error " + e.getMessage());
115             e.printStackTrace();
116         }
117     }
118
119     private int getCurrentVersionCode(){
120         return BuildConfig.VERSION_CODE;
121     }
122
123     private void goToDownloadApk(String downloadUrl){
124         Intent intent = new Intent(mContext, DownloadApkService.class);
125         intent.putExtra("apkUrl", downloadUrl);
126         mContext.startService(intent);
127     }
128 }