// Path: gearshift/src/main/java/org/sugr/gearshift/ui/util/Colorizer.java
// public class Colorizer {
//     public static void colorizeView(ImageView view, int color, int shape) {
//         Resources res = view.getContext().getResources();
// 
//         Drawable currentDrawable = view.getDrawable();
//         GradientDrawable colorChoiceDrawable;
//         if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
//             // Reuse drawable
//             colorChoiceDrawable = (GradientDrawable) currentDrawable;
//         } else {
//             colorChoiceDrawable = new GradientDrawable();
//             colorChoiceDrawable.setShape(shape);
//         }
// 
//         int darkenedColor;
//         if (color == defaultColor(view.getContext())) {
//             darkenedColor = Color.rgb(95, 95, 95);
//         } else {
//             // Set stroke to dark version of color
//             darkenedColor = Color.rgb(
//                     Color.red(color) * 192 / 256,
//                     Color.green(color) * 192 / 256,
//                     Color.blue(color) * 192 / 256);
//         }
// 
//         colorChoiceDrawable.setColor(color);
//         colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
//                 TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics()), darkenedColor);
//         view.setImageDrawable(colorChoiceDrawable);
// 
//     }
// 
//     public static void colorizeView(TextView view, int color) {
//         view.setTextColor(color);
//     }
// 
//     public static int defaultColor(Context context) {
// 
//         int[] colorArray = context.getResources().getIntArray(R.array.default_color_choice_values);
// 
//         if (colorArray != null && colorArray.length > 0) {
//             return colorArray[0];
//         } else {
//             return context.getResources().getColor(R.color.primary);
//         }
//     }
// 
// }


import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import org.sugr.gearshift.R;
import org.sugr.gearshift.ui.util.Colorizer;
/*******************************************************************************
 * Copyright 2013 Gabriele Mariotti
 *
 * 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 org.sugr.gearshift.ui.util.colorpicker;



/**
 * ColorPreference based on ColorPickerDialog of Stock Calendar
 * 
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 *
 */
public class ColorPickerPreference extends Preference{
	
	private int[] mColorChoices = {};
    private int mValue = 0;
    private int mItemLayoutId = R.layout.calendar_grid_item_color;
    private int mNumColumns = 5;
    private View mPreviewView;

	public ColorPickerPreference(Context context) {
		super(context);
		initAttrs(null, 0);
	}
	
	public ColorPickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs, 0);
    }

    public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(attrs, defStyle);
    }
	
    private void initAttrs(AttributeSet attrs, int defStyle) {
    	TypedArray a = getContext().getTheme().obtainStyledAttributes(
                attrs, R.styleable.ColorPickerPreference, defStyle, defStyle);

        try {
            mItemLayoutId = a.getResourceId(R.styleable.ColorPickerPreference_cal_itemLayout, mItemLayoutId);
            mNumColumns = a.getInteger(R.styleable.ColorPickerPreference_cal_numColumns, mNumColumns);
// Path: gearshift/src/main/java/org/sugr/gearshift/ui/util/Colorizer.java
// public class Colorizer {
//     public static void colorizeView(ImageView view, int color, int shape) {
//         Resources res = view.getContext().getResources();
// 
//         Drawable currentDrawable = view.getDrawable();
//         GradientDrawable colorChoiceDrawable;
//         if (currentDrawable != null && currentDrawable instanceof GradientDrawable) {
//             // Reuse drawable
//             colorChoiceDrawable = (GradientDrawable) currentDrawable;
//         } else {
//             colorChoiceDrawable = new GradientDrawable();
//             colorChoiceDrawable.setShape(shape);
//         }
// 
//         int darkenedColor;
//         if (color == defaultColor(view.getContext())) {
//             darkenedColor = Color.rgb(95, 95, 95);
//         } else {
//             // Set stroke to dark version of color
//             darkenedColor = Color.rgb(
//                     Color.red(color) * 192 / 256,
//                     Color.green(color) * 192 / 256,
//                     Color.blue(color) * 192 / 256);
//         }
// 
//         colorChoiceDrawable.setColor(color);
//         colorChoiceDrawable.setStroke((int) TypedValue.applyDimension(
//                 TypedValue.COMPLEX_UNIT_DIP, 1, res.getDisplayMetrics()), darkenedColor);
//         view.setImageDrawable(colorChoiceDrawable);
// 
//     }
// 
//     public static void colorizeView(TextView view, int color) {
//         view.setTextColor(color);
//     }
// 
//     public static int defaultColor(Context context) {
// 
//         int[] colorArray = context.getResources().getIntArray(R.array.default_color_choice_values);
// 
//         if (colorArray != null && colorArray.length > 0) {
//             return colorArray[0];
//         } else {
//             return context.getResources().getColor(R.color.primary);
//         }
//     }
// 
// }


// Path: gearshift/src/main/java/org/sugr/gearshift/ui/util/colorpicker/ColorPickerPreference.java
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import org.sugr.gearshift.R;
import org.sugr.gearshift.ui.util.Colorizer;

/*******************************************************************************
 * Copyright 2013 Gabriele Mariotti
 *
 * 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 org.sugr.gearshift.ui.util.colorpicker;



/**
 * ColorPreference based on ColorPickerDialog of Stock Calendar
 * 
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 *
 */
public class ColorPickerPreference extends Preference{
	
	private int[] mColorChoices = {};
    private int mValue = 0;
    private int mItemLayoutId = R.layout.calendar_grid_item_color;
    private int mNumColumns = 5;
    private View mPreviewView;

	public ColorPickerPreference(Context context) {
		super(context);
		initAttrs(null, 0);
	}
	
	public ColorPickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs, 0);
    }

    public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initAttrs(attrs, defStyle);
    }
	
    private void initAttrs(AttributeSet attrs, int defStyle) {
    	TypedArray a = getContext().getTheme().obtainStyledAttributes(
                attrs, R.styleable.ColorPickerPreference, defStyle, defStyle);

        try {
            mItemLayoutId = a.getResourceId(R.styleable.ColorPickerPreference_cal_itemLayout, mItemLayoutId);
            mNumColumns = a.getInteger(R.styleable.ColorPickerPreference_cal_numColumns, mNumColumns);
            int choicesResId = a.getResourceId(R.styleable.ColorPickerPreference_cal_choices,