「Update」

1、修复无法读取插件APK中的raw资源问题
This commit is contained in:
donghongyu
2024-10-15 18:22:16 +08:00
parent 4bc66d0387
commit 54bd1227be
3 changed files with 70 additions and 15 deletions

View File

@@ -131,7 +131,7 @@ public class SkinAttribute {
* @param typeface 字体
*/
public void applySkin(Typeface typeface) {
Log.d(TAG, "applySkin() called with: typeface = [" + typeface + "] view = [" + view + "] Parent = " + view.getParent());
//Log.d(TAG, "applySkin() called with: typeface = [" + typeface + "] view = [" + view + "] Parent = " + view.getParent());
//applySkinTypeface(typeface);
applySkinViewSupport();
try {
@@ -226,7 +226,7 @@ public class SkinAttribute {
private void applySkinTypeface(final Typeface typeface) {
if (view instanceof TextView) {
Log.d(TAG, "applySkinTypeface() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//Log.d(TAG, "applySkinTypeface() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override
@@ -239,7 +239,7 @@ public class SkinAttribute {
private void applyText(final String textStr) {
if (view instanceof TextView) {
Log.d(TAG, "applyText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//Log.d(TAG, "applyText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override
@@ -252,7 +252,7 @@ public class SkinAttribute {
private void applyHintText(final String textStr) {
if (view instanceof EditText) {
Log.d(TAG, "applyHintText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//Log.d(TAG, "applyHintText() called with: view = [" + ((TextView) view).getText() + "] Parent = " + view.getParent());
//post 防止某些控件的属性设置是在构造函数调用完成之后进行的。
mHandler.post(new Runnable() {
@Override

View File

@@ -20,6 +20,8 @@ import java.io.File;
import java.lang.reflect.Method;
import java.util.Observable;
import dalvik.system.DexClassLoader;
/**
* donghongyu
*/
@@ -61,34 +63,39 @@ public class SkinManager extends Observable {
/**
* 加载皮肤包 并 更新
*
* @param path 皮肤包路径
* @param skinPath 皮肤包路径
*/
public void loadSkin(String path) {
Log.d(TAG, "加载皮肤路径:" + path);
public void loadSkin(String skinPath) {
Log.d(TAG, "加载皮肤路径:" + skinPath);
//还原默认皮肤包
if (TextUtils.isEmpty(path)) {
if (TextUtils.isEmpty(skinPath)) {
SkinPreference.getInstance().setSkin("");
SkinResources.getInstance().reset();
}
// 加载指定目录下的皮肤
else {
try {
// 通过反射获取AssetManager
AssetManager assetManager = AssetManager.class.newInstance();
// 添加资源进入资源管理器
// 获取插件APK的AssetManager
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.setAccessible(true);
addAssetPath.invoke(assetManager, path);
int result = (int) addAssetPath.invoke(assetManager, skinPath);
if (result == 0) {
Log.e(TAG, "加载皮肤APK失败" + skinPath);
}
// 获取当前Resources
Resources resources = application.getResources();
// 横竖、语言
// 获取皮肤插件的 Resources 横竖、语言
Resources skinResource = new Resources(assetManager, resources.getDisplayMetrics(), resources.getConfiguration());
//获取外部Apk(皮肤包) 包名
// 获取外部Apk(皮肤包) 包名
PackageManager mPm = application.getPackageManager();
PackageInfo info = mPm.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
PackageInfo info = mPm.getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES);
String packageName = info.packageName;
SkinResources.getInstance().applySkin(skinResource, packageName);
//保存当前使用的皮肤包
SkinPreference.getInstance().setSkin(path);
SkinPreference.getInstance().setSkin(skinPath);
} catch (Exception e) {
e.printStackTrace();
}
@@ -145,6 +152,7 @@ public class SkinManager extends Observable {
//皮肤包的md5校验 防止下载文件损坏(但是会减慢速度。从数据库查询已下载皮肤表数据库中保留md5字段)
if (TextUtils.equals(SkinUtils.getSkinMD5(skinFile), skin.md5)) {
Log.d("SkinActivity", "校验成功,修改文件名。");
} else {
Log.e("SkinActivity", "皮肤文件校验出错,本地文件MD5 与云端文件MD5 不一致。");
Toast.makeText(mApplication, "皮肤文件校验出错,本地文件MD5 与云端文件MD5 不一致。", Toast.LENGTH_SHORT).show();

View File

@@ -8,6 +8,8 @@ import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
@@ -72,6 +74,7 @@ public class SkinResources {
/**
* 获取皮肤中的Raw文件InputStream
*
* @param resId
* @return
*/
@@ -83,7 +86,51 @@ public class SkinResources {
if (skinId == 0) {
return mAppResources.openRawResource(resId);
}
return mSkinResources.openRawResource(resId);
return mSkinResources.openRawResource(skinId);
}
/**
* 获取Raw的byte[]
* @param resId
* @return
*/
public byte[] getRawResourceBytes(int resId) {
byte[] totBuffer = null;
DataInputStream dis = null;
try {
dis = new DataInputStream(getRawInputStream(resId));
int curTotSize = 64 * 1024;
totBuffer = new byte[curTotSize];
byte[] buffer = new byte[1024];
int size = 0;
int totSize = 0;
try {
while ((size = dis.read(buffer)) >= 0) {
if (totSize + size > curTotSize) {
curTotSize = (totSize + size) * 3 / 2;
byte[] newTotBuffer = new byte[curTotSize];
System.arraycopy(totBuffer, 0, newTotBuffer, 0, totSize);
totBuffer = newTotBuffer;
}
System.arraycopy(buffer, 0, totBuffer, totSize, size);
totSize += size;
}
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return totBuffer;
}
public int getColor(int resId) {