package android.support.v7.preference;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.xpece.android.support.preference.ColorableTextPreference;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Eugen on 17. 3. 2016.
 */
@SuppressWarnings("RestrictedApi")
class XpPreferenceGroupAdapter extends PreferenceGroupAdapter {

    private static final int OFFSET = 0xffff;

    private final List<PreferenceLayout> mPreferenceLayouts = new ArrayList<>();

    private PreferenceLayout mTempPreferenceLayout = new PreferenceLayout();

    public XpPreferenceGroupAdapter(final PreferenceGroup preferenceGroup) {
        super(preferenceGroup);
    }

    @Override
    public int getItemViewType(final int position) {
        int offset = 0;
        Preference preference = this.getItem(position);
        if (preference instanceof ColorableTextPreference) {
            ColorableTextPreference p = (ColorableTextPreference) preference;
            if (p.hasTitleTextAppearance()) {
                offset += OFFSET;
            }
            if (p.hasTitleTextColor()) {
                offset += OFFSET;
            }
            if (p.hasSummaryTextAppearance()) {
                offset += OFFSET;
            }
            if (p.hasSummaryTextColor()) {
                offset += OFFSET;
            }
        }
        return offset + getItemViewTypeOriginal(position);
    }

    public int getItemViewTypeOriginal(int position) {
        Preference preference = this.getItem(position);
        this.mTempPreferenceLayout = this.createPreferenceLayout(preference, this.mTempPreferenceLayout);
        int viewType = this.mPreferenceLayouts.indexOf(this.mTempPreferenceLayout);
        if (viewType != -1) {
            return viewType;
        } else {
            viewType = this.mPreferenceLayouts.size();
            this.mPreferenceLayouts.add(new PreferenceLayout(this.mTempPreferenceLayout));
            return viewType;
        }
    }

    @Override
    public void onViewDetachedFromWindow(PreferenceViewHolder holder) {
        super.onViewDetachedFromWindow(holder);

        holder.itemView.setOnKeyListener(null);
    }

    @Override
    public PreferenceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        viewType %= OFFSET;

        PreferenceLayout pl = this.mPreferenceLayouts.get(viewType);
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(pl.resId, parent, false);
        ViewGroup widgetFrame = (ViewGroup) view.findViewById(android.R.id.widget_frame);
        if (widgetFrame != null) {
            if (pl.widgetResId != 0) {
                inflater.inflate(pl.widgetResId, widgetFrame);
            } else {
                widgetFrame.setVisibility(View.GONE);
            }
        }

        return new PreferenceViewHolder(view);
    }

    private PreferenceLayout createPreferenceLayout(Preference preference, PreferenceLayout in) {
        PreferenceLayout pl = in != null ? in : new PreferenceLayout();
        pl.name = preference.getClass().getName();
        pl.resId = preference.getLayoutResource();
        pl.widgetResId = preference.getWidgetLayoutResource();
        return pl;
    }

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        final Preference preference = getItem(position);
        XpPreferenceHelpers.onBindViewHolder(preference, holder);
    }

    private static class PreferenceLayout {
        int resId;
        int widgetResId;
        String name;

        public PreferenceLayout() {
        }

        public PreferenceLayout(PreferenceLayout other) {
            this.resId = other.resId;
            this.widgetResId = other.widgetResId;
            this.name = other.name;
        }

        public boolean equals(Object o) {
            if (!(o instanceof PreferenceLayout)) {
                return false;
            } else {
                PreferenceLayout other = (PreferenceLayout) o;
                return this.resId == other.resId && this.widgetResId == other.widgetResId && TextUtils.equals(this.name, other.name);
            }
        }

        public int hashCode() {
            byte result = 17;
            int result1 = 31 * result + this.resId;
            result1 = 31 * result1 + this.widgetResId;
            result1 = 31 * result1 + this.name.hashCode();
            return result1;
        }
    }
}

@@ -52,84 +52,34 @@
 oid.
-text.TextUtils;%0Aimport android.view.LayoutInflater;%0Aimport android.view.View
+support.annotation.NonNull
 ;%0Aim
@@ -182,60 +182,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -370,168 +370,8 @@
 f;%0A%0A
-    private final List%3CPreferenceLayout%3E mPreferenceLayouts = new ArrayList%3C%3E();%0A%0A    private PreferenceLayout mTempPreferenceLayout = new PreferenceLayout();%0A%0A
     
@@ -1172,17 +1172,22 @@
 ffset + 
-g
+superG
 etItemVi
