/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.camera.ui; import com.android.camera.ListPreference; import com.android.camera.PreferenceGroup; import com.android.camera.R; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* A popup window that contains several camera settings. */ public class OtherSettingsPopup extends AbstractSettingPopup implements InLineSettingPicker.Listener, AdapterView.OnItemClickListener { private static final String TAG = "OtherSettingsPopup"; private static final String ITEM_KEY = "key"; private static final String ITEM_TITLE = "text"; private static final String ITEM_VALUE = "value"; private static final String ITEM_RESTORE = "reset"; private Context mContext; private Listener mListener; private ArrayList> mListItem = new ArrayList>(); static public interface Listener { public void onSettingChanged(); public void onRestorePreferencesClicked(); } private class OtherSettingsAdapter extends SimpleAdapter { OtherSettingsAdapter(Context context, List> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView != null) return convertView; InLineSettingPicker view = (InLineSettingPicker) super.getView(position, convertView, parent); TextView restoreSettings = (TextView) view.findViewById(R.id.restore); View settingItem = view.findViewById(R.id.setting_item); // We apply the same View(InLineSettingPicker) as the listview's // components. To show the restore setting line, we control the // visibilities of components in InLineSettingPicker. boolean isRestoreItem = (position == mListItem.size() - 1); settingItem.setVisibility( isRestoreItem ? View.GONE : View.VISIBLE); restoreSettings.setVisibility( isRestoreItem ? View.VISIBLE : View.GONE); if (!isRestoreItem) { HashMap map = (HashMap) mListItem.get(position); ListPreference pref = (ListPreference) map.get(ITEM_KEY); view.initialize(pref); view.setSettingChangedListener(OtherSettingsPopup.this); } return view; } } public void setSettingChangedListener(Listener listener) { mListener = listener; } public OtherSettingsPopup(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; } public void initialize(PreferenceGroup group, String[] keys) { // Prepare the setting items. for (int i = 0; i < keys.length; ++i) { HashMap map = new HashMap(); ListPreference pref = group.findPreference(keys[i]); if (pref != null) { map.put(ITEM_KEY, pref); map.put(ITEM_TITLE, pref.getTitle()); map.put(ITEM_VALUE, pref.getEntry()); mListItem.add(map); } } // Prepare the restore setting line. HashMap map = new HashMap(); map.put(ITEM_RESTORE, mContext.getString(R.string.pref_restore_detail)); mListItem.add(map); SimpleAdapter mListItemAdapter = new OtherSettingsAdapter(mContext, mListItem, R.layout.in_line_setting_picker, new String[] {ITEM_TITLE, ITEM_VALUE, ITEM_RESTORE}, new int[] {R.id.title, R.id.current_setting, R.id.restore}); ((ListView) mSettingList).setAdapter(mListItemAdapter); ((ListView) mSettingList).setOnItemClickListener(this); ((ListView) mSettingList).setSelector(android.R.color.transparent); } @Override public void onSettingChanged() { if (mListener != null) { mListener.onSettingChanged(); } } // Scene mode can override other camera settings (ex: flash mode). public void overrideSettings(final String ... keyvalues) { int count = mSettingList.getChildCount(); for (int i = 0; i < keyvalues.length; i += 2) { String key = keyvalues[i]; String value = keyvalues[i + 1]; for (int j = 0; j < count; j++) { ListPreference pref = (ListPreference) mListItem.get(j).get(ITEM_KEY); if (pref != null && key.equals(pref.getKey())) { InLineSettingPicker picker = (InLineSettingPicker) mSettingList.getChildAt(j); picker.overrideSettings(value); } } } } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { if ((position == mListItem.size() - 1) && (mListener != null)) { mListener.onRestorePreferencesClicked(); } } @Override public void reloadPreference() { int count = mSettingList.getChildCount(); for (int i = 0; i < count; i++) { ListPreference pref = (ListPreference) mListItem.get(i).get(ITEM_KEY); if (pref != null) { InLineSettingPicker picker = (InLineSettingPicker) mSettingList.getChildAt(i); picker.reloadPreference(); } } } }