Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 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 | package com.android.contacts.list; |
| 17 | |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 18 | import android.accounts.Account; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.content.ActivityNotFoundException; |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 22 | import android.content.ContentUris; |
Jay Shrauner | 8c42b52 | 2013-08-15 09:32:47 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 24 | import android.content.CursorLoader; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 26 | import android.content.Loader; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
| 28 | import android.content.pm.ResolveInfo; |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 29 | import android.content.res.Resources; |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 30 | import android.database.Cursor; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 31 | import android.graphics.Rect; |
Brian Attwell | b831b14 | 2014-10-03 15:45:18 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 33 | import android.os.Bundle; |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract; |
Wenyi Wang | 5db1821 | 2016-08-10 17:11:26 -0700 | [diff] [blame] | 36 | import android.provider.ContactsContract.Directory; |
Walter Jang | 6367d2a | 2016-08-15 14:51:18 -0700 | [diff] [blame] | 37 | import android.support.v4.widget.SwipeRefreshLayout; |
Dmitri Plotnikov | 1a57fb2 | 2011-01-14 14:53:37 -0800 | [diff] [blame] | 38 | import android.text.TextUtils; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 39 | import android.util.Log; |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 40 | import android.view.Gravity; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 41 | import android.view.LayoutInflater; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 42 | import android.view.Menu; |
| 43 | import android.view.MenuInflater; |
| 44 | import android.view.MenuItem; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
Daisuke Miyakawa | 8907f66 | 2011-09-06 11:36:43 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
Wenyi Wang | 4cc72d5 | 2016-10-06 12:42:54 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityManager; |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 49 | import android.widget.Button; |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 50 | import android.widget.FrameLayout; |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 51 | import android.widget.ImageView; |
| 52 | import android.widget.LinearLayout.LayoutParams; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 53 | import android.widget.TextView; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 54 | import android.widget.Toast; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 55 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 56 | import com.android.contacts.ContactSaveService; |
| 57 | import com.android.contacts.ContactsDrawerActivity; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 58 | import com.android.contacts.R; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 59 | import com.android.contacts.activities.ActionBarAdapter; |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 60 | import com.android.contacts.activities.PeopleActivity; |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 61 | import com.android.contacts.common.Experiments; |
Wenyi Wang | 4cc72d5 | 2016-10-06 12:42:54 -0700 | [diff] [blame] | 62 | import com.android.contacts.common.compat.CompatUtils; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 63 | import com.android.contacts.common.list.ContactEntryListFragment; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 64 | import com.android.contacts.common.list.ContactListAdapter; |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 65 | import com.android.contacts.common.list.ContactListFilter; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 66 | import com.android.contacts.common.list.ContactListFilterController; |
Paul Sliwowski | fd4df88 | 2013-09-10 22:07:24 -0700 | [diff] [blame] | 67 | import com.android.contacts.common.list.ContactListItemView; |
Chiao Cheng | a0233a0 | 2012-11-01 16:41:08 -0700 | [diff] [blame] | 68 | import com.android.contacts.common.list.DefaultContactListAdapter; |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 69 | import com.android.contacts.common.list.DirectoryListLoader; |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 70 | import com.android.contacts.common.list.FavoritesAndContactsLoader; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 71 | import com.android.contacts.common.logging.ListEvent; |
| 72 | import com.android.contacts.common.logging.Logger; |
| 73 | import com.android.contacts.common.logging.ScreenEvent; |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 74 | import com.android.contacts.common.model.AccountTypeManager; |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 75 | import com.android.contacts.common.model.account.AccountWithDataSet; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 76 | import com.android.contacts.common.util.AccountFilterUtil; |
| 77 | import com.android.contacts.common.util.ImplicitIntentsUtil; |
| 78 | import com.android.contacts.interactions.ContactDeletionInteraction; |
| 79 | import com.android.contacts.interactions.ContactMultiDeletionInteraction; |
| 80 | import com.android.contacts.interactions.ContactMultiDeletionInteraction.MultiContactDeleteListener; |
| 81 | import com.android.contacts.quickcontact.QuickContactActivity; |
| 82 | import com.android.contacts.util.SharedPreferenceUtil; |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 83 | import com.android.contacts.util.SyncUtil; |
Walter Jang | 6367d2a | 2016-08-15 14:51:18 -0700 | [diff] [blame] | 84 | import com.android.contactsbind.experiments.Flags; |
Tingting Wang | 8857b94 | 2016-08-29 13:53:41 -0700 | [diff] [blame] | 85 | import com.android.contactsbind.FeatureHighlightHelper; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 86 | |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 87 | import java.util.List; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 88 | import java.util.Locale; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 89 | |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Fragment containing a contact list used for browsing (as compared to |
| 92 | * picking a contact with one of the PICK intents). |
| 93 | */ |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 94 | public class DefaultContactBrowseListFragment extends ContactBrowseListFragment |
| 95 | implements EnableGlobalSyncDialogFragment.Listener { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 96 | |
| 97 | private static final String TAG = "DefaultListFragment"; |
| 98 | private static final String ENABLE_DEBUG_OPTIONS_HIDDEN_CODE = "debug debug!"; |
| 99 | private static final String KEY_DELETION_IN_PROGRESS = "deletionInProgress"; |
| 100 | |
| 101 | private static final int ACTIVITY_REQUEST_CODE_SHARE = 0; |
| 102 | |
Dmitri Plotnikov | 17abbae | 2010-09-28 20:36:45 -0700 | [diff] [blame] | 103 | private View mSearchHeaderView; |
Josh Gargus | f4aaa47 | 2012-04-26 17:27:22 -0700 | [diff] [blame] | 104 | private View mSearchProgress; |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 105 | private View mEmptyAccountView; |
| 106 | private View mEmptyHomeView; |
| 107 | private View mAccountFilterContainer; |
Josh Gargus | f4aaa47 | 2012-04-26 17:27:22 -0700 | [diff] [blame] | 108 | private TextView mSearchProgressText; |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 109 | |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 110 | private SwipeRefreshLayout mSwipeRefreshLayout; |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 111 | private final Handler mHandler = new Handler(); |
| 112 | private final Runnable mCancelRefresh = new Runnable() { |
| 113 | @Override |
| 114 | public void run() { |
| 115 | if (mSwipeRefreshLayout.isRefreshing()) { |
| 116 | mSwipeRefreshLayout.setRefreshing(false); |
| 117 | } |
| 118 | } |
| 119 | }; |
Tingting Wang | 1243cb4 | 2016-08-05 16:42:21 -0700 | [diff] [blame] | 120 | |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 121 | private View mAlertContainer; |
| 122 | private TextView mAlertText; |
| 123 | private ImageView mAlertDismissIcon; |
| 124 | private int mReasonSyncOff = SyncUtil.SYNC_SETTING_SYNC_ON; |
| 125 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 126 | private boolean mContactsAvailable; |
| 127 | private boolean mEnableDebugMenuOptions; |
| 128 | private boolean mIsRecreatedInstance; |
| 129 | private boolean mOptionsMenuContactsAvailable; |
| 130 | |
James Laskey | 1842bb2 | 2016-10-20 14:38:04 -0700 | [diff] [blame] | 131 | private boolean mCanSetActionBar = false; |
| 132 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 133 | /** |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 134 | * If {@link #configureFragment()} is already called. Used to avoid calling it twice |
| 135 | * in {@link #onResume()}. |
| 136 | * (This initialization only needs to be done once in onResume() when the Activity was just |
| 137 | * created from scratch -- i.e. onCreate() was just called) |
| 138 | */ |
| 139 | private boolean mFragmentInitialized; |
| 140 | |
| 141 | private boolean mFromOnNewIntent; |
| 142 | |
| 143 | /** |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 144 | * This is to tell whether we need to restart ContactMultiDeletionInteraction and set listener. |
| 145 | * if screen is rotated while deletion dialog is shown. |
| 146 | */ |
| 147 | private boolean mIsDeletionInProgress; |
| 148 | |
| 149 | /** |
| 150 | * This is to disable {@link #onOptionsItemSelected} when we trying to stop the |
| 151 | * activity/fragment. |
| 152 | */ |
| 153 | private boolean mDisableOptionItemSelected; |
| 154 | |
| 155 | private ActionBarAdapter mActionBarAdapter; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 156 | private ContactsDrawerActivity mActivity; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 157 | private ContactsRequest mContactsRequest; |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 158 | private ContactListFilterController mContactListFilterController; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 159 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 160 | private final ActionBarAdapter.Listener mActionBarListener = new ActionBarAdapter.Listener() { |
| 161 | @Override |
| 162 | public void onAction(int action) { |
| 163 | switch (action) { |
| 164 | case ActionBarAdapter.Listener.Action.START_SELECTION_MODE: |
| 165 | displayCheckBoxes(true); |
| 166 | startSearchOrSelectionMode(); |
| 167 | break; |
| 168 | case ActionBarAdapter.Listener.Action.START_SEARCH_MODE: |
| 169 | if (!mIsRecreatedInstance) { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 170 | Logger.logScreenView(mActivity, ScreenEvent.ScreenType.SEARCH); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 171 | } |
| 172 | startSearchOrSelectionMode(); |
| 173 | break; |
| 174 | case ActionBarAdapter.Listener.Action.BEGIN_STOPPING_SEARCH_AND_SELECTION_MODE: |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 175 | mActivity.showFabWithAnimation(/* showFab */ true); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 176 | break; |
| 177 | case ActionBarAdapter.Listener.Action.STOP_SEARCH_AND_SELECTION_MODE: |
| 178 | // If queryString is empty, fragment data will not be reloaded, |
| 179 | // so hamburger promo should be checked now. |
| 180 | // Otherwise, promo should be checked and displayed after reloading, b/30706521. |
| 181 | if (TextUtils.isEmpty(getQueryString())) { |
| 182 | maybeShowHamburgerFeatureHighlight(); |
| 183 | } |
| 184 | setQueryTextToFragment(""); |
| 185 | maybeHideCheckBoxes(); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 186 | mActivity.invalidateOptionsMenu(); |
| 187 | mActivity.showFabWithAnimation(/* showFab */ true); |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 188 | |
| 189 | // Alert user if sync is off and not dismissed before |
| 190 | setSyncOffAlert(); |
| 191 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 192 | // Determine whether the account has pullToRefresh feature |
Walter Jang | 9adc9ef | 2016-11-02 18:50:38 -0700 | [diff] [blame] | 193 | setSwipeRefreshLayoutEnabledOrNot(getFilter()); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 194 | break; |
| 195 | case ActionBarAdapter.Listener.Action.CHANGE_SEARCH_QUERY: |
| 196 | final String queryString = mActionBarAdapter.getQueryString(); |
| 197 | setQueryTextToFragment(queryString); |
| 198 | updateDebugOptionsVisibility( |
| 199 | ENABLE_DEBUG_OPTIONS_HIDDEN_CODE.equals(queryString)); |
| 200 | break; |
| 201 | default: |
| 202 | throw new IllegalStateException("Unknown ActionBarAdapter action: " + action); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private void startSearchOrSelectionMode() { |
| 207 | configureContactListFragment(); |
| 208 | maybeHideCheckBoxes(); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 209 | mActivity.invalidateOptionsMenu(); |
| 210 | mActivity.showFabWithAnimation(/* showFab */ false); |
| 211 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 212 | final Context context = getContext(); |
| 213 | if (!SharedPreferenceUtil.getHamburgerPromoTriggerActionHappenedBefore(context)) { |
| 214 | SharedPreferenceUtil.setHamburgerPromoTriggerActionHappenedBefore(context); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | private void updateDebugOptionsVisibility(boolean visible) { |
| 219 | if (mEnableDebugMenuOptions != visible) { |
| 220 | mEnableDebugMenuOptions = visible; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 221 | mActivity.invalidateOptionsMenu(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | private void setQueryTextToFragment(String query) { |
| 226 | setQueryString(query, true); |
| 227 | setVisibleScrollbarEnabled(!isSearchMode()); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void onUpButtonPressed() { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 232 | mActivity.onBackPressed(); |
| 233 | } |
| 234 | }; |
| 235 | |
| 236 | private final View.OnClickListener mAddContactListener = new View.OnClickListener() { |
| 237 | @Override |
| 238 | public void onClick(View v) { |
| 239 | AccountFilterUtil.startEditorIntent(getContext(), mActivity.getIntent(), getFilter()); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 240 | } |
| 241 | }; |
Dmitri Plotnikov | 4b4681d | 2010-05-06 21:06:28 -0700 | [diff] [blame] | 242 | |
Dmitri Plotnikov | 9716d05 | 2010-05-10 18:03:54 -0700 | [diff] [blame] | 243 | public DefaultContactBrowseListFragment() { |
| 244 | setPhotoLoaderEnabled(true); |
Brian Attwell | 8a6f4ad | 2014-06-06 21:54:53 -0700 | [diff] [blame] | 245 | // Don't use a QuickContactBadge. Just use a regular ImageView. Using a QuickContactBadge |
| 246 | // inside the ListView prevents us from using MODE_FULLY_EXPANDED and messes up ripples. |
| 247 | setQuickContactEnabled(false); |
Dmitri Plotnikov | 1ce1e7c | 2010-05-13 16:41:00 -0700 | [diff] [blame] | 248 | setSectionHeaderDisplayEnabled(true); |
Dmitri Plotnikov | 5245ea6 | 2010-12-10 15:03:13 -0800 | [diff] [blame] | 249 | setVisibleScrollbarEnabled(true); |
Wenyi Wang | 9746ad8 | 2016-06-28 12:48:28 -0700 | [diff] [blame] | 250 | setDisplayDirectoryHeader(false); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 251 | setHasOptionsMenu(true); |
Tingting Wang | 1243cb4 | 2016-08-05 16:42:21 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Dmitri Plotnikov | 92a6fdf | 2010-07-09 13:19:33 -0700 | [diff] [blame] | 254 | @Override |
Jay Shrauner | 8c42b52 | 2013-08-15 09:32:47 -0700 | [diff] [blame] | 255 | public CursorLoader createCursorLoader(Context context) { |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 256 | return new FavoritesAndContactsLoader(context); |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 260 | public void onLoadFinished(Loader<Cursor> loader, Cursor data) { |
Wenyi Wang | 5db1821 | 2016-08-10 17:11:26 -0700 | [diff] [blame] | 261 | if (loader.getId() == Directory.DEFAULT) { |
Walter Jang | a773fb9 | 2016-09-30 15:23:30 -0700 | [diff] [blame] | 262 | bindListHeader(data == null ? 0 : data.getCount()); |
Wenyi Wang | 5db1821 | 2016-08-10 17:11:26 -0700 | [diff] [blame] | 263 | } |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 264 | super.onLoadFinished(loader, data); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 265 | if (!isSearchMode()) { |
| 266 | maybeShowHamburgerFeatureHighlight(); |
| 267 | } |
| 268 | } |
| 269 | |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 270 | private void maybeShowHamburgerFeatureHighlight() { |
Tingting Wang | 8857b94 | 2016-08-29 13:53:41 -0700 | [diff] [blame] | 271 | if (mActionBarAdapter!= null && !mActionBarAdapter.isSearchMode() |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 272 | && !mActionBarAdapter.isSelectionMode() |
Wenyi Wang | 4cc72d5 | 2016-10-06 12:42:54 -0700 | [diff] [blame] | 273 | && !isTalkbackOnAndOnPreLollipopMr1() |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 274 | && SharedPreferenceUtil.getShouldShowHamburgerPromo(getContext())) { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 275 | if (FeatureHighlightHelper.showHamburgerFeatureHighlight(mActivity)) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 276 | SharedPreferenceUtil.setHamburgerPromoDisplayedBefore(getContext()); |
| 277 | } |
Tingting Wang | 1243cb4 | 2016-08-05 16:42:21 -0700 | [diff] [blame] | 278 | } |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Wenyi Wang | 4cc72d5 | 2016-10-06 12:42:54 -0700 | [diff] [blame] | 281 | // There's a crash if we show feature highlight when Talkback is on, on API 21 and below. |
| 282 | // See b/31180524. |
| 283 | private boolean isTalkbackOnAndOnPreLollipopMr1(){ |
| 284 | return ((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)) |
| 285 | .isTouchExplorationEnabled() |
| 286 | && !CompatUtils.isLollipopMr1Compatible(); |
| 287 | } |
| 288 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 289 | private void bindListHeader(int numberOfContacts) { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 290 | final ContactListFilter filter = getFilter(); |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 291 | // If the phone has at least one Google account whose sync status is unsyncable or pending |
| 292 | // or active, we have to make mAccountFilterContainer visible. |
| 293 | if (!isSearchMode() && numberOfContacts <= 0 && shouldShowEmptyView(filter)) { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 294 | if (filter != null && filter.isContactsFilterType()) { |
| 295 | makeViewVisible(mEmptyHomeView); |
| 296 | } else { |
| 297 | makeViewVisible(mEmptyAccountView); |
| 298 | } |
| 299 | return; |
| 300 | } |
| 301 | makeViewVisible(mAccountFilterContainer); |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 302 | if (isSearchMode()) { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 303 | hideHeaderAndAddPadding(getContext(), getListView(), mAccountFilterContainer); |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 304 | } else if (filter.filterType == ContactListFilter.FILTER_TYPE_CUSTOM) { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 305 | bindListHeaderCustom(getListView(), mAccountFilterContainer); |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 306 | } else if (filter.filterType != ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) { |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 307 | final AccountWithDataSet accountWithDataSet = new AccountWithDataSet( |
| 308 | filter.accountName, filter.accountType, filter.dataSet); |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 309 | bindListHeader(getContext(), getListView(), mAccountFilterContainer, |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 310 | accountWithDataSet, numberOfContacts); |
| 311 | } else { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 312 | hideHeaderAndAddPadding(getContext(), getListView(), mAccountFilterContainer); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 316 | /** |
| 317 | * If at least one Google account is unsyncable or its sync status is pending or active, we |
| 318 | * should not show empty view even if the number of contacts is 0. We should show sync status |
| 319 | * with empty list instead. |
| 320 | */ |
| 321 | private boolean shouldShowEmptyView(ContactListFilter filter) { |
| 322 | if (filter == null) { |
| 323 | return true; |
| 324 | } |
| 325 | // TODO(samchen) : Check ContactListFilter.FILTER_TYPE_CUSTOM |
| 326 | if (ContactListFilter.FILTER_TYPE_DEFAULT == filter.filterType |
| 327 | || ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS == filter.filterType) { |
| 328 | final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(getContext()) |
| 329 | .getAccounts(/* contactsWritableOnly */ true); |
| 330 | final List<Account> syncableAccounts = filter.getSyncableAccounts(accounts); |
| 331 | |
| 332 | if (syncableAccounts != null && syncableAccounts.size() > 0) { |
| 333 | for (Account account : syncableAccounts) { |
| 334 | if (SyncUtil.isSyncStatusPendingOrActive(account) |
| 335 | || SyncUtil.isUnsyncableGoogleAccount(account)) { |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } else if (ContactListFilter.FILTER_TYPE_ACCOUNT == filter.filterType) { |
| 341 | final Account account = new Account(filter.accountName, filter.accountType); |
| 342 | return !(SyncUtil.isSyncStatusPendingOrActive(account) |
| 343 | || SyncUtil.isUnsyncableGoogleAccount(account)); |
| 344 | } |
| 345 | return true; |
| 346 | } |
| 347 | |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 348 | // Show the view that's specified by id and hide the other two. |
| 349 | private void makeViewVisible(View view) { |
| 350 | mEmptyAccountView.setVisibility(view == mEmptyAccountView ? View.VISIBLE : View.GONE); |
| 351 | mEmptyHomeView.setVisibility(view == mEmptyHomeView ? View.VISIBLE : View.GONE); |
| 352 | mAccountFilterContainer.setVisibility( |
| 353 | view == mAccountFilterContainer ? View.VISIBLE : View.GONE); |
| 354 | } |
| 355 | |
Wenyi Wang | 32912eb | 2016-09-29 17:17:07 -0700 | [diff] [blame] | 356 | public void scrollToTop() { |
| 357 | if (getListView() != null) { |
| 358 | getListView().setSelection(0); |
| 359 | } |
| 360 | } |
| 361 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 362 | @Override |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 363 | protected void onItemClick(int position, long id) { |
Brian Attwell | b831b14 | 2014-10-03 15:45:18 -0700 | [diff] [blame] | 364 | final Uri uri = getAdapter().getContactUri(position); |
| 365 | if (uri == null) { |
| 366 | return; |
| 367 | } |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 368 | if (getAdapter().isDisplayingCheckBoxes()) { |
| 369 | super.onItemClick(position, id); |
| 370 | return; |
| 371 | } |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 372 | viewContact(position, uri, getAdapter().isEnterpriseContact(position)); |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | protected ContactListAdapter createListAdapter() { |
Dmitri Plotnikov | 0b976fc | 2010-06-28 16:56:17 -0700 | [diff] [blame] | 377 | DefaultContactListAdapter adapter = new DefaultContactListAdapter(getContext()); |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 378 | adapter.setSectionHeaderDisplayEnabled(isSectionHeaderDisplayEnabled()); |
Brian Attwell | dff843a | 2014-07-09 23:10:09 -0700 | [diff] [blame] | 379 | adapter.setDisplayPhotos(true); |
| 380 | adapter.setPhotoPosition( |
| 381 | ContactListItemView.getDefaultPhotoPosition(/* opposite = */ false)); |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 382 | return adapter; |
| 383 | } |
| 384 | |
| 385 | @Override |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 386 | public ContactListFilter getFilter() { |
| 387 | return mContactListFilterController.getFilter(); |
| 388 | } |
| 389 | |
| 390 | @Override |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 391 | protected View inflateView(LayoutInflater inflater, ViewGroup container) { |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 392 | final View view = inflater.inflate(R.layout.contact_list_content, null); |
| 393 | |
| 394 | mAccountFilterContainer = view.findViewById(R.id.account_filter_header_container); |
| 395 | |
| 396 | // Add empty main view and account view to list. |
| 397 | final FrameLayout contactListLayout = (FrameLayout) view.findViewById(R.id.contact_list); |
| 398 | mEmptyAccountView = getEmptyAccountView(inflater); |
| 399 | mEmptyHomeView = getEmptyHomeView(inflater); |
| 400 | contactListLayout.addView(mEmptyAccountView); |
| 401 | contactListLayout.addView(mEmptyHomeView); |
| 402 | |
| 403 | return view; |
| 404 | } |
| 405 | |
| 406 | private View getEmptyHomeView(LayoutInflater inflater) { |
| 407 | final View emptyHomeView = inflater.inflate(R.layout.empty_home_view, null); |
| 408 | // Set image margins. |
| 409 | final ImageView image = (ImageView) emptyHomeView.findViewById(R.id.empty_home_image); |
| 410 | final LayoutParams params = (LayoutParams) image.getLayoutParams(); |
| 411 | final int screenHeight = getResources().getDisplayMetrics().heightPixels; |
| 412 | final int marginTop = screenHeight / 2 - |
| 413 | getResources().getDimensionPixelSize(R.dimen.empty_home_view_image_offset) ; |
| 414 | params.setMargins(0, marginTop, 0, 0); |
| 415 | params.gravity = Gravity.CENTER_HORIZONTAL; |
| 416 | image.setLayoutParams(params); |
| 417 | |
| 418 | // Set up add contact button. |
| 419 | final Button addContactButton = |
| 420 | (Button) emptyHomeView.findViewById(R.id.add_contact_button); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 421 | addContactButton.setOnClickListener(mAddContactListener); |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 422 | return emptyHomeView; |
| 423 | } |
| 424 | |
| 425 | private View getEmptyAccountView(LayoutInflater inflater) { |
| 426 | final View emptyAccountView = inflater.inflate(R.layout.empty_account_view, null); |
| 427 | // Set image margins. |
| 428 | final ImageView image = (ImageView) emptyAccountView.findViewById(R.id.empty_account_image); |
| 429 | final LayoutParams params = (LayoutParams) image.getLayoutParams(); |
| 430 | final int height = getResources().getDisplayMetrics().heightPixels; |
| 431 | final int divisor = |
| 432 | getResources().getInteger(R.integer.empty_account_view_image_margin_divisor); |
| 433 | final int offset = |
| 434 | getResources().getDimensionPixelSize(R.dimen.empty_account_view_image_offset); |
| 435 | params.setMargins(0, height / divisor + offset, 0, 0); |
| 436 | params.gravity = Gravity.CENTER_HORIZONTAL; |
| 437 | image.setLayoutParams(params); |
| 438 | |
| 439 | // Set up add contact button. |
| 440 | final Button addContactButton = |
| 441 | (Button) emptyAccountView.findViewById(R.id.add_contact_button); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 442 | addContactButton.setOnClickListener(mAddContactListener); |
Wenyi Wang | f128ea8 | 2016-07-25 11:56:40 -0700 | [diff] [blame] | 443 | return emptyAccountView; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 447 | public void onCreate(Bundle savedState) { |
| 448 | super.onCreate(savedState); |
| 449 | mIsRecreatedInstance = (savedState != null); |
| 450 | mContactListFilterController = ContactListFilterController.getInstance(getContext()); |
| 451 | mContactListFilterController.checkFilterValidity(false); |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 452 | // Use FILTER_TYPE_ALL_ACCOUNTS filter if the instance is not a re-created one. |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 453 | // This is useful when user upgrades app while an account filter was |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 454 | // stored in sharedPreference in a previous version of Contacts app. |
| 455 | final ContactListFilter filter = mIsRecreatedInstance |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 456 | ? getFilter() |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 457 | : AccountFilterUtil.createContactsFilter(getContext()); |
| 458 | setContactListFilter(filter); |
| 459 | } |
| 460 | |
| 461 | @Override |
Dmitri Plotnikov | bba62ac | 2010-06-03 10:22:59 -0700 | [diff] [blame] | 462 | protected void onCreateView(LayoutInflater inflater, ViewGroup container) { |
| 463 | super.onCreateView(inflater, container); |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 464 | |
Walter Jang | 9adc9ef | 2016-11-02 18:50:38 -0700 | [diff] [blame] | 465 | initSwipeRefreshLayout(); |
| 466 | |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 467 | // Putting the header view inside a container will allow us to make |
| 468 | // it invisible later. See checkHeaderViewVisibility() |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 469 | final FrameLayout headerContainer = new FrameLayout(inflater.getContext()); |
Dmitri Plotnikov | 17abbae | 2010-09-28 20:36:45 -0700 | [diff] [blame] | 470 | mSearchHeaderView = inflater.inflate(R.layout.search_header, null, false); |
| 471 | headerContainer.addView(mSearchHeaderView); |
Dmitri Plotnikov | 8db1913 | 2011-01-13 14:24:52 -0800 | [diff] [blame] | 472 | getListView().addHeaderView(headerContainer, null, false); |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 473 | checkHeaderViewVisibility(); |
Josh Gargus | f4aaa47 | 2012-04-26 17:27:22 -0700 | [diff] [blame] | 474 | |
| 475 | mSearchProgress = getView().findViewById(R.id.search_progress); |
| 476 | mSearchProgressText = (TextView) mSearchHeaderView.findViewById(R.id.totalContactsText); |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 477 | |
| 478 | mAlertContainer = getView().findViewById(R.id.alert_container); |
| 479 | mAlertText = (TextView) mAlertContainer.findViewById(R.id.alert_text); |
| 480 | mAlertDismissIcon = (ImageView) mAlertContainer.findViewById(R.id.alert_dismiss_icon); |
| 481 | mAlertText.setOnClickListener(new View.OnClickListener() { |
| 482 | @Override |
| 483 | public void onClick(View v) { |
| 484 | turnSyncOn(); |
| 485 | } |
| 486 | }); |
| 487 | mAlertDismissIcon.setOnClickListener(new View.OnClickListener() { |
| 488 | @Override |
| 489 | public void onClick(View v) { |
| 490 | dismiss(); |
| 491 | } |
| 492 | }); |
| 493 | |
| 494 | mAlertContainer.setVisibility(View.GONE); |
| 495 | } |
| 496 | |
| 497 | private void turnSyncOn() { |
| 498 | final ContactListFilter filter = getFilter(); |
| 499 | if (filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT |
| 500 | && mReasonSyncOff == SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF) { |
| 501 | ContentResolver.setSyncAutomatically( |
| 502 | new Account(filter.accountName, filter.accountType), |
| 503 | ContactsContract.AUTHORITY, true); |
| 504 | mAlertContainer.setVisibility(View.GONE); |
| 505 | } else { |
| 506 | final EnableGlobalSyncDialogFragment dialog = new |
| 507 | EnableGlobalSyncDialogFragment(); |
| 508 | dialog.show(this, filter); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void onEnableAutoSync(ContactListFilter filter) { |
| 514 | // Turn on auto-sync |
| 515 | ContentResolver.setMasterSyncAutomatically(true); |
| 516 | // Also enable Contacts sync |
| 517 | final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance( |
| 518 | getContext()).getAccounts(/* contactsWritableOnly */ true); |
| 519 | final List<Account> syncableAccounts = filter.getSyncableAccounts(accounts); |
| 520 | if (syncableAccounts != null && syncableAccounts.size() > 0) { |
| 521 | for (Account account : syncableAccounts) { |
| 522 | ContentResolver.setSyncAutomatically(new Account(account.name, account.type), |
| 523 | ContactsContract.AUTHORITY, true); |
| 524 | } |
| 525 | } |
| 526 | mAlertContainer.setVisibility(View.GONE); |
| 527 | } |
| 528 | |
| 529 | private void dismiss() { |
| 530 | if (mReasonSyncOff == SyncUtil.SYNC_SETTING_GLOBAL_SYNC_OFF) { |
| 531 | SharedPreferenceUtil.incNumOfDismissesForAutoSyncOff(getContext()); |
| 532 | } else if (mReasonSyncOff == SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF) { |
| 533 | SharedPreferenceUtil.incNumOfDismissesForAccountSyncOff( |
| 534 | getContext(), getFilter().accountName); |
| 535 | } |
| 536 | mAlertContainer.setVisibility(View.GONE); |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 539 | private void initSwipeRefreshLayout() { |
| 540 | mSwipeRefreshLayout = (SwipeRefreshLayout) mView.findViewById(R.id.swipe_refresh); |
| 541 | if (mSwipeRefreshLayout == null) { |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | mSwipeRefreshLayout.setEnabled(true); |
| 546 | // Request sync contacts |
| 547 | mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { |
| 548 | @Override |
| 549 | public void onRefresh() { |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 550 | mHandler.removeCallbacks(mCancelRefresh); |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 551 | |
| 552 | final boolean isNetworkConnected = SyncUtil.isNetworkConnected(getContext()); |
| 553 | if (!isNetworkConnected) { |
| 554 | mSwipeRefreshLayout.setRefreshing(false); |
| 555 | ((PeopleActivity)getActivity()).showConnectionErrorMsg(); |
| 556 | return; |
| 557 | } |
| 558 | |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 559 | syncContacts(getFilter()); |
Walter Jang | df86ede | 2016-10-19 09:48:29 -0700 | [diff] [blame] | 560 | mHandler.postDelayed(mCancelRefresh, Flags.getInstance() |
yaolu | 9d99859 | 2016-10-14 23:21:00 -0700 | [diff] [blame] | 561 | .getInteger(Experiments.PULL_TO_REFRESH_CANCEL_REFRESH_MILLIS)); |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 562 | } |
| 563 | }); |
| 564 | mSwipeRefreshLayout.setColorSchemeResources( |
| 565 | R.color.swipe_refresh_color1, |
| 566 | R.color.swipe_refresh_color2, |
| 567 | R.color.swipe_refresh_color3, |
| 568 | R.color.swipe_refresh_color4); |
| 569 | mSwipeRefreshLayout.setDistanceToTriggerSync( |
| 570 | (int) getResources().getDimension(R.dimen.pull_to_refresh_distance)); |
| 571 | } |
| 572 | |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 573 | /** |
| 574 | * Request sync for the Google accounts (not include Google+ accounts) specified by the given |
| 575 | * filter. |
| 576 | */ |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 577 | private void syncContacts(ContactListFilter filter) { |
| 578 | if (filter == null) { |
| 579 | return; |
| 580 | } |
yaolu | 395c3f8 | 2016-10-18 11:13:39 -0700 | [diff] [blame] | 581 | |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 582 | final Bundle bundle = new Bundle(); |
| 583 | bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); |
| 584 | bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); |
| 585 | |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 586 | final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance( |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 587 | getContext()).getAccounts(/* contactsWritableOnly */ true); |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 588 | final List<Account> syncableAccounts = filter.getSyncableAccounts(accounts); |
| 589 | if (syncableAccounts != null && syncableAccounts.size() > 0) { |
| 590 | for (Account account : syncableAccounts) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 591 | // We can prioritize Contacts sync if sync is not initialized yet. |
Xi Chen | a455bd5 | 2016-07-25 17:00:22 -0700 | [diff] [blame] | 592 | if (!SyncUtil.isSyncStatusPendingOrActive(account) |
| 593 | || SyncUtil.isUnsyncableGoogleAccount(account)) { |
Xi Chen | 4a7a9ea | 2016-07-20 11:23:30 -0700 | [diff] [blame] | 594 | ContentResolver.requestSync(account, ContactsContract.AUTHORITY, bundle); |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 600 | private void setSyncOffAlert() { |
| 601 | final ContactListFilter filter = getFilter(); |
| 602 | final Account account = filter.filterType == ContactListFilter.FILTER_TYPE_ACCOUNT |
| 603 | && filter.isGoogleAccountType() |
| 604 | ? new Account(filter.accountName, filter.accountType) : null; |
| 605 | |
| 606 | if (account == null && !filter.isContactsFilterType()) { |
| 607 | mAlertContainer.setVisibility(View.GONE); |
| 608 | } else { |
| 609 | mReasonSyncOff = SyncUtil.calculateReasonSyncOff(getContext(), account); |
| 610 | final boolean isAlertVisible = |
| 611 | SyncUtil.isAlertVisible(getContext(), account, mReasonSyncOff); |
| 612 | setSyncOffMsg(mReasonSyncOff); |
| 613 | mAlertContainer.setVisibility(isAlertVisible ? View.VISIBLE : View.GONE); |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | private void setSyncOffMsg(int reason) { |
| 618 | final Resources resources = getResources(); |
| 619 | switch (reason) { |
| 620 | case SyncUtil.SYNC_SETTING_GLOBAL_SYNC_OFF: |
| 621 | mAlertText.setText(resources.getString(R.string.auto_sync_off)); |
| 622 | break; |
| 623 | case SyncUtil.SYNC_SETTING_ACCOUNT_SYNC_OFF: |
| 624 | mAlertText.setText(resources.getString(R.string.account_sync_off)); |
| 625 | break; |
| 626 | default: |
| 627 | } |
| 628 | } |
| 629 | |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 630 | @Override |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 631 | public void onActivityCreated(Bundle savedInstanceState) { |
| 632 | super.onActivityCreated(savedInstanceState); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 633 | |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 634 | mActivity = (ContactsDrawerActivity) getActivity(); |
| 635 | mActionBarAdapter = new ActionBarAdapter(mActivity, mActionBarListener, |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 636 | mActivity.getSupportActionBar(), mActivity.getToolbar(), |
| 637 | R.string.enter_contact_name); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 638 | mActionBarAdapter.setShowHomeIcon(true); |
| 639 | initializeActionBarAdapter(savedInstanceState); |
| 640 | if (isSearchMode()) { |
| 641 | mActionBarAdapter.setFocusOnSearchView(); |
| 642 | } |
| 643 | |
| 644 | setCheckBoxListListener(new CheckBoxListListener()); |
| 645 | setOnContactListActionListener(new ContactBrowserActionListener()); |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 646 | if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_DELETION_IN_PROGRESS)) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 647 | deleteSelectedContacts(); |
| 648 | } |
Wenyi Wang | 72b0f45 | 2016-09-30 15:29:04 -0700 | [diff] [blame] | 649 | |
| 650 | setDirectorySearchMode(); |
James Laskey | 1842bb2 | 2016-10-20 14:38:04 -0700 | [diff] [blame] | 651 | mCanSetActionBar = true; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | public void initializeActionBarAdapter(Bundle savedInstanceState) { |
| 655 | if (mActionBarAdapter != null) { |
| 656 | mActionBarAdapter.initialize(savedInstanceState, mContactsRequest); |
| 657 | } |
| 658 | } |
| 659 | |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 660 | private void configureFragment() { |
| 661 | if (mFragmentInitialized && !mFromOnNewIntent) { |
| 662 | return; |
| 663 | } |
| 664 | |
| 665 | mFragmentInitialized = true; |
| 666 | |
| 667 | if (mFromOnNewIntent || !mIsRecreatedInstance) { |
Wenyi Wang | a730d9d | 2016-09-16 18:25:51 -0700 | [diff] [blame] | 668 | mFromOnNewIntent = false; |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 669 | configureFragmentForRequest(); |
| 670 | } |
| 671 | |
| 672 | configureContactListFragment(); |
| 673 | } |
| 674 | |
| 675 | private void configureFragmentForRequest() { |
| 676 | ContactListFilter filter = null; |
| 677 | final int actionCode = mContactsRequest.getActionCode(); |
| 678 | boolean searchMode = mContactsRequest.isSearchMode(); |
| 679 | switch (actionCode) { |
| 680 | case ContactsRequest.ACTION_ALL_CONTACTS: |
| 681 | filter = AccountFilterUtil.createContactsFilter(getContext()); |
| 682 | break; |
| 683 | case ContactsRequest.ACTION_CONTACTS_WITH_PHONES: |
| 684 | filter = ContactListFilter.createFilterWithType( |
| 685 | ContactListFilter.FILTER_TYPE_WITH_PHONE_NUMBERS_ONLY); |
| 686 | break; |
| 687 | |
| 688 | case ContactsRequest.ACTION_FREQUENT: |
| 689 | case ContactsRequest.ACTION_STREQUENT: |
| 690 | case ContactsRequest.ACTION_STARRED: |
| 691 | case ContactsRequest.ACTION_VIEW_CONTACT: |
| 692 | default: |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | if (filter != null) { |
| 697 | setContactListFilter(filter); |
| 698 | searchMode = false; |
| 699 | } |
| 700 | |
| 701 | if (mContactsRequest.getContactUri() != null) { |
| 702 | searchMode = false; |
| 703 | } |
| 704 | |
| 705 | mActionBarAdapter.setSearchMode(searchMode); |
| 706 | configureContactListFragmentForRequest(); |
| 707 | } |
| 708 | |
| 709 | private void configureContactListFragmentForRequest() { |
| 710 | final Uri contactUri = mContactsRequest.getContactUri(); |
| 711 | if (contactUri != null) { |
| 712 | setSelectedContactUri(contactUri); |
| 713 | } |
| 714 | |
| 715 | setQueryString(mActionBarAdapter.getQueryString(), true); |
| 716 | setVisibleScrollbarEnabled(!isSearchMode()); |
Wenyi Wang | 72b0f45 | 2016-09-30 15:29:04 -0700 | [diff] [blame] | 717 | } |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 718 | |
Wenyi Wang | 72b0f45 | 2016-09-30 15:29:04 -0700 | [diff] [blame] | 719 | private void setDirectorySearchMode() { |
Wenyi Wang | c3857fa | 2016-11-07 17:10:07 -0800 | [diff] [blame] | 720 | if (mContactsRequest != null && mContactsRequest.isDirectorySearchEnabled()) { |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 721 | setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_DEFAULT); |
| 722 | } else { |
| 723 | setDirectorySearchMode(DirectoryListLoader.SEARCH_MODE_NONE); |
| 724 | } |
| 725 | } |
| 726 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 727 | @Override |
| 728 | public void onResume() { |
| 729 | super.onResume(); |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 730 | configureFragment(); |
| 731 | maybeShowHamburgerFeatureHighlight(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 732 | // Re-register the listener, which may have been cleared when onSaveInstanceState was |
| 733 | // called. See also: onSaveInstanceState |
| 734 | mActionBarAdapter.setListener(mActionBarListener); |
| 735 | mDisableOptionItemSelected = false; |
| 736 | maybeHideCheckBoxes(); |
| 737 | } |
| 738 | |
| 739 | private void maybeHideCheckBoxes() { |
| 740 | if (!mActionBarAdapter.isSelectionMode()) { |
| 741 | displayCheckBoxes(false); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | public ActionBarAdapter getActionBarAdapter(){ |
| 746 | return mActionBarAdapter; |
| 747 | } |
| 748 | |
| 749 | @Override |
Makoto Onuki | 1db00f6 | 2011-10-19 12:40:12 -0700 | [diff] [blame] | 750 | protected void setSearchMode(boolean flag) { |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 751 | super.setSearchMode(flag); |
| 752 | checkHeaderViewVisibility(); |
Josh Gargus | f4aaa47 | 2012-04-26 17:27:22 -0700 | [diff] [blame] | 753 | if (!flag) showSearchProgress(false); |
| 754 | } |
| 755 | |
| 756 | /** Show or hide the directory-search progress spinner. */ |
| 757 | private void showSearchProgress(boolean show) { |
Andrew Lee | e599bf0 | 2014-05-15 14:13:17 -0700 | [diff] [blame] | 758 | if (mSearchProgress != null) { |
| 759 | mSearchProgress.setVisibility(show ? View.VISIBLE : View.GONE); |
| 760 | } |
Dmitri Plotnikov | fe3f790 | 2010-06-08 14:24:29 -0700 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | private void checkHeaderViewVisibility() { |
Andrew Lee | e15828e | 2014-05-01 14:00:25 -0700 | [diff] [blame] | 764 | // Hide the search header by default. |
Dmitri Plotnikov | 17abbae | 2010-09-28 20:36:45 -0700 | [diff] [blame] | 765 | if (mSearchHeaderView != null) { |
| 766 | mSearchHeaderView.setVisibility(View.GONE); |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 767 | } |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | @Override |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 771 | protected void setListHeader() { |
| 772 | if (!isSearchMode()) { |
| 773 | return; |
Dmitri Plotnikov | 238d984 | 2010-05-04 14:14:57 -0700 | [diff] [blame] | 774 | } |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 775 | ContactListAdapter adapter = getAdapter(); |
| 776 | if (adapter == null) { |
| 777 | return; |
| 778 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 779 | |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 780 | // In search mode we only display the header if there is nothing found |
| 781 | if (TextUtils.isEmpty(getQueryString()) || !adapter.areAllPartitionsEmpty()) { |
| 782 | mSearchHeaderView.setVisibility(View.GONE); |
| 783 | showSearchProgress(false); |
| 784 | } else { |
| 785 | mSearchHeaderView.setVisibility(View.VISIBLE); |
| 786 | if (adapter.isLoading()) { |
| 787 | mSearchProgressText.setText(R.string.search_results_searching); |
| 788 | showSearchProgress(true); |
| 789 | } else { |
| 790 | mSearchProgressText.setText(R.string.listFoundAllContactsZero); |
| 791 | mSearchProgressText.sendAccessibilityEvent( |
| 792 | AccessibilityEvent.TYPE_VIEW_SELECTED); |
| 793 | showSearchProgress(false); |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 794 | } |
Wenyi Wang | aad30f5 | 2016-05-12 14:45:22 -0700 | [diff] [blame] | 795 | } |
Isaac Katzenelson | ead19c5 | 2011-07-29 18:24:53 -0700 | [diff] [blame] | 796 | } |
Xi Chen | 947c23d | 2016-06-30 20:23:52 -0700 | [diff] [blame] | 797 | |
| 798 | public SwipeRefreshLayout getSwipeRefreshLayout() { |
| 799 | return mSwipeRefreshLayout; |
| 800 | } |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 801 | |
| 802 | private final class CheckBoxListListener implements OnCheckBoxListActionListener { |
| 803 | @Override |
| 804 | public void onStartDisplayingCheckBoxes() { |
| 805 | mActionBarAdapter.setSelectionMode(true); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 806 | mActivity.invalidateOptionsMenu(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | @Override |
| 810 | public void onSelectedContactIdsChanged() { |
| 811 | mActionBarAdapter.setSelectionCount(getSelectedContactIds().size()); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 812 | mActivity.invalidateOptionsMenu(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | @Override |
| 816 | public void onStopDisplayingCheckBoxes() { |
| 817 | mActionBarAdapter.setSelectionMode(false); |
| 818 | } |
| 819 | } |
| 820 | |
Wenyi Wang | 32912eb | 2016-09-29 17:17:07 -0700 | [diff] [blame] | 821 | public void setFilterAndUpdateTitle(ContactListFilter filter) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 822 | setFilterAndUpdateTitle(filter, true); |
| 823 | } |
| 824 | |
| 825 | private void setFilterAndUpdateTitle(ContactListFilter filter, boolean restoreSelectedUri) { |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 826 | setContactListFilter(filter); |
| 827 | updateListFilter(filter, restoreSelectedUri); |
Wenyi Wang | cfe9465 | 2016-10-03 21:05:04 -0700 | [diff] [blame] | 828 | mActivity.setTitle(AccountFilterUtil.getActionBarTitleForFilter(mActivity, filter)); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 829 | |
yaolu | 491cca5 | 2016-09-30 14:04:40 -0700 | [diff] [blame] | 830 | // Alert user if sync is off and not dismissed before |
| 831 | setSyncOffAlert(); |
| 832 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 833 | // Determine whether the account has pullToRefresh feature |
Walter Jang | 9adc9ef | 2016-11-02 18:50:38 -0700 | [diff] [blame] | 834 | setSwipeRefreshLayoutEnabledOrNot(filter); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 837 | private void setSwipeRefreshLayoutEnabledOrNot(ContactListFilter filter) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 838 | final SwipeRefreshLayout swipeRefreshLayout = getSwipeRefreshLayout(); |
| 839 | if (swipeRefreshLayout == null) { |
| 840 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 841 | Log.d(TAG, "Can not load swipeRefreshLayout, swipeRefreshLayout is null"); |
| 842 | } |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | swipeRefreshLayout.setRefreshing(false); |
| 847 | swipeRefreshLayout.setEnabled(false); |
| 848 | |
| 849 | if (filter != null && !mActionBarAdapter.isSearchMode() |
| 850 | && !mActionBarAdapter.isSelectionMode()) { |
| 851 | final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(getContext()) |
| 852 | .getAccounts(/* contactsWritableOnly */ true); |
| 853 | if (filter.isSyncable(accounts)) { |
| 854 | swipeRefreshLayout.setEnabled(true); |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 859 | private void configureContactListFragment() { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 860 | // Filter may be changed when activity is in background. |
| 861 | setFilterAndUpdateTitle(getFilter()); |
| 862 | setVerticalScrollbarPosition(getScrollBarPosition()); |
| 863 | setSelectionVisible(false); |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 864 | mActivity.invalidateOptionsMenu(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | private int getScrollBarPosition() { |
| 868 | final Locale locale = Locale.getDefault(); |
| 869 | final boolean isRTL = |
| 870 | TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL; |
| 871 | return isRTL ? View.SCROLLBAR_POSITION_LEFT : View.SCROLLBAR_POSITION_RIGHT; |
| 872 | } |
| 873 | |
| 874 | private final class ContactBrowserActionListener implements OnContactBrowserActionListener { |
| 875 | ContactBrowserActionListener() {} |
| 876 | |
| 877 | @Override |
| 878 | public void onSelectionChange() { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public void onViewContactAction(int position, Uri contactLookupUri, |
| 883 | boolean isEnterpriseContact) { |
| 884 | if (isEnterpriseContact) { |
| 885 | // No implicit intent as user may have a different contacts app in work profile. |
| 886 | ContactsContract.QuickContact.showQuickContact(getContext(), new Rect(), |
| 887 | contactLookupUri, QuickContactActivity.MODE_FULLY_EXPANDED, null); |
| 888 | } else { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 889 | final int previousScreen; |
| 890 | if (isSearchMode()) { |
| 891 | previousScreen = ScreenEvent.ScreenType.SEARCH; |
| 892 | } else { |
Wenyi Wang | df3e849 | 2016-09-09 11:10:07 -0700 | [diff] [blame] | 893 | if (isAllContactsFilter(getFilter())) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 894 | if (position < getAdapter().getNumberOfFavorites()) { |
| 895 | previousScreen = ScreenEvent.ScreenType.FAVORITES; |
| 896 | } else { |
| 897 | previousScreen = ScreenEvent.ScreenType.ALL_CONTACTS; |
| 898 | } |
| 899 | } else { |
| 900 | previousScreen = ScreenEvent.ScreenType.LIST_ACCOUNT; |
| 901 | } |
| 902 | } |
Walter Jang | 0396cf7 | 2016-09-21 13:23:29 -0700 | [diff] [blame] | 903 | |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 904 | Logger.logListEvent(ListEvent.ActionType.CLICK, |
| 905 | /* listType */ getListTypeIncludingSearch(), |
| 906 | /* count */ getAdapter().getCount(), |
| 907 | /* clickedIndex */ position, /* numSelected */ 0); |
Walter Jang | 0396cf7 | 2016-09-21 13:23:29 -0700 | [diff] [blame] | 908 | |
| 909 | ImplicitIntentsUtil.startQuickContact( |
| 910 | getActivity(), contactLookupUri, previousScreen); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public void onDeleteContactAction(Uri contactUri) { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 916 | ContactDeletionInteraction.start(mActivity, contactUri, false); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | @Override |
| 920 | public void onFinishAction() { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 921 | mActivity.onBackPressed(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | @Override |
| 925 | public void onInvalidSelection() { |
| 926 | ContactListFilter filter; |
| 927 | ContactListFilter currentFilter = getFilter(); |
| 928 | if (currentFilter != null |
| 929 | && currentFilter.filterType == ContactListFilter.FILTER_TYPE_SINGLE_CONTACT) { |
| 930 | filter = AccountFilterUtil.createContactsFilter(getContext()); |
| 931 | setFilterAndUpdateTitle(filter); |
| 932 | } else { |
| 933 | filter = ContactListFilter.createFilterWithType( |
| 934 | ContactListFilter.FILTER_TYPE_SINGLE_CONTACT); |
| 935 | setFilterAndUpdateTitle(filter, /* restoreSelectedUri */ false); |
| 936 | } |
| 937 | setContactListFilter(filter); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | private boolean isAllContactsFilter(ContactListFilter filter) { |
| 942 | return filter != null && filter.isContactsFilterType(); |
| 943 | } |
| 944 | |
| 945 | public void setContactsAvailable(boolean contactsAvailable) { |
| 946 | mContactsAvailable = contactsAvailable; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * Set filter via ContactListFilterController |
| 951 | */ |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 952 | private void setContactListFilter(ContactListFilter filter) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 953 | mContactListFilterController.setContactListFilter(filter, |
| 954 | /* persistent */ isAllContactsFilter(filter)); |
| 955 | } |
| 956 | |
| 957 | @Override |
| 958 | public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 959 | if (!mContactsAvailable || mActivity.isInSecondLevel()) { |
| 960 | // If contacts aren't available or this fragment is not visible, hide all menu items. |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | super.onCreateOptionsMenu(menu, inflater); |
| 964 | inflater.inflate(R.menu.people_options, menu); |
| 965 | } |
| 966 | |
| 967 | @Override |
| 968 | public void onPrepareOptionsMenu(Menu menu) { |
| 969 | mOptionsMenuContactsAvailable = mContactsAvailable; |
| 970 | if (!mOptionsMenuContactsAvailable) { |
| 971 | return; |
| 972 | } |
| 973 | |
| 974 | final boolean isSearchOrSelectionMode = mActionBarAdapter.isSearchMode() |
| 975 | || mActionBarAdapter.isSelectionMode(); |
| 976 | makeMenuItemVisible(menu, R.id.menu_search, !isSearchOrSelectionMode); |
| 977 | |
| 978 | final boolean showSelectedContactOptions = mActionBarAdapter.isSelectionMode() |
| 979 | && getSelectedContactIds().size() != 0; |
| 980 | makeMenuItemVisible(menu, R.id.menu_share, showSelectedContactOptions); |
| 981 | makeMenuItemVisible(menu, R.id.menu_delete, showSelectedContactOptions); |
| 982 | final boolean showLinkContactsOptions = mActionBarAdapter.isSelectionMode() |
| 983 | && getSelectedContactIds().size() > 1; |
| 984 | makeMenuItemVisible(menu, R.id.menu_join, showLinkContactsOptions); |
| 985 | |
| 986 | // Debug options need to be visible even in search mode. |
| 987 | makeMenuItemVisible(menu, R.id.export_database, mEnableDebugMenuOptions && |
| 988 | hasExportIntentHandler()); |
| 989 | } |
| 990 | |
| 991 | private void makeMenuItemVisible(Menu menu, int itemId, boolean visible) { |
| 992 | final MenuItem item = menu.findItem(itemId); |
| 993 | if (item != null) { |
| 994 | item.setVisible(visible); |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | private boolean hasExportIntentHandler() { |
| 999 | final Intent intent = new Intent(); |
| 1000 | intent.setAction("com.android.providers.contacts.DUMP_DATABASE"); |
| 1001 | final List<ResolveInfo> receivers = |
| 1002 | getContext().getPackageManager().queryIntentActivities(intent, |
| 1003 | PackageManager.MATCH_DEFAULT_ONLY); |
| 1004 | return receivers != null && receivers.size() > 0; |
| 1005 | } |
| 1006 | |
| 1007 | @Override |
| 1008 | public boolean onOptionsItemSelected(MenuItem item) { |
| 1009 | if (mDisableOptionItemSelected) { |
| 1010 | return false; |
| 1011 | } |
| 1012 | |
| 1013 | switch (item.getItemId()) { |
| 1014 | case android.R.id.home: { |
| 1015 | // The home icon on the action bar is pressed |
| 1016 | if (mActionBarAdapter.isUpShowing()) { |
| 1017 | // "UP" icon press -- should be treated as "back". |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 1018 | mActivity.onBackPressed(); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1019 | } |
| 1020 | return true; |
| 1021 | } |
| 1022 | case R.id.menu_search: { |
| 1023 | if (!mActionBarAdapter.isSelectionMode()) { |
| 1024 | mActionBarAdapter.setSearchMode(true); |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | case R.id.menu_share: { |
| 1029 | shareSelectedContacts(); |
| 1030 | return true; |
| 1031 | } |
| 1032 | case R.id.menu_join: { |
| 1033 | Logger.logListEvent(ListEvent.ActionType.LINK, |
| 1034 | /* listType */ getListTypeIncludingSearch(), |
| 1035 | /* count */ getAdapter().getCount(), /* clickedIndex */ -1, |
| 1036 | /* numSelected */ getAdapter().getSelectedContactIds().size()); |
| 1037 | joinSelectedContacts(); |
| 1038 | return true; |
| 1039 | } |
| 1040 | case R.id.menu_delete: { |
| 1041 | deleteSelectedContacts(); |
| 1042 | return true; |
| 1043 | } |
| 1044 | case R.id.export_database: { |
| 1045 | final Intent intent = new Intent("com.android.providers.contacts.DUMP_DATABASE"); |
| 1046 | intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 1047 | ImplicitIntentsUtil.startActivityOutsideApp(getContext(), intent); |
| 1048 | return true; |
| 1049 | } |
| 1050 | } |
| 1051 | return super.onOptionsItemSelected(item); |
| 1052 | } |
| 1053 | |
| 1054 | /** |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 1055 | * Share all contacts that are currently selected. This method is pretty inefficient for |
| 1056 | * handling large numbers of contacts. I don't expect this to be a problem. |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1057 | */ |
| 1058 | private void shareSelectedContacts() { |
| 1059 | final StringBuilder uriListBuilder = new StringBuilder(); |
| 1060 | for (Long contactId : getSelectedContactIds()) { |
| 1061 | final Uri contactUri = ContentUris.withAppendedId( |
| 1062 | ContactsContract.Contacts.CONTENT_URI, contactId); |
| 1063 | final Uri lookupUri = ContactsContract.Contacts.getLookupUri( |
| 1064 | getContext().getContentResolver(), contactUri); |
| 1065 | if (lookupUri == null) { |
| 1066 | continue; |
| 1067 | } |
| 1068 | final List<String> pathSegments = lookupUri.getPathSegments(); |
| 1069 | if (pathSegments.size() < 2) { |
| 1070 | continue; |
| 1071 | } |
| 1072 | final String lookupKey = pathSegments.get(pathSegments.size() - 2); |
| 1073 | if (uriListBuilder.length() > 0) { |
| 1074 | uriListBuilder.append(':'); |
| 1075 | } |
| 1076 | uriListBuilder.append(Uri.encode(lookupKey)); |
| 1077 | } |
| 1078 | if (uriListBuilder.length() == 0) { |
| 1079 | return; |
| 1080 | } |
| 1081 | final Uri uri = Uri.withAppendedPath( |
| 1082 | ContactsContract.Contacts.CONTENT_MULTI_VCARD_URI, |
| 1083 | Uri.encode(uriListBuilder.toString())); |
| 1084 | final Intent intent = new Intent(Intent.ACTION_SEND); |
| 1085 | intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE); |
| 1086 | intent.putExtra(Intent.EXTRA_STREAM, uri); |
| 1087 | try { |
| 1088 | startActivityForResult(Intent.createChooser(intent, getResources().getQuantityString( |
| 1089 | R.plurals.title_share_via,/* quantity */ getSelectedContactIds().size())) |
| 1090 | , ACTIVITY_REQUEST_CODE_SHARE); |
| 1091 | } catch (final ActivityNotFoundException ex) { |
| 1092 | Toast.makeText(getContext(), R.string.share_error, Toast.LENGTH_SHORT).show(); |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | private void joinSelectedContacts() { |
| 1097 | final Context context = getContext(); |
| 1098 | final Intent intent = ContactSaveService.createJoinSeveralContactsIntent( |
| 1099 | context, getSelectedContactIdsArray()); |
| 1100 | context.startService(intent); |
| 1101 | |
| 1102 | mActionBarAdapter.setSelectionMode(false); |
| 1103 | } |
| 1104 | |
| 1105 | private void deleteSelectedContacts() { |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 1106 | final ContactMultiDeletionInteraction multiDeletionInteraction = |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1107 | ContactMultiDeletionInteraction.start(this, getSelectedContactIds()); |
Wenyi Wang | cc8f272 | 2016-09-07 14:52:32 -0700 | [diff] [blame] | 1108 | multiDeletionInteraction.setListener(new MultiDeleteListener()); |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1109 | mIsDeletionInProgress = true; |
| 1110 | } |
| 1111 | |
| 1112 | private final class MultiDeleteListener implements MultiContactDeleteListener { |
| 1113 | @Override |
| 1114 | public void onDeletionFinished() { |
| 1115 | // The parameters count and numSelected are both the number of contacts before deletion. |
| 1116 | Logger.logListEvent(ListEvent.ActionType.DELETE, |
| 1117 | /* listType */ getListTypeIncludingSearch(), |
| 1118 | /* count */ getAdapter().getCount(), /* clickedIndex */ -1, |
| 1119 | /* numSelected */ getSelectedContactIds().size()); |
| 1120 | mActionBarAdapter.setSelectionMode(false); |
| 1121 | mIsDeletionInProgress = false; |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | private int getListTypeIncludingSearch() { |
| 1126 | return isSearchMode() ? ListEvent.ListType.SEARCH_RESULT : getListType(); |
| 1127 | } |
| 1128 | |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 1129 | public void setParameters(ContactsRequest contactsRequest, boolean fromOnNewIntent) { |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1130 | mContactsRequest = contactsRequest; |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 1131 | mFromOnNewIntent = fromOnNewIntent; |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | @Override |
| 1135 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 1136 | switch (requestCode) { |
| 1137 | // TODO: Using the new startActivityWithResultFromFragment API this should not be needed |
| 1138 | // anymore |
| 1139 | case ContactEntryListFragment.ACTIVITY_REQUEST_CODE_PICKER: |
| 1140 | if (resultCode == Activity.RESULT_OK) { |
| 1141 | onPickerResult(data); |
| 1142 | } |
| 1143 | case ACTIVITY_REQUEST_CODE_SHARE: |
| 1144 | Logger.logListEvent(ListEvent.ActionType.SHARE, |
| 1145 | /* listType */ getListTypeIncludingSearch(), |
| 1146 | /* count */ getAdapter().getCount(), /* clickedIndex */ -1, |
| 1147 | /* numSelected */ getAdapter().getSelectedContactIds().size()); |
| 1148 | |
| 1149 | // TODO fix or remove multipicker code: ag/54762 |
| 1150 | // else if (resultCode == RESULT_CANCELED && mMode == MODE_PICK_MULTIPLE_PHONES) { |
| 1151 | // // Finish the activity if the sub activity was canceled as back key is used |
| 1152 | // // to confirm user selection in MODE_PICK_MULTIPLE_PHONES. |
| 1153 | // finish(); |
| 1154 | // } |
| 1155 | // break; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | public boolean getOptionsMenuContactsAvailable() { |
| 1160 | return mOptionsMenuContactsAvailable; |
| 1161 | } |
| 1162 | |
| 1163 | @Override |
| 1164 | public void onSaveInstanceState(Bundle outState) { |
| 1165 | super.onSaveInstanceState(outState); |
| 1166 | // Clear the listener to make sure we don't get callbacks after onSaveInstanceState, |
| 1167 | // in order to avoid doing fragment transactions after it. |
| 1168 | // TODO Figure out a better way to deal with the issue (ag/120686). |
| 1169 | if (mActionBarAdapter != null) { |
| 1170 | mActionBarAdapter.setListener(null); |
| 1171 | mActionBarAdapter.onSaveInstanceState(outState); |
| 1172 | } |
| 1173 | mDisableOptionItemSelected = true; |
| 1174 | outState.putBoolean(KEY_DELETION_IN_PROGRESS, mIsDeletionInProgress); |
| 1175 | } |
| 1176 | |
| 1177 | @Override |
| 1178 | public void onPause() { |
| 1179 | mOptionsMenuContactsAvailable = false; |
| 1180 | super.onPause(); |
| 1181 | } |
| 1182 | |
| 1183 | @Override |
| 1184 | public void onDestroy() { |
| 1185 | if (mActionBarAdapter != null) { |
| 1186 | mActionBarAdapter.setListener(null); |
| 1187 | } |
Wenyi Wang | 587b8f2 | 2016-08-17 17:40:45 -0700 | [diff] [blame] | 1188 | super.onDestroy(); |
| 1189 | } |
Wenyi Wang | ed9f974 | 2016-09-08 13:50:39 -0700 | [diff] [blame] | 1190 | |
| 1191 | public boolean onKeyDown(int unicodeChar) { |
| 1192 | if (mActionBarAdapter.isSelectionMode()) { |
| 1193 | // Ignore keyboard input when in selection mode. |
| 1194 | return true; |
| 1195 | } |
| 1196 | |
| 1197 | if (!mActionBarAdapter.isSearchMode()) { |
| 1198 | final String query = new String(new int[]{unicodeChar}, 0, 1); |
| 1199 | mActionBarAdapter.setSearchMode(true); |
| 1200 | mActionBarAdapter.setQueryString(query); |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
| 1204 | return false; |
| 1205 | } |
James Laskey | 1842bb2 | 2016-10-20 14:38:04 -0700 | [diff] [blame] | 1206 | |
| 1207 | public boolean canSetActionBar() { |
| 1208 | return mCanSetActionBar; |
| 1209 | } |
Tingting Wang | 5d612da | 2016-08-08 10:00:45 -0700 | [diff] [blame] | 1210 | } |