Commit 1698d4bc authored by gaodapeng's avatar gaodapeng

同步bugfix到androidx版本

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