1/*
2 * Copyright (C) 2009 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;
18
19import java.util.ArrayList;
20
21import android.content.Context;
22import android.util.AttributeSet;
23
24/**
25 * A collection of <code>CameraPreference</code>s. It may contain other
26 * <code>PreferenceGroup</code> and form a tree structure.
27 */
28public class PreferenceGroup extends CameraPreference {
29    private ArrayList<CameraPreference> list =
30            new ArrayList<CameraPreference>();
31
32    public PreferenceGroup(Context context, AttributeSet attrs) {
33        super(context, attrs);
34    }
35
36    public void addChild(CameraPreference child) {
37        list.add(child);
38    }
39
40    public void removePreference(int index) {
41        list.remove(index);
42    }
43
44    public CameraPreference get(int index) {
45        return list.get(index);
46    }
47
48    public int size() {
49        return list.size();
50    }
51
52    @Override
53    public void reloadValue() {
54        for (CameraPreference pref : list) {
55            pref.reloadValue();
56        }
57    }
58
59    /**
60     * Finds the preference with the given key recursively. Returns
61     * <code>null</code> if cannot find.
62     */
63    public ListPreference findPreference(String key) {
64        // Find a leaf preference with the given key. Currently, the base
65        // type of all "leaf" preference is "ListPreference". If we add some
66        // other types later, we need to change the code.
67        for (CameraPreference pref : list) {
68            if (pref instanceof ListPreference) {
69                ListPreference listPref = (ListPreference) pref;
70                if(listPref.getKey().equals(key)) return listPref;
71            } else if(pref instanceof PreferenceGroup) {
72                ListPreference listPref =
73                        ((PreferenceGroup) pref).findPreference(key);
74                if (listPref != null) return listPref;
75            }
76        }
77        return null;
78    }
79}
80