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 package com.android.camera.ui; 17 18 import android.content.Context; 19 import android.content.res.Resources; 20 import android.util.AttributeSet; 21 import android.util.Log; 22 import android.view.View; 23 import android.widget.Button; 24 import android.widget.CompoundButton; 25 import android.widget.NumberPicker; 26 import android.widget.Switch; 27 import android.widget.TextView; 28 29 import com.android.camera.IconListPreference; 30 import com.android.camera.ListPreference; 31 import com.android.camera.R; 32 33 /** 34 * This is a popup window that allows users to turn on/off time lapse feature, 35 * and to select a time interval for taking a time lapse video. 36 */ 37 public class TimeIntervalPopup extends AbstractSettingPopup { 38 private static final String TAG = "TimeIntervalPopup"; 39 private NumberPicker mNumberSpinner; 40 private NumberPicker mUnitSpinner; 41 private Switch mTimeLapseSwitch; 42 private final String[] mUnits; 43 private final String[] mDurations; 44 private IconListPreference mPreference; 45 private Listener mListener; 46 private Button mConfirmButton; 47 private TextView mHelpText; 48 private View mTimePicker; 49 50 static public interface Listener { 51 public void onListPrefChanged(ListPreference pref); 52 } 53 54 public void setSettingChangedListener(Listener listener) { 55 mListener = listener; 56 } 57 58 public TimeIntervalPopup(Context context, AttributeSet attrs) { 59 super(context, attrs); 60 61 Resources res = context.getResources(); 62 mUnits = res.getStringArray(R.array.pref_video_time_lapse_frame_interval_units); 63 mDurations = res 64 .getStringArray(R.array.pref_video_time_lapse_frame_interval_duration_values); 65 } 66 67 public void initialize(IconListPreference preference) { 68 mPreference = preference; 69 70 // Set title. 71 mTitle.setText(mPreference.getTitle()); 72 73 // Duration 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 81 // Units for duration (i.e. seconds, minutes, etc) 82 mUnitSpinner = (NumberPicker) findViewById(R.id.duration_unit); 83 mUnitSpinner.setMinValue(0); 84 mUnitSpinner.setMaxValue(mUnits.length - 1); 85 mUnitSpinner.setDisplayedValues(mUnits); 86 mUnitSpinner.setWrapSelectorWheel(false); 87 88 mTimePicker = findViewById(R.id.time_interval_picker); 89 mTimeLapseSwitch = (Switch) findViewById(R.id.time_lapse_switch); 90 mHelpText = (TextView) findViewById(R.id.set_time_interval_help_text); 91 mConfirmButton = (Button) findViewById(R.id.time_lapse_interval_set_button); 92 93 // Disable focus on the spinners to prevent keyboard from coming up 94 mNumberSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 95 mUnitSpinner.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 96 97 mTimeLapseSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 98 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 99 setTimeSelectionEnabled(isChecked); 100 } 101 }); 102 mConfirmButton.setOnClickListener(new View.OnClickListener() { 103 public void onClick(View v) { 104 updateInputState(); 105 } 106 }); 107 } 108 109 private void restoreSetting() { 110 int index = mPreference.findIndexOfValue(mPreference.getValue()); 111 if (index == -1) { 112 Log.e(TAG, "Invalid preference value."); 113 mPreference.print(); 114 throw new IllegalArgumentException(); 115 } else if (index == 0) { 116 // default choice: time lapse off 117 mTimeLapseSwitch.setChecked(false); 118 setTimeSelectionEnabled(false); 119 } else { 120 mTimeLapseSwitch.setChecked(true); 121 setTimeSelectionEnabled(true); 122 int durationCount = mNumberSpinner.getMaxValue() + 1; 123 int unit = (index - 1) / durationCount; 124 int number = (index - 1) % durationCount; 125 mUnitSpinner.setValue(unit); 126 mNumberSpinner.setValue(number); 127 } 128 } 129 130 @Override 131 public void setVisibility(int visibility) { 132 if (visibility == View.VISIBLE) { 133 if (getVisibility() != View.VISIBLE) { 134 // Set the number pickers and on/off switch to be consistent 135 // with the preference 136 restoreSetting(); 137 } 138 } 139 super.setVisibility(visibility); 140 } 141 142 protected void setTimeSelectionEnabled(boolean enabled) { 143 mHelpText.setVisibility(enabled ? GONE : VISIBLE); 144 mTimePicker.setVisibility(enabled ? VISIBLE : GONE); 145 } 146 147 @Override 148 public void reloadPreference() { 149 } 150 151 private void updateInputState() { 152 if (mTimeLapseSwitch.isChecked()) { 153 int newId = mUnitSpinner.getValue() * (mNumberSpinner.getMaxValue() + 1) 154 + mNumberSpinner.getValue() + 1; 155 mPreference.setValueIndex(newId); 156 } else { 157 mPreference.setValueIndex(0); 158 } 159 160 if (mListener != null) { 161 mListener.onListPrefChanged(mPreference); 162 } 163 } 164 } 165