Skip to content

Commit

Permalink
fix(dynamic.apk): ChangeApkContextWrapper的Theme应该克隆BaseContext
Browse files Browse the repository at this point in the history
模仿ContextThemeWrapper的做法。
否则Android内置的View,如ProgressBar在从Theme.obtainStyledAttributes
时会获取不到自己的资源。

fix #1271
  • Loading branch information
shifujun committed Dec 19, 2023
1 parent d9deb2f commit 73cb5d8
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package com.tencent.shadow.dynamic.apk;

import static android.content.pm.PackageManager.GET_META_DATA;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.pm.PackageInfo;
Expand All @@ -26,8 +28,6 @@
import android.content.res.Resources;
import android.view.LayoutInflater;

import static android.content.pm.PackageManager.GET_META_DATA;

/**
* 修改Context的apk路径的Wrapper。可将原Context的Resource和ClassLoader重新修改为新的Apk。
*/
Expand All @@ -39,6 +39,8 @@ public class ChangeApkContextWrapper extends ContextWrapper {

final private ClassLoader mClassloader;

private Resources.Theme mTheme;

public ChangeApkContextWrapper(Context base, String apkPath, ClassLoader mClassloader) {
super(base);
this.mClassloader = mClassloader;
Expand Down Expand Up @@ -70,7 +72,16 @@ public Resources getResources() {

@Override
public Resources.Theme getTheme() {
return mResources.newTheme();
// 模仿android.view.ContextThemeWrapper#initializeTheme
if (mTheme == null) {
Resources.Theme newTheme = mResources.newTheme();
final Resources.Theme theme = getBaseContext().getTheme();
if (theme != null) {
newTheme.setTo(theme);
}
mTheme = newTheme;
}
return mTheme;
}

@Override
Expand Down

0 comments on commit 73cb5d8

Please sign in to comment.