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 android.content.Context;
20
21import com.android.camera.IconListPreference;
22import com.android.camera.PreferenceGroup;
23import com.android.camera.R;
24import com.android.camera.Util;
25import com.android.camera.ui.GLListView.OnItemSelectedListener;
26
27public class BasicIndicator extends AbstractIndicator {
28
29    private final ResourceTexture mIcon[];
30    private final IconListPreference mPreference;
31    protected int mIndex;
32    private GLListView mPopupContent;
33    private PreferenceAdapter mModel;
34    private String mOverride;
35
36    public BasicIndicator(Context context,
37            PreferenceGroup group, IconListPreference preference) {
38        super(context);
39        mPreference = preference;
40        mIcon = new ResourceTexture[preference.getLargeIconIds().length];
41        mIndex = preference.findIndexOfValue(preference.getValue());
42    }
43
44    // Set the override and/or reload the value from preferences.
45    private void updateContent(String override, boolean reloadValue) {
46        if (!reloadValue && Util.equals(mOverride, override)) return;
47        IconListPreference pref = mPreference;
48        mOverride = override;
49        int index = pref.findIndexOfValue(
50                override == null ? pref.getValue() : override);
51        if (mIndex != index) {
52            mIndex = index;
53            invalidate();
54        }
55    }
56
57    @Override
58    public void overrideSettings(String key, String settings) {
59        IconListPreference pref = mPreference;
60        if (!pref.getKey().equals(key)) return;
61        updateContent(settings, false);
62    }
63
64    @Override
65    public void reloadPreferences() {
66        if (mModel != null) mModel.reload();
67        updateContent(null, true);
68    }
69
70    @Override
71    public GLView getPopupContent() {
72        if (mPopupContent == null) {
73            Context context = getGLRootView().getContext();
74            mPopupContent = new GLListView(context);
75            mPopupContent.setHighLight(new NinePatchTexture(
76                    context, R.drawable.optionitem_highlight));
77            mPopupContent.setScroller(new NinePatchTexture(
78                    context, R.drawable.scrollbar_handle_vertical));
79            mModel = new PreferenceAdapter(context, mPreference);
80            mPopupContent.setOnItemSelectedListener(new MyListener(mModel));
81            mPopupContent.setDataModel(mModel);
82        }
83        mModel.overrideSettings(mOverride);
84        return mPopupContent;
85    }
86
87    protected void onPreferenceChanged(int newIndex) {
88        if (newIndex == mIndex) return;
89        mIndex = newIndex;
90        invalidate();
91    }
92
93    private class MyListener implements OnItemSelectedListener {
94
95        private final PreferenceAdapter mAdapter;
96
97        public MyListener(PreferenceAdapter adapter) {
98            mAdapter = adapter;
99        }
100
101        public void onItemSelected(GLView view, int position) {
102            mAdapter.onItemSelected(view, position);
103            onPreferenceChanged(position - 1);
104        }
105    }
106
107    @Override
108    protected ResourceTexture getIcon() {
109        int index = mIndex;
110        if (mIcon[index] == null) {
111            Context context = getGLRootView().getContext();
112            mIcon[index] = new ResourceTexture(
113                    context, mPreference.getLargeIconIds()[index]);
114        }
115        return mIcon[index];
116    }
117}
118