package xyz.geminiwen.skinsprite.app;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v4.view.LayoutInflaterFactory;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.VectorEnabledTintResources;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import xyz.geminiwen.skinsprite.view.Skinnable;

/**
 * Created by geminiwen on 16/6/15.
 */
public class SkinnableActivity extends AppCompatActivity implements LayoutInflaterFactory{

    private SkinnableViewInflater mSkinnableViewInflater;
    private SkinnableCallback mSkinnableCallback;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        LayoutInflaterCompat.setFactory(layoutInflater, this);
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        if (mSkinnableViewInflater == null) {
            mSkinnableViewInflater = new SkinnableViewInflater();
        }
        final boolean isPre21 = Build.VERSION.SDK_INT < 21;
        final boolean inheritContext = isPre21 && shouldInheritContext((ViewParent) parent);
        return mSkinnableViewInflater.createView(parent, name, context, attrs, inheritContext,
                isPre21, /* Only read android:theme pre-L (L+ handles this anyway) */
                true, /* Read read app:theme as a fallback at all times for legacy reasons */
                VectorEnabledTintResources.shouldBeUsed() /* Only tint wrap the context if enabled */
        );
    }

    public void setSkinnableCallback(SkinnableCallback skinnableCallback) {
        mSkinnableCallback = skinnableCallback;
    }

    private boolean shouldInheritContext(ViewParent parent) {
        if (parent == null) {
            // The initial parent is null so just return false
            return false;
        }
        final View windowDecor = getWindow().getDecorView();
        while (true) {
            if (parent == null) {
                // Bingo. We've hit a view which has a null parent before being terminated from
                // the loop. This is (most probably) because it's the root view in an inflation
                // call, therefore we should inherit. This works as the inflated layout is only
                // added to the hierarchy at the end of the inflate() call.
                return true;
            } else if (parent == windowDecor || !(parent instanceof View)
                    || ViewCompat.isAttachedToWindow((View) parent)) {
                // We have either hit the window's decor view, a parent which isn't a View
                // (i.e. ViewRootImpl), or an attached view, so we know that the original parent
                // is currently added to the view hierarchy. This means that it has not be
                // inflated in the current inflate() call and we should not inherit the context.
                return false;
            }
            parent = parent.getParent();
        }
    }

    public void setDayNightMode(@AppCompatDelegate.NightMode int nightMode) {
        final boolean isPost21 = Build.VERSION.SDK_INT >= 21;

        if (mSkinnableCallback != null) {
            mSkinnableCallback.beforeApplyDayNight();
        }

        getDelegate().setLocalNightMode(nightMode);

        if (isPost21) {
            applyDayNightForStatusBar();
        }

        View decorView = getWindow().getDecorView();
        applyDayNightForView(decorView);

        if (mSkinnableCallback != null) {
            mSkinnableCallback.onApplyDayNight();
        }
    }

    private void applyDayNightForView(View view) {
        if (view instanceof Skinnable) {
            Skinnable skinnable = (Skinnable) view;
            if (skinnable.isSkinnable()) {
                skinnable.applyDayNight();
            }
        }
//        if (view instanceof ViewGroup) {
//            ViewGroup parent = (ViewGroup)view;
//            int childCount = parent.getChildCount();
//            for (int i = 0; i < childCount; i++) {
//                applyDayNightForView(parent.getChildAt(i));
//            }
//        }
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void applyDayNightForStatusBar() {
        TypedArray a = getTheme().obtainStyledAttributes(0, new int[] {
                android.R.attr.statusBarColor
        });
        int color = a.getColor(0, 0);
        getWindow().setStatusBarColor(color);
        a.recycle();
    }

    public interface SkinnableCallback {
        void beforeApplyDayNight();
        void onApplyDayNight();
    }
}

@@ -4321,34 +4321,32 @@
     %7D%0A        %7D%0A
-//
         if (view
@@ -4366,26 +4366,24 @@
 iewGroup) %7B%0A
-//
             
@@ -4418,18 +4418,16 @@
 p)view;%0A
-//
         
@@ -4471,18 +4471,16 @@
 ount();%0A
-//
         
@@ -4522,18 +4522,16 @@
  i++) %7B%0A
-//
         
@@ -4582,18 +4582,16 @@
 At(i));%0A
-//
         
@@ -4596,18 +4596,16 @@
       %7D%0A
-//
         

reset activity