1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera.ui; 18 19 import java.util.Locale; 20 21 import android.content.Context; 22 import android.util.AttributeSet; 23 import android.util.Log; 24 import android.view.View; 25 import android.widget.Button; 26 import android.widget.CheckBox; 27 import android.widget.NumberPicker; 28 import android.widget.NumberPicker.OnValueChangeListener; 29 30 import com.android.camera.ListPreference; 31 import com.android.camera2.R; 32 33 /** 34 * This is a popup window that allows users to specify a countdown timer 35 */ 36 37 public class CountdownTimerPopup extends AbstractSettingPopup { 38 private static final String TAG = "TimerSettingPopup"; 39 private NumberPicker mNumberSpinner; 40 private String[] mDurations; 41 private ListPreference mTimer; 42 private ListPreference mBeep; 43 private Listener mListener; 44 private Button mConfirmButton; 45 private View mPickerTitle; 46 private CheckBox mTimerSound; 47 private View mSoundTitle; 48 49 static public interface Listener { 50 public void onListPrefChanged(ListPreference pref); 51 } 52 53 public void setSettingChangedListener(Listener listener) { 54 mListener = listener; 55 } 56 57 public CountdownTimerPopup(Context context, AttributeSet attrs) { 58 super(context, attrs); 59 } 60 61 public void initialize(ListPreference timer, ListPreference beep) { 62 mTimer = timer; 63 mBeep = beep; 64 // Set title. 65 mTitle.setText(mTimer.getTitle()); 66 67 // Duration 68 CharSequence[] entries = mTimer.getEntryValues(); 69 mDurations = new String[entries.length]; 70 Locale locale = getResources().getConfiguration().locale; 71 mDurations[0] = getResources().getString(R.string.setting_off); // Off 72 for (int i = 1; i < entries.length; i++) 73 mDurations[i] = String.format(locale, "%d", Integer.parseInt(entries[i].toString())); 74 int durationCount = mDurations.length; 75 mNumberSpinner = (NumberPicker) findViewById(R.id.duration); 76 mNumberSpinner.setMinValue(0); 77 mNumberSpinner.setMaxValue(durationCount - 1); 78 mNumberSpinner.setDisplayedValues(mDurations); 79 mNumberSpinner.setWrapSelectorWheel(false); 80 mNumberSpinner.setOnValueChangedListener(new OnValueChangeListener() { 81 @Override 82 public void onValueChange(NumberPicker picker, int oldValue, int newValue) { 83 setTimeSelectionEnabled(newValue != 0); 84 } 85 }); 86 mConfirmButton = (Button) findViewById(R.id.timer_set_button); 87 mPickerTitle = findViewById(R.id.set_time_interval_title); 88 89 // Disable focus on the spinners to prevent keyboard from coming up 90 mNumberSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 91 92 mConfirmButton.setOnClickListener(new View.OnClickListener() { 93 public void onClick(View v) { 94 updateInputState(); 95 } 96 }); 97 mTimerSound = (CheckBox) findViewById(R.id.sound_check_box); 98 mSoundTitle = findViewById(R.id.beep_title); 99 } 100 101 private void restoreSetting() { 102 int index = mTimer.findIndexOfValue(mTimer.getValue()); 103 if (index == -1) { 104 Log.e(TAG, "Invalid preference value."); 105 mTimer.print(); 106 throw new IllegalArgumentException(); 107 } else { 108 setTimeSelectionEnabled(index != 0); 109 mNumberSpinner.setValue(index); 110 } 111 boolean checked = mBeep.findIndexOfValue(mBeep.getValue()) != 0; 112 mTimerSound.setChecked(checked); 113 } 114 115 @Override 116 public void setVisibility(int visibility) { 117 if (visibility == View.VISIBLE) { 118 if (getVisibility() != View.VISIBLE) { 119 // Set the number pickers and on/off switch to be consistent 120 // with the preference 121 restoreSetting(); 122 } 123 } 124 super.setVisibility(visibility); 125 } 126 127 protected void setTimeSelectionEnabled(boolean enabled) { 128 mPickerTitle.setVisibility(enabled ? VISIBLE : INVISIBLE); 129 mTimerSound.setEnabled(enabled); 130 mSoundTitle.setEnabled(enabled); 131 } 132 133 @Override 134 public void reloadPreference() { 135 } 136 137 private void updateInputState() { 138 mTimer.setValueIndex(mNumberSpinner.getValue()); 139 mBeep.setValueIndex(mTimerSound.isChecked() ? 1 : 0); 140 if (mListener != null) { 141 mListener.onListPrefChanged(mTimer); 142 mListener.onListPrefChanged(mBeep); 143 } 144 } 145 } 146