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