@@ -1192,24 +1192,16 @@
 ViewType
-Original
 (positio
@@ -1212,35 +1212,41 @@
     %7D%0A%0A    p
-ublic int g
+rivate int superG
 etItemViewTy
@@ -1251,16 +1251,8 @@
 Type
-Original
 (int
@@ -1276,506 +1276,47 @@
     
-P
 re
-ference preference = this.getItem(position);%0A        this.mTempPreferenceLayout = this.createPreferenceLayout(preference, this.mTempPreferenceLayout);%0A        int viewType = this.mPreferenceLayouts.indexOf(this.mTempPreferenceLayout);%0A        if (viewType != -1) %7B%0A            return viewType;%0A        %7D else %7B%0A            viewType = this.mPreferenceLayouts.size();%0A            this.mPreferenceLayouts.add(new PreferenceLayout(this.mTempPreferenceLayout));%0A            return viewType;%0A        %7D
+turn super.getItemViewType(position);
 %0A   
@@ -1374,16 +1374,25 @@
 mWindow(
+@NonNull 
 Preferen
@@ -1459,24 +1459,78 @@
 w(holder);%0A%0A
+        // Clear key listener from SeekBarPreference.%0A
         hold
@@ -1564,32 +1564,45 @@
 r(null);%0A    %7D%0A%0A
+    @NonNull%0A
     @Override%0A  
@@ -1650,16 +1650,25 @@
 wHolder(
+@NonNull 
 ViewGrou
@@ -1705,2234 +1705,340 @@
     
-viewType %25= OFFSET;%0A%0A        PreferenceLayout pl = this.mPreferenceLayouts.get(viewType);%0A        LayoutInflater inflater = LayoutInflater.from(parent.getContext());%0A        View view = inflater.inflate(pl.resId, parent, false);%0A        ViewGroup widgetFrame = (ViewGroup) view.findViewById(android.R.id.widget_frame);%0A        if (widgetFrame != null) %7B%0A            if (pl.widgetResId != 0) %7B%0A                inflater.inflate(pl.widgetResId, widgetFrame);%0A            %7D else %7B%0A                widgetFrame.setVisibility(View.GONE);%0A            %7D%0A        %7D%0A%0A        return new PreferenceViewHolder(view);%0A    %7D%0A%0A    private PreferenceLayout createPreferenceLayout(Preference preference, PreferenceLayout in) %7B%0A        PreferenceLayout pl = in != null ? in : new PreferenceLayout();%0A        pl.name = preference.getClass().getName();%0A        pl.resId = preference.getLayoutResource();%0A        pl.widgetResId = preference.getWidgetLayoutResource();%0A        return pl;%0A    %7D%0A%0A    @Override%0A    public void onBindViewHolder(PreferenceViewHolder holder, int position) %7B%0A        super.onBindViewHolder(holder, position);%0A%0A        final Preference preference = getItem(position);%0A        XpPreferenceHelpers.onBindViewHolder(preference, holder);%0A    %7D%0A%0A    private static class PreferenceLayout %7B%0A        int resId;%0A        int widgetResId;%0A        String name;%0A%0A        public PreferenceLayout() %7B%0A        %7D%0A%0A        public PreferenceLayout(PreferenceLayout other) %7B%0A            this.resId = other.resId;%0A            this.widgetResId = other.widgetResId;%0A            this.name = other.name;%0A        %7D%0A%0A        public boolean equals(Object o) %7B%0A            if (!(o instanceof PreferenceLayout)) %7B%0A                return false;%0A            %7D else %7B%0A                PreferenceLayout other = (PreferenceLayout) o;%0A                return this.resId == other.resId && this.widgetResId == other.widgetResId && TextUtils.equals(this.name, other.name);%0A            %7D%0A        %7D%0A%0A        public int hashCode() %7B%0A            byte result = 17;%0A            int result1 = 31 * result + this.resId;%0A            result1 = 31 * result1 + this.widgetResId;%0A            result1 = 31 * result1 + this.name.hashCode();%0A            return result1;%0A        %7D
+return super.onCreateViewHolder(parent, viewType %25 OFFSET);%0A    %7D%0A%0A    @Override%0A    public void onBindViewHolder(PreferenceViewHolder holder, int position) %7B%0A        super.onBindViewHolder(holder, position);%0A%0A        final Preference preference = getItem(position);%0A        XpPreferenceHelpers.onBindViewHolder(preference, holder);
 %0A   

Simplify XpPreferenceGroupAdapter, rely on super implementation