blob: 7cc307f2647d418f6ee0ca9a695e71f1c248e80a [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;
36import android.provider.ContactsContract.Profile;
Marcus Hagerott819214d2016-09-29 14:58:27 -070037import android.support.design.widget.Snackbar;
38import android.support.v4.content.LocalBroadcastManager;
Tingting Wangf5b12582016-06-30 18:27:22 -070039import android.telecom.TelecomManager;
40import android.telephony.TelephonyManager;
Marcus Hagerott819214d2016-09-29 14:58:27 -070041import android.view.LayoutInflater;
42import android.view.View;
43import android.view.ViewGroup;
44import android.widget.FrameLayout;
Yorke Leeb3d841a2014-07-10 11:38:55 -070045
Gary Mai0a49afa2016-12-05 15:53:58 -080046import com.android.contacts.ContactsUtils;
Marcus Hagerott819214d2016-09-29 14:58:27 -070047import com.android.contacts.R;
Marcus Hagerott95246bb2016-11-11 10:56:09 -080048import com.android.contacts.SimImportService;
Gary Mai69c182a2016-12-05 13:07:03 -080049import com.android.contacts.compat.TelecomManagerUtil;
50import com.android.contacts.compat.TelephonyManagerCompat;
51import com.android.contacts.interactions.ExportDialogFragment;
52import com.android.contacts.interactions.ImportDialogFragment;
53import com.android.contacts.list.ContactListFilter;
54import com.android.contacts.list.ContactListFilterController;
55import com.android.contacts.logging.ScreenEvent.ScreenType;
56import com.android.contacts.model.AccountTypeManager;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080057import com.android.contacts.model.account.AccountInfo;
58import com.android.contacts.model.account.AccountType;
Gary Mai69c182a2016-12-05 13:07:03 -080059import com.android.contacts.model.account.AccountWithDataSet;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080060import com.android.contacts.model.account.AccountsLoader;
Gary Mai69c182a2016-12-05 13:07:03 -080061import com.android.contacts.util.AccountFilterUtil;
62import com.android.contacts.util.ImplicitIntentsUtil;
James Laskeyff9a19d2016-11-15 13:36:34 -080063import com.android.contactsbind.HelpUtils;
Tingting Wangc939ae32015-09-01 16:45:08 -070064
65import java.util.List;
Yorke Leeb3d841a2014-07-10 11:38:55 -070066
67/**
Wenyi Wangcc3016e2015-12-30 16:54:05 -080068 * This fragment shows the preferences for "display options"
Yorke Leeb3d841a2014-07-10 11:38:55 -070069 */
Wenyi Wang13874012016-05-27 15:23:53 -070070public class DisplayOptionsPreferenceFragment extends PreferenceFragment
71 implements Preference.OnPreferenceClickListener {
Yorke Leeb3d841a2014-07-10 11:38:55 -070072
Walter Jangf2cad222016-07-14 19:49:06 +000073 private static final int REQUEST_CODE_CUSTOM_CONTACTS_FILTER = 0;
74
Wenyi Wang13874012016-05-27 15:23:53 -070075 private static final String ARG_CONTACTS_AVAILABLE = "are_contacts_available";
Wenyi Wang13874012016-05-27 15:23:53 -070076 private static final String ARG_NEW_LOCAL_PROFILE = "new_local_profile";
Wenyi Wanga0313442016-05-16 11:55:19 -070077
78 private static final String KEY_ABOUT = "about";
79 private static final String KEY_ACCOUNTS = "accounts";
Tingting Wang940f0152016-06-30 13:15:49 -070080 private static final String KEY_DEFAULT_ACCOUNT = "defaultAccount";
Tingting Wangf5b12582016-06-30 18:27:22 -070081 private static final String KEY_BLOCKED_NUMBERS = "blockedNumbers";
Wenyi Wanga0313442016-05-16 11:55:19 -070082 private static final String KEY_DISPLAY_ORDER = "displayOrder";
Walter Jangf2cad222016-07-14 19:49:06 +000083 private static final String KEY_CUSTOM_CONTACTS_FILTER = "customContactsFilter";
James Laskey1e2102f2016-09-19 10:09:08 -070084 private static final String KEY_IMPORT = "import";
85 private static final String KEY_EXPORT = "export";
Wenyi Wanga0313442016-05-16 11:55:19 -070086 private static final String KEY_MY_INFO = "myInfo";
87 private static final String KEY_SORT_ORDER = "sortOrder";
88
89 private static final int LOADER_PROFILE = 0;
Marcus Hagerottdc91dd62016-12-21 16:57:32 -080090 private static final int LOADER_ACCOUNTS = 1;
Wenyi Wanga0313442016-05-16 11:55:19 -070091
92 /**
93 * Callbacks for hosts of the {@link DisplayOptionsPreferenceFragment}.
94 */
95 public interface ProfileListener {
96 /**
97 * Invoked after profile has been loaded.
98 */
99 void onProfileLoaded(Cursor data);
100 }
101
102 /**
103 * The projections that are used to obtain user profile
104 */
105 public static class ProfileQuery {
106 /**
107 * Not instantiable.
108 */
109 private ProfileQuery() {}
110
111 private static final String[] PROFILE_PROJECTION_PRIMARY = new String[] {
112 Contacts._ID, // 0
113 Contacts.DISPLAY_NAME_PRIMARY, // 1
114 Contacts.IS_USER_PROFILE, // 2
115 };
116
117 private static final String[] PROFILE_PROJECTION_ALTERNATIVE = new String[] {
118 Contacts._ID, // 0
119 Contacts.DISPLAY_NAME_ALTERNATIVE, // 1
120 Contacts.IS_USER_PROFILE, // 2
121 };
122
123 public static final int CONTACT_ID = 0;
124 public static final int CONTACT_DISPLAY_NAME = 1;
125 public static final int CONTACT_IS_USER_PROFILE = 2;
126 }
127
128 private String mNewLocalProfileExtra;
Wenyi Wang13874012016-05-27 15:23:53 -0700129 private boolean mAreContactsAvailable;
130
131 private boolean mHasProfile;
132 private long mProfileContactId;
Wenyi Wanga0313442016-05-16 11:55:19 -0700133
134 private Preference mMyInfoPreference;
135
136 private ProfileListener mListener;
137
Marcus Hagerott819214d2016-09-29 14:58:27 -0700138 private ViewGroup mRootView;
139 private SaveServiceResultListener mSaveServiceListener;
140
Wenyi Wanga0313442016-05-16 11:55:19 -0700141 private final LoaderManager.LoaderCallbacks<Cursor> mProfileLoaderListener =
142 new LoaderManager.LoaderCallbacks<Cursor>() {
143
144 @Override
145 public CursorLoader onCreateLoader(int id, Bundle args) {
146 final CursorLoader loader = createCursorLoader(getContext());
147 loader.setUri(Profile.CONTENT_URI);
148 loader.setProjection(getProjection(getContext()));
149 return loader;
150 }
151
152 @Override
153 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
154 if (mListener != null) {
155 mListener.onProfileLoaded(data);
156 }
157 }
158
159 public void onLoaderReset(Loader<Cursor> loader) {
160 }
161 };
162
Marcus Hagerottdc91dd62016-12-21 16:57:32 -0800163 private final LoaderManager.LoaderCallbacks<List<AccountInfo>> mAccountsLoaderListener =
164 new LoaderManager.LoaderCallbacks<List<AccountInfo>>() {
165 @Override
166 public Loader<List<AccountInfo>> onCreateLoader(int id, Bundle args) {
167 return new AccountsLoader(getActivity(), AccountTypeManager.writableFilter());
168 }
169
170 @Override
171 public void onLoadFinished(
172 Loader<List<AccountInfo>> loader, List<AccountInfo> data) {
173 if (data.isEmpty()) {
174 removeUnsupportedAccountPreferences();
175 }
176 }
177
178 @Override
179 public void onLoaderReset(Loader<List<AccountInfo>> loader) {
180 }
181 };
182
Wenyi Wanga0313442016-05-16 11:55:19 -0700183 public static DisplayOptionsPreferenceFragment newInstance(String newLocalProfileExtra,
Walter Jang0396cf72016-09-21 13:23:29 -0700184 boolean areContactsAvailable) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700185 final DisplayOptionsPreferenceFragment fragment = new DisplayOptionsPreferenceFragment();
186 final Bundle args = new Bundle();
187 args.putString(ARG_NEW_LOCAL_PROFILE, newLocalProfileExtra);
Wenyi Wang13874012016-05-27 15:23:53 -0700188 args.putBoolean(ARG_CONTACTS_AVAILABLE, areContactsAvailable);
Wenyi Wanga0313442016-05-16 11:55:19 -0700189 fragment.setArguments(args);
190 return fragment;
191 }
192
Yorke Leeb3d841a2014-07-10 11:38:55 -0700193 @Override
Wenyi Wang13874012016-05-27 15:23:53 -0700194 public void onAttach(Activity activity) {
195 super.onAttach(activity);
196 try {
197 mListener = (ProfileListener) activity;
198 } catch (ClassCastException e) {
199 throw new ClassCastException(activity.toString() + " must implement ProfileListener");
200 }
201 }
202
203 @Override
Marcus Hagerott819214d2016-09-29 14:58:27 -0700204 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
205 // Wrap the preference view in a FrameLayout so we can show a snackbar
206 mRootView = new FrameLayout(getActivity());
207 final View list = super.onCreateView(inflater, mRootView, savedInstanceState);
208 mRootView.addView(list);
209 return mRootView;
210 }
211
212 @Override
213 public void onViewCreated(View view, Bundle savedInstanceState) {
214 super.onViewCreated(view, savedInstanceState);
215
216 mSaveServiceListener = new SaveServiceResultListener();
217 LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
218 mSaveServiceListener,
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800219 new IntentFilter(SimImportService.BROADCAST_SIM_IMPORT_COMPLETE));
Marcus Hagerott819214d2016-09-29 14:58:27 -0700220 }
221
222 @Override
Yorke Leeb3d841a2014-07-10 11:38:55 -0700223 public void onCreate(Bundle savedInstanceState) {
224 super.onCreate(savedInstanceState);
225
226 // Load the preferences from an XML resource
227 addPreferencesFromResource(R.xml.preference_display_options);
Tingting Wangc939ae32015-09-01 16:45:08 -0700228
Wenyi Wanga0313442016-05-16 11:55:19 -0700229 final Bundle args = getArguments();
230 mNewLocalProfileExtra = args.getString(ARG_NEW_LOCAL_PROFILE);
Wenyi Wang13874012016-05-27 15:23:53 -0700231 mAreContactsAvailable = args.getBoolean(ARG_CONTACTS_AVAILABLE);
Wenyi Wanga0313442016-05-16 11:55:19 -0700232
James Laskey1e2102f2016-09-19 10:09:08 -0700233 removeUnsupportedPreferences();
James Laskey1e2102f2016-09-19 10:09:08 -0700234
Wenyi Wanga0313442016-05-16 11:55:19 -0700235 mMyInfoPreference = findPreference(KEY_MY_INFO);
Tingting Wang940f0152016-06-30 13:15:49 -0700236
237 final Preference accountsPreference = findPreference(KEY_ACCOUNTS);
238 accountsPreference.setOnPreferenceClickListener(this);
239
James Laskey1e2102f2016-09-19 10:09:08 -0700240 final Preference importPreference = findPreference(KEY_IMPORT);
241 importPreference.setOnPreferenceClickListener(this);
242
243 final Preference exportPreference = findPreference(KEY_EXPORT);
244 if (exportPreference != null) {
245 exportPreference.setOnPreferenceClickListener(this);
246 }
Wenyi Wanga0313442016-05-16 11:55:19 -0700247
Tingting Wangf5b12582016-06-30 18:27:22 -0700248 final Preference blockedNumbersPreference = findPreference(KEY_BLOCKED_NUMBERS);
249 if (blockedNumbersPreference != null) {
250 blockedNumbersPreference.setOnPreferenceClickListener(this);
251 }
252
Wenyi Wanga0313442016-05-16 11:55:19 -0700253 final Preference aboutPreference = findPreference(KEY_ABOUT);
James Laskeyff9a19d2016-11-15 13:36:34 -0800254 if (aboutPreference != null) {
255 aboutPreference.setOnPreferenceClickListener(this);
256 }
Walter Jangf2cad222016-07-14 19:49:06 +0000257
258 final Preference customFilterPreference = findPreference(KEY_CUSTOM_CONTACTS_FILTER);
259 if (customFilterPreference != null) {
260 customFilterPreference.setOnPreferenceClickListener(this);
Walter Jang362b7332016-07-18 18:26:07 -0700261 setCustomContactsFilterSummary();
Walter Jangf2cad222016-07-14 19:49:06 +0000262 }
Yorke Leeb3d841a2014-07-10 11:38:55 -0700263 }
Wenyi Wang9342fbb2015-11-17 19:36:35 -0800264
Wenyi Wanga0313442016-05-16 11:55:19 -0700265 @Override
266 public void onActivityCreated(Bundle savedInstanceState) {
267 super.onActivityCreated(savedInstanceState);
Marcus Hagerottdc91dd62016-12-21 16:57:32 -0800268 getLoaderManager().initLoader(LOADER_PROFILE, null, mProfileLoaderListener);
269 getLoaderManager().initLoader(LOADER_ACCOUNTS, null, mAccountsLoaderListener);
Wenyi Wanga0313442016-05-16 11:55:19 -0700270 }
271
Marcus Hagerott819214d2016-09-29 14:58:27 -0700272 @Override
273 public void onDestroyView() {
274 super.onDestroyView();
275 LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mSaveServiceListener);
276 mRootView = null;
277 }
278
Wenyi Wang13874012016-05-27 15:23:53 -0700279 public void updateMyInfoPreference(boolean hasProfile, String displayName, long contactId) {
Wenyi Wang912c5df2016-06-10 13:44:42 -0700280 final CharSequence summary = hasProfile ? displayName : getString(R.string.set_up_profile);
Wenyi Wanga0313442016-05-16 11:55:19 -0700281 mMyInfoPreference.setSummary(summary);
Wenyi Wang13874012016-05-27 15:23:53 -0700282 mHasProfile = hasProfile;
283 mProfileContactId = contactId;
284 mMyInfoPreference.setOnPreferenceClickListener(this);
Wenyi Wanga0313442016-05-16 11:55:19 -0700285 }
286
Walter Jang5665f362016-01-20 21:44:10 +0000287 private void removeUnsupportedPreferences() {
Wenyi Wangb297ad02016-04-17 12:53:46 -0700288 // Disable sort order for CJK locales where it is not supported
Wenyi Wang2882a5a2016-05-13 19:54:45 +0000289 final Resources resources = getResources();
Walter Jang5665f362016-01-20 21:44:10 +0000290 if (!resources.getBoolean(R.bool.config_sort_order_user_changeable)) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700291 getPreferenceScreen().removePreference(findPreference(KEY_SORT_ORDER));
Walter Jang5665f362016-01-20 21:44:10 +0000292 }
293
James Laskeyff9a19d2016-11-15 13:36:34 -0800294 if (HelpUtils.isHelpAndFeedbackAvailable()) {
295 getPreferenceScreen().removePreference(findPreference(KEY_ABOUT));
296 }
297
Walter Jang5665f362016-01-20 21:44:10 +0000298 // Disable display order for CJK locales as well
299 if (!resources.getBoolean(R.bool.config_display_order_user_changeable)) {
Wenyi Wanga0313442016-05-16 11:55:19 -0700300 getPreferenceScreen().removePreference(findPreference(KEY_DISPLAY_ORDER));
Walter Jang5665f362016-01-20 21:44:10 +0000301 }
302
Tingting Wangf5b12582016-06-30 18:27:22 -0700303 final boolean isPhone = TelephonyManagerCompat.isVoiceCapable(
304 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE));
305 final boolean showBlockedNumbers = isPhone && ContactsUtils.FLAG_N_FEATURE
Wenyi Wangae5c3a02016-06-27 12:43:31 -0700306 && BlockedNumberContract.canCurrentUserBlockNumbers(getContext());
Tingting Wangf5b12582016-06-30 18:27:22 -0700307 if (!showBlockedNumbers) {
308 getPreferenceScreen().removePreference(findPreference(KEY_BLOCKED_NUMBERS));
309 }
James Laskey1e2102f2016-09-19 10:09:08 -0700310
311 if (!mAreContactsAvailable) {
312 getPreferenceScreen().removePreference(findPreference(KEY_EXPORT));
313 }
Tingting Wang918f0b22016-02-09 15:06:24 -0800314 }
Walter Jang5665f362016-01-20 21:44:10 +0000315
Marcus Hagerottdc91dd62016-12-21 16:57:32 -0800316 private void removeUnsupportedAccountPreferences() {
317 getPreferenceScreen().removePreference(findPreference(KEY_DEFAULT_ACCOUNT));
318 getPreferenceScreen().removePreference(findPreference(KEY_CUSTOM_CONTACTS_FILTER));
319 }
320
Wenyi Wang9342fbb2015-11-17 19:36:35 -0800321 @Override
322 public Context getContext() {
323 return getActivity();
324 }
Wenyi Wanga0313442016-05-16 11:55:19 -0700325
326 private CursorLoader createCursorLoader(Context context) {
327 return new CursorLoader(context) {
328 @Override
329 protected Cursor onLoadInBackground() {
330 try {
331 return super.onLoadInBackground();
332 } catch (RuntimeException e) {
333 return null;
334 }
335 }
336 };
337 }
338
339 private String[] getProjection(Context context) {
340 final ContactsPreferences contactsPrefs = new ContactsPreferences(context);
341 final int displayOrder = contactsPrefs.getDisplayOrder();
342 if (displayOrder == ContactsPreferences.DISPLAY_ORDER_PRIMARY) {
343 return ProfileQuery.PROFILE_PROJECTION_PRIMARY;
344 }
345 return ProfileQuery.PROFILE_PROJECTION_ALTERNATIVE;
346 }
347
Wenyi Wang13874012016-05-27 15:23:53 -0700348 @Override
349 public boolean onPreferenceClick(Preference p) {
350 final String prefKey = p.getKey();
351
352 if (KEY_ABOUT.equals(prefKey)) {
353 ((ContactsPreferenceActivity) getActivity()).showAboutFragment();
354 return true;
James Laskey1e2102f2016-09-19 10:09:08 -0700355 } else if (KEY_IMPORT.equals(prefKey)) {
Marcus Hagerott02f5dba2016-10-18 12:34:57 -0700356 ImportDialogFragment.show(getFragmentManager());
Wenyi Wang13874012016-05-27 15:23:53 -0700357 return true;
James Laskey1e2102f2016-09-19 10:09:08 -0700358 } else if (KEY_EXPORT.equals(prefKey)) {
359 ExportDialogFragment.show(getFragmentManager(), ContactsPreferenceActivity.class,
360 ExportDialogFragment.EXPORT_MODE_ALL_CONTACTS);
361 return true;
Walter Jang0396cf72016-09-21 13:23:29 -0700362 } else if (KEY_MY_INFO.equals(prefKey)) {
Wenyi Wang13874012016-05-27 15:23:53 -0700363 if (mHasProfile) {
364 final Uri uri = ContentUris.withAppendedId(Contacts.CONTENT_URI, mProfileContactId);
Walter Jang0396cf72016-09-21 13:23:29 -0700365 ImplicitIntentsUtil.startQuickContact(getActivity(), uri, ScreenType.ME_CONTACT);
Wenyi Wang13874012016-05-27 15:23:53 -0700366 } else {
Walter Jang0396cf72016-09-21 13:23:29 -0700367 final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
Wenyi Wang13874012016-05-27 15:23:53 -0700368 intent.putExtra(mNewLocalProfileExtra, true);
Walter Jang0396cf72016-09-21 13:23:29 -0700369 ImplicitIntentsUtil.startActivityInApp(getActivity(), intent);
Wenyi Wang13874012016-05-27 15:23:53 -0700370 }
Wenyi Wang13874012016-05-27 15:23:53 -0700371 return true;
Tingting Wang940f0152016-06-30 13:15:49 -0700372 } else if (KEY_ACCOUNTS.equals(prefKey)) {
Walter Jang8be77012016-07-07 21:05:34 -0700373 ImplicitIntentsUtil.startActivityOutsideApp(getContext(),
374 ImplicitIntentsUtil.getIntentForAddingAccount());
Tingting Wang940f0152016-06-30 13:15:49 -0700375 return true;
Tingting Wangf5b12582016-06-30 18:27:22 -0700376 } else if (KEY_BLOCKED_NUMBERS.equals(prefKey)) {
377 final Intent intent = TelecomManagerUtil.createManageBlockedNumbersIntent(
378 (TelecomManager) getContext().getSystemService(Context.TELECOM_SERVICE));
379 startActivity(intent);
380 return true;
Walter Jangf2cad222016-07-14 19:49:06 +0000381 } else if (KEY_CUSTOM_CONTACTS_FILTER.equals(prefKey)) {
382 final ContactListFilter filter =
383 ContactListFilterController.getInstance(getContext()).getFilter();
384 AccountFilterUtil.startAccountFilterActivityForResult(
385 this, REQUEST_CODE_CUSTOM_CONTACTS_FILTER, filter);
Wenyi Wang13874012016-05-27 15:23:53 -0700386 }
387 return false;
Wenyi Wanga0313442016-05-16 11:55:19 -0700388 }
Walter Jangf2cad222016-07-14 19:49:06 +0000389
390 @Override
391 public void onActivityResult(int requestCode, int resultCode, Intent data) {
392 if (requestCode == REQUEST_CODE_CUSTOM_CONTACTS_FILTER
393 && resultCode == Activity.RESULT_OK) {
394 AccountFilterUtil.handleAccountFilterResult(
395 ContactListFilterController.getInstance(getContext()), resultCode, data);
Walter Jang362b7332016-07-18 18:26:07 -0700396 setCustomContactsFilterSummary();
Walter Jangf2cad222016-07-14 19:49:06 +0000397 } else {
398 super.onActivityResult(requestCode, resultCode, data);
399 }
400 }
Walter Jang362b7332016-07-18 18:26:07 -0700401
402 private void setCustomContactsFilterSummary() {
403 final Preference customFilterPreference = findPreference(KEY_CUSTOM_CONTACTS_FILTER);
404 if (customFilterPreference != null) {
405 final ContactListFilter filter =
Walter Jang398bca92016-08-02 14:37:57 -0700406 ContactListFilterController.getInstance(getContext()).getPersistedFilter();
Walter Jang362b7332016-07-18 18:26:07 -0700407 if (filter != null) {
408 if (filter.filterType == ContactListFilter.FILTER_TYPE_DEFAULT ||
409 filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) {
410 customFilterPreference.setSummary(R.string.list_filter_all_accounts);
411 } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) {
Wenyi Wang2b1aa122016-07-19 19:31:07 -0700412 customFilterPreference.setSummary(R.string.listCustomView);
Walter Jang362b7332016-07-18 18:26:07 -0700413 } else {
414 customFilterPreference.setSummary(null);
415 }
416 }
417 }
418 }
Marcus Hagerott819214d2016-09-29 14:58:27 -0700419
420 private class SaveServiceResultListener extends BroadcastReceiver {
421 @Override
422 public void onReceive(Context context, Intent intent) {
423 final long now = System.currentTimeMillis();
424 final long opStart = intent.getLongExtra(
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800425 SimImportService.EXTRA_OPERATION_REQUESTED_AT_TIME, now);
Marcus Hagerott819214d2016-09-29 14:58:27 -0700426
427 // If it's been over 30 seconds the user is likely in a different context so suppress
428 // the toast message.
429 if (now - opStart > 30*1000) return;
430
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800431 final int code = intent.getIntExtra(SimImportService.EXTRA_RESULT_CODE,
432 SimImportService.RESULT_UNKNOWN);
433 final int count = intent.getIntExtra(SimImportService.EXTRA_RESULT_COUNT, -1);
434 if (code == SimImportService.RESULT_SUCCESS && count > 0) {
Marcus Hagerott819214d2016-09-29 14:58:27 -0700435 Snackbar.make(mRootView, getResources().getQuantityString(
436 R.plurals.sim_import_success_toast_fmt, count, count),
437 Snackbar.LENGTH_LONG).show();
Marcus Hagerott95246bb2016-11-11 10:56:09 -0800438 } else if (code == SimImportService.RESULT_FAILURE) {
Marcus Hagerott819214d2016-09-29 14:58:27 -0700439 Snackbar.make(mRootView, R.string.sim_import_failed_toast,
440 Snackbar.LENGTH_LONG).show();
441 }
442 }
443 }
Yorke Leeb3d841a2014-07-10 11:38:55 -0700444}
445