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.ui;
18
19import com.android.contacts.R;
20
21import android.content.ContentResolver;
22import android.content.Context;
23import android.database.ContentObserver;
24import android.provider.ContactsContract;
25import android.provider.Settings;
26import android.provider.Settings.SettingNotFoundException;
27
28/**
29 * Manages user preferences for contacts.
30 */
31public final class ContactsPreferences  {
32
33    private Context mContext;
34    private ContentResolver mContentResolver;
35    private int mSortOrder = -1;
36    private int mDisplayOrder = -1;
37    private SettingsObserver mSettingsObserver;
38
39    public ContactsPreferences(Context context) {
40        mContext = context;
41        mContentResolver = context.getContentResolver();
42
43        mSettingsObserver = new SettingsObserver();
44        mSettingsObserver.register();
45    }
46
47    public boolean isSortOrderUserChangeable() {
48        return mContext.getResources().getBoolean(R.bool.config_sort_order_user_changeable);
49    }
50
51    private int getDefaultSortOrder() {
52        if (mContext.getResources().getBoolean(R.bool.config_default_sort_order_primary)) {
53            return ContactsContract.Preferences.SORT_ORDER_PRIMARY;
54        } else {
55            return ContactsContract.Preferences.SORT_ORDER_ALTERNATIVE;
56        }
57    }
58
59    public int getSortOrder() {
60        if (!isSortOrderUserChangeable()) {
61            return getDefaultSortOrder();
62        }
63
64        if (mSortOrder == -1) {
65            try {
66                mSortOrder = Settings.System.getInt(mContext.getContentResolver(),
67                        ContactsContract.Preferences.SORT_ORDER);
68            } catch (SettingNotFoundException e) {
69                mSortOrder = getDefaultSortOrder();
70            }
71        }
72        return mSortOrder;
73    }
74
75    public void setSortOrder(int sortOrder) {
76        mSortOrder = sortOrder;
77        Settings.System.putInt(mContext.getContentResolver(),
78                ContactsContract.Preferences.SORT_ORDER, sortOrder);
79    }
80
81    public boolean isDisplayOrderUserChangeable() {
82        return mContext.getResources().getBoolean(R.bool.config_display_order_user_changeable);
83    }
84
85    private int getDefaultDisplayOrder() {
86        if (mContext.getResources().getBoolean(R.bool.config_default_display_order_primary)) {
87            return ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY;
88        } else {
89            return ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE;
90        }
91    }
92
93    public int getDisplayOrder() {
94        if (!isDisplayOrderUserChangeable()) {
95            return getDefaultDisplayOrder();
96        }
97
98        if (mDisplayOrder == -1) {
99            try {
100                mDisplayOrder = Settings.System.getInt(mContext.getContentResolver(),
101                        ContactsContract.Preferences.DISPLAY_ORDER);
102            } catch (SettingNotFoundException e) {
103                mDisplayOrder = getDefaultDisplayOrder();
104            }
105        }
106        return mDisplayOrder;
107    }
108
109    public void setDisplayOrder(int displayOrder) {
110        mDisplayOrder = displayOrder;
111        Settings.System.putInt(mContext.getContentResolver(),
112                ContactsContract.Preferences.DISPLAY_ORDER, displayOrder);
113    }
114
115    private class SettingsObserver extends ContentObserver {
116
117        public SettingsObserver() {
118            super(null);
119        }
120
121        public void register() {
122            mContentResolver.registerContentObserver(
123                    Settings.System.getUriFor(
124                            ContactsContract.Preferences.SORT_ORDER), false, this);
125            mContentResolver.registerContentObserver(
126                    Settings.System.getUriFor(
127                            ContactsContract.Preferences.DISPLAY_ORDER), false, this);
128        }
129
130        @Override
131        public void onChange(boolean selfChange) {
132            mSortOrder = -1;
133            mDisplayOrder = -1;
134
135            // TODO send a message to parent context to notify of the change
136        }
137    }
138}
139