Add auto Update
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / DownloadApkService.java
1 package ai.suanzi.rtmpclient;
2
3 import android.app.IntentService;
4 import org.apache.log4j.Logger;
5
6 import android.content.Context;
7 import android.content.Intent;
8 import android.app.NotificationManager;
9 import android.net.Uri;
10 import android.support.v4.app.NotificationCompat;
11
12 import java.io.FileOutputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.HttpURLConnection;
16 import java.net.URL;
17 import java.io.File;
18
19
20 public class DownloadApkService extends IntentService {
21
22     private static final int BUFFER_SIZE = 10 * 1024;
23     private Logger gLogger = Logger.getLogger("DownloadApkService");
24
25     private static final int NOTIFICATION_ID = 0;
26     private NotificationManager mNotifyManager;
27     private NotificationCompat.Builder mBuilder;
28
29
30
31     public DownloadApkService() {
32         super("DownloadApkService");
33     }
34
35     @Override
36     protected void onHandleIntent(Intent intent) {
37         mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
38         mBuilder = new NotificationCompat.Builder(this);
39         String appName = getString(getApplicationInfo().labelRes);
40         int icon = getApplicationInfo().icon;
41         mBuilder.setContentTitle(appName).setSmallIcon(icon);
42
43         String urlStr = intent.getStringExtra("apkUrl");
44         gLogger.debug("onHandleIntent - " + urlStr);
45         InputStream in = null;
46         FileOutputStream out = null;
47         try{
48             URL url = new URL(urlStr);
49             HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
50             urlConnection.setRequestMethod("GET");
51             urlConnection.setDoOutput(false);
52             urlConnection.setConnectTimeout(10 * 1000);
53             urlConnection.setReadTimeout(10 * 1000);
54             urlConnection.setRequestProperty("Connection", "Keep-Alive");
55             urlConnection.setRequestProperty("Charset", "UTF-8");
56             urlConnection.setRequestProperty("Accept-Encoding", "gzip, defalte");
57             urlConnection.connect();
58
59             long bytetotal = urlConnection.getContentLength();
60             int bytesum = 0;
61             int byteread = 0;
62             in = urlConnection.getInputStream();
63             File dir = this.getExternalCacheDir();
64             String apkName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length());
65             File apkFile = new File(dir, apkName);
66             out = new FileOutputStream(apkFile);
67             byte[] buffer = new byte[BUFFER_SIZE];
68
69             int limit = 0;
70             int oldProgress = 0;
71             while((byteread = in.read(buffer)) != -1){
72                 bytesum += byteread;
73                 out.write(buffer, 0, byteread);
74                 int progress = (int) (bytesum * 100L / bytetotal);
75                 if(progress != oldProgress){
76                     updateProgress(progress);
77                 }
78                 oldProgress = progress;
79             }
80             installApk(apkFile);
81             gLogger.debug("onHandleIntent, download apk finish");
82             mNotifyManager.cancel(NOTIFICATION_ID);
83         } catch (Exception e){
84             gLogger.error("download apk file error, " + e.getMessage());
85             e.printStackTrace();
86         } finally {
87             if (out != null){
88                 try {
89                     out.close();
90                 } catch (IOException e){
91                     gLogger.error(e.getMessage());
92                 }
93             }
94             if (in != null) {
95                 try {
96                     in.close();
97                 } catch (IOException e){
98                     gLogger.error(e.getMessage());
99                 }
100             }
101         }
102     }
103
104     private void updateProgress(int progress){
105         if((progress % 5) == 0)
106             gLogger.debug("updateProgress - " + progress + "%");
107     }
108
109     private void installApk(File apkFile) {
110
111         gLogger.debug("installApk - " + apkFile.toString());
112         Intent intent = new Intent(Intent.ACTION_VIEW);
113 //        try {
114 //            String[] command = { "chmod", "777", apkFile.toString() };
115 //            ProcessBuilder builder = new ProcessBuilder(command);
116 //            gLogger.error("installApk - start");
117 //            builder.start();
118 //        } catch (IOException e){
119 //            gLogger.error(e.getMessage());
120 //            e.printStackTrace();
121 //        }
122         intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
123         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // "application/vnd.android.package-archive"
124         startActivity(intent);
125     }
126 }