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 619952c..9f76c4e 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 @@ -2,6 +2,7 @@ package top.jerryyan.RN.A.VersionUpgrade; import android.app.Activity; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageInfo; @@ -14,6 +15,8 @@ import android.os.Message; import android.provider.Settings; import android.util.Log; +import androidx.core.content.FileProvider; + import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; @@ -57,12 +60,12 @@ public class UpgradeModule extends ReactContextBaseJavaModule { return constants; } - private boolean _hasPermissionToInstall() { + boolean _hasPermissionToInstall() { Activity activity = reactContext.getCurrentActivity(); return activity != null && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || activity.getPackageManager().canRequestPackageInstalls()); } - private void _requestInstallPermission() { + void _requestInstallPermission() { Activity activity = reactContext.getCurrentActivity(); if (activity == null) return; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { @@ -96,6 +99,21 @@ public class UpgradeModule extends ReactContextBaseJavaModule { .show(); } + void installPackage(String fileUri){ + Intent install = new Intent(Intent.ACTION_VIEW); + Uri fileUri1 = Uri.parse(fileUri); + File file = new File(fileUri1.getPath()); + if (Build.VERSION.SDK_INT >= 24) { + Uri apkUri = FileProvider.getUriForFile(reactContext, reactContext.getPackageName().concat(".fileprovider"), file); + install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + install.setDataAndType(apkUri, "application/vnd.android.package-archive"); + } else { + install.setDataAndType(Uri.parse(fileUri), "application/vnd.android.package-archive"); + } + install.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + reactContext.startActivity(install); + } + private void _startDownload() { OkHttpClient okHttpClient = new OkHttpClient(); final Request request = new Request.Builder() @@ -104,6 +122,16 @@ public class UpgradeModule extends ReactContextBaseJavaModule { .build(); Call call = okHttpClient.newCall(request); final Handler handler = new DoInstallHandler(this); + if(!this._hasPermissionToInstall()) this._requestInstallPermission(); + final ProgressDialog dialog = new ProgressDialog(reactContext.getCurrentActivity()); + dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + dialog.setCanceledOnTouchOutside(false); + dialog.setCancelable(true); + dialog.setTitle("正在下载中"); + dialog.setMessage("请稍后..."); + dialog.setProgress(0); + dialog.setMax(100); + dialog.show(); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { @@ -119,13 +147,22 @@ public class UpgradeModule extends ReactContextBaseJavaModule { try { stream = response.body().byteStream(); byte[] buffer = new byte[2048]; + long total = response.body().contentLength(); int len; - File file = new File(Environment.getDataDirectory() ,"update.apk"); - fileOutputStream = new FileOutputStream(file); + long current = 0; + + File file = new File(reactContext.getCacheDir().getAbsolutePath() ,"update.apk"); + fileOutputStream = new FileOutputStream(file, false); while ((len = stream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, len); + current += len; + dialog.setProgress((int)((100.0f*current)/total)); } fileOutputStream.flush(); + Message message = new Message(); + message.what = 0; + message.obj = file.toURI().toString(); + handler.sendMessage(message); } catch (IOException e) { Log.e("REQUEST FAILED", "AAA", e); } finally { @@ -220,6 +257,10 @@ final class DoInstallHandler extends Handler { @Override public void handleMessage(Message message) { - module._alertDialog(); + if(!module._hasPermissionToInstall()) module._requestInstallPermission(); + if(message.what == 0){ + String fileUri = (String)message.obj; + module.installPackage(fileUri); + } } } \ No newline at end of file