优化下载直播插件流程
This commit is contained in:
246
common/src/main/java/com/yunbao/common/utils/PluginManager.java
Normal file
246
common/src/main/java/com/yunbao/common/utils/PluginManager.java
Normal file
@@ -0,0 +1,246 @@
|
||||
package com.yunbao.common.utils;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import com.yunbao.common.CommonAppContext;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
/**
|
||||
* 插件加载器
|
||||
*/
|
||||
public class PluginManager {
|
||||
private static PluginManager manager;
|
||||
private static final String TAG = "插件管理器";
|
||||
private String anchorPluginDownloadUrl = "http://nas.cnmglz.com:9999/apk/pluginsForAnchor-release.apk";
|
||||
|
||||
private PluginManager() {
|
||||
}
|
||||
|
||||
public static PluginManager getInstance() {
|
||||
if (manager == null) {
|
||||
manager = new PluginManager();
|
||||
}
|
||||
return manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置主播插件下载地址
|
||||
*/
|
||||
public void setAnchorPluginDownloadUrl(String anchorPluginDownloadUrl) {
|
||||
this.anchorPluginDownloadUrl = anchorPluginDownloadUrl;
|
||||
}
|
||||
|
||||
public boolean isDownloadApk() {
|
||||
return new File(CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin_download" + File.separator + "anchorPlugin.apk").exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载主播插件
|
||||
*/
|
||||
public void loadAnchorPlugin(DialogInterface.OnDismissListener dismissListener) {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
if (Arrays.asList(CommonAppContext.sInstance.getAssets().list("")).contains("anchorPlugin.apk")) {
|
||||
copyAssetsFile(CommonAppContext.sInstance, "anchorPlugin.apk", "anchorPlugin.apk", CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin_download");
|
||||
Log.d(TAG, "解压assets下的文件");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
File sdk = new File(CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin_download" + File.separator + "anchorPlugin.apk");
|
||||
if (!sdk.exists()) {
|
||||
if (StringUtil.isEmpty(anchorPluginDownloadUrl)) {
|
||||
Log.e(TAG, "主播下载地址为空");
|
||||
return;
|
||||
}
|
||||
downloadAnchorSdk(dismissListener);
|
||||
return;
|
||||
}
|
||||
String outDir = CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin";
|
||||
loadFaceSo(sdk, outDir);
|
||||
loadFaceBundle(sdk, outDir);
|
||||
new Handler(Looper.getMainLooper()).post(() -> {
|
||||
dismissListener.onDismiss(null);
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载美颜so文件
|
||||
*
|
||||
* @param plugin 插件apk文件
|
||||
* @param outDir 解压路径
|
||||
*/
|
||||
private void loadFaceSo(File plugin, String outDir) {
|
||||
if(!new File(CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin"+File.separator+"lib").exists()) {
|
||||
unzip(plugin.getAbsolutePath(), outDir, ".so");
|
||||
}
|
||||
String[] abis = Build.SUPPORTED_ABIS;
|
||||
String abi = Arrays.asList(abis).contains("arm64-v8a") ? "arm64-v8a" : "armeabi-v7a";
|
||||
File plugins = new File(outDir + File.separator + "lib" + File.separator + abi);
|
||||
loadSo(plugins, "libfuai");
|
||||
loadSo(plugins, "libCNamaSDK");
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置美颜Bundle文件
|
||||
*
|
||||
* @param plugin 插件apk文件
|
||||
* @param outDir 解压路径
|
||||
*/
|
||||
private void loadFaceBundle(File plugin, String outDir) {
|
||||
if(!new File(CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin"+File.separator+"assets").exists()) {
|
||||
unzip(plugin.getAbsolutePath(), outDir, ".bundle");
|
||||
}
|
||||
try {
|
||||
Object BUNDLE_AI_FACE = Class.forName("com.yunbao.faceunity.utils.FURenderer").getField("BUNDLE_AI_FACE").get(null);
|
||||
Object BUNDLE_FACE_BEAUTIFICATION = Class.forName("com.yunbao.faceunity.utils.FaceUnityConfig").getField("BUNDLE_FACE_BEAUTIFICATION").get(null);
|
||||
Class.forName("com.yunbao.faceunity.utils.FURenderer").getField("BUNDLE_AI_FACE").set(null, outDir + File.separator + "assets" + File.separator + BUNDLE_AI_FACE);
|
||||
Class.forName("com.yunbao.faceunity.utils.FaceUnityConfig").getField("BUNDLE_FACE_BEAUTIFICATION").set(null, outDir + File.separator + "assets" + File.separator + BUNDLE_FACE_BEAUTIFICATION);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载指定so
|
||||
*
|
||||
* @param dir so文件路径
|
||||
* @param file so文件名字
|
||||
*/
|
||||
private void loadSo(File dir, String file) {
|
||||
file += ".so";
|
||||
if (new File(dir.getAbsolutePath() + File.separator + file).exists()) {
|
||||
System.load(dir + File.separator + file);
|
||||
Log.d(TAG, "加载成功 " + dir + File.separator + file);
|
||||
} else {
|
||||
Log.e(TAG, "不存在 " + dir + File.separator + file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载主播插件包
|
||||
*/
|
||||
private void downloadAnchorSdk(DialogInterface.OnDismissListener dismissListener) {
|
||||
String downloadDir = new File(CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin_download").getAbsolutePath();
|
||||
new DownloadUtil().download("plugin", downloadDir, "anchorPlugin.apk", anchorPluginDownloadUrl, new DownloadUtil.Callback() {
|
||||
@Override
|
||||
public void onSuccess(File file) {
|
||||
Log.d(TAG, "下载成功 " + file);
|
||||
loadAnchorPlugin(dismissListener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProgress(int progress) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 解压zip文件
|
||||
*
|
||||
* @param zip zip文件
|
||||
* @param outDir 解压路径
|
||||
* @param suffix 过滤
|
||||
*/
|
||||
private boolean unzip(String zip, String outDir, String suffix) {
|
||||
FileOutputStream out;
|
||||
byte buffer[] = new byte[1024];
|
||||
try {
|
||||
ZipInputStream zis = new ZipInputStream(new FileInputStream(zip));
|
||||
ZipEntry entry = zis.getNextEntry();
|
||||
while (entry != null) {
|
||||
String name = entry.getName();
|
||||
if (entry.isDirectory()) {
|
||||
File newDir = new File(outDir + entry.getName());
|
||||
newDir.mkdir();
|
||||
} else if (name.endsWith(suffix)) {
|
||||
File outputFile = new File(outDir + File.separator + name);
|
||||
String outputPath = outputFile.getCanonicalPath();
|
||||
name = outputPath
|
||||
.substring(outputPath.lastIndexOf("/") + 1);
|
||||
outputPath = outputPath.substring(0, outputPath
|
||||
.lastIndexOf("/"));
|
||||
File outputDir = new File(outputPath);
|
||||
outputDir.mkdirs();
|
||||
outputFile = new File(outputPath, name);
|
||||
outputFile.createNewFile();
|
||||
out = new FileOutputStream(outputFile);
|
||||
|
||||
int tmp = 0;
|
||||
while ((tmp = zis.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, tmp);
|
||||
}
|
||||
/* do {
|
||||
tmp = zis.read(buffer);
|
||||
if (tmp <= 0) {
|
||||
break;
|
||||
} else {
|
||||
out.write(buffer, 0, tmp);
|
||||
}
|
||||
} while (true);*/
|
||||
out.close();
|
||||
}
|
||||
entry = zis.getNextEntry();
|
||||
}
|
||||
zis.close();
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static String copyAssetsFile(Context context, String assetsPath, String fileName, String saveFileDir) {
|
||||
File fileDir = new File(saveFileDir);
|
||||
if (!fileDir.exists()) {
|
||||
fileDir.mkdirs();
|
||||
}
|
||||
File file = new File(fileDir, fileName);
|
||||
if (file.exists()) {
|
||||
return file.getAbsolutePath();
|
||||
}
|
||||
try {
|
||||
InputStream inputStream = context.getAssets().open(assetsPath);
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
BufferedInputStream bis = new BufferedInputStream(inputStream);
|
||||
BufferedOutputStream bos = new BufferedOutputStream(fos);
|
||||
byte[] byteArray = new byte[1024];
|
||||
int bytes = bis.read(byteArray);
|
||||
while (bytes > 0) {
|
||||
bos.write(byteArray, 0, bytes);
|
||||
bos.flush();
|
||||
bytes = bis.read(byteArray);
|
||||
}
|
||||
bos.close();
|
||||
fos.close();
|
||||
return file.getAbsolutePath();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user