From 82cc6c94fdc08b1814fbf7195e88cf335eed3e8d Mon Sep 17 00:00:00 2001
From: haijiang <181069201@qq.com>
Date: Wed, 19 Sep 2018 14:00:15 +0800
Subject: [PATCH] 提交

---
 app/src/main/java/com/moral/yunfushao/update/UpdateManager.java |  171 ++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 120 insertions(+), 51 deletions(-)

diff --git a/app/src/main/java/com/moral/yunfushao/update/UpdateManager.java b/app/src/main/java/com/moral/yunfushao/update/UpdateManager.java
index 98d5e8c..0ebaa4d 100644
--- a/app/src/main/java/com/moral/yunfushao/update/UpdateManager.java
+++ b/app/src/main/java/com/moral/yunfushao/update/UpdateManager.java
@@ -20,18 +20,34 @@
 import android.view.View;
 import android.widget.ProgressBar;
 
+import com.lzy.okgo.OkGo;
+import com.lzy.okgo.cache.CacheMode;
+import com.lzy.okgo.callback.StringCallback;
+import com.lzy.okgo.request.BaseRequest;
+import com.moral.andbrickslib.utils.log.XLog;
 import com.moral.yunfushao.MainActivity;
 import com.moral.yunfushao.R;
 import com.moral.yunfushao.common.API;
+import com.moral.yunfushao.httputils.HttpCallBack;
+import com.moral.yunfushao.httputils.HttpUtils;
+import com.moral.yunfushao.utils.T;
 
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.HashMap;
+
+import okhttp3.Call;
+import okhttp3.Response;
 
 public class UpdateManager {
     /* ��������� */
@@ -99,6 +115,33 @@
     }
 
     /**
+     * ������������������
+     */
+    public void checkUpdateJson() {
+        String data = getUrlData(API.UPDATE);
+        try {
+            JSONObject resJosn = new JSONObject(data);
+            mHashMap = new HashMap<>();
+            mHashMap.put("version", resJosn.optString("version"));
+            mHashMap.put("name", resJosn.optString("name"));
+            mHashMap.put("message", "");
+            mHashMap.put("url", resJosn.optString("url"));
+            if (isUpdate()) {
+                if (Build.VERSION.SDK_INT >= 23) {
+                    mActivity.updateAPP();
+                } else {
+                    // ���������������������
+                    showNoticeDialog();
+                }
+            } else {
+//                Log.d("haijiang","���������������������");
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
      * ���������������������������������
      *
      * @return
@@ -141,7 +184,7 @@
         // ���������������
         Builder builder = new Builder(mActivity);
         builder.setTitle(R.string.soft_update_title);
-        if(message == null || message.isEmpty()) {
+        if (message == null || message.isEmpty()) {
             builder.setMessage(R.string.soft_update_info);
         } else {
             builder.setMessage(message);
@@ -172,8 +215,7 @@
     /**
      * ���������������������������
      */
-    private void showDownloadDialog()
-    {
+    private void showDownloadDialog() {
         // ���������������������������
         Builder builder = new Builder(mActivity);
         builder.setTitle(R.string.soft_updating);
@@ -183,11 +225,9 @@
         mProgress = (ProgressBar) v.findViewById(R.id.update_progress);
         builder.setView(v);
         // ������������
-        builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener()
-        {
+        builder.setNegativeButton(R.string.soft_update_cancel, new OnClickListener() {
             @Override
-            public void onClick(DialogInterface dialog, int which)
-            {
+            public void onClick(DialogInterface dialog, int which) {
                 dialog.dismiss();
                 // ������������������
                 cancelUpdate = true;
@@ -202,8 +242,7 @@
     /**
      * ������apk������
      */
-    private void downloadApk()
-    {
+    private void downloadApk() {
         // ���������������������������
         new downloadApkThread().start();
     }
@@ -212,8 +251,8 @@
      * ������������������
      *
      * @author coolszy
-     *@date 2012-4-26
-     *@blog http://blog.92coding.com
+     * @date 2012-4-26
+     * @blog http://blog.92coding.com
      */
     private class downloadApkThread extends Thread {
         @Override
@@ -221,49 +260,49 @@
             try {
                 // ������SD������������������������������������������������
                 //if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
-                    // ������������������������
-                    //String sdpath = Environment.getExternalStorageDirectory() + "/";
-                    //mSavePath = sdpath + "download";
+                // ������������������������
+                //String sdpath = Environment.getExternalStorageDirectory() + "/";
+                //mSavePath = sdpath + "download";
 
-                    mSavePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
+                mSavePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
 
-                    URL url = new URL(mHashMap.get("url"));
-                    // ������������
-                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-                    conn.connect();
-                    // ������������������
-                    int length = conn.getContentLength();
-                    // ���������������
-                    InputStream is = conn.getInputStream();
+                URL url = new URL(mHashMap.get("url"));
+                // ������������
+                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+                conn.connect();
+                // ������������������
+                int length = conn.getContentLength();
+                // ���������������
+                InputStream is = conn.getInputStream();
 
-                    File file = new File(mSavePath);
-                    // ������������������������������
-                    if (!file.exists()) {
-                        file.mkdir();
+                File file = new File(mSavePath);
+                // ������������������������������
+                if (!file.exists()) {
+                    file.mkdir();
+                }
+                File apkFile = new File(mSavePath, mHashMap.get("name"));
+                FileOutputStream fos = new FileOutputStream(apkFile);
+                int count = 0;
+                // ������
+                byte buf[] = new byte[1024];
+                // ������������������
+                do {
+                    int numread = is.read(buf);
+                    count += numread;
+                    // ���������������������
+                    progress = (int) (((float) count / length) * 100);
+                    // ������������
+                    mHandler.sendEmptyMessage(DOWNLOAD);
+                    if (numread <= 0) {
+                        // ������������
+                        mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
+                        break;
                     }
-                    File apkFile = new File(mSavePath, mHashMap.get("name"));
-                    FileOutputStream fos = new FileOutputStream(apkFile);
-                    int count = 0;
-                    // ������
-                    byte buf[] = new byte[1024];
-                    // ������������������
-                    do {
-                        int numread = is.read(buf);
-                        count += numread;
-                        // ���������������������
-                        progress = (int) (((float) count / length) * 100);
-                        // ������������
-                        mHandler.sendEmptyMessage(DOWNLOAD);
-                        if (numread <= 0) {
-                            // ������������
-                            mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
-                            break;
-                        }
-                        // ������������
-                        fos.write(buf, 0, numread);
-                    } while (!cancelUpdate);// ���������������������������.
-                    fos.close();
-                    is.close();
+                    // ������������
+                    fos.write(buf, 0, numread);
+                } while (!cancelUpdate);// ���������������������������.
+                fos.close();
+                is.close();
                 //}
             } catch (MalformedURLException e) {
                 e.printStackTrace();
@@ -273,7 +312,9 @@
             // ���������������������������
             mDownloadDialog.dismiss();
         }
-    };
+    }
+
+    ;
 
     /**
      * ������APK������
@@ -288,4 +329,32 @@
         i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
         mActivity.startActivity(i);
     }
+
+    public static String getUrlData(String path) {
+        BufferedReader br = null;
+        StringBuffer sb = new StringBuffer();
+        HttpURLConnection connection = null;
+        try {
+            URL url = new URL(path);
+            connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("GET");
+            connection.setConnectTimeout(8000);
+            connection.setReadTimeout(8000);
+            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String s = null;
+            while ((s = br.readLine()) != null) {
+                sb.append(s);
+            }
+            br.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (connection != null) {
+                connection.disconnect();
+            }
+        }
+        return sb.toString();
+    }
+
+
 }

--
Gitblit v1.8.0