/* The contents of this file are subject to the Mozilla Public License Version 1.1 (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.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is collection of files collectively known as Open Camera. The Initial Developer of the Original Code is Almalence Inc. Portions created by Initial Developer are Copyright (C) 2013 by Almalence Inc. All Rights Reserved. */ /* <!-- +++ package com.almalence.opencam_plus.ui; +++ --> */ // <!-- -+- package com.almalence.opencam.ui; //-+- --> import java.util.HashMap; import java.util.Map; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.preference.DialogPreference; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; public class SeekBarPreference extends DialogPreference implements SeekBar.OnSeekBarChangeListener, OnClickListener { // ------------------------------------------------------------------------------------------ // Private attributes : private static final String androidns="http://schemas.android.com/apk/res/android"; private SeekBar mSeekBar; private TextView mSplashText,mValueText; private Context mContext; private String mDialogMessage, mSuffix; private int mDefault, mMax, mValue = 0; // ------------------------------------------------------------------------------------------ private static final Map<Integer, Float> gamma_map = new HashMap<Integer, Float>() { { put(0, 0.5f); put(1, 0.55f); put(2, 0.6f); put(3, 0.65f); put(4, 0.7f); } }; // ------------------------------------------------------------------------------------------ // Constructor : public SeekBarPreference(Context context, AttributeSet attrs) { super(context,attrs); mContext = context; // Get string value for dialogMessage : int mDialogMessageId = attrs.getAttributeResourceValue(androidns, "dialogMessage", 0); if(mDialogMessageId == 0) mDialogMessage = attrs.getAttributeValue(androidns, "dialogMessage"); else mDialogMessage = mContext.getString(mDialogMessageId); // Get string value for suffix (text attribute in xml file) : int mSuffixId = attrs.getAttributeResourceValue(androidns, "text", 0); if(mSuffixId == 0) mSuffix = attrs.getAttributeValue(androidns, "text"); else mSuffix = mContext.getString(mSuffixId); // Get default and max seekbar values : mDefault = attrs.getAttributeIntValue(androidns, "defaultValue", 0); mMax = attrs.getAttributeIntValue(androidns, "max", 4); } // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // DialogPreference methods : @Override protected View onCreateDialogView() { LinearLayout.LayoutParams params; LinearLayout layout = new LinearLayout(mContext); layout.setOrientation(LinearLayout.VERTICAL); layout.setPadding(6,6,6,6); mSplashText = new TextView(mContext); mSplashText.setPadding(30, 10, 30, 10); if (mDialogMessage != null) mSplashText.setText(mDialogMessage); layout.addView(mSplashText); mValueText = new TextView(mContext); mValueText.setGravity(Gravity.CENTER_HORIZONTAL); mValueText.setTextSize(32); params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); layout.addView(mValueText, params); mSeekBar = new SeekBar(mContext); mSeekBar.setOnSeekBarChangeListener(this); layout.addView(mSeekBar, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); if (shouldPersist()) mValue = getPersistedInt(mDefault); mSeekBar.setMax(mMax); mSeekBar.setProgress(mValue); return layout; } @Override protected void onBindDialogView(View v) { super.onBindDialogView(v); mSeekBar.setMax(mMax); mSeekBar.setProgress(mValue); } @Override protected void onSetInitialValue(boolean restore, Object defaultValue) { super.onSetInitialValue(restore, defaultValue); if (restore) mValue = shouldPersist() ? getPersistedInt(mDefault) : 0; else mValue = (Integer)defaultValue; } // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // OnSeekBarChangeListener methods : @Override public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) { String t = String.valueOf(value); float gamma = gamma_map.get(Integer.valueOf(t)); mValueText.setText("" + gamma + " ev"); } @Override public void onStartTrackingTouch(SeekBar seek) {} @Override public void onStopTrackingTouch(SeekBar seek) {} public void setMax(int max) { mMax = max; } public int getMax() { return mMax; } public void setProgress(int progress) { mValue = progress; if (mSeekBar != null) mSeekBar.setProgress(progress); } public int getProgress() { return mValue; } // ------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------ // Set the positive button listener and onClick action : @Override public void showDialog(Bundle state) { super.showDialog(state); Button positiveButton = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE); positiveButton.setOnClickListener(this); } @Override public void onClick(View v) { if (shouldPersist()) { mValue = mSeekBar.getProgress(); persistInt(mSeekBar.getProgress()); callChangeListener(Integer.valueOf(mSeekBar.getProgress())); } ((AlertDialog) getDialog()).dismiss(); } // ------------------------------------------------------------------------------------------ }