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.contacts.common.preference; 18 19 import android.content.ContentResolver; 20 import android.content.Context; 21 import android.database.ContentObserver; 22 import android.os.Handler; 23 import android.provider.ContactsContract; 24 import android.provider.Settings; 25 import android.provider.Settings.SettingNotFoundException; 26 27 import com.android.contacts.common.R; 28 29 /** 30 * Manages user preferences for contacts. 31 */ 32 public final class ContactsPreferences extends ContentObserver { 33 34 public static final String PREF_DISPLAY_ONLY_PHONES = "only_phones"; 35 public static final boolean PREF_DISPLAY_ONLY_PHONES_DEFAULT = false; 36 37 private Context mContext; 38 private int mSortOrder = -1; 39 private int mDisplayOrder = -1; 40 private ChangeListener mListener = null; 41 private Handler mHandler; 42 43 public ContactsPreferences(Context context) { 44 super(null); 45 mContext = context; 46 mHandler = new Handler(); 47 } 48 49 public boolean isSortOrderUserChangeable() { 50 return mContext.getResources().getBoolean(R.bool.config_sort_order_user_changeable); 51 } 52 53 public int getDefaultSortOrder() { 54 if (mContext.getResources().getBoolean(R.bool.config_default_sort_order_primary)) { 55 return ContactsContract.Preferences.SORT_ORDER_PRIMARY; 56 } else { 57 return ContactsContract.Preferences.SORT_ORDER_ALTERNATIVE; 58 } 59 } 60 61 public int getSortOrder() { 62 if (!isSortOrderUserChangeable()) { 63 return getDefaultSortOrder(); 64 } 65 66 if (mSortOrder == -1) { 67 try { 68 mSortOrder = Settings.System.getInt(mContext.getContentResolver(), 69 ContactsContract.Preferences.SORT_ORDER); 70 } catch (SettingNotFoundException e) { 71 mSortOrder = getDefaultSortOrder(); 72 } 73 } 74 return mSortOrder; 75 } 76 77 public void setSortOrder(int sortOrder) { 78 mSortOrder = sortOrder; 79 Settings.System.putInt(mContext.getContentResolver(), 80 ContactsContract.Preferences.SORT_ORDER, sortOrder); 81 } 82 83 public boolean isDisplayOrderUserChangeable() { 84 return mContext.getResources().getBoolean(R.bool.config_display_order_user_changeable); 85 } 86 87 public int getDefaultDisplayOrder() { 88 if (mContext.getResources().getBoolean(R.bool.config_default_display_order_primary)) { 89 return ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY; 90 } else { 91 return ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE; 92 } 93 } 94 95 public int getDisplayOrder() { 96 if (!isDisplayOrderUserChangeable()) { 97 return getDefaultDisplayOrder(); 98 } 99 100 if (mDisplayOrder == -1) { 101 try { 102 mDisplayOrder = Settings.System.getInt(mContext.getContentResolver(), 103 ContactsContract.Preferences.DISPLAY_ORDER); 104 } catch (SettingNotFoundException e) { 105 mDisplayOrder = getDefaultDisplayOrder(); 106 } 107 } 108 return mDisplayOrder; 109 } 110 111 public void setDisplayOrder(int displayOrder) { 112 mDisplayOrder = displayOrder; 113 Settings.System.putInt(mContext.getContentResolver(), 114 ContactsContract.Preferences.DISPLAY_ORDER, displayOrder); 115 } 116 117 public void registerChangeListener(ChangeListener listener) { 118 if (mListener != null) unregisterChangeListener(); 119 120 mListener = listener; 121 122 // Reset preferences to "unknown" because they may have changed while the 123 // observer was unregistered. 124 mDisplayOrder = -1; 125 mSortOrder = -1; 126 127 final ContentResolver contentResolver = mContext.getContentResolver(); 128 contentResolver.registerContentObserver( 129 Settings.System.getUriFor( 130 ContactsContract.Preferences.SORT_ORDER), false, this); 131 contentResolver.registerContentObserver( 132 Settings.System.getUriFor( 133 ContactsContract.Preferences.DISPLAY_ORDER), false, this); 134 } 135 136 public void unregisterChangeListener() { 137 if (mListener != null) { 138 mContext.getContentResolver().unregisterContentObserver(this); 139 mListener = null; 140 } 141 } 142 143 @Override 144 public void onChange(boolean selfChange) { 145 // This notification is not sent on the Ui thread. Use the previously created Handler 146 // to switch to the Ui thread 147 mHandler.post(new Runnable() { 148 @Override 149 public void run() { 150 mSortOrder = -1; 151 mDisplayOrder = -1; 152 if (mListener != null) mListener.onChange(); 153 } 154 }); 155 } 156 157 public interface ChangeListener { 158 void onChange(); 159 } 160 } 161