修改更新版本:1、改成以服务形式下载。2、新增断点续传功能。3、新增下载后检查apk完整性。

修改主播设置联系方式接口地址
This commit is contained in:
zlzw 2024-08-16 14:22:58 +08:00
parent cb87974320
commit 3aeabfa32b
12 changed files with 439 additions and 101 deletions

View File

@ -97,11 +97,12 @@ android {
String variantName = variant.name.capitalize()
def processManifestTask = project.tasks.getByName("process${variantName}Manifest")
processManifestTask.doLast { pm ->
String manifestPath = "build/intermediates/bundle_manifest/google_onlineRelease/bundle-manifest/AndroidManifest.xml"
// String manifestPath = "build/intermediates/bundle_manifest/google_onlineRelease/bundle-manifest/AndroidManifest.xml"
String manifestPath = "build/intermediates/merged_manifests/google_onlineRelease/processGoogle_onlineReleaseManifest/AndroidManifest.xml"
def isGooglePlay = variant.name.contains("google")
println "谷歌版本:" + isGooglePlay
println "文件存在" + file(manifestPath).exists()
println "" + (isGooglePlay != 0)
println "" + (isGooglePlay == 1)
println "" + (file(manifestPath).exists() && isGooglePlay)
if (file(manifestPath).exists() && isGooglePlay) {
def manifestContent = file(manifestPath).getText()

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission
android:name="android.permission.CALL_PHONE"

View File

@ -64,7 +64,9 @@
<activity
android:name="com.yunbao.common.activity.PreviewVideoActivity"
android:screenOrientation="portrait" />
<service android:name="com.yunbao.common.server.DownloadService"
/>
</application>
</manifest>

View File

@ -542,6 +542,13 @@ public class LiveHttpUtil {
HttpClient.getInstance().get("Live.getGiftListApp", LiveHttpConsts.GET_GIFT_LIST)
.execute(callback);
}
/**
* 获取礼物列表同时会返回剩余的钱新版 -用于获取联系方式时的礼物设置
*/
public static void getHotGiftList(HttpCallback callback) {
HttpClient.getInstance().get("Gift.getHotGiftList", LiveHttpConsts.GET_GIFT_LIST)
.execute(callback);
}
/**
* 获取包裹列表

View File

@ -0,0 +1,282 @@
package com.yunbao.common.server;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider;
import com.blankj.utilcode.util.LogUtils;
import com.yunbao.common.R;
import com.yunbao.common.utils.APKDownloadUtil;
import com.yunbao.common.utils.AppManager;
import com.yunbao.common.utils.SpUtil;
import com.yunbao.common.utils.WordUtil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Locale;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class DownloadService extends Service {
public static final String DOWNLOAD_TAG = "downloadProgress";
public static final String DOWNLOAD_TOTAL = "downloadTotal";
private Context mContext;
private int progress;
private boolean isDownload = false;
private boolean isDownloading;
private boolean isForceInstall;
ServiceBinder binder = new ServiceBinder();
@Nullable
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onCreate() {
super.onCreate();
mContext = this;
}
public class ServiceBinder extends Binder {
public void startDownload(String url, boolean isForceInstall, APKDownloadUtil.OnUpdateListener listener) {
if (isDownloading) return;
DownloadService.this.isForceInstall = isForceInstall;
downloadAPK(DownloadService.this, url, listener, 3);
}
public void install() {
LogUtils.e("文件大小:" + downloadFile.length(), "总大小:" + SpUtil.getLongValue(DOWNLOAD_TAG));
if (SpUtil.getLongValue(DOWNLOAD_TAG) == -1 && downloadFile.exists()) {
installApk(mContext, downloadFile, mContext.getPackageName() + ".fileprovider");
if (!isForceInstall) {
isDownload = false;
}
}
}
public boolean isDownloadOK() {
if (downloadFile == null) {
return false;
}
if (SpUtil.getLongValue(DOWNLOAD_TAG) == -1) {
return true;
}
return downloadFile.exists() && checkApk();
}
}
File downloadFile;
public void downloadAPK(Context context, final String url, APKDownloadUtil.OnUpdateListener listener, final int replay) {
if (replay <= 0) {
listener.updateFailure(-2, "DownloadError");
return;
}
downloadFile = new File(context.getCacheDir(), "update_app.apk");
if (SpUtil.getLongValue(DOWNLOAD_TAG) == -1 && checkApk()) {
installApk(mContext, downloadFile, mContext.getPackageName() + ".fileprovider");
return;
}
isDownloading = true;
Request.Builder builder = new Request.Builder().url(url).addHeader("Accept-Encoding", "identity");
if (SpUtil.getLongValue(DOWNLOAD_TAG) > 0) {
builder.addHeader("Range", "bytes=" + SpUtil.getLongValue(DOWNLOAD_TAG) + "-");
}
Request request = builder.build();
LogUtils.e("保存路径:" + downloadFile.getAbsolutePath());
try {
if (!downloadFile.exists() && !downloadFile.createNewFile()) {
isDownload = false;
return;
}
} catch (IOException e) {
e.printStackTrace();
}
createNotification();
new OkHttpClient().newCall(request).enqueue(new Callback() {
private Handler handler = new Handler(Looper.getMainLooper());
@Override
public void onFailure(Call call, IOException e) {
// 下载失败
handler.post(() -> {
listener.updateFailure(-1, e.getMessage());
});
}
@Override
public void onResponse(Call call, Response response) {
Looper.prepare();
byte[] buf = new byte[2048];
int len;
long downloadProgress = SpUtil.getLongValue(DOWNLOAD_TAG);
long total = response.body().contentLength();
try (InputStream inputStream = response.body().byteStream(); RandomAccessFile outputStream = new RandomAccessFile(downloadFile, "rw")) {
long sum = 0;
if (downloadProgress > 0) {
LogUtils.e("断点续传:" + downloadProgress);
}
if (downloadProgress > 0) {
sum = downloadProgress;
outputStream.seek(downloadProgress);
total = downloadProgress + total + 2048;
} else {
outputStream.setLength(total);
SpUtil.setLongValue(DOWNLOAD_TOTAL, total);
}
int tmp = 0;
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
SpUtil.setLongValue(DOWNLOAD_TAG, outputStream.getFilePointer());
sum += len;
progress = (int) (sum * 1.0f / total * 100);
if (tmp != progress) {
tmp = progress;
updateNotification(progress, 100);
}
listener.onProgress(progress);
}
progress = 100;
outputStream.close();
inputStream.close();
SpUtil.setLongValue(DOWNLOAD_TAG, -1);
//启动安装app
LogUtils.e("开始下载:" + "下载完成", "文件路径:" + downloadFile.getAbsolutePath(), "文件是否存在:" + downloadFile.exists(), "已下载大小:" + sum, "文件大小:" + total);
isDownload = true;
updateNotification(progress, 100);
listener.onProgress(progress);
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
isDownloading = false;
} catch (Exception e) {
e.printStackTrace();
LogUtils.e("下载失败:" + e.getMessage(), "重试次数:" + replay);
downloadAPK(mContext, url, listener, replay - 1);
}
}
});
}
private boolean checkApk() {
long apkTotal = SpUtil.getLongValue(DOWNLOAD_TOTAL);
if (apkTotal == 0) return false;
if (downloadFile == null || !downloadFile.exists()) return false;
try {
PackageManager pm = mContext.getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(downloadFile.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
if (info == null) return false;
} catch (Exception e) {
e.printStackTrace();
}
LogUtils.e("文件大小:" + downloadFile.length(), "已下载大小:" + apkTotal);
return apkTotal == downloadFile.length();
}
private NotificationChannel channel;
NotificationCompat.Builder builder;
private void createNotification() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Intent intent = new Intent(this, AppManager.getInstance().getMainActivity().getClass());
intent.putExtra("updateApk", true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
channel = new NotificationChannel("Download_channel", "pd_download", NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
builder = new NotificationCompat.Builder(mContext, "Download_channel")
.setChannelId(channel.getId())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(WordUtil.isNewZh() ? "正在下载" : "Downloading")
.setContentText(WordUtil.isNewZh() ? "下载中" : "Downloading")
.setProgress(100, 0, true)
.setOnlyAlertOnce(true)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_MAX);
manager.notify(114514, builder.build());
}
}
private void updateNotification(int downloadProgress, int total) {
if (channel != null && builder != null) {
builder.setProgress(total, downloadProgress, false);
if (downloadProgress != total) {
builder.setContentText(String.format(Locale.getDefault(), "(%d/%d)", downloadProgress, total));
} else {
builder.setContentText(WordUtil.isNewZh() ? "完成" : "Over");
}
NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(114514, builder.build());
}
}
/**
* 安装apk
*
* @param context
* @param file
*/
public void installApk(Context context, File file, String authority) {
if (!checkApk()) return;
Intent intent = getInstallIntent(context, file, authority);
context.startActivity(intent);
}
/**
* 获取安装Intent
*
* @param context
* @param file
* @param authority
* @return
*/
public Intent getInstallIntent(Context context, File file, String authority) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uriData;
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uriData = FileProvider.getUriForFile(context, authority, file);
} else {
uriData = Uri.fromFile(file);
}
intent.setDataAndType(uriData, type);
return intent;
}
}

