Merge branch 'refs/heads/master' into v681小游戏

# Conflicts:
#	live/src/main/java/com/yunbao/live/activity/SystemMessageActivity.java
#	main/src/main/java/com/yunbao/main/activity/MainActivity.java
This commit is contained in:
2024-08-16 14:36:44 +08:00
13 changed files with 414 additions and 104 deletions

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()) {
return;
}
} catch (IOException e) {
e.printStackTrace();
}
new OkHttpClient().newCall(request).enqueue(new Callback() {
private Handler handler = new Handler();
private void connection(String url) {
connection = new ServiceConnection() {
@Override
public void onFailure(Call call, IOException e) {
// 下载失败
handler.post(() -> {
listener.updateFailure(-1, e.getMessage());
dismiss();
});
}
@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() {
progressBar.setProgress(progress);
}
});
public void onServiceConnected(ComponentName name, IBinder service) {
binder = (DownloadService.ServiceBinder) service;
if (!StringUtil.isEmpty(url)) {
if ("install".equals(url)) {
install();
} else {
startServer(mContext, url);
}
outputStream.flush();
//启动安装app
installApk(context, downloadFile, context.getPackageName() + ".fileprovider");
}
}
@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;
}
binder.install();
}
public void startServer(Context mContext, String url) {
if (binder == null) {
connection(url);
return;
}
binder.startDownload(url, isForceInstall, new APKDownloadUtil.OnUpdateListener() {
@Override
public void updateFailure(int code, String error) {
handler.post(() -> ToastUtil.show(error));
}
@Override
public void onProgress(int progress) {
progressBar.setProgress(progress);
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);
@Override
public void onDestroy() {
super.onDestroy();
if (connection != null) {
mContext.unbindService(connection);
}
intent.setDataAndType(uriData, type);
return intent;
}
}