Home | History | Annotate | Download | only in ui
      1 /*
      2  * Copyright (C) 2010 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 android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.util.Log;
     22 import android.view.View;
     23 import android.widget.ListView;
     24 import android.widget.AdapterView;
     25 import android.widget.ImageView;
     26 import android.widget.SimpleAdapter;
     27 
     28 import com.android.camera.IconListPreference;
     29 import com.android.camera.ListPreference;
     30 import com.android.camera.R;
     31 
     32 import java.util.ArrayList;
     33 import java.util.HashMap;
     34 import java.util.List;
     35 import java.util.Map;
     36 
     37 // A popup window that shows one camera setting. The title is the name of the
     38 // setting (ex: white-balance). The entries are the supported values (ex:
     39 // daylight, incandescent, etc). If initialized with an IconListPreference,
     40 // the entries will contain both text and icons. Otherwise, entries will be
     41 // shown in text.
     42 public class ListPrefSettingPopup extends AbstractSettingPopup implements
     43         AdapterView.OnItemClickListener {
     44     private static final String TAG = "ListPrefSettingPopup";
     45     private ListPreference mPreference;
     46     private Listener mListener;
     47 
     48     static public interface Listener {
     49         public void onListPrefChanged(ListPreference pref);
     50     }
     51 
     52     public ListPrefSettingPopup(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54     }
     55 
     56     private class ListPrefSettingAdapter extends SimpleAdapter {
     57         ListPrefSettingAdapter(Context context, List<? extends Map<String, ?>> data,
     58                 int resource, String[] from, int[] to) {
     59             super(context, data, resource, from, to);
     60         }
     61 
     62         @Override
     63         public void setViewImage(ImageView v, String value) {
     64             if ("".equals(value)) {
     65                 // Some settings have no icons. Ex: exposure compensation.
     66                 v.setVisibility(View.GONE);
     67             } else {
     68                 super.setViewImage(v, value);
     69             }
     70         }
     71     }
     72 
     73     public void initialize(ListPreference preference) {
     74         mPreference = preference;
     75         Context context = getContext();
     76         CharSequence[] entries = mPreference.getEntries();
     77         int[] iconIds = null;
     78         if (preference instanceof IconListPreference) {
     79             iconIds = ((IconListPreference) mPreference).getImageIds();
     80             if (iconIds == null) {
     81                 iconIds = ((IconListPreference) mPreference).getLargeIconIds();
     82             }
     83         }
     84         // Set title.
     85         mTitle.setText(mPreference.getTitle());
     86 
     87         // Prepare the ListView.
     88         ArrayList<HashMap<String, Object>> listItem =
     89                 new ArrayList<HashMap<String, Object>>();
     90         for(int i = 0; i < entries.length; ++i) {
     91             HashMap<String, Object> map = new HashMap<String, Object>();
     92             map.put("text", entries[i].toString());
     93             if (iconIds != null) map.put("image", iconIds[i]);
     94             listItem.add(map);
     95         }
     96         SimpleAdapter listItemAdapter = new ListPrefSettingAdapter(context, listItem,
     97                 R.layout.setting_item,
     98                 new String[] {"text", "image"},
     99                 new int[] {R.id.text, R.id.image});
    100         ((ListView) mSettingList).setAdapter(listItemAdapter);
    101         ((ListView) mSettingList).setOnItemClickListener(this);
    102         reloadPreference();
    103     }
    104 
    105     // The value of the preference may have changed. Update the UI.
    106     @Override
    107     public void reloadPreference() {
    108         int index = mPreference.findIndexOfValue(mPreference.getValue());
    109         if (index != -1) {
    110             ((ListView) mSettingList).setItemChecked(index, true);
    111         } else {
    112             Log.e(TAG, "Invalid preference value.");
    113             mPreference.print();
    114         }
    115     }
    116 
    117     public void setSettingChangedListener(Listener listener) {
    118         mListener = listener;
    119     }
    120 
    121     @Override
    122     public void onItemClick(AdapterView<?> parent, View view,
    123             int index, long id) {
    124         mPreference.setValueIndex(index);
    125         if (mListener != null) mListener.onListPrefChanged(mPreference);
    126     }
    127 }
    128