Add auto Update
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / DownloadApkService.java
diff --git a/app/src/main/java/ai/suanzi/rtmpclient/DownloadApkService.java b/app/src/main/java/ai/suanzi/rtmpclient/DownloadApkService.java
new file mode 100644 (file)
index 0000000..09bfe43
--- /dev/null
@@ -0,0 +1,126 @@
+package ai.suanzi.rtmpclient;
+
+import android.app.IntentService;
+import org.apache.log4j.Logger;
+
+import android.content.Context;
+import android.content.Intent;
+import android.app.NotificationManager;
+import android.net.Uri;
+import android.support.v4.app.NotificationCompat;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.io.File;
+
+
+public class DownloadApkService extends IntentService {
+
+    private static final int BUFFER_SIZE = 10 * 1024;
+    private Logger gLogger = Logger.getLogger("DownloadApkService");
+
+    private static final int NOTIFICATION_ID = 0;
+    private NotificationManager mNotifyManager;
+    private NotificationCompat.Builder mBuilder;
+
+
+
+    public DownloadApkService() {
+        super("DownloadApkService");
+    }
+
+    @Override
+    protected void onHandleIntent(Intent intent) {
+        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
+        mBuilder = new NotificationCompat.Builder(this);
+        String appName = getString(getApplicationInfo().labelRes);
+        int icon = getApplicationInfo().icon;
+        mBuilder.setContentTitle(appName).setSmallIcon(icon);
+
+        String urlStr = intent.getStringExtra("apkUrl");
+        gLogger.debug("onHandleIntent - " + urlStr);
+        InputStream in = null;
+        FileOutputStream out = null;
+        try{
+            URL url = new URL(urlStr);
+            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
+            urlConnection.setRequestMethod("GET");
+            urlConnection.setDoOutput(false);
+            urlConnection.setConnectTimeout(10 * 1000);
+            urlConnection.setReadTimeout(10 * 1000);
+            urlConnection.setRequestProperty("Connection", "Keep-Alive");
+            urlConnection.setRequestProperty("Charset", "UTF-8");
+            urlConnection.setRequestProperty("Accept-Encoding", "gzip, defalte");
+            urlConnection.connect();
+
+            long bytetotal = urlConnection.getContentLength();
+            int bytesum = 0;
+            int byteread = 0;
+            in = urlConnection.getInputStream();
+            File dir = this.getExternalCacheDir();
+            String apkName = urlStr.substring(urlStr.lastIndexOf("/") + 1, urlStr.length());
+            File apkFile = new File(dir, apkName);
+            out = new FileOutputStream(apkFile);
+            byte[] buffer = new byte[BUFFER_SIZE];
+
+            int limit = 0;
+            int oldProgress = 0;
+            while((byteread = in.read(buffer)) != -1){
+                bytesum += byteread;
+                out.write(buffer, 0, byteread);
+                int progress = (int) (bytesum * 100L / bytetotal);
+                if(progress != oldProgress){
+                    updateProgress(progress);
+                }
+                oldProgress = progress;
+            }
+            installApk(apkFile);
+            gLogger.debug("onHandleIntent, download apk finish");
+            mNotifyManager.cancel(NOTIFICATION_ID);
+        } catch (Exception e){
+            gLogger.error("download apk file error, " + e.getMessage());
+            e.printStackTrace();
+        } finally {
+            if (out != null){
+                try {
+                    out.close();
+                } catch (IOException e){
+                    gLogger.error(e.getMessage());
+                }
+            }
+            if (in != null) {
+                try {
+                    in.close();
+                } catch (IOException e){
+                    gLogger.error(e.getMessage());
+                }
+            }
+        }
+    }
+
+    private void updateProgress(int progress){
+        if((progress % 5) == 0)
+            gLogger.debug("updateProgress - " + progress + "%");
+    }
+
+    private void installApk(File apkFile) {
+
+        gLogger.debug("installApk - " + apkFile.toString());
+        Intent intent = new Intent(Intent.ACTION_VIEW);
+//        try {
+//            String[] command = { "chmod", "777", apkFile.toString() };
+//            ProcessBuilder builder = new ProcessBuilder(command);
+//            gLogger.error("installApk - start");
+//            builder.start();
+//        } catch (IOException e){
+//            gLogger.error(e.getMessage());
+//            e.printStackTrace();
+//        }
+        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // "application/vnd.android.package-archive"
+        startActivity(intent);
+    }
+}