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.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.AdapterView; 25 import android.widget.ArrayAdapter; 26 import android.widget.ListView; 27 28 import com.android.camera.CameraSettings; 29 import com.android.camera.ListPreference; 30 import com.android.camera.PreferenceGroup; 31 import com.android.camera.R; 32 33 import java.util.ArrayList; 34 35 /* A popup window that contains several camera settings. */ 36 public class OtherSettingsPopup extends AbstractSettingPopup 37 implements InLineSettingItem.Listener, 38 AdapterView.OnItemClickListener { 39 @SuppressWarnings("unused") 40 private static final String TAG = "OtherSettingsPopup"; 41 42 private Listener mListener; 43 private ArrayList<ListPreference> mListItem = new ArrayList<ListPreference>(); 44 45 static public interface Listener { 46 public void onSettingChanged(); 47 public void onRestorePreferencesClicked(); 48 } 49 50 private class OtherSettingsAdapter extends ArrayAdapter<ListPreference> { 51 LayoutInflater mInflater; 52 53 OtherSettingsAdapter() { 54 super(OtherSettingsPopup.this.getContext(), 0, mListItem); 55 mInflater = LayoutInflater.from(getContext()); 56 } 57 58 private int getSettingLayoutId(ListPreference pref) { 59 // If the preference is null, it will be the only item , i.e. 60 // 'Restore setting' in the popup window. 61 if (pref == null) return R.layout.in_line_setting_restore; 62 63 // Currently, the RecordLocationPreference is the only setting 64 // which applies the on/off switch. 65 if (CameraSettings.KEY_RECORD_LOCATION.equals(pref.getKey())) { 66 return R.layout.in_line_setting_switch; 67 } 68 return R.layout.in_line_setting_knob; 69 } 70 71 @Override 72 public View getView(int position, View convertView, ViewGroup parent) { 73 if (convertView != null) return convertView; 74 75 ListPreference pref = mListItem.get(position); 76 77 int viewLayoutId = getSettingLayoutId(pref); 78 InLineSettingItem view = (InLineSettingItem) 79 mInflater.inflate(viewLayoutId, parent, false); 80 if (viewLayoutId == R.layout.in_line_setting_restore) { 81 view.setId(R.id.restore_default); 82 } 83 84 view.initialize(pref); // no init for restore one 85 view.setSettingChangedListener(OtherSettingsPopup.this); 86 return view; 87 } 88 } 89 90 public void setSettingChangedListener(Listener listener) { 91 mListener = listener; 92 } 93 94 public OtherSettingsPopup(Context context, AttributeSet attrs) { 95 super(context, attrs); 96 } 97 98 public void initialize(PreferenceGroup group, String[] keys) { 99 // Prepare the setting items. 100 for (int i = 0; i < keys.length; ++i) { 101 ListPreference pref = group.findPreference(keys[i]); 102 if (pref != null) mListItem.add(pref); 103 } 104 105 // Prepare the restore setting line. 106 mListItem.add(null); 107 108 ArrayAdapter<ListPreference> mListItemAdapter = new OtherSettingsAdapter(); 109 ((ListView) mSettingList).setAdapter(mListItemAdapter); 110 ((ListView) mSettingList).setOnItemClickListener(this); 111 ((ListView) mSettingList).setSelector(android.R.color.transparent); 112 } 113 114 @Override 115 public void onSettingChanged() { 116 if (mListener != null) { 117 mListener.onSettingChanged(); 118 } 119 } 120 121 // Scene mode can override other camera settings (ex: flash mode). 122 public void overrideSettings(final String ... keyvalues) { 123 int count = mSettingList.getChildCount(); 124 for (int i = 0; i < keyvalues.length; i += 2) { 125 String key = keyvalues[i]; 126 String value = keyvalues[i + 1]; 127 for (int j = 0; j < count; j++) { 128 ListPreference pref = mListItem.get(j); 129 if (pref != null && key.equals(pref.getKey())) { 130 InLineSettingItem settingItem = 131 (InLineSettingItem) mSettingList.getChildAt(j); 132 settingItem.overrideSettings(value); 133 } 134 } 135 } 136 } 137 138 @Override 139 public void onItemClick(AdapterView<?> parent, View view, int position, 140 long id) { 141 if ((position == mListItem.size() - 1) && (mListener != null)) { 142 mListener.onRestorePreferencesClicked(); 143 } 144 } 145 146 @Override 147 public void reloadPreference() { 148 int count = mSettingList.getChildCount(); 149 for (int i = 0; i < count; i++) { 150 ListPreference pref = mListItem.get(i); 151 if (pref != null) { 152 InLineSettingItem settingItem = 153 (InLineSettingItem) mSettingList.getChildAt(i); 154 settingItem.reloadPreference(); 155 } 156 } 157 } 158 } 159