Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2011 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 com.android.camera.ListPreference;
     20 import com.android.camera.R;
     21 
     22 import android.content.Context;
     23 import android.util.AttributeSet;
     24 import android.view.accessibility.AccessibilityEvent;
     25 import android.widget.LinearLayout;
     26 import android.widget.TextView;
     27 
     28 /**
     29  * A one-line camera setting could be one of three types: knob, switch or restore
     30  * preference button. The setting includes a title for showing the preference
     31  * title which is initialized in the SimpleAdapter. A knob also includes
     32  * (ex: Picture size), a previous button, the current value (ex: 5MP),
     33  * and a next button. A switch, i.e. the preference RecordLocationPreference,
     34  * has only two values on and off which will be controlled in a switch button.
     35  * Other setting popup window includes several InLineSettingItem items with
     36  * different types if possible.
     37  */
     38 public abstract class InLineSettingItem extends LinearLayout {
     39     private Listener mListener;
     40     protected ListPreference mPreference;
     41     protected int mIndex;
     42     // Scene mode can override the original preference value.
     43     protected String mOverrideValue;
     44 
     45     static public interface Listener {
     46         public void onSettingChanged();
     47     }
     48 
     49     public InLineSettingItem(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51     }
     52 
     53     protected void setTitle(ListPreference preference) {
     54         ((TextView) findViewById(R.id.title)).setText(preference.getTitle());
     55     }
     56 
     57     public void initialize(ListPreference preference) {
     58         setTitle(preference);
     59         if (preference == null) return;
     60         mPreference = preference;
     61         reloadPreference();
     62     }
     63 
     64     protected abstract void updateView();
     65 
     66     protected boolean changeIndex(int index) {
     67         if (index >= mPreference.getEntryValues().length || index < 0) return false;
     68         mIndex = index;
     69         mPreference.setValueIndex(mIndex);
     70         if (mListener != null) {
     71             mListener.onSettingChanged();
     72         }
     73         updateView();
     74         sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
     75         return true;
     76     }
     77 
     78     // The value of the preference may have changed. Update the UI.
     79     public void reloadPreference() {
     80         mIndex = mPreference.findIndexOfValue(mPreference.getValue());
     81         updateView();
     82     }
     83 
     84     public void setSettingChangedListener(Listener listener) {
     85         mListener = listener;
     86     }
     87 
     88     public void overrideSettings(String value) {
     89         mOverrideValue = value;
     90         updateView();
     91     }
     92 }
     93