/*
 * Original work Copyright (C) 2013 The ChameleonOS Open Source Project
 * Modified work Copyright (C) 2013-2014 GermainZ@xda-developers.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.germainz.identiconizer;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.res.TypedArray;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.ListAdapter;

public class ImageListPreference extends ListPreference {
    private int[] mResourceIds = null;

    /**
     * Constructor of the ImageListPreference. Initializes the custom images.
     * @param context application context.
     * @param attrs custom xml attributes.
     */
    public ImageListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.ImageListPreference);

        String[] imageNames = context.getResources().getStringArray(
                typedArray.getResourceId(typedArray.getIndexCount()-1, -1));

        mResourceIds = new int[imageNames.length];

        for (int i=0;i<imageNames.length;i++) {
            String imageName = imageNames[i].substring(
                    imageNames[i].lastIndexOf('/') + 1,
                    imageNames[i].lastIndexOf('.'));

            mResourceIds[i] = context.getResources().getIdentifier(imageName,
                    "drawable", context.getPackageName());
        }

        typedArray.recycle();
    }

    /**
     * {@inheritDoc}
     */
    protected void onPrepareDialogBuilder(Builder builder) {
        int index = findIndexOfValue(getSharedPreferences().getString(
                getKey(), "1"));

        ListAdapter listAdapter = new ImageArrayAdapter(getContext(),
                R.layout.image_list_item, getEntries(), mResourceIds, index);

        // Order matters.
        builder.setAdapter(listAdapter, this);
        super.onPrepareDialogBuilder(builder);
    }

    public class ImageArrayAdapter extends ArrayAdapter<CharSequence> {
        private int index = 0;
        private int[] resourceIds = null;

        /**
         * ImageArrayAdapter constructor.
         * @param context the context.
         * @param textViewResourceId resource id of the text view.
         * @param objects to be displayed.
         * @param ids resource id of the images to be displayed.
         * @param i index of the previous selected item.
         */
        public ImageArrayAdapter(Context context, int textViewResourceId,
                                 CharSequence[] objects, int[] ids, int i) {
            super(context, textViewResourceId, objects);

            index = i;
            resourceIds = ids;
        }
        /**
         * {@inheritDoc}
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
            View row = inflater.inflate(R.layout.image_list_item, parent, false);

            ImageView imageView = (ImageView)row.findViewById(R.id.image);
            imageView.setImageResource(resourceIds[position]);

            CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(
                    R.id.check);

            checkedTextView.setText(getItem(position));

            if (position == index) {
                checkedTextView.setChecked(true);
            }

            return row;
        }
    }
}

@@ -839,16 +839,54 @@
 ontext;%0A
+import android.content.res.Resources;%0A
 import a
@@ -1321,24 +1321,58 @@
 eIds = null;
+%0A    private int mRadioDrawableId;
 %0A%0A    /**%0A  
@@ -1443,24 +1443,31 @@
 tom images.%0A
+     *%0A
      * @para
@@ -1517,16 +1517,18 @@
 m attrs 
+  
 custom x
@@ -1908,17 +1908,19 @@
 xCount()
--
+ - 
 1, -1));
@@ -1995,13 +1995,18 @@
 nt i
-=0;i%3C
+ = 0; i %3C 
 imag
@@ -2019,16 +2019,17 @@
 .length;
+ 
 i++) %7B%0A 
@@ -2370,16 +2370,361 @@
 cycle();
+%0A%0A        // in order to use the holo themed radio button we must get the id%0A        // from the system resources and then get the drawable for that id%0A        // Because it uses reflection twice, we'll store this value for future use.%0A        mRadioDrawableId = Resources.getSystem().getIdentifier(%22btn_radio_holo_dark%22, %22drawable%22, %22android%22);
 %0A    %7D%0A%0A
@@ -3402,16 +3402,27 @@
 ructor.%0A
+         *%0A
         
@@ -3439,16 +3439,27 @@
 context 
+           
 the cont
@@ -3556,16 +3556,27 @@
 objects 
+           
 to be di
@@ -3606,16 +3606,31 @@
 ram ids 
+               
 resource
@@ -3684,16 +3684,33 @@
 param i 
+                 
 index of
@@ -4019,24 +4019,25 @@
 ;%0A        %7D%0A
+%0A
         /**%0A
@@ -4202,16 +4202,17 @@
 ctivity)
+ 
 getConte
@@ -4362,24 +4362,25 @@
  (ImageView)
+ 
 row.findView
@@ -4524,16 +4524,17 @@
 extView)
+ 
 row.find
@@ -4632,16 +4632,119 @@
 ition));
+%0A            checkedTextView.setCheckMarkDrawable(Resources.getSystem().getDrawable(mRadioDrawableId));
 %0A%0A      

Use radio button for style picker (partially reverts 7eb7dd3)