2023-06-15 13:41:57 +08:00

199 lines
6.8 KiB
Java

package com.shayu.phonelive.utils;
import static com.yunbao.faceunity.utils.FURenderer.BUNDLE_AI_FACE;
import static com.yunbao.faceunity.utils.FaceUnityConfig.BUNDLE_FACE_BEAUTIFICATION;
import android.os.Build;
import android.util.Log;
import com.yunbao.common.CommonAppContext;
import com.yunbao.common.utils.DownloadUtil;
import com.yunbao.common.utils.StringUtil;
import com.yunbao.faceunity.utils.FileUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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://192.168.137.1:12345/shares/ce47aa99-3e73-4a79-8a2f-e17a2d527953";
private PluginManager() {
}
public static PluginManager getInstance() {
if (manager == null) {
manager = new PluginManager();
}
return manager;
}
/**
* 设置主播插件下载地址
*/
public void setAnchorPluginDownloadUrl(String anchorPluginDownloadUrl) {
this.anchorPluginDownloadUrl = anchorPluginDownloadUrl;
}
/**
* 加载主播插件
*/
public void loadAnchorPlugin() {
new Thread(() -> {
try {
if (Arrays.asList(CommonAppContext.sInstance.getAssets().list("")).contains("anchorPlugin.apk")) {
FileUtils.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();
return;
}
String outDir = CommonAppContext.sInstance.getFilesDir().getAbsolutePath() + File.separator + "plugin";
loadFaceSo(sdk, outDir);
loadFaceBundle(sdk, outDir);
}).start();
}
/**
* 加载美颜so文件
*
* @param plugin 插件apk文件
* @param outDir 解压路径
*/
private void loadFaceSo(File plugin, String outDir) {
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) {
unzip(plugin.getAbsolutePath(), outDir, ".bundle");
BUNDLE_AI_FACE = outDir + File.separator + "assets" + File.separator + BUNDLE_AI_FACE;
BUNDLE_FACE_BEAUTIFICATION = outDir + File.separator + "assets" + File.separator + BUNDLE_FACE_BEAUTIFICATION;
}
/**
* 加载指定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() {
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();
}
@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;
}
}
}