Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
| 32 | package com.savoirfairelinux.sflphone.client; |
| 33 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 34 | import android.app.Activity; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 35 | import android.content.Context; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 36 | import android.content.res.TypedArray; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 37 | import android.graphics.Typeface; |
| 38 | import android.preference.EditTextPreference; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 39 | import android.preference.ListPreference; |
| 40 | import android.preference.Preference; |
| 41 | import android.preference.PreferenceCategory; |
| 42 | import android.preference.PreferenceFragment; |
| 43 | import android.preference.PreferenceScreen; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 44 | import android.os.Bundle; |
| 45 | import android.util.Log; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 46 | import android.util.AttributeSet; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 47 | import android.view.View; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 48 | import android.view.ViewGroup; |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 50 | import android.widget.SeekBar; |
| 51 | import android.widget.SeekBar.OnSeekBarChangeListener; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 52 | import android.widget.TextView; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 53 | |
| 54 | import com.savoirfairelinux.sflphone.R; |
| 55 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 56 | public class PrefManagementFragment extends PreferenceFragment |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 57 | { |
| 58 | static final String TAG = "PrefManagementFragment"; |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 59 | static final String CURRENT_VALUE = "Current value:: "; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 60 | |
| 61 | @Override |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 62 | public void onCreate(Bundle savedInstanceState) |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 63 | { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 64 | super.onCreate(savedInstanceState); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 65 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 66 | setPreferenceScreen(getAudioPreferenceScreen()); |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 69 | Preference.OnPreferenceChangeListener changePreferenceListener = new Preference.OnPreferenceChangeListener() { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 70 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 71 | preference.setSummary(CURRENT_VALUE + (CharSequence)newValue); |
| 72 | return true; |
| 73 | } |
| 74 | }; |
| 75 | |
| 76 | public PreferenceScreen getAudioPreferenceScreen() |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 77 | { |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 78 | Activity currentContext = getActivity(); |
| 79 | |
| 80 | PreferenceScreen root = getPreferenceManager().createPreferenceScreen(currentContext); |
| 81 | |
| 82 | PreferenceCategory audioPrefCat = new PreferenceCategory(currentContext); |
| 83 | audioPrefCat.setTitle(R.string.audio_preferences); |
| 84 | root.addPreference(audioPrefCat); |
| 85 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 86 | // Codec List |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 87 | ListPreference codecListPref = new ListPreference(currentContext); |
| 88 | codecListPref.setEntries(R.array.audio_codec_list); |
| 89 | codecListPref.setEntryValues(R.array.audio_codec_list_value); |
| 90 | codecListPref.setDialogTitle(R.string.dialogtitle_audio_codec_list); |
| 91 | codecListPref.setPersistent(false); |
| 92 | codecListPref.setTitle(R.string.title_audio_codec_list); |
| 93 | codecListPref.setSummary(CURRENT_VALUE + "PCMU"); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 94 | codecListPref.setOnPreferenceChangeListener(changePreferenceListener); |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 95 | audioPrefCat.addPreference(codecListPref); |
| 96 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 97 | // Ringtone |
| 98 | EditTextPreference audioRingtonePref = new EditTextPreference(currentContext); |
| 99 | audioRingtonePref.setDialogTitle(R.string.dialogtitle_audio_ringtone_field); |
| 100 | audioRingtonePref.setPersistent(false); |
| 101 | audioRingtonePref.setTitle(R.string.title_audio_ringtone_field); |
| 102 | audioRingtonePref.setSummary(CURRENT_VALUE + "path/to/ringtone"); |
| 103 | audioRingtonePref.setOnPreferenceChangeListener(changePreferenceListener); |
| 104 | audioPrefCat.addPreference(audioRingtonePref); |
| 105 | |
| 106 | // Speaker volume seekbar |
| 107 | SeekBarPreference speakerSeekBarPref = new SeekBarPreference(currentContext); |
| 108 | speakerSeekBarPref.setPersistent(false); |
| 109 | speakerSeekBarPref.setTitle("Speaker Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 110 | speakerSeekBarPref.setProgress(50); |
| 111 | speakerSeekBarPref.setSummary(CURRENT_VALUE + speakerSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 112 | audioPrefCat.addPreference(speakerSeekBarPref); |
| 113 | |
| 114 | // Capture volume seekbar |
| 115 | SeekBarPreference captureSeekBarPref = new SeekBarPreference(currentContext); |
| 116 | captureSeekBarPref.setPersistent(false); |
| 117 | captureSeekBarPref.setTitle("Capture Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 118 | captureSeekBarPref.setProgress(50); |
| 119 | captureSeekBarPref.setSummary(CURRENT_VALUE + captureSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 120 | audioPrefCat.addPreference(captureSeekBarPref); |
| 121 | |
| 122 | // Ringtone volume seekbar |
| 123 | SeekBarPreference ringtoneSeekBarPref = new SeekBarPreference(currentContext); |
| 124 | ringtoneSeekBarPref.setPersistent(false); |
| 125 | ringtoneSeekBarPref.setTitle("Ringtone Volume"); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 126 | ringtoneSeekBarPref.setProgress(50); |
| 127 | ringtoneSeekBarPref.setSummary(CURRENT_VALUE + ringtoneSeekBarPref.getProgress()); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 128 | audioPrefCat.addPreference(ringtoneSeekBarPref); |
| 129 | |
Alexandre Savard | 393ccab | 2012-09-11 15:01:20 -0400 | [diff] [blame] | 130 | return root; |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 131 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 132 | |
| 133 | public class SeekBarPreference extends Preference implements OnSeekBarChangeListener |
| 134 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 135 | private SeekBar seekbar; |
| 136 | private int progress; |
| 137 | private int max = 100; |
| 138 | private TextView summary; |
| 139 | private boolean discard; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 140 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 141 | public SeekBarPreference (Context context) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 142 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 143 | super( context ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 146 | public SeekBarPreference (Context context, AttributeSet attrs) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 147 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 148 | super( context, attrs ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 151 | public SeekBarPreference (Context context, AttributeSet attrs, int defStyle) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 152 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 153 | super( context, attrs, defStyle ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 156 | protected View onCreateView (ViewGroup p) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 157 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 158 | final Context ctx = getContext(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 159 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 160 | LinearLayout layout = new LinearLayout( ctx ); |
| 161 | layout.setId( android.R.id.widget_frame ); |
| 162 | layout.setOrientation( LinearLayout.VERTICAL ); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 163 | layout.setPadding(65, 10, 15, 10); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 164 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 165 | TextView title = new TextView( ctx ); |
| 166 | int textColor = title.getCurrentTextColor(); |
| 167 | title.setId( android.R.id.title ); |
| 168 | title.setSingleLine(); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 169 | title.setTextAppearance( ctx, android.R.style.TextAppearance_Medium ); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 170 | title.setTextColor( textColor ); |
| 171 | layout.addView( title ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 172 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 173 | summary = new TextView( ctx ); |
| 174 | summary.setId( android.R.id.summary ); |
| 175 | summary.setSingleLine(); |
| 176 | summary.setTextAppearance( ctx, android.R.style.TextAppearance_Small ); |
| 177 | summary.setTextColor( textColor ); |
| 178 | layout.addView( summary ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 179 | |
Alexandre Savard | 616c380 | 2012-09-12 09:44:46 -0400 | [diff] [blame] | 180 | seekbar = new SeekBar( ctx ); |
| 181 | seekbar.setId( android.R.id.progress ); |
| 182 | seekbar.setMax( max ); |
| 183 | seekbar.setOnSeekBarChangeListener( this ); |
| 184 | layout.addView( seekbar ); |
| 185 | |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 186 | return layout; |
| 187 | } |
| 188 | |
| 189 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 190 | protected void onBindView (View view) |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 191 | { |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 192 | super.onBindView( view ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 193 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 194 | if (seekbar != null) |
| 195 | seekbar.setProgress( progress ); |
| 196 | } |
| 197 | |
| 198 | public void setProgress (int pcnt) { |
| 199 | if (progress != pcnt) { |
| 200 | persistInt( progress = pcnt ); |
| 201 | |
| 202 | notifyDependencyChange( shouldDisableDependents() ); |
| 203 | notifyChanged(); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 204 | } |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 205 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 206 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 207 | public int getProgress () { |
| 208 | return progress; |
| 209 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 210 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 211 | public void setMax (int max) { |
| 212 | this.max = max; |
| 213 | if (seekbar != null) |
| 214 | seekbar.setMax( max ); |
| 215 | } |
| 216 | |
| 217 | private SeekBar getSeekBar () { |
| 218 | return seekbar; |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 222 | protected Object onGetDefaultValue (TypedArray a, int index) { |
| 223 | return a.getInt( index, progress ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 227 | protected void onSetInitialValue (boolean restoreValue, Object defaultValue) { |
| 228 | setProgress( restoreValue ? getPersistedInt( progress ) : (Integer)defaultValue ); |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 231 | @Override |
| 232 | public boolean shouldDisableDependents () { |
| 233 | return progress == 0 || super.shouldDisableDependents(); |
| 234 | } |
| 235 | |
| 236 | public void onProgressChanged (SeekBar seekBar, int progress, boolean fromUser) { |
| 237 | discard = !callChangeListener( progress ); |
Alexandre Savard | 74eccf3 | 2012-09-11 19:12:29 -0400 | [diff] [blame] | 238 | summary.setText(CURRENT_VALUE + progress); |
Alexandre Savard | e2bb259 | 2012-09-11 18:39:57 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | public void onStartTrackingTouch (SeekBar seekBar) { |
| 242 | discard = false; |
| 243 | } |
| 244 | |
| 245 | public void onStopTrackingTouch (SeekBar seekBar) { |
| 246 | if (discard) |
| 247 | seekBar.setProgress( progress ); |
| 248 | else { |
| 249 | setProgress( seekBar.getProgress() ); |
| 250 | |
| 251 | OnPreferenceChangeListener listener = getOnPreferenceChangeListener(); |
| 252 | //if (listener instanceof AbstractSeekBarListener) |
| 253 | //// setSummary( ((AbstractSeekBarListener)listener).toSummary( seekBar.getProgress() ) ); |
| 254 | } |
Alexandre Savard | 5e0b947 | 2012-09-11 17:56:30 -0400 | [diff] [blame] | 255 | } |
| 256 | } |
Alexandre Savard | dcd3740 | 2012-09-06 18:36:48 -0400 | [diff] [blame] | 257 | } |