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.contacts.common.preference;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.os.Bundle;
22import android.preference.Preference;
23import android.preference.PreferenceFragment;
24
25import com.android.contacts.common.R;
26import com.android.contacts.common.model.AccountTypeManager;
27import com.android.contacts.common.model.account.AccountWithDataSet;
28import com.android.contacts.commonbind.ObjectFactory;
29
30import java.util.List;
31
32/**
33 * This fragment shows the preferences for "display options"
34 */
35public class DisplayOptionsPreferenceFragment extends PreferenceFragment {
36
37    @Override
38    public void onCreate(Bundle savedInstanceState) {
39        super.onCreate(savedInstanceState);
40
41        // Load the preferences from an XML resource
42        addPreferencesFromResource(R.xml.preference_display_options);
43
44        removeUnsupportedPreferences();
45        addExtraPreferences();
46
47        final Preference aboutPreference = findPreference("about");
48        aboutPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
49            @Override
50            public boolean onPreferenceClick(Preference preference) {
51                ((ContactsPreferenceActivity) getActivity()).showAboutFragment();
52                return true;
53            }
54        });
55    }
56
57    private void removeUnsupportedPreferences() {
58        // Disable sort order for CJK locales where it is not supported
59        final Resources resources = getResources();
60        if (!resources.getBoolean(R.bool.config_sort_order_user_changeable)) {
61            getPreferenceScreen().removePreference(findPreference("sortOrder"));
62        }
63
64        // Disable display order for CJK locales as well
65        if (!resources.getBoolean(R.bool.config_display_order_user_changeable)) {
66            getPreferenceScreen().removePreference(findPreference("displayOrder"));
67        }
68
69        // Remove the "Default account" setting if there aren't any writable accounts
70        final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(getContext());
71        final List<AccountWithDataSet> accounts = accountTypeManager.getAccounts(
72                /* contactWritableOnly */ true);
73        if (accounts.isEmpty()) {
74            getPreferenceScreen().removePreference(findPreference("accounts"));
75        }
76    }
77
78    private void addExtraPreferences() {
79        final PreferenceManager preferenceManager = ObjectFactory.getPreferenceManager(
80                getContext());
81        if (preferenceManager != null) {
82            for (Preference preference : preferenceManager.getPreferences()) {
83                getPreferenceScreen().addPreference(preference);
84            }
85        }
86    }
87
88    @Override
89    public Context getContext() {
90        return getActivity();
91    }
92}
93