进度条

This commit is contained in:
Jerry Yan 2020-03-14 18:22:59 +08:00
parent e23d8e8db7
commit 4deaecbcf6

View File

@ -2,6 +2,7 @@ package top.jerryyan.RN.A.VersionUpgrade;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
@ -14,6 +15,8 @@ import android.os.Message;
import android.provider.Settings; import android.provider.Settings;
import android.util.Log; import android.util.Log;
import androidx.core.content.FileProvider;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactMethod;
@ -57,12 +60,12 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
return constants; return constants;
} }
private boolean _hasPermissionToInstall() { boolean _hasPermissionToInstall() {
Activity activity = reactContext.getCurrentActivity(); Activity activity = reactContext.getCurrentActivity();
return activity != null && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || activity.getPackageManager().canRequestPackageInstalls()); return activity != null && (Build.VERSION.SDK_INT < Build.VERSION_CODES.O || activity.getPackageManager().canRequestPackageInstalls());
} }
private void _requestInstallPermission() { void _requestInstallPermission() {
Activity activity = reactContext.getCurrentActivity(); Activity activity = reactContext.getCurrentActivity();
if (activity == null) return; if (activity == null) return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -96,6 +99,21 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
.show(); .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() { private void _startDownload() {
OkHttpClient okHttpClient = new OkHttpClient(); OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder() final Request request = new Request.Builder()
@ -104,6 +122,16 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
.build(); .build();
Call call = okHttpClient.newCall(request); Call call = okHttpClient.newCall(request);
final Handler handler = new DoInstallHandler(this); 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() { call.enqueue(new Callback() {
@Override @Override
public void onFailure(Call call, IOException e) { public void onFailure(Call call, IOException e) {
@ -119,13 +147,22 @@ public class UpgradeModule extends ReactContextBaseJavaModule {
try { try {
stream = response.body().byteStream(); stream = response.body().byteStream();
byte[] buffer = new byte[2048]; byte[] buffer = new byte[2048];
long total = response.body().contentLength();
int len; int len;
File file = new File(Environment.getDataDirectory() ,"update.apk"); long current = 0;
fileOutputStream = new FileOutputStream(file);
File file = new File(reactContext.getCacheDir().getAbsolutePath() ,"update.apk");
fileOutputStream = new FileOutputStream(file, false);
while ((len = stream.read(buffer)) != -1) { while ((len = stream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len); fileOutputStream.write(buffer, 0, len);
current += len;
dialog.setProgress((int)((100.0f*current)/total));
} }
fileOutputStream.flush(); fileOutputStream.flush();
Message message = new Message();
message.what = 0;
message.obj = file.toURI().toString();
handler.sendMessage(message);
} catch (IOException e) { } catch (IOException e) {
Log.e("REQUEST FAILED", "AAA", e); Log.e("REQUEST FAILED", "AAA", e);
} finally { } finally {
@ -220,6 +257,10 @@ final class DoInstallHandler extends Handler {
@Override @Override
public void handleMessage(Message message) { public void handleMessage(Message message) {
module._alertDialog(); if(!module._hasPermissionToInstall()) module._requestInstallPermission();
if(message.what == 0){
String fileUri = (String)message.obj;
module.installPackage(fileUri);
}
} }
} }