View File

@ -104,6 +104,7 @@ public class APKDownloadUtil {
*/
public interface OnUpdateListener {
void updateFailure(int code, String error);
void onProgress(int progress);
}
//
// /**

View File

@ -9,7 +9,6 @@ import com.google.gson.Gson;
import com.yunbao.common.CommonAppContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -164,7 +163,14 @@ public class SpUtil {
editor.putBoolean(key, value);
editor.apply();
}
public static void setLongValue(String key, long value) {
SharedPreferences.Editor editor=mSharedPreferences.edit();
editor.putLong(key, value);
editor.apply();
}
public static long getLongValue(String key) {
return mSharedPreferences.getLong(key,0);
}
/**
* 获取一个布尔值
*/

View File

@ -119,6 +119,11 @@ public class VersionUtil {
public void updateFailure(int code, String error) {
}
@Override
public void onProgress(int progress) {
}
});
}
});
@ -147,6 +152,11 @@ public class VersionUtil {
public void updateFailure(int code, String error) {
}
@Override
public void onProgress(int progress) {
}
});
}
})

View File

@ -1,41 +1,31 @@
package com.yunbao.common.views;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.IBinder;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;
import com.alibaba.android.arouter.utils.TextUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.yunbao.common.CommonAppConfig;
import com.yunbao.common.R;
import com.yunbao.common.manager.APKManager;
import com.yunbao.common.server.DownloadService;
import com.yunbao.common.utils.APKDownloadUtil;
import com.yunbao.common.utils.StringUtil;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.views.weight.ViewClicksAntiShake;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* apk更新弹窗
*/
@ -45,8 +35,10 @@ public class APKUpdateCustomPopup extends CenterPopupView {
private ProgressBar progressBar;
private Activity mContext;
private boolean mInside;
private boolean isForceInstall;
private DialogInterface.OnDismissListener onDismissListener;
public APKUpdateCustomPopup(@NonNull Activity context, boolean inside) {
super(context);
mContext = context;
@ -64,6 +56,11 @@ public class APKUpdateCustomPopup extends CenterPopupView {
return this;
}
public APKUpdateCustomPopup setForceInstall(boolean forceInstall) {
isForceInstall = forceInstall;
return this;
}
// 执行初始化操作比如findView设置点击或者任何你弹窗内的业务逻辑
@Override
protected void onCreate() {
@ -140,12 +137,13 @@ public class APKUpdateCustomPopup extends CenterPopupView {
} else {
versionImmediateUse.setVisibility(GONE);
updateLine.setVisibility(VISIBLE);
downloadAPK(mContext, APKManager.get().getAPKUrl(), new APKDownloadUtil.OnUpdateListener() {
/* downloadAPK(mContext, APKManager.get().getAPKUrl(), new APKDownloadUtil.OnUpdateListener() {
@Override
public void updateFailure(int code, String error) {
ToastUtil.show(error);
}
});
});*/
startServer(mContext, APKManager.get().getAPKUrl());
}
}
});
@ -199,96 +197,66 @@ public class APKUpdateCustomPopup extends CenterPopupView {
}
}
DownloadService.ServiceBinder binder = null;
ServiceConnection connection = null;
public void downloadAPK(Activity context, String url, APKDownloadUtil.OnUpdateListener listener) {
Request request = new Request.Builder().url(url).addHeader("Accept-Encoding", "identity").build();
File downloadFile = new File(context.getCacheDir(), "update_app.apk");
try {
if (!downloadFile.exists() && !downloadFile.createNewFile()) {
private void connection(String url) {
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (DownloadService.ServiceBinder) service;
if (!StringUtil.isEmpty(url)) {
if ("install".equals(url)) {
install();
} else {
startServer(mContext, url);
}
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
mContext.bindService(new Intent(mContext, DownloadService.class), connection, Context.BIND_AUTO_CREATE);
}
private void install() {
if (binder == null) {
connection("install");
return;
}
} catch (IOException e) {
e.printStackTrace();
binder.install();
}
new OkHttpClient().newCall(request).enqueue(new Callback() {
private Handler handler = new Handler();
public void startServer(Context mContext, String url) {
if (binder == null) {
connection(url);
return;
}
binder.startDownload(url, isForceInstall, new APKDownloadUtil.OnUpdateListener() {
@Override
public void onFailure(Call call, IOException e) {
// 下载失败
handler.post(() -> {
listener.updateFailure(-1, e.getMessage());
dismiss();
});
public void updateFailure(int code, String error) {
handler.post(() -> ToastUtil.show(error));
}
@Override
public void onResponse(Call call, Response response) {
Looper.prepare();
byte[] buf = new byte[2048];
int len;
try (InputStream inputStream = response.body().byteStream(); FileOutputStream outputStream = new FileOutputStream(downloadFile)) {
long total = response.body().contentLength();
long sum = 0;
while ((len = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, len);
sum += len;
int progress = (int) (sum * 1.0f / total * 100);
// 下载中
handler.post(new Runnable() {
@Override
public void run() {
public void onProgress(int progress) {
progressBar.setProgress(progress);
}
});
}
outputStream.flush();
//启动安装app
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
if (progress == 100) {
handler.post(() -> dismiss());
} catch (Exception e) {
e.printStackTrace();
new Handler().post(() -> {
listener.updateFailure(-1, e.getMessage());
dismiss();
});
}
}
});
}
/**
* 安装apk
*
* @param context
* @param file
*/
public void installApk(Context context, File file, String authority) {
Intent intent = getInstallIntent(context, file, authority);
context.startActivity(intent);
}
/**
* 获取安装Intent
*
* @param context
* @param file
* @param authority
* @return
*/
public Intent getInstallIntent(Context context, File file, String authority) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
Uri uriData;
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
uriData = FileProvider.getUriForFile(context, authority, file);
} else {
uriData = Uri.fromFile(file);
}
intent.setDataAndType(uriData, type);
return intent;
@Override
public void onDestroy() {
super.onDestroy();
if (connection != null) {
mContext.unbindService(connection);
}
}
}

View File

@ -176,6 +176,7 @@ public class LiveAnchorEditCallMeAdapter extends RecyclerView.Adapter<LiveAnchor
public void onDismiss() {
}
})
.setHotGiftListUrl(true)
.setShowNumber(false)
.setTitle(mContext.getString(R.string.live_anchor_edit_call_me_select_gift))
.setHideGiftType(true)

View File

@ -62,7 +62,12 @@ public class GiftPopDialog extends AbsDialogPopupWindow implements ActionListene
private TextView mTitle;
private boolean isOldGiftList = false;
private boolean hideGiftType = false;
private boolean isHotGiftListUrl = false;
public GiftPopDialog setHotGiftListUrl(boolean hotGiftListUrl) {
isHotGiftListUrl = hotGiftListUrl;
return this;
}
public GiftPopDialog(@NonNull Context context) {
super(context);
@ -183,6 +188,25 @@ public class GiftPopDialog extends AbsDialogPopupWindow implements ActionListene
}
}
@Override
public void onFinish() {
if (mLoading != null) {
mLoading.setVisibility(View.INVISIBLE);
}
}
});
} else if (isHotGiftListUrl) {
LiveHttpUtil.getHotGiftList(new HttpCallback() {
@Override
public void onSuccess(int code, String msg, String[] info) {
if (code == 0 && info.length > 0) {
JSONObject obj = JSON.parseObject(info[0]);
JSONArray list = obj.getJSONArray("listarray");
setDate(list);
}
}
@Override
public void onFinish() {
if (mLoading != null) {

View File

@ -2,6 +2,7 @@ package com.yunbao.main.activity;
import static com.yunbao.common.CommonAppContext.isReady;
import static com.yunbao.common.manager.imrongcloud.RongcloudIMManager.connectIM;
import static com.yunbao.common.server.DownloadService.DOWNLOAD_TAG;
import android.Manifest;
import android.animation.Animator;
@ -12,13 +13,16 @@ import android.app.Dialog;
import android.app.NotificationChannel;
import android.app.PendingIntent;
import android.content.ClipData;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
@ -53,6 +57,7 @@ import com.tencent.imsdk.v2.V2TIMSDKListener;
import com.tencent.imsdk.v2.V2TIMUserFullInfo;
import com.yunbao.common.bean.QiniuLog;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.server.DownloadService;
import com.yunbao.common.utils.AppManager;
import com.yunbao.common.utils.LogUtil;
import com.yunbao.common.utils.MobclickAgent;
@ -231,6 +236,7 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
SWAuManager.get().initRtcEngine(this);
ActivityCompat.postponeEnterTransition(this);
ConversationIMListManager.get(this);
connectionInstallApkService();
//在请求一下这个接口给我后台版本号
CommonHttpUtil.getConfig(mContext, new CommonCallback<ConfigBean>() {
@Override
@ -900,6 +906,7 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
.dismissOnTouchOutside(false) // 点击外部是否关闭弹窗默认为true
.asCustom(
new APKUpdateCustomPopup(mContext, false)
.setForceInstall(APKManager.get().getAPKGoogleIsUp() == 1)
.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
@ -907,6 +914,8 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
}
})
).show();
}else{
SpUtil.setLongValue(DOWNLOAD_TAG, 0);
}
} else {
requestBonus();
@ -920,6 +929,24 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
OpenAdManager.getInstance().show(OpenAdManager.TYPE_HOME, false);
}
DownloadService.ServiceBinder binder = null;
ServiceConnection connection=null;
private void connectionInstallApkService() {
connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (DownloadService.ServiceBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
mContext.bindService(new Intent(mContext, DownloadService.class), connection, Context.BIND_AUTO_CREATE);
}
/**
* 填写邀请码
*/
@ -1008,6 +1035,11 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
@Override
protected void onResume() {
super.onResume();
if (binder != null) {
if (binder.isDownloadOK()) {
binder.install();
}
}
if (mFristLoad) {
mFristLoad = false;
// getLocation();
@ -1141,6 +1173,9 @@ public class MainActivity extends AbsActivity implements MainAppBarLayoutListene
LiveStorge.getInstance().clear();
VideoStorge.getInstance().clear();
floatBanner.release();
if(connection!=null){
mContext.unbindService(connection);
}
super.onDestroy();
}