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