blob: 69abf0594d88262f62597130ff2ca63b68bdde9b [file] [log] [blame]
Yorke Leeb3d841a2014-07-10 11:38:55 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.preference;
Yorke Leeb3d841a2014-07-10 11:38:55 -070018
Wenyi Wang13874012016-05-27 15:23:53 -070019import android.app.Activity;
Wenyi Wanga0313442016-05-16 11:55:19 -070020import android.app.LoaderManager;
Marcus Hagerott819214d2016-09-29 14:58:27 -070021import android.content.BroadcastReceiver;
Wenyi Wanga0313442016-05-16 11:55:19 -070022import android.content.ContentUris;
Wenyi Wang9342fbb2015-11-17 19:36:35 -080023import android.content.Context;
Wenyi Wanga0313442016-05-16 11:55:19 -070024import android.content.CursorLoader;
25import android.content.Intent;
Marcus Hagerott819214d2016-09-29 14:58:27 -070026import android.content.IntentFilter;
Wenyi Wanga0313442016-05-16 11:55:19 -070027import android.content.Loader;
Walter Jang5665f362016-01-20 21:44:10 +000028import android.content.res.Resources;
Wenyi Wanga0313442016-05-16 11:55:19 -070029import android.database.Cursor;
30import android.net.Uri;
Yorke Leeb3d841a2014-07-10 11:38:55 -070031import android.os.Bundle;
Tingting Wangdebe2da2015-11-01 20:19:03 -080032import android.preference.Preference;
Brian Attwellc2e912c2014-10-27 12:29:44 -070033import android.preference.PreferenceFragment;
Wenyi Wangae5c3a02016-06-27 12:43:31 -070034import android.provider.BlockedNumberContract;
Wenyi Wanga0313442016-05-16 11:55:19 -070035import android.provider.ContactsContract.Contacts;
taiki tsutsumiae4208e2017-07-21 16:57:32 +090036import android.provider.ContactsContract.DisplayNameSources;
Wenyi Wanga0313442016-05-16 11:55:19 -070037import android.provider.ContactsContract.Profile;
Aurimas Liutikas1f8811c2019-01-30 21:39:48 -080038import com.google.android.material.snackbar.Snackbar;
Aravind Sreekumar71212852018-04-06 15:47:45 -070039import androidx.localbroadcastmanager.content.LocalBroadcastManager;
Tingting Wangf5b12582016-06-30 18:27:22 -070040import android.telecom.TelecomManager;
41import android.telephony.TelephonyManager;
taiki tsutsumiae4208e2017-07-21 16:57:32 +090042import android.text.BidiFormatter;
43import android.text.TextDirectionHeuristics;
Marcus Hagerott819214d2016-09-29 14:58:27 -070044import android.view.LayoutInflater;
45import android.view.View;
46import android.view.ViewGroup;
47import android.widget.FrameLayout;
Yorke Leeb3d841a2014-07-10 11:38:55 -070048
Gary Mai0a49afa2016-12-05 15:53:58 -080049import com.android.contacts.ContactsUtils;
Marcus Hagerott819214d2016-09-29 14:58:27 -070050import com.android.contacts.R;
Marcus Hagerott95246bb2016-11-11 10:56:09 -080051import com.android.contacts.SimImportService;
Gary Mai69c182a2016-12-05 13:07:03 -080052import com.android.contacts.compat.TelecomManagerUtil;
53import com.android.contacts.compat.TelephonyManagerCompat;
54import com.android.contacts.interactions.ExportDialogFragment;
55import com.android.contacts.interactions.ImportDialogFragment;
56import com.android.contacts.list.ContactListFilter;
57import com.android.contacts.list.ContactListFilterController;
58import com.android.contacts.logging.ScreenEvent.ScreenType;
59import com.android.contacts.model.AccountTypeManager;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080060import com.android.contacts.model.account.AccountInfo;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080061import com.android.contacts.model.account.AccountsLoader;
Gary Mai69c182a2016-12-05 13:07:03 -080062import com.android.contacts.util.AccountFilterUtil;
63import com.android.contacts.util.ImplicitIntentsUtil;
James Laskeyff9a19d2016-11-15 13:36:34 -080064import com.android.contactsbind.HelpUtils;
Tingting Wangc939ae32015-09-01 16:45:08 -070065
66import java.util.List;
Yorke Leeb3d841a2014-07-10 11:38:55 -070067
68/**
Wenyi Wangcc3016e2015-12-30 16:54:05 -080069 * This fragment shows the preferences for "display options"
Yorke Leeb3d841a2014-07-10 11:38:55 -070070 */
Wenyi Wang13874012016-05-27 15:23:53 -070071public class DisplayOptionsPreferenceFragment extends PreferenceFragment
Marcus Hagerott8c6b5bd2016-12-21 17:14:52 -080072 implements Preference.OnPreferenceClickListener, AccountsLoader.AccountsListener {
Yorke Leeb3d841a2014-07-10 11:38:55 -070073
Walter Jangf2cad222016-07-14 19:49:06 +000074 private static final int REQUEST_CODE_CUSTOM_CONTACTS_FILTER = 0;
75
Wenyi Wang13874012016-05-27 15:23:53 -070076 private static final String ARG_CONTACTS_AVAILABLE = "are_contacts_available";
Wenyi Wang13874012016-05-27 15:23:53 -070077 private static final String ARG_NEW_LOCAL_PROFILE = "new_local_profile";
Wenyi Wanga0313442016-05-16 11:55:19 -070078
79 private static final String KEY_ABOUT = "about";
80 private static final String KEY_ACCOUNTS = "accounts";
Tingting Wang940f0152016-06-30 13:15:49 -070081 private static final String KEY_DEFAULT_ACCOUNT = "defaultAccount";
Tingting Wangf5b12582016-06-30 18:27:22 -070082 private static final String KEY_BLOCKED_NUMBERS = "blockedNumbers";
Wenyi Wanga0313442016-05-16 11:55:19 -070083 private static final String KEY_DISPLAY_ORDER = "displayOrder";
Walter Jangf2cad222016-07-14 19:49:06 +000084 private static final String KEY_CUSTOM_CONTACTS_FILTER = "customContactsFilter";
James Laskey1e2102f2016-09-19 10:09:08 -070085 private static final String KEY_IMPORT = "import";
86 private static final String KEY_EXPORT = "export";
Wenyi Wanga0313442016-05-16 11:55:19 -070087 private static final String KEY_MY_INFO = "myInfo";
88 private static final String KEY_SORT_ORDER = "sortOrder";
avipul030b1262017-02-28 12:14:44 -080089 private static final String KEY_PHONETIC_NAME_DISPLAY = "phoneticNameDisplay";
Wenyi Wanga0313442016-05-16 11:55:19 -070090
91 private static final int LOADER_PROFILE = 0;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080092 private static final int LOADER_ACCOUNTS = 1;
Wenyi Wanga0313442016-05-16 11:55:19 -070093
94 /**
95 * Callbacks for hosts of the {@link DisplayOptionsPreferenceFragment}.
96 */
97 public interface ProfileListener {
98 /**
99 * Invoked after profile has been loaded.
100 */
101 void onProfileLoaded(Cursor data);
102 }
103
104 /**
105 * The projections that are used to obtain user profile
106 */
107 public static class ProfileQuery {
108 /**
109 * Not instantiable.
110 */
111 private ProfileQuery() {}
112
113 private static final String[] PROFILE_PROJECTION_PRIMARY = new String[] {
114 Contacts._ID, // 0
115 Contacts.DISPLAY_NAME_PRIMARY, // 1
116 Contacts.IS_USER_PROFILE, // 2
taiki tsutsumiae4208e2017-07-21 16:57:32 +0900117 Contacts.DISPLAY_NAME_SOURCE, // 3
Wenyi Wanga0313442016-05-16 11:55:19 -0700118 };
119
120 private static final String[] PROFILE_PROJECTION_ALTERNATIVE = new String[] {
121 Contacts._ID, // 0
122 Contacts.DISPLAY_NAME_ALTERNATIVE, // 1
123 Contacts.IS_USER_PROFILE, // 2
taiki tsutsumiae4208e2017-07-21 16:57:32 +0900124 Contacts.DISPLAY_NAME_SOURCE, // 3
Wenyi Wanga0313442016-05-16 11:55:19 -0700125 };
126
127 public static final int CONTACT_ID = 0;
128 public static final int CONTACT_DISPLAY_NAME = 1;
129 public static final int CONTACT_IS_USER_PROFILE = 2;
taiki tsutsumiae4208e2017-07-21 16:57:32 +0900130 public static final int DISPLAY_NAME_SOURCE = 3;
Wenyi Wanga0313442016-05-16 11:55:19 -0700131 }
132
133 private String mNewLocalProfileExtra;
Wenyi Wang13874012016-05-27 15:23:53 -0700134 private boolean mAreContactsAvailable;
135
136 private boolean mHasProfile;
137 private long mProfileContactId;
Wenyi Wanga0313442016-05-16 11:55:19 -0700138
139 private Preference mMyInfoPreference;
140
141 private ProfileListener mListener;
142
Marcus Hagerott819214d2016-09-29 14:58:27 -0700143 private ViewGroup mRootView;
144 private SaveServiceResultListener mSaveServiceListener;
145
Wenyi Wanga0313442016-05-16 11:55:19 -0700146 private final LoaderManager.LoaderCallbacks<Cursor> mProfileLoaderListener =
147 new LoaderManager.LoaderCallbacks<Cursor>() {
148
149 @Override
150 public CursorLoader onCreateLoader(int id, Bundle args) {
151 final CursorLoader loader = createCursorLoader(getContext());
152 loader.setUri(Profile.CONTENT_URI);
153 loader.setProjection(getProjection(getContext()));
154 return loader;
155 }
156
157 @Override
158 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
159 if (mListener != null) {
160 mListener.onProfileLoaded(data);
161 }
162 }
163
164 public void onLoaderReset(Loader<Cursor> loader) {
165 }
166 };
167
168 public static DisplayOptionsPreferenceFragment newInstance(String newLocalProfileExtra,
Walter Jang0396cf72016-09-21 13:23:29 -0700169 boolean areContactsAvailable) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700170 final DisplayOptionsPreferenceFragment fragment = new DisplayOptionsPreferenceFragment();
171 final Bundle args = new Bundle();
172 args.putString(ARG_NEW_LOCAL_PROFILE, newLocalProfileExtra);
Wenyi Wang13874012016-05-27 15:23:53 -0700173 args.putBoolean(ARG_CONTACTS_AVAILABLE, areContactsAvailable);
Wenyi Wanga0313442016-05-16 11:55:19 -0700174 fragment.setArguments(args);
175 return fragment;
176 }
177
Yorke Leeb3d841a2014-07-10 11:38:55 -0700178 @Override
Wenyi Wang13874012016-05-27 15:23:53 -0700179 public void onAttach(Activity activity) {
180 super.onAttach(activity);
181 try {
182 mListener = (ProfileListener) activity;
183 } catch (ClassCastException e) {
184 throw new ClassCastException(activity.toString() + " must implement ProfileListener");
185 }
186 }
187
188 @Override
Marcus Hagerott819214d2016-09-29 14:58:27 -0700189 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
190 // Wrap the preference view in a FrameLayout so we can show a snackbar
191 mRootView = new FrameLayout(getActivity());
192 final View list = super.onCreateView(inflater, mRootView, savedInstanceState);
193 mRootView.addView(list);
194 return mRootView;
195 }
196
197 @Override
198 public void onViewCreated(View view, Bundle savedInstanceState) {
199 super.onViewCreated(view, savedInstanceState);
200
201 mSaveServiceListener = new SaveServiceResultListener();
202 LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
203 mSaveServiceListener,
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800204 new IntentFilter(SimImportService.BROADCAST_SIM_IMPORT_COMPLETE));
Marcus Hagerott819214d2016-09-29 14:58:27 -0700205 }
206
207 @Override
Yorke Leeb3d841a2014-07-10 11:38:55 -0700208 public void onCreate(Bundle savedInstanceState) {
209 super.onCreate(savedInstanceState);
210
211 // Load the preferences from an XML resource
212 addPreferencesFromResource(R.xml.preference_display_options);
Tingting Wangc939ae32015-09-01 16:45:08 -0700213
Wenyi Wanga0313442016-05-16 11:55:19 -0700214 final Bundle args = getArguments();
215 mNewLocalProfileExtra = args.getString(ARG_NEW_LOCAL_PROFILE);
Wenyi Wang13874012016-05-27 15:23:53 -0700216 mAreContactsAvailable = args.getBoolean(ARG_CONTACTS_AVAILABLE);
Wenyi Wanga0313442016-05-16 11:55:19 -0700217
James Laskey1e2102f2016-09-19 10:09:08 -0700218 removeUnsupportedPreferences();
James Laskey1e2102f2016-09-19 10:09:08 -0700219
Wenyi Wanga0313442016-05-16 11:55:19 -0700220 mMyInfoPreference = findPreference(KEY_MY_INFO);
Tingting Wang940f0152016-06-30 13:15:49 -0700221
222 final Preference accountsPreference = findPreference(KEY_ACCOUNTS);
223 accountsPreference.setOnPreferenceClickListener(this);
224
James Laskey1e2102f2016-09-19 10:09:08 -0700225 final Preference importPreference = findPreference(KEY_IMPORT);
226 importPreference.setOnPreferenceClickListener(this);
227
228 final Preference exportPreference = findPreference(KEY_EXPORT);
229 if (exportPreference != null) {
230 exportPreference.setOnPreferenceClickListener(this);
231 }
Wenyi Wanga0313442016-05-16 11:55:19 -0700232
Tingting Wangf5b12582016-06-30 18:27:22 -0700233 final Preference blockedNumbersPreference = findPreference(KEY_BLOCKED_NUMBERS);
234 if (blockedNumbersPreference != null) {
235 blockedNumbersPreference.setOnPreferenceClickListener(this);
236 }
237
Wenyi Wanga0313442016-05-16 11:55:19 -0700238 final Preference aboutPreference = findPreference(KEY_ABOUT);
James Laskeyff9a19d2016-11-15 13:36:34 -0800239 if (aboutPreference != null) {
240 aboutPreference.setOnPreferenceClickListener(this);
241 }
Walter Jangf2cad222016-07-14 19:49:06 +0000242
243 final Preference customFilterPreference = findPreference(KEY_CUSTOM_CONTACTS_FILTER);
244 if (customFilterPreference != null) {
245 customFilterPreference.setOnPreferenceClickListener(this);
Walter Jang362b7332016-07-18 18:26:07 -0700246 setCustomContactsFilterSummary();
Walter Jangf2cad222016-07-14 19:49:06 +0000247 }
Yorke Leeb3d841a2014-07-10 11:38:55 -0700248 }
Wenyi Wang9342fbb2015-11-17 19:36:35 -0800249
Wenyi Wanga0313442016-05-16 11:55:19 -0700250 @Override
251 public void onActivityCreated(Bundle savedInstanceState) {
252 super.onActivityCreated(savedInstanceState);
Marcus Hagerottdc91dd62016-12-21 16:57:32 -0800253 getLoaderManager().initLoader(LOADER_PROFILE, null, mProfileLoaderListener);
Marcus Hagerott8c6b5bd2016-12-21 17:14:52 -0800254 AccountsLoader.loadAccounts(this, LOADER_ACCOUNTS, AccountTypeManager.writableFilter());
Wenyi Wanga0313442016-05-16 11:55:19 -0700255 }
256
Marcus Hagerott819214d2016-09-29 14:58:27 -0700257 @Override
258 public void onDestroyView() {
259 super.onDestroyView();
260 LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mSaveServiceListener);
261 mRootView = null;
262 }
263
taiki tsutsumiae4208e2017-07-21 16:57:32 +0900264 public void updateMyInfoPreference(boolean hasProfile, String displayName, long contactId,
265 int displayNameSource) {
266 final CharSequence summary = !hasProfile ?
267 getString(R.string.set_up_profile) :
268 displayNameSource == DisplayNameSources.PHONE ?
269 BidiFormatter.getInstance().unicodeWrap(displayName, TextDirectionHeuristics.LTR) :
270 displayName;
Wenyi Wanga0313442016-05-16 11:55:19 -0700271 mMyInfoPreference.setSummary(summary);
Wenyi Wang13874012016-05-27 15:23:53 -0700272 mHasProfile = hasProfile;
273 mProfileContactId = contactId;
274 mMyInfoPreference.setOnPreferenceClickListener(this);
Wenyi Wanga0313442016-05-16 11:55:19 -0700275 }
276
Walter Jang5665f362016-01-20 21:44:10 +0000277 private void removeUnsupportedPreferences() {
Wenyi Wangb297ad02016-04-17 12:53:46 -0700278 // Disable sort order for CJK locales where it is not supported
Wenyi Wang2882a5a2016-05-13 19:54:45 +0000279 final Resources resources = getResources();
Walter Jang5665f362016-01-20 21:44:10 +0000280 if (!resources.getBoolean(R.bool.config_sort_order_user_changeable)) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700281 getPreferenceScreen().removePreference(findPreference(KEY_SORT_ORDER));
Walter Jang5665f362016-01-20 21:44:10 +0000282 }
283
avipul030b1262017-02-28 12:14:44 -0800284 if (!resources.getBoolean(R.bool.config_phonetic_name_display_user_changeable)) {
285 getPreferenceScreen().removePreference(findPreference(KEY_PHONETIC_NAME_DISPLAY));
286 }
287
James Laskeyff9a19d2016-11-15 13:36:34 -0800288 if (HelpUtils.isHelpAndFeedbackAvailable()) {
289 getPreferenceScreen().removePreference(findPreference(KEY_ABOUT));
290 }
291
Walter Jang5665f362016-01-20 21:44:10 +0000292 // Disable display order for CJK locales as well
293 if (!resources.getBoolean(R.bool.config_display_order_user_changeable)) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700294 getPreferenceScreen().removePreference(findPreference(KEY_DISPLAY_ORDER));
Walter Jang5665f362016-01-20 21:44:10 +0000295 }
296
Tingting Wangf5b12582016-06-30 18:27:22 -0700297 final boolean isPhone = TelephonyManagerCompat.isVoiceCapable(
298 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE));
299 final boolean showBlockedNumbers = isPhone && ContactsUtils.FLAG_N_FEATURE
Wenyi Wangae5c3a02016-06-27 12:43:31 -0700300 && BlockedNumberContract.canCurrentUserBlockNumbers(getContext());
Tingting Wangf5b12582016-06-30 18:27:22 -0700301 if (!showBlockedNumbers) {
302 getPreferenceScreen().removePreference(findPreference(KEY_BLOCKED_NUMBERS));
303 }
James Laskey1e2102f2016-09-19 10:09:08 -0700304
305 if (!mAreContactsAvailable) {
306 getPreferenceScreen().removePreference(findPreference(KEY_EXPORT));
307 }
Tingting Wang918f0b22016-02-09 15:06:24 -0800308 }
Walter Jang5665f362016-01-20 21:44:10 +0000309
Marcus Hagerott8c6b5bd2016-12-21 17:14:52 -0800310 @Override
311 public void onAccountsLoaded(List<AccountInfo> accounts) {
312 // Hide accounts preferences if no writable accounts exist
Marcus Hagerott614d8122017-01-04 13:56:10 -0800313 final DefaultAccountPreference preference =
314 (DefaultAccountPreference) findPreference(KEY_DEFAULT_ACCOUNT);
315 preference.setAccounts(accounts);
Marcus Hagerottdc91dd62016-12-21 16:57:32 -0800316 }
317
Wenyi Wang9342fbb2015-11-17 19:36:35 -0800318 @Override
319 public Context getContext() {
320 return getActivity();
321 }
Wenyi Wanga0313442016-05-16 11:55:19 -0700322
323 private CursorLoader createCursorLoader(Context context) {
324 return new CursorLoader(context) {
325 @Override
326 protected Cursor onLoadInBackground() {
327 try {
328 return super.onLoadInBackground();
329 } catch (RuntimeException e) {
330 return null;
331 }
332 }
333 };
334 }
335
336 private String[] getProjection(Context context) {
337 final ContactsPreferences contactsPrefs = new ContactsPreferences(context);
338 final int displayOrder = contactsPrefs.getDisplayOrder();
339 if (displayOrder == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
340 return ProfileQuery.PROFILE_PROJECTION_PRIMARY;
341 }
342 return ProfileQuery.PROFILE_PROJECTION_ALTERNATIVE;
343 }
344
Wenyi Wang13874012016-05-27 15:23:53 -0700345 @Override
346 public boolean onPreferenceClick(Preference p) {
347 final String prefKey = p.getKey();
348
349 if (KEY_ABOUT.equals(prefKey)) {
350 ((ContactsPreferenceActivity) getActivity()).showAboutFragment();
351 return true;
James Laskey1e2102f2016-09-19 10:09:08 -0700352 } else if (KEY_IMPORT.equals(prefKey)) {
Marcus Hagerott02f5dba2016-10-18 12:34:57 -0700353 ImportDialogFragment.show(getFragmentManager());
Wenyi Wang13874012016-05-27 15:23:53 -0700354 return true;
James Laskey1e2102f2016-09-19 10:09:08 -0700355 } else if (KEY_EXPORT.equals(prefKey)) {
356 ExportDialogFragment.show(getFragmentManager(), ContactsPreferenceActivity.class,
357 ExportDialogFragment.EXPORT_MODE_ALL_CONTACTS);
358 return true;
Walter Jang0396cf72016-09-21 13:23:29 -0700359 } else if (KEY_MY_INFO.equals(prefKey)) {
Wenyi Wang13874012016-05-27 15:23:53 -0700360 if (mHasProfile) {
361 final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, mProfileContactId);
Walter Jang0396cf72016-09-21 13:23:29 -0700362 ImplicitIntentsUtil.startQuickContact(getActivity(), uri, ScreenType.ME_CONTACT);
Wenyi Wang13874012016-05-27 15:23:53 -0700363 } else {
Walter Jang0396cf72016-09-21 13:23:29 -0700364 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
Wenyi Wang13874012016-05-27 15:23:53 -0700365 intent.putExtra(mNewLocalProfileExtra, true);
Walter Jang0396cf72016-09-21 13:23:29 -0700366 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
Wenyi Wang13874012016-05-27 15:23:53 -0700367 }
Wenyi Wang13874012016-05-27 15:23:53 -0700368 return true;
Tingting Wang940f0152016-06-30 13:15:49 -0700369 } else if (KEY_ACCOUNTS.equals(prefKey)) {
Walter Jang8be77012016-07-07 21:05:34 -0700370 ImplicitIntentsUtil.startActivityOutsideApp(getContext(),
371 ImplicitIntentsUtil.getIntentForAddingAccount());
Tingting Wang940f0152016-06-30 13:15:49 -0700372 return true;
Tingting Wangf5b12582016-06-30 18:27:22 -0700373 } else if (KEY_BLOCKED_NUMBERS.equals(prefKey)) {
374 final Intent intent = TelecomManagerUtil.createManageBlockedNumbersIntent(
375 (TelecomManager) getContext().getSystemService(Context.TELECOM_SERVICE));
376 startActivity(intent);
377 return true;
Walter Jangf2cad222016-07-14 19:49:06 +0000378 } else if (KEY_CUSTOM_CONTACTS_FILTER.equals(prefKey)) {
379 final ContactListFilter filter =
380 ContactListFilterController.getInstance(getContext()).getFilter();
381 AccountFilterUtil.startAccountFilterActivityForResult(
382 this, REQUEST_CODE_CUSTOM_CONTACTS_FILTER, filter);
Wenyi Wang13874012016-05-27 15:23:53 -0700383 }
384 return false;
Wenyi Wanga0313442016-05-16 11:55:19 -0700385 }
Walter Jangf2cad222016-07-14 19:49:06 +0000386
387 @Override
388 public void onActivityResult(int requestCode, int resultCode, Intent data) {
389 if (requestCode == REQUEST_CODE_CUSTOM_CONTACTS_FILTER
390 && resultCode == Activity.RESULT_OK) {
391 AccountFilterUtil.handleAccountFilterResult(
392 ContactListFilterController.getInstance(getContext()), resultCode, data);
Walter Jang362b7332016-07-18 18:26:07 -0700393 setCustomContactsFilterSummary();
Walter Jangf2cad222016-07-14 19:49:06 +0000394 } else {
395 super.onActivityResult(requestCode, resultCode, data);
396 }
397 }
Walter Jang362b7332016-07-18 18:26:07 -0700398
399 private void setCustomContactsFilterSummary() {
400 final Preference customFilterPreference = findPreference(KEY_CUSTOM_CONTACTS_FILTER);
401 if (customFilterPreference != null) {
402 final ContactListFilter filter =
Walter Jang398bca92016-08-02 14:37:57 -0700403 ContactListFilterController.getInstance(getContext()).getPersistedFilter();
Walter Jang362b7332016-07-18 18:26:07 -0700404 if (filter != null) {
405 if (filter.filterType == ContactListFilter.FILTER_TYPE_DEFAULT ||
406 filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
407 customFilterPreference.setSummary(R.string.list_filter_all_accounts);
408 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
Wenyi Wang2b1aa122016-07-19 19:31:07 -0700409 customFilterPreference.setSummary(R.string.listCustomView);
Walter Jang362b7332016-07-18 18:26:07 -0700410 } else {
411 customFilterPreference.setSummary(null);
412 }
413 }
414 }
415 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700416
417 private class SaveServiceResultListener extends BroadcastReceiver {
418 @Override
419 public void onReceive(Context context, Intent intent) {
420 final long now = System.currentTimeMillis();
421 final long opStart = intent.getLongExtra(
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800422 SimImportService.EXTRA_OPERATION_REQUESTED_AT_TIME, now);
Marcus Hagerott819214d2016-09-29 14:58:27 -0700423
424 // If it's been over 30 seconds the user is likely in a different context so suppress
425 // the toast message.
426 if (now - opStart > 30*1000) return;
427
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800428 final int code = intent.getIntExtra(SimImportService.EXTRA_RESULT_CODE,
429 SimImportService.RESULT_UNKNOWN);
430 final int count = intent.getIntExtra(SimImportService.EXTRA_RESULT_COUNT, -1);
431 if (code == SimImportService.RESULT_SUCCESS && count > 0) {
Marcus Hagerott819214d2016-09-29 14:58:27 -0700432 Snackbar.make(mRootView, getResources().getQuantityString(
433 R.plurals.sim_import_success_toast_fmt, count, count),
434 Snackbar.LENGTH_LONG).show();
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800435 } else if (code == SimImportService.RESULT_FAILURE) {
Marcus Hagerott819214d2016-09-29 14:58:27 -0700436 Snackbar.make(mRootView, R.string.sim_import_failed_toast,
437 Snackbar.LENGTH_LONG).show();
438 }
439 }
440 }
Yorke Leeb3d841a2014-07-10 11:38:55 -0700441}
442