Commit 74b1d9cd by amateur

1

1 parent 597b8752
package com.polysoft.nafmii.fragment.dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;
import com.alibaba.fastjson.JSON;
import com.polysoft.nafmii.bean.UpdateVersion;
import com.polysoft.nafmii.net.OkhttpApi;
import com.polysoft.nafmii.utils.CtxUtils;
import com.polysoft.nafmii.utils.FileUtils;
import com.polysoft.nafmii.utils.TimerUtils;
import com.polysoft.nafmii.utils.ToastUtils;
import com.whaty.nafmii.BuildConfig;
import com.whaty.nafmii.R;
import java.io.File;
public class UpdateDialog extends DialogFragment implements View.OnClickListener {
public static UpdateDialog show(FragmentManager mgr, UpdateVersion info) {
if (null == info) {
info = new UpdateVersion();
info.setIsForce("0");
info.setvNumber("2.1.1");
info.setLocation("https://cos.pgyer.com/319735d279de31ab4aea67d07c0706e3.apk?sign=34fa92d8e78cc084322dc868a10299ab&t=1634904770&response-content-disposition=attachment%3Bfilename%3DNAFMII%E4%B9%8B%E7%AA%97-qp_2.1.apk");
}
UpdateDialog dialog = new UpdateDialog();
Bundle bundle = new Bundle();
bundle.putString("data", JSON.toJSONString(info));
dialog.setArguments(bundle);
dialog.show(mgr, "UpdateDialog");
return dialog;
}
private UpdateVersion versionInfo;
private View tipsLayout, progressLayout;
private ProgressBar progressBar;
private TextView progressText, tipsContent, tipsOk;
private boolean isInstall = false;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
super.setCancelable(false);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
if (null != bundle) {
String json = bundle.getString("data");
if (TextUtils.isEmpty(json)) {
this.versionInfo = new UpdateVersion();
} else {
this.versionInfo = JSON.parseObject(json, UpdateVersion.class);
}
}
View view = inflater.inflate(R.layout.update_dialog, container, false);
this.initView(view);
return view;
}
void initView(View view) {
this.tipsLayout = view.findViewById(R.id.update_layout_tips);
this.tipsLayout.findViewById(R.id.update_tips_cancel).setOnClickListener(this);
this.tipsLayout.findViewById(R.id.update_tips_ok).setOnClickListener(this);
this.tipsContent = this.tipsLayout.findViewById(R.id.update_tips_content);
this.tipsOk = this.tipsLayout.findViewById(R.id.update_tips_ok);
this.progressLayout = view.findViewById(R.id.update_layout_progress);
this.progressBar = this.progressLayout.findViewById(R.id.update_progress_bar);
this.progressText = this.progressLayout.findViewById(R.id.update_progress_text);
if (this.isMustUpdate()) {
this.tipsLayout.findViewById(R.id.update_tips_cancel).setVisibility(View.GONE);
}
}
boolean isMustUpdate() {
return "0".equals(this.versionInfo.getIsForce());
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.update_tips_cancel:
dismiss();
break;
case R.id.update_tips_ok:
this.start();
break;
}
}
void showFailTips() {
this.progressLayout.setVisibility(View.GONE);
this.tipsLayout.setVisibility(View.VISIBLE);
this.tipsContent.setText("版本更新失败,请重新更新!");
}
void showInstallTips() {
this.progressLayout.setVisibility(View.GONE);
this.tipsLayout.setVisibility(View.VISIBLE);
this.tipsContent.setText("下载完成立即安装?");
}
boolean checkPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//是否有安装位置来源的权限
if (!getContext().getPackageManager().canRequestPackageInstalls()) {
ToastUtils.showLong("安装应用需要打开安装未知来源应用权限,请去设置中开启权限");
Uri packageUri = Uri.parse("package:" + CtxUtils.getPackageName());
Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageUri);
;
TimerUtils.setTimeout(() -> {
startActivity(intent);
}, 2000);
return false;
}
}
return true;
}
void start() {
if (!this.checkPermission()) {
return;
}
this.tipsLayout.setVisibility(View.GONE);
this.progressLayout.setVisibility(View.VISIBLE);
this.download();
}
@Override
public void onResume() {
super.onResume();
if (this.isInstall) {
if (!this.isMustUpdate()) {
dismiss();
}
ToastUtils.showLong("安装失败!");
}
}
void download() {
this.updateProgress(0);
String url = this.versionInfo.getLocation();
String version = this.versionInfo.getvNumber();
String name = String.format("nafmii-%s.apk", version);
File file = new File(FileUtils.getSysDownLoadDir(), name);
if (file.exists() && BuildConfig.DEBUG) {
updateProgress(100);
CtxUtils.installApk(getActivity(), file.getAbsolutePath());
isInstall = true;
return;
}
OkhttpApi.doDownload(url, file, new OkhttpApi.IDownListener() {
@Override
public void onProgress(int progress) {
if (progress == 100) {
progressText.post(() -> {
isInstall = true;
updateProgress(progress);
CtxUtils.installApk(getActivity(), file.getAbsolutePath());
});
return;
}
progressText.post(() -> updateProgress(progress));
}
@Override
public void onFail() {
progressText.post(() -> showFailTips());
}
});
}
private void updateProgress(int progress) {
progressBar.setProgress(progress);
progressText.setText(String.format("%d%%", progress));
}
}
package com.polysoft.nafmii.utils; package com.polysoft.nafmii.utils;
import android.app.Application; import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Environment; import android.os.Environment;
import androidx.core.content.FileProvider;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import ren.yale.android.cachewebviewlib.utils.AppUtils;
public class CtxUtils { public class CtxUtils {
private static Application application; private static Application application;
...@@ -65,5 +73,24 @@ public class CtxUtils { ...@@ -65,5 +73,24 @@ public class CtxUtils {
return ""; return "";
} }
public static String getPackageName() {
return getPackageInfo().packageName;
}
public static void installApk(Context context, String downloadApk) {
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(downloadApk);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(context, getPackageName() + ".fileprovider", file);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
}
context.startActivity(intent);
}
} }
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#f3f3f3" />
</shape>
</item>
<item android:right="1px">
<shape android:shape="rectangle">
<solid android:color="@android:color/white" />
</shape>
</item>
</layer-list>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical">
<include
android:id="@+id/update_layout_tips"
layout="@layout/update_layout_tips" />
<include
android:id="@+id/update_layout_progress"
layout="@layout/update_layout_progress"
android:visibility="gone" />
</LinearLayout>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp">
<TextView
android:id="@+id/update_progress_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版本更新"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal"
android:padding="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/update_progress_title"
tools:ignore="MissingConstraints">
<ProgressBar
android:id="@+id/update_progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:max="100" />
<TextView
android:id="@+id/update_progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="20dp"
android:text="0%"
tools:layout_editor_absoluteX="251dp"
tools:layout_editor_absoluteY="56dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file \ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/update_tips_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="温馨提示"
android:textColor="#333"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/update_tips_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="当前检测到最新版本,是否更新?"
app:layout_constraintEnd_toEndOf="@+id/update_tips_title"
app:layout_constraintStart_toStartOf="@+id/update_tips_title"
app:layout_constraintTop_toBottomOf="@+id/update_tips_title" />
<LinearLayout
android:id="@+id/update_tips_line"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="30dp"
android:background="#f3f3f3"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/update_tips_content"
tools:ignore="MissingConstraints" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/update_tips_line">
<TextView
android:id="@+id/update_tips_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/border_right"
android:gravity="center"
android:text="取消" />
<TextView
android:id="@+id/update_tips_ok"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="立即更新"
android:textColor="#206CB3" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!