Commit ccfe73a2 authored by gaodapeng's avatar gaodapeng

fix bugs

parent 05e635c3
...@@ -2,10 +2,8 @@ package com.study.libs; ...@@ -2,10 +2,8 @@ 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.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.view.LayoutInflaterCompat; import android.support.v4.view.LayoutInflaterCompat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
...@@ -72,6 +70,9 @@ public class ActivityLifeCycleCallback implements Application.ActivityLifecycleC ...@@ -72,6 +70,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);
SkinManager.getInstance().deleteObserver(factory); if (factory != null) {
SkinManager.getInstance().deleteObserver(factory);
factory.release();
}
} }
} }
...@@ -50,15 +50,17 @@ public class SkinAttribute { ...@@ -50,15 +50,17 @@ 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));
} }
SkinPair skinPair = new SkinPair(attributeName, resId); if (resId != 0) {
skinPairs.add(skinPair); SkinPair skinPair = new SkinPair(attributeName, resId);
skinPairs.add(skinPair);
}
} }
} }
if (!skinPairs.isEmpty() || view instanceof SkinViewSupport) { if (!skinPairs.isEmpty() || view instanceof SkinViewSupport) {
...@@ -79,10 +81,10 @@ public class SkinAttribute { ...@@ -79,10 +81,10 @@ public class SkinAttribute {
} }
/** /**
* todo 这里需要对activity已经关闭的view进行统一remove,避免内存泄露 * 移除所有view
*/ */
public void removeViews() { public void clearAll() {
skinViewList.clear();
} }
static class SkinView { static class SkinView {
...@@ -102,6 +104,9 @@ public class SkinAttribute { ...@@ -102,6 +104,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,19 +2,17 @@ package com.study.libs; ...@@ -2,19 +2,17 @@ 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.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
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 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;
...@@ -37,7 +35,6 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -37,7 +35,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;
...@@ -48,6 +45,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -48,6 +45,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) {
...@@ -67,6 +65,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -67,6 +65,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(".")) {//如果不包含点
...@@ -85,13 +84,15 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -85,13 +84,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;
} }
...@@ -99,6 +100,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -99,6 +100,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) {
...@@ -109,7 +111,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -109,7 +111,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;
...@@ -119,6 +121,7 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -119,6 +121,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) {
...@@ -132,7 +135,21 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser ...@@ -132,7 +135,21 @@ public class SkinLayoutInflaterFactory implements LayoutInflater.Factory2, Obser
*/ */
@Override @Override
public void update(Observable o, Object arg) { public void update(Observable o, Object arg) {
SkinThemeUtils.updateStatusBarColor(activity); if (activity != null) {
skinAttribute.applySkin(); SkinThemeUtils.updateStatusBarColor(activity);
}
if (skinAttribute != null) {
skinAttribute.applySkin();
}
}
/**
* 清理
*/
public void release() {
activity = null;
if (skinAttribute != null) {
skinAttribute.clearAll();
}
} }
} }
...@@ -6,6 +6,7 @@ import android.content.pm.PackageManager; ...@@ -6,6 +6,7 @@ import android.content.pm.PackageManager;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils; import android.text.TextUtils;
import com.study.libs.utils.SkinResources; import com.study.libs.utils.SkinResources;
...@@ -24,6 +25,7 @@ public class SkinManager extends Observable { ...@@ -24,6 +25,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 +34,7 @@ public class SkinManager extends Observable { ...@@ -32,6 +34,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 +56,7 @@ public class SkinManager extends Observable { ...@@ -53,6 +56,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 +81,14 @@ public class SkinManager extends Observable { ...@@ -77,4 +81,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