「Update」

1、完成只针对video_开头的raw文件的复制和引用替换。
2、增加皮肤包下载回调。
This commit is contained in:
donghongyu
2024-10-24 12:53:04 +08:00
parent 4552201a8c
commit 33340fa94f
5 changed files with 52 additions and 17 deletions

View File

@@ -30,6 +30,11 @@ public class SkinManager extends Observable {
private static Application mApplication;
private static SkinManager instance;
/**
* 皮肤加载回调监听
*/
private SkinLoadListener mSkinLoadListener;
/**
* Activity生命周期回调
*/
@@ -136,7 +141,7 @@ public class SkinManager extends Observable {
/**
* 下载皮肤包
*/
public void downloadSkin(Context context, Skin skin) {
public void downloadSkin(Context context, Skin skin, SkinLoadListener skinLoadListener) {
File theme = new File(FileUtils.getExternalAppDirectory(context), "theme");
if (theme.exists() && theme.isFile()) {
theme.delete();
@@ -151,13 +156,13 @@ public class SkinManager extends Observable {
return;
}
Log.e(TAG, "皮肤不存在,开始下载……skin=" + skin);
DownloadManager manager = getDownloadManager(skin, theme);
DownloadManager manager = getDownloadManager(skin, theme, skinLoadListener);
// 启动文件下载
manager.startDownload();
}
private static DownloadManager getDownloadManager(Skin skin, File themeDirectory) {
private static DownloadManager getDownloadManager(Skin skin, File themeDirectory, SkinLoadListener skinLoadListener) {
DownloadManager.DownloadListener downloadListener = new DownloadManager.DownloadListener() {
@Override
public void onProgressUpdate(int progress) {
@@ -179,14 +184,19 @@ public class SkinManager extends Observable {
Log.d(TAG, "DownloadManager 校验成功,修改文件名。");
// 加载指定皮肤包
SkinManager.getInstance().loadSkin(skin.getPathLocal());
// 回调给使用方成功
skinLoadListener.onLoadSuccess();
// 校验文件签名没问题后删除其他主题
deleteFilesExcept(themeDirectory, skin.getSkinFileName());
} else {
// 回调给使用方成功
skinLoadListener.onLoadFailed(new Exception("皮肤文件校验出错,本地文件MD5 与云端文件MD5 不一致。"));
Log.e(TAG, "DownloadManager 皮肤文件校验出错,本地文件MD5 与云端文件MD5 不一致。");
Toast.makeText(mApplication, "皮肤文件校验出错,本地文件MD5 与云端文件MD5 不一致。", Toast.LENGTH_SHORT).show();
// TODO 删除本地皮肤文件并重新下载这个操作比较危险需要严格保证配置文件中的md5与实际皮肤包一致否则将是灾难
}
} catch (Exception e) {
skinLoadListener.onLoadFailed(e);
e.printStackTrace();
}
}
@@ -195,6 +205,7 @@ public class SkinManager extends Observable {
public void onDownloadFailed(Exception e) {
// 下载失败后的处理
Log.e(TAG, "DownloadManager onDownloadFailed");
skinLoadListener.onLoadFailed(e);
e.printStackTrace();
}
@@ -202,6 +213,7 @@ public class SkinManager extends Observable {
public void onAlreadyDownloading(String url) {
// 处理重复下载的情况
Log.w(TAG, "DownloadManager onAlreadyDownloading url=" + url);
skinLoadListener.onLoadFailed(new Exception("DownloadManager onAlreadyDownloading url=" + url));
}
};
@@ -240,4 +252,15 @@ public class SkinManager extends Observable {
// 尝试重命名文件
return oldFile.renameTo(newFile);
}
/**
* 皮肤加载回调
*/
public interface SkinLoadListener {
void onLoadSuccess();
void onLoadFailed(Exception e);
}
}

View File

@@ -47,10 +47,13 @@ public class FileUtils {
*
* @param fields 获取raw文件夹的资源ID
*/
public static void copyAppRawToFile(Context context, Field[] fields) {
public static void copyVideoRawToFile(Context context, Field[] fields) {
for (Field field : fields) {// 将资源文件的名称添加到列表中
copyRawToFile(SkinResources.getInstance().getIdentifier(field.getName(), "raw"),
getExternalAppDirectory(context).getAbsolutePath() + "/raw/");
// 过滤名称带有Video的Raw文件到指定目录
if (field.getName().startsWith("video_")) {
copyVideoRawToFile(SkinResources.getInstance().getIdentifier(field.getName(), "raw"),
getExternalAppDirectory(context).getAbsolutePath() + "/raw/");
}
}
}
@@ -61,7 +64,7 @@ public class FileUtils {
* @param targetPath 外部存储的目标路径
* @return 是否复制成功
*/
public static boolean copyRawToFile(int rawResId, String targetPath) {
public static boolean copyVideoRawToFile(int rawResId, String targetPath) {
try {
// 获取 raw 目录下的文件输入流
InputStream inputStream = SkinResources.getInstance().getRawInputStream(rawResId);