Commit 1698d4bc authored by gaodapeng's avatar gaodapeng

同步bugfix到androidx版本

parent f124de4e
...@@ -2,10 +2,12 @@ package com.study.libs; ...@@ -2,10 +2,12 @@ package com.study.libs;
import android.app.Activity; import android.app.Activity;
import android.app.Application; import android.app.Application;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.view.LayoutInflaterCompat; import androidx.core.view.LayoutInflaterCompat;
import com.study.libs.utils.SkinThemeUtils; import com.study.libs.utils.SkinThemeUtils;
...@@ -23,6 +25,7 @@ public class ActivityLifeCycleCallback implements Application.ActivityLifecycleC ...@@ -23,6 +25,7 @@ public class ActivityLifeCycleCallback implements Application.ActivityLifecycleC
inflaterFactory = new HashMap<>(); inflaterFactory = new HashMap<>();
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override @Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) { public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
//先更改status bar //先更改status bar
...@@ -71,6 +74,9 @@ public class ActivityLifeCycleCallback implements Application.ActivityLifecycleC ...@@ -71,6 +74,9 @@ public class ActivityLifeCycleCallback implements Application.ActivityLifecycleC
@Override @Override
public void onActivityDestroyed(Activity activity) { public void onActivityDestroyed(Activity activity) {
SkinLayoutInflaterFactory factory = inflaterFactory.remove(activity); SkinLayoutInflaterFactory factory = inflaterFactory.remove(activity);
if (factory != null) {
SkinManager.getInstance().deleteObserver(factory); SkinManager.getInstance().deleteObserver(factory);
factory.release();
}
} }
} }
...@@ -51,17 +51,19 @@ public class SkinAttribute { ...@@ -51,17 +51,19 @@ public class SkinAttribute {
if (attributeValue.startsWith("#")) {//写死的颜色,不用管 if (attributeValue.startsWith("#")) {//写死的颜色,不用管
continue; continue;
} }
int resId; int resId = 0;
if (attributeValue.startsWith("?")) {//以?开头的应该是系统theme,需要做一次转换获取属性 if (attributeValue.startsWith("?")) {//以?开头的应该是系统theme,需要做一次转换获取属性
int attrId = Integer.parseInt(attributeValue.substring(1));//todo 这里的为何要转换成Integer int attrId = Integer.parseInt(attributeValue.substring(1));//todo 这里的为何要转换成Integer
resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0]; resId = SkinThemeUtils.getResId(view.getContext(), new int[]{attrId})[0];
} else {//正常是以@开头,后面跟地址 } else {//正常是以@开头,后面跟地址
resId = Integer.parseInt(attributeValue.substring(1)); resId = Integer.parseInt(attributeValue.substring(1));
} }
if (resId != 0) {
SkinPair skinPair = new SkinPair(attributeName, resId); SkinPair skinPair = new SkinPair(attributeName, resId);
skinPairs.add(skinPair); skinPairs.add(skinPair);
} }
} }
}
if (!skinPairs.isEmpty() || view instanceof SkinViewSupport) { if (!skinPairs.isEmpty() || view instanceof SkinViewSupport) {
SkinView skinView = new SkinView(view, skinPairs); SkinView skinView = new SkinView(view, skinPairs);
//view替换属性为skin包中的属性,即换肤 //view替换属性为skin包中的属性,即换肤
...@@ -80,10 +82,10 @@ public class SkinAttribute { ...@@ -80,10 +82,10 @@ public class SkinAttribute {
} }
/** /**
* todo 这里需要对activity已经关闭的view进行统一remove,避免内存泄露 * 移除所有view
*/ */
public void removeViews() { public void clearAll() {
skinViewList.clear();
} }
static class SkinView { static class SkinView {
...@@ -103,6 +105,9 @@ public class SkinAttribute { ...@@ -103,6 +105,9 @@ public class SkinAttribute {
applySkinSupport(); applySkinSupport();
//遍历skinPair的列表,查看view的属性,对可操作的属性进行替换 //遍历skinPair的列表,查看view的属性,对可操作的属性进行替换
for (SkinPair skinPair : skinPairList) { for (SkinPair skinPair : skinPairList) {
if (skinPair.resId == 0) {//如果属性无效,就不
continue;
}
Drawable left = null, top = null, right = null, bottom = null; Drawable left = null, top = null, right = null, bottom = null;
switch (skinPair.attributeName) { switch (skinPair.attributeName) {
case "background": case "background":
......
...@@ -2,20 +2,18 @@ package com.study.libs; ...@@ -2,20 +2,18 @@ package com.study.libs;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Trace; import android.os.Build;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewStub;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinThemeUtils; import com.study.libs.utils.SkinThemeUtils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; import java.util.Observer;
...@@ -38,7 +36,6 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -38,7 +36,6 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
"android.view." "android.view."
}; };
private static final ClassLoader BOOT_CLASS_LOADER = LayoutInflater.class.getClassLoader();
//属性管理器 //属性管理器
private SkinAttribute skinAttribute; private SkinAttribute skinAttribute;
...@@ -49,6 +46,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -49,6 +46,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
skinAttribute = new SkinAttribute(); skinAttribute = new SkinAttribute();
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable @Nullable
@Override @Override
public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) { public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
...@@ -68,6 +66,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -68,6 +66,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
/** /**
* 创建系统自带的view * 创建系统自带的view
*/ */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private View createSDKView(String name, Context context, AttributeSet attributeSet) { private View createSDKView(String name, Context context, AttributeSet attributeSet) {
//如果xml中包含点,则可能是自定义的view,这里不对其进行处理 //如果xml中包含点,则可能是自定义的view,这里不对其进行处理
if (name.contains(".")) {//如果不包含点 if (name.contains(".")) {//如果不包含点
...@@ -86,13 +85,15 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -86,13 +85,15 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
/** /**
* 创建view * 创建view
*/ */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private View createView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) { private View createView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
Constructor<? extends View> constructor = findConstructor(context, name); Constructor<? extends View> constructor = findConstructor(context, name);
try { try {
//创建实例 //创建实例
return constructor.newInstance(constructor, attrs); return constructor.newInstance(context, attrs);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); //不必输出
// e.printStackTrace();
} }
return null; return null;
} }
...@@ -100,6 +101,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -100,6 +101,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
/** /**
* 获取类的构造函数,并存起来后面继续用 * 获取类的构造函数,并存起来后面继续用
*/ */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private Constructor<? extends View> findConstructor(Context context, String name) { private Constructor<? extends View> findConstructor(Context context, String name) {
Constructor<? extends View> constructor = sConstructorMap.get(name); Constructor<? extends View> constructor = sConstructorMap.get(name);
if (constructor == null) { if (constructor == null) {
...@@ -110,7 +112,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -110,7 +112,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
constructor.setAccessible(true); constructor.setAccessible(true);
sConstructorMap.put(name, constructor); sConstructorMap.put(name, constructor);
} catch (ClassNotFoundException | NoSuchMethodException e) { } catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace(); // e.printStackTrace();
} }
} }
return constructor; return constructor;
...@@ -120,6 +122,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -120,6 +122,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
/** /**
* 这个基本不用 * 这个基本不用
*/ */
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) { public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
...@@ -133,7 +136,21 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -133,7 +136,21 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
*/ */
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
if (activity != null) {
SkinThemeUtils.updateStatusBarColor(activity); SkinThemeUtils.updateStatusBarColor(activity);
}
if (skinAttribute != null) {
skinAttribute.applySkin(); skinAttribute.applySkin();
} }
}
/**
* 清理
*/
public void release() {
activity = null;
if (skinAttribute != null) {
skinAttribute.clearAll();
}
}
} }
...@@ -8,6 +8,8 @@ import android.content.res.Resources; ...@@ -8,6 +8,8 @@ import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import com.study.libs.utils.SkinResources; import com.study.libs.utils.SkinResources;
import java.io.File; import java.io.File;
...@@ -24,6 +26,7 @@ public class SkinManager extends Observable { ...@@ -24,6 +26,7 @@ public class SkinManager extends Observable {
private Application application; private Application application;
private static volatile SkinManager skinManager; private static volatile SkinManager skinManager;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private SkinManager(Application application) { private SkinManager(Application application) {
this.application = application; this.application = application;
SkinPreference.init(application.getApplicationContext()); SkinPreference.init(application.getApplicationContext());
...@@ -32,6 +35,7 @@ public class SkinManager extends Observable { ...@@ -32,6 +35,7 @@ public class SkinManager extends Observable {
loadSkin(SkinPreference.getInstance().getSkin()); loadSkin(SkinPreference.getInstance().getSkin());
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static SkinManager init(Application application) { public static SkinManager init(Application application) {
if (null == skinManager) { if (null == skinManager) {
synchronized (SkinManager.class) { synchronized (SkinManager.class) {
...@@ -53,6 +57,7 @@ public class SkinManager extends Observable { ...@@ -53,6 +57,7 @@ public class SkinManager extends Observable {
} }
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void loadSkin(String skinPath) { public void loadSkin(String skinPath) {
if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管 if (TextUtils.isEmpty(skinPath) && !new File(skinPath).exists()) {//如果为空或者不存在该文件,则不用管
SkinPreference.getInstance().reset(); SkinPreference.getInstance().reset();
...@@ -77,4 +82,14 @@ public class SkinManager extends Observable { ...@@ -77,4 +82,14 @@ public class SkinManager extends Observable {
setChanged(); setChanged();
notifyObservers(); notifyObservers();
} }
/**
* 重置一切
*/
public void reset() {
SkinPreference.getInstance().reset();
SkinResources.getInstance().reset();
setChanged();
notifyObservers();
}
} }
...@@ -122,5 +122,4 @@ public class SkinResources { ...@@ -122,5 +122,4 @@ public class SkinResources {
} }
} }
} }
...@@ -38,6 +38,9 @@ public class SkinThemeUtils { ...@@ -38,6 +38,9 @@ public class SkinThemeUtils {
} }
public static void updateStatusBarColor(Activity activity) { public static void updateStatusBarColor(Activity activity) {
if (activity == null) {
return;
}
//5.0以上才能修改 //5.0以上才能修改
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
return; return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment