diff --git a/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java b/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java index 538eb9e..0fc11bb 100644 --- a/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java +++ b/android/src/main/java/top/jerryyan/RN/A/VersionUpgrade/UpgradeModule.java @@ -20,7 +20,10 @@ import com.facebook.react.bridge.ReactMethod; import org.json.JSONException; import org.json.JSONObject; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -33,7 +36,7 @@ import okhttp3.Response; public class UpgradeModule extends ReactContextBaseJavaModule { private final ReactApplicationContext reactContext; private String DOWNLOAD_URL = "http://luntan.qgmzbxs.com/source/tsgzCircles.apk"; - private String HOST = "http://luntan.qgmzbxs.com/"; + private String HOST = "http://www.tsgzvore.com/"; private String TITLE = "更新"; private String CONTENT = "大家好,我是勤劳的催更新菌\n点击更新,让我们一起成为一天更新八次的暴躁老哥吧"; @@ -68,41 +71,80 @@ public class UpgradeModule extends ReactContextBaseJavaModule { } } - void _alertDialog(){ + void _alertDialog() { final UpgradeModule that = this; AlertDialog dialog = new AlertDialog.Builder(reactContext.getCurrentActivity()) - .setTitle(TITLE) - .setMessage(CONTENT) - .setNeutralButton("取消", - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - } - ) - .setNegativeButton("更新", - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - that._startDownload(); - } - } - ) - .show(); + .setTitle(TITLE) + .setMessage(CONTENT) + .setNeutralButton("取消", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + } + ) + .setNegativeButton("更新", + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + that._startDownload(); + } + } + ) + .show(); } - private void _startDownload(){ + private void _startDownload() { OkHttpClient okHttpClient = new OkHttpClient(); final Request request = new Request.Builder() .url(DOWNLOAD_URL) .method("GET", null) .build(); Call call = okHttpClient.newCall(request); + final Handler handler = new DoInstallHandler(this); + call.enqueue(new Callback() { + @Override + public void onFailure(Call call, IOException e) { + Log.e("PACKAGE DOWNLOAD", call.toString(), e); + } + + @Override + public void onResponse(Call call, Response response) { + if (response.isSuccessful()) { + if (response.body() == null) return; + FileOutputStream fileOutputStream = null; + InputStream stream = null; + try { + stream = response.body().byteStream(); + byte[] buffer = new byte[2048]; + int len = 0; + long total = response.body().contentLength(); + File file = new File("update.apk"); + fileOutputStream = new FileOutputStream(file); + long sum = 0; + while ((len = stream.read(buffer)) != -1) { + fileOutputStream.write(buffer, 0, len); + sum += len; + } + fileOutputStream.flush(); + } catch (IOException e) { + Log.e("REQUEST FAILED", "AAA", e); + } finally { + try { + if (fileOutputStream != null) fileOutputStream.close(); + if (stream != null) stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + }); } @ReactMethod - public void checkUpgrade(){ + public void checkUpgrade() { OkHttpClient okHttpClient = new OkHttpClient(); String upgradeUrl = HOST.concat("versionCheck"); PackageManager manager = reactContext.getPackageManager(); @@ -133,14 +175,14 @@ public class UpgradeModule extends ReactContextBaseJavaModule { @Override public void onResponse(Call call, Response response) { - if(response.isSuccessful()){ - if(response.body() == null) return; + if (response.isSuccessful()) { + if (response.body() == null) return; try { String jsonText = response.body().string(); Log.d("VERSION CHECK", jsonText); JSONObject json = new JSONObject(jsonText); int status = json.getInt("status"); - if(status > 0){ + if (status > 0) { CONTENT = json.getString("message"); // String version = json.getString("version"); JSONObject jsonData = json.getJSONObject("data"); @@ -158,16 +200,28 @@ public class UpgradeModule extends ReactContextBaseJavaModule { } } -final class ShowUpgradeAlertHandler extends Handler -{ +final class ShowUpgradeAlertHandler extends Handler { private UpgradeModule module; - ShowUpgradeAlertHandler(UpgradeModule module) - { + ShowUpgradeAlertHandler(UpgradeModule module) { this.module = module; } + @Override - public void handleMessage(Message message){ + public void handleMessage(Message message) { + module._alertDialog(); + } +} + +final class DoInstallHandler extends Handler { + private UpgradeModule module; + + DoInstallHandler(UpgradeModule module) { + this.module = module; + } + + @Override + public void handleMessage(Message message) { module._alertDialog(); } } \ No newline at end of file