|
|
@@ -0,0 +1,130 @@
|
|
|
+package com.sheep.gamegroup.module.skin.util;
|
|
|
+
|
|
|
+import android.app.Activity;
|
|
|
+import android.text.TextUtils;
|
|
|
+
|
|
|
+import com.sheep.gamegroup.absBase.AbsObserver;
|
|
|
+import com.sheep.gamegroup.module.plugin.model.Plugin;
|
|
|
+import com.sheep.gamegroup.module.plugin.util.SheepPluginUtil;
|
|
|
+import com.sheep.gamegroup.util.DataUtil;
|
|
|
+import com.sheep.gamegroup.util.LogUtil;
|
|
|
+import com.sheep.gamegroup.util.PreferenceUtils;
|
|
|
+import com.sheep.gamegroup.util.TestUtil;
|
|
|
+import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
+
|
|
|
+import io.reactivex.android.schedulers.AndroidSchedulers;
|
|
|
+import io.reactivex.schedulers.Schedulers;
|
|
|
+import rx.functions.Action1;
|
|
|
+import skin.support.SkinCompatManager;
|
|
|
+//import skin.support.app.SkinAppCompatViewInflater;
|
|
|
+import skin.support.app.SkinCardViewInflater;
|
|
|
+import skin.support.constraint.app.SkinConstraintViewInflater;
|
|
|
+import skin.support.design.app.SkinMaterialViewInflater;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by realicing on 2019/1/15.
|
|
|
+ * realicing@sina.com
|
|
|
+ *
|
|
|
+ * 换肤工具类
|
|
|
+ */
|
|
|
+public class SkinUtil {
|
|
|
+ //初始化皮肤框架
|
|
|
+ public static void init() {
|
|
|
+ SkinCompatManager.withoutActivity(SheepApp.getInstance())
|
|
|
+ .addStrategy(new CustomSDCardLoader()) // 自定义加载策略,指定SDCard路径
|
|
|
+// .addInflater(new SkinAppCompatViewInflater()) // 基础控件换肤初始化
|
|
|
+ .addInflater(new SkinMaterialViewInflater()) // material design 控件换肤初始化[可选]
|
|
|
+ .addInflater(new SkinConstraintViewInflater()) // ConstraintLayout 控件换肤初始化[可选]
|
|
|
+ .addInflater(new SkinCardViewInflater()) // CardView v7 控件换肤初始化[可选]
|
|
|
+// .setSkinStatusBarColorEnable(false) // 关闭状态栏换肤,默认打开[可选]
|
|
|
+ .setSkinWindowBackgroundEnable(false) // 关闭windowBackground换肤,默认打开[可选]
|
|
|
+ .loadSkin();
|
|
|
+ }
|
|
|
+
|
|
|
+ //恢复默认皮肤
|
|
|
+ public static void restoreDefaultTheme() {
|
|
|
+ // 恢复应用默认皮肤
|
|
|
+ SkinCompatManager.getInstance().restoreDefaultTheme();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Plugin curPlugin;
|
|
|
+ public static final String KEY_CUR_SKIN_MD5 = "cur_skin";//当前正在使用的皮肤的key
|
|
|
+ //切换皮肤
|
|
|
+ public static void changeSkin(Action1<Object> action1) {
|
|
|
+ curPlugin = getSkinByTime();//需要加载的皮肤
|
|
|
+ String curSkinMd5 = PreferenceUtils.getPrefString(SheepApp.getInstance(), KEY_CUR_SKIN_MD5, null);
|
|
|
+ if(curPlugin == null){//默认皮肤
|
|
|
+ if(curSkinMd5 != null){
|
|
|
+ restoreDefaultTheme();
|
|
|
+ }
|
|
|
+ action1.call(null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Activity activity = SheepApp.getInstance().getCurrentActivity();
|
|
|
+ SheepPluginUtil.checkAndRunPlugin(activity, curPlugin)
|
|
|
+ .subscribeOn(Schedulers.io())
|
|
|
+ .observeOn(AndroidSchedulers.mainThread())
|
|
|
+ .subscribe(new AbsObserver<Plugin>() {
|
|
|
+ @Override
|
|
|
+ public void onNext(Plugin plugin) {
|
|
|
+ if(!SheepPluginUtil.getFile(plugin).exists()){//检查文件是否存在
|
|
|
+ action1.call(null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Object md5 = DataUtil.getInstance().getData(SheepPluginUtil.getKey(plugin));
|
|
|
+ String skinName = SheepPluginUtil.getFileName(curPlugin);
|
|
|
+ if(curSkinMd5 != null && md5 instanceof String && TextUtils.equals(md5.toString(), curSkinMd5)){//已经加载皮肤
|
|
|
+ action1.call(null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 指定皮肤插件
|
|
|
+ SkinCompatManager.getInstance().loadSkin(skinName, new SkinCompatManager.SkinLoaderListener() {
|
|
|
+ @Override
|
|
|
+ public void onStart() {
|
|
|
+ LogUtil.println(SkinUtil.class.getSimpleName(), "changeSkin", "loadSkin", skinName, "onStart");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onSuccess() {
|
|
|
+ LogUtil.println(SkinUtil.class.getSimpleName(), "changeSkin", "loadSkin", skinName, "onSuccess");
|
|
|
+ if(md5 instanceof String)
|
|
|
+ PreferenceUtils.setPrefString(SheepApp.getInstance(), KEY_CUR_SKIN_MD5, md5.toString());
|
|
|
+ action1.call(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailed(String errMsg) {
|
|
|
+ LogUtil.println(SkinUtil.class.getSimpleName(), "changeSkin", "loadSkin", skinName, "onFailed", errMsg);
|
|
|
+ action1.call(errMsg);
|
|
|
+ }
|
|
|
+ }, CustomSDCardLoader.SKIN_LOADER_STRATEGY_SDCARD);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据时间生成对应的主题
|
|
|
+ private static Plugin getSkinByTime() {
|
|
|
+ java.util.Calendar curCalendar = java.util.Calendar.getInstance();
|
|
|
+ int year = curCalendar.get(java.util.Calendar.YEAR);
|
|
|
+ int month = curCalendar.get(java.util.Calendar.MONTH);
|
|
|
+ int day = curCalendar.get(java.util.Calendar.DAY_OF_MONTH);
|
|
|
+ if(isNewYear(year, month, day)){//春节
|
|
|
+ return Plugin.skin_new_year;
|
|
|
+ } else if(isChristmas(year, month, day)){//元旦
|
|
|
+ return Plugin.skin_christmas;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isChristmas(int year, int month, int day) {
|
|
|
+ return year == 2019 && month == 1 || TestUtil.isDev();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static boolean isNewYear(int year, int month, int day) {
|
|
|
+ return year == 2019 && month == 2 && day > 3 && day < 20;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getSkinPath(String skinName) {
|
|
|
+ return SheepPluginUtil.getFileByName(skinName, curPlugin).getAbsolutePath();
|
|
|
+ }
|
|
|
+}
|