package com.maniu.skin_lib;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;

import java.lang.reflect.Field;

public class BaseActvity extends AppCompatActivity {
    SkinFactory skinFactory;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SkinManager.getInstance().setContext(getApplicationContext());
        skinFactory = new SkinFactory();
        setLayoutInflaterFactory(getLayoutInflater());
        LayoutInflaterCompat.setFactory2(getLayoutInflater(),skinFactory);
    }

    public void apply(){
        skinFactory.apply();
    }

    @Override
    protected void onResume() {
        super.onResume();
        skinFactory.apply();
    }


    /**
     * 改变mFactorySet的值
     * @param original
     */
    public void setLayoutInflaterFactory(LayoutInflater original) {
        LayoutInflater layoutInflater = original;
        try {
            Field mFactorySet = LayoutInflater.class.getDeclaredField("mFactorySet");
            mFactorySet.setAccessible(true);
            mFactorySet.set(layoutInflater, false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
