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
17package com.android.camera.ui;
18
19import com.android.camera.IconListPreference;
20import com.android.camera.R;
21
22import android.content.Context;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.view.View;
26import android.widget.AbsListView;
27import android.widget.AdapterView;
28import android.widget.SimpleAdapter;
29
30import java.util.ArrayList;
31import java.util.HashMap;
32
33// A popup window that shows one camera setting. The title is the name of the
34// setting (ex: white-balance). The entries are the supported values (ex:
35// daylight, incandescent, etc).
36public class BasicSettingPopup extends AbstractSettingPopup implements
37        AdapterView.OnItemClickListener {
38    private final String TAG = "BasicSettingPopup";
39    private IconListPreference mPreference;
40    private Listener mListener;
41
42    static public interface Listener {
43        public void onSettingChanged();
44    }
45
46    public BasicSettingPopup(Context context, AttributeSet attrs) {
47        super(context, attrs);
48    }
49
50    public void initialize(IconListPreference preference) {
51        mPreference = preference;
52        Context context = getContext();
53        CharSequence[] entries = mPreference.getEntries();
54        int[] iconIds = mPreference.getImageIds();
55        if (iconIds == null) {
56            iconIds = mPreference.getLargeIconIds();
57        }
58
59        // Set title.
60        mTitle.setText(mPreference.getTitle());
61
62        // Prepare the ListView.
63        ArrayList<HashMap<String, Object>> listItem =
64                new ArrayList<HashMap<String, Object>>();
65        for(int i = 0; i < entries.length; ++i) {
66            HashMap<String, Object> map = new HashMap<String, Object>();
67            map.put("text", entries[i].toString());
68            if (iconIds != null) map.put("image", iconIds[i]);
69            listItem.add(map);
70        }
71        SimpleAdapter listItemAdapter = new SimpleAdapter(context, listItem,
72                R.layout.setting_item,
73                new String[] {"text", "image"},
74                new int[] {R.id.text, R.id.image});
75        ((AbsListView) mSettingList).setAdapter(listItemAdapter);
76        ((AbsListView) mSettingList).setOnItemClickListener(this);
77        reloadPreference();
78    }
79
80    // The value of the preference may have changed. Update the UI.
81    @Override
82    public void reloadPreference() {
83        int index = mPreference.findIndexOfValue(mPreference.getValue());
84        if (index != -1) {
85            ((AbsListView) mSettingList).setItemChecked(index, true);
86        } else {
87            Log.e(TAG, "Invalid preference value.");
88            mPreference.print();
89        }
90    }
91
92    public void setSettingChangedListener(Listener listener) {
93        mListener = listener;
94    }
95
96    @Override
97    public void onItemClick(AdapterView<?> parent, View view,
98            int index, long id) {
99        mPreference.setValueIndex(index);
100        if (mListener != null) mListener.onSettingChanged();
101    }
102}
103