Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.contacts.list; |
| 18 | |
Gary Mai | 967cffd | 2016-08-01 16:54:25 -0700 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.database.Cursor; |
| 21 | import android.graphics.drawable.Drawable; |
| 22 | import android.os.Bundle; |
| 23 | import android.provider.ContactsContract; |
| 24 | import android.support.v4.view.ViewCompat; |
| 25 | import android.util.Log; |
| 26 | import android.view.LayoutInflater; |
| 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
| 29 | import android.view.accessibility.AccessibilityEvent; |
| 30 | import android.view.animation.Animation; |
| 31 | import android.view.animation.AnimationUtils; |
| 32 | import android.widget.AbsListView; |
| 33 | import android.widget.ImageView; |
| 34 | import android.widget.TextView; |
| 35 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 36 | import com.android.contacts.R; |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 37 | import com.android.contacts.activities.ActionBarAdapter; |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 38 | import com.android.contacts.common.list.ContactEntryListFragment; |
| 39 | import com.android.contacts.common.list.MultiSelectEntryContactListAdapter; |
| 40 | import com.android.contacts.common.list.MultiSelectEntryContactListAdapter.SelectedContactsListener; |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 41 | import com.android.contacts.common.logging.ListEvent.ActionType; |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 42 | import com.android.contacts.common.logging.Logger; |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 43 | import com.android.contacts.common.logging.SearchState; |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 44 | import com.android.contacts.common.model.AccountTypeManager; |
| 45 | import com.android.contacts.common.model.account.AccountType; |
| 46 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 47 | import com.android.contacts.common.model.account.GoogleAccountType; |
Wenyi Wang | 96fb8b5 | 2016-11-07 14:12:11 -0800 | [diff] [blame] | 48 | import com.android.contacts.group.GroupMembersFragment; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 49 | |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 50 | import java.util.ArrayList; |
| 51 | import java.util.List; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 52 | import java.util.TreeSet; |
| 53 | |
| 54 | /** |
| 55 | * Fragment containing a contact list used for browsing contacts and optionally selecting |
| 56 | * multiple contacts via checkboxes. |
| 57 | */ |
Walter Jang | e9ea4f0 | 2016-05-10 09:39:46 -0700 | [diff] [blame] | 58 | public abstract class MultiSelectContactsListFragment<T extends MultiSelectEntryContactListAdapter> |
| 59 | extends ContactEntryListFragment<T> |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 60 | implements SelectedContactsListener { |
| 61 | |
Gary Mai | 967cffd | 2016-08-01 16:54:25 -0700 | [diff] [blame] | 62 | protected boolean mAnimateOnLoad; |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 63 | private static final String TAG = "MultiContactsList"; |
| 64 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 65 | public interface OnCheckBoxListActionListener { |
| 66 | void onStartDisplayingCheckBoxes(); |
| 67 | void onSelectedContactIdsChanged(); |
Brian Attwell | 8f8937f | 2015-03-05 14:07:43 -0800 | [diff] [blame] | 68 | void onStopDisplayingCheckBoxes(); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | private static final String EXTRA_KEY_SELECTED_CONTACTS = "selected_contacts"; |
| 72 | |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 73 | private static final String KEY_SEARCH_RESULT_CLICKED = "search_result_clicked"; |
| 74 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 75 | private OnCheckBoxListActionListener mCheckBoxListListener; |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 76 | private boolean mSearchResultClicked; |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 77 | |
| 78 | public void setCheckBoxListListener(OnCheckBoxListActionListener checkBoxListListener) { |
| 79 | mCheckBoxListListener = checkBoxListListener; |
| 80 | } |
| 81 | |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 82 | /** |
| 83 | * Whether a search result was clicked by the user. Tracked so that we can distinguish |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 84 | * between exiting the search mode after a result was clicked from exiting w/o clicking |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 85 | * any search result. |
| 86 | */ |
| 87 | public boolean wasSearchResultClicked() { |
| 88 | return mSearchResultClicked; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Resets whether a search result was clicked by the user to false. |
| 93 | */ |
| 94 | public void resetSearchResultClicked() { |
| 95 | mSearchResultClicked = false; |
| 96 | } |
| 97 | |
Gary Mai | 967cffd | 2016-08-01 16:54:25 -0700 | [diff] [blame] | 98 | public void setAnimateOnLoad(boolean shouldAnimate) { |
| 99 | mAnimateOnLoad = shouldAnimate; |
| 100 | } |
| 101 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 102 | @Override |
| 103 | public void onSelectedContactsChanged() { |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 104 | if (mCheckBoxListListener != null) mCheckBoxListListener.onSelectedContactIdsChanged(); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
Gary Mai | 967cffd | 2016-08-01 16:54:25 -0700 | [diff] [blame] | 108 | public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 109 | Bundle savedInstanceState) { |
| 110 | super.onCreateView(inflater, container, savedInstanceState); |
| 111 | if (savedInstanceState == null && mAnimateOnLoad) { |
| 112 | setLayoutAnimation(getListView(), R.anim.slide_and_fade_in_layout_animation); |
| 113 | } |
| 114 | return getView(); |
| 115 | } |
| 116 | |
| 117 | @Override |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 118 | public void onActivityCreated(Bundle savedInstanceState) { |
| 119 | super.onActivityCreated(savedInstanceState); |
| 120 | if (savedInstanceState != null) { |
| 121 | final TreeSet<Long> selectedContactIds = (TreeSet<Long>) |
| 122 | savedInstanceState.getSerializable(EXTRA_KEY_SELECTED_CONTACTS); |
| 123 | getAdapter().setSelectedContactIds(selectedContactIds); |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 124 | mSearchResultClicked = savedInstanceState.getBoolean(KEY_SEARCH_RESULT_CLICKED); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Wenyi Wang | 6927bf3 | 2016-08-15 18:31:24 -0700 | [diff] [blame] | 128 | @Override |
| 129 | public void onStart() { |
| 130 | super.onStart(); |
| 131 | if (mCheckBoxListListener != null) { |
| 132 | mCheckBoxListListener.onSelectedContactIdsChanged(); |
| 133 | } |
| 134 | } |
| 135 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 136 | public TreeSet<Long> getSelectedContactIds() { |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 137 | return getAdapter().getSelectedContactIds(); |
| 138 | } |
| 139 | |
| 140 | public long[] getSelectedContactIdsArray() { |
| 141 | return getAdapter().getSelectedContactIdsArray(); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 145 | protected void configureAdapter() { |
| 146 | super.configureAdapter(); |
| 147 | getAdapter().setSelectedContactsListener(this); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void onSaveInstanceState(Bundle outState) { |
| 152 | super.onSaveInstanceState(outState); |
| 153 | outState.putSerializable(EXTRA_KEY_SELECTED_CONTACTS, getSelectedContactIds()); |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 154 | outState.putBoolean(KEY_SEARCH_RESULT_CLICKED, mSearchResultClicked); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public void displayCheckBoxes(boolean displayCheckBoxes) { |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 158 | if (getAdapter() != null) { |
| 159 | getAdapter().setDisplayCheckBoxes(displayCheckBoxes); |
| 160 | if (!displayCheckBoxes) { |
| 161 | clearCheckBoxes(); |
| 162 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
Brian Attwell | d2962a3 | 2015-03-02 14:48:50 -0800 | [diff] [blame] | 165 | |
| 166 | public void clearCheckBoxes() { |
| 167 | getAdapter().setSelectedContactIds(new TreeSet<Long>()); |
| 168 | } |
| 169 | |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 170 | @Override |
| 171 | protected boolean onItemLongClick(int position, long id) { |
Brian Attwell | 8f8937f | 2015-03-05 14:07:43 -0800 | [diff] [blame] | 172 | final int previouslySelectedCount = getAdapter().getSelectedContactIds().size(); |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 173 | final long contactId = getContactId(position); |
Tingting Wang | 80dfab3 | 2015-07-29 14:42:40 -0700 | [diff] [blame] | 174 | final int partition = getAdapter().getPartitionForPosition(position); |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 175 | if (contactId >= 0 && partition == ContactsContract.Directory.DEFAULT) { |
| 176 | if (mCheckBoxListListener != null) { |
| 177 | mCheckBoxListListener.onStartDisplayingCheckBoxes(); |
| 178 | } |
| 179 | getAdapter().toggleSelectionOfContactId(contactId); |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 180 | Logger.logListEvent(ActionType.SELECT, getListType(), |
| 181 | /* count */ getAdapter().getCount(), /* clickedIndex */ position, |
| 182 | /* numSelected */ 1); |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 183 | // Manually send clicked event if there is a checkbox. |
Wenyi Wang | 2b94399 | 2016-05-20 17:21:35 -0700 | [diff] [blame] | 184 | // See b/24098561. TalkBack will not read it otherwise. |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 185 | final int index = position + getListView().getHeaderViewsCount() - getListView() |
| 186 | .getFirstVisiblePosition(); |
| 187 | if (index >= 0 && index < getListView().getChildCount()) { |
| 188 | getListView().getChildAt(index).sendAccessibilityEvent(AccessibilityEvent |
| 189 | .TYPE_VIEW_CLICKED); |
Brian Attwell | c00112f | 2015-03-02 18:32:35 -0800 | [diff] [blame] | 190 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 191 | } |
Brian Attwell | 8f8937f | 2015-03-05 14:07:43 -0800 | [diff] [blame] | 192 | final int nowSelectedCount = getAdapter().getSelectedContactIds().size(); |
| 193 | if (mCheckBoxListListener != null |
| 194 | && previouslySelectedCount != 0 && nowSelectedCount == 0) { |
| 195 | // Last checkbox has been unchecked. So we should stop displaying checkboxes. |
| 196 | mCheckBoxListListener.onStopDisplayingCheckBoxes(); |
| 197 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 198 | return true; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void onItemClick(int position, long id) { |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 203 | final long contactId = getContactId(position); |
| 204 | if (contactId < 0) { |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 205 | return; |
| 206 | } |
| 207 | if (getAdapter().isDisplayingCheckBoxes()) { |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 208 | getAdapter().toggleSelectionOfContactId(contactId); |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 209 | } else { |
Walter Jang | 82c9033 | 2016-02-24 17:34:34 -0800 | [diff] [blame] | 210 | if (isSearchMode()) { |
| 211 | mSearchResultClicked = true; |
| 212 | Logger.logSearchEvent(createSearchStateForSearchResultClick(position)); |
| 213 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 214 | } |
Brian Attwell | 8f8937f | 2015-03-05 14:07:43 -0800 | [diff] [blame] | 215 | if (mCheckBoxListListener != null && getAdapter().getSelectedContactIds().size() == 0) { |
| 216 | mCheckBoxListListener.onStopDisplayingCheckBoxes(); |
| 217 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Walter Jang | 9c1fa5d | 2016-05-17 09:04:40 -0700 | [diff] [blame] | 220 | private long getContactId(int position) { |
| 221 | final int contactIdColumnIndex = getAdapter().getContactColumnIdIndex(); |
| 222 | |
| 223 | final Cursor cursor = (Cursor) getAdapter().getItem(position); |
| 224 | if (cursor != null) { |
| 225 | if (cursor.getColumnCount() > contactIdColumnIndex) { |
| 226 | return cursor.getLong(contactIdColumnIndex); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | Log.w(TAG, "Failed to get contact ID from cursor column " + contactIdColumnIndex); |
| 231 | return -1; |
| 232 | } |
| 233 | |
Walter Jang | a84fe61 | 2016-01-13 16:49:04 -0800 | [diff] [blame] | 234 | /** |
| 235 | * Returns the state of the search results currently presented to the user. |
| 236 | */ |
| 237 | public SearchState createSearchState() { |
| 238 | return createSearchState(/* selectedPosition */ -1); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Returns the state of the search results presented to the user |
| 243 | * at the time the result in the given position was clicked. |
| 244 | */ |
| 245 | public SearchState createSearchStateForSearchResultClick(int selectedPosition) { |
| 246 | return createSearchState(selectedPosition); |
| 247 | } |
| 248 | |
| 249 | private SearchState createSearchState(int selectedPosition) { |
| 250 | final MultiSelectEntryContactListAdapter adapter = getAdapter(); |
| 251 | if (adapter == null) { |
| 252 | return null; |
| 253 | } |
| 254 | final SearchState searchState = new SearchState(); |
| 255 | searchState.queryLength = adapter.getQueryString() == null |
| 256 | ? 0 : adapter.getQueryString().length(); |
| 257 | searchState.numPartitions = adapter.getPartitionCount(); |
| 258 | |
| 259 | // Set the number of results displayed to the user. Note that the adapter.getCount(), |
| 260 | // value does not always match the number of results actually displayed to the user, |
| 261 | // which is why we calculate it manually. |
| 262 | final List<Integer> numResultsInEachPartition = new ArrayList<>(); |
| 263 | for (int i = 0; i < adapter.getPartitionCount(); i++) { |
| 264 | final Cursor cursor = adapter.getCursor(i); |
| 265 | if (cursor == null || cursor.isClosed()) { |
| 266 | // Something went wrong, abort. |
| 267 | numResultsInEachPartition.clear(); |
| 268 | break; |
| 269 | } |
| 270 | numResultsInEachPartition.add(cursor.getCount()); |
| 271 | } |
| 272 | if (!numResultsInEachPartition.isEmpty()) { |
| 273 | int numResults = 0; |
| 274 | for (int i = 0; i < numResultsInEachPartition.size(); i++) { |
| 275 | numResults += numResultsInEachPartition.get(i); |
| 276 | } |
| 277 | searchState.numResults = numResults; |
| 278 | } |
| 279 | |
| 280 | // If a selection was made, set additional search state |
| 281 | if (selectedPosition >= 0) { |
| 282 | searchState.selectedPartition = adapter.getPartitionForPosition(selectedPosition); |
| 283 | searchState.selectedIndexInPartition = adapter.getOffsetInPartition(selectedPosition); |
| 284 | final Cursor cursor = adapter.getCursor(searchState.selectedPartition); |
| 285 | searchState.numResultsInSelectedPartition = |
| 286 | cursor == null || cursor.isClosed() ? -1 : cursor.getCount(); |
| 287 | |
| 288 | // Calculate the index across all partitions |
| 289 | if (!numResultsInEachPartition.isEmpty()) { |
| 290 | int selectedIndex = 0; |
| 291 | for (int i = 0; i < searchState.selectedPartition; i++) { |
| 292 | selectedIndex += numResultsInEachPartition.get(i); |
| 293 | } |
| 294 | selectedIndex += searchState.selectedIndexInPartition; |
| 295 | searchState.selectedIndex = selectedIndex; |
| 296 | } |
| 297 | } |
| 298 | return searchState; |
| 299 | } |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 300 | |
Gary Mai | 967cffd | 2016-08-01 16:54:25 -0700 | [diff] [blame] | 301 | protected void setLayoutAnimation(final ViewGroup view, int animationId) { |
| 302 | if (view == null) { |
| 303 | return; |
| 304 | } |
| 305 | view.setLayoutAnimationListener(new Animation.AnimationListener() { |
| 306 | @Override |
| 307 | public void onAnimationStart(Animation animation) { |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void onAnimationEnd(Animation animation) { |
| 312 | view.setLayoutAnimation(null); |
| 313 | } |
| 314 | |
| 315 | @Override |
| 316 | public void onAnimationRepeat(Animation animation) { |
| 317 | } |
| 318 | }); |
| 319 | view.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(getActivity(), animationId)); |
| 320 | } |
| 321 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 322 | @Override |
| 323 | public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, |
| 324 | int totalItemCount) { |
| 325 | final View accountFilterContainer = getView().findViewById( |
| 326 | R.id.account_filter_header_container); |
| 327 | if (accountFilterContainer == null) { |
| 328 | return; |
| 329 | } |
Wenyi Wang | 141b837 | 2016-08-03 11:13:10 -0700 | [diff] [blame] | 330 | |
| 331 | int firstCompletelyVisibleItem = firstVisibleItem; |
| 332 | if (view != null && view.getChildAt(0) != null && view.getChildAt(0).getTop() < 0) { |
| 333 | firstCompletelyVisibleItem++; |
| 334 | } |
| 335 | |
| 336 | if (firstCompletelyVisibleItem == 0) { |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 337 | ViewCompat.setElevation(accountFilterContainer, 0); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 338 | } else { |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 339 | ViewCompat.setElevation(accountFilterContainer, |
| 340 | getResources().getDimension(R.dimen.contact_list_header_elevation)); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 344 | protected void bindListHeaderCustom(View listView, View accountFilterContainer) { |
| 345 | bindListHeaderCommon(listView, accountFilterContainer); |
| 346 | |
| 347 | final TextView accountFilterHeader = (TextView) accountFilterContainer.findViewById( |
| 348 | R.id.account_filter_header); |
| 349 | accountFilterHeader.setText(R.string.listCustomView); |
| 350 | accountFilterHeader.setAllCaps(false); |
| 351 | |
| 352 | final ImageView accountFilterHeaderIcon = (ImageView) accountFilterContainer |
| 353 | .findViewById(R.id.account_filter_icon); |
Wenyi Wang | 20db985 | 2016-07-18 18:41:41 -0700 | [diff] [blame] | 354 | accountFilterHeaderIcon.setVisibility(View.GONE); |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Show account icon, count of contacts and account name in the header of the list. |
| 359 | */ |
| 360 | protected void bindListHeader(Context context, View listView, View accountFilterContainer, |
| 361 | AccountWithDataSet accountWithDataSet, int memberCount) { |
| 362 | if (memberCount < 0) { |
| 363 | hideHeaderAndAddPadding(context, listView, accountFilterContainer); |
| 364 | return; |
| 365 | } |
| 366 | |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 367 | bindListHeaderCommon(listView, accountFilterContainer); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 368 | |
Wenyi Wang | 96fb8b5 | 2016-11-07 14:12:11 -0800 | [diff] [blame] | 369 | final AccountTypeManager accountTypeManager = AccountTypeManager.getInstance(context); |
| 370 | final AccountType accountType = accountTypeManager.getAccountType( |
| 371 | accountWithDataSet.type, accountWithDataSet.dataSet); |
| 372 | |
| 373 | // Set text of count of contacts and account name |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 374 | final TextView accountFilterHeader = (TextView) accountFilterContainer.findViewById( |
| 375 | R.id.account_filter_header); |
Wenyi Wang | 96fb8b5 | 2016-11-07 14:12:11 -0800 | [diff] [blame] | 376 | final String headerText = shouldShowAccountName(accountType) |
Wenyi Wang | 3d8803e | 2016-07-18 14:29:37 -0700 | [diff] [blame] | 377 | ? String.format(context.getResources().getQuantityString( |
| 378 | R.plurals.contacts_count_with_account, memberCount), |
| 379 | memberCount, accountWithDataSet.name) |
| 380 | : context.getResources().getQuantityString( |
| 381 | R.plurals.contacts_count, memberCount, memberCount); |
| 382 | accountFilterHeader.setText(headerText); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 383 | accountFilterHeader.setAllCaps(false); |
| 384 | |
| 385 | // Set icon of the account |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 386 | final Drawable icon = accountType != null ? accountType.getDisplayIcon(context) : null; |
| 387 | final ImageView accountFilterHeaderIcon = (ImageView) accountFilterContainer |
| 388 | .findViewById(R.id.account_filter_icon); |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 389 | |
| 390 | // If it's a writable Google account, we set icon size as 24dp; otherwise, we set it as |
| 391 | // 20dp. And we need to change margin accordingly. This is because the Google icon looks |
| 392 | // smaller when the icons are of the same size. |
| 393 | if (accountType instanceof GoogleAccountType) { |
| 394 | accountFilterHeaderIcon.getLayoutParams().height = getResources() |
| 395 | .getDimensionPixelOffset(R.dimen.contact_browser_list_header_icon_size); |
Wenyi Wang | 6c46e5b | 2016-11-17 10:57:42 -0800 | [diff] [blame] | 396 | accountFilterHeaderIcon.getLayoutParams().width = getResources() |
| 397 | .getDimensionPixelOffset(R.dimen.contact_browser_list_header_icon_size); |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 398 | |
| 399 | setMargins(accountFilterHeaderIcon, |
| 400 | getResources().getDimensionPixelOffset( |
| 401 | R.dimen.contact_browser_list_header_icon_left_margin), |
| 402 | getResources().getDimensionPixelOffset( |
| 403 | R.dimen.contact_browser_list_header_icon_right_margin)); |
| 404 | } else { |
| 405 | accountFilterHeaderIcon.getLayoutParams().height = getResources() |
| 406 | .getDimensionPixelOffset(R.dimen.contact_browser_list_header_icon_size_alt); |
Wenyi Wang | 6c46e5b | 2016-11-17 10:57:42 -0800 | [diff] [blame] | 407 | accountFilterHeaderIcon.getLayoutParams().width = getResources() |
| 408 | .getDimensionPixelOffset(R.dimen.contact_browser_list_header_icon_size_alt); |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 409 | |
| 410 | setMargins(accountFilterHeaderIcon, |
| 411 | getResources().getDimensionPixelOffset( |
| 412 | R.dimen.contact_browser_list_header_icon_left_margin_alt), |
| 413 | getResources().getDimensionPixelOffset( |
| 414 | R.dimen.contact_browser_list_header_icon_right_margin_alt)); |
| 415 | } |
| 416 | accountFilterHeaderIcon.requestLayout(); |
| 417 | |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 418 | accountFilterHeaderIcon.setVisibility(View.VISIBLE); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 419 | accountFilterHeaderIcon.setImageDrawable(icon); |
| 420 | } |
| 421 | |
Wenyi Wang | 96fb8b5 | 2016-11-07 14:12:11 -0800 | [diff] [blame] | 422 | private boolean shouldShowAccountName(AccountType accountType) { |
| 423 | return (accountType.isGroupMembershipEditable() && this instanceof GroupMembersFragment) |
| 424 | || GoogleAccountType.ACCOUNT_TYPE.equals(accountType.accountType); |
| 425 | } |
| 426 | |
Wenyi Wang | 81067f5 | 2016-07-26 18:18:11 -0700 | [diff] [blame] | 427 | private void setMargins(View v, int l, int r) { |
| 428 | if (v.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) { |
| 429 | ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); |
| 430 | p.setMarginStart(l); |
| 431 | p.setMarginEnd(r); |
| 432 | v.setLayoutParams(p); |
| 433 | v.requestLayout(); |
| 434 | } |
| 435 | } |
| 436 | |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 437 | private void bindListHeaderCommon(View listView, View accountFilterContainer) { |
| 438 | // Show header and remove top padding of the list |
| 439 | accountFilterContainer.setVisibility(View.VISIBLE); |
Wenyi Wang | 7cd9af3 | 2016-07-17 16:00:43 -0700 | [diff] [blame] | 440 | setListViewPaddingTop(listView, /* paddingTop */ 0); |
Walter Jang | 9294263 | 2016-07-14 19:49:32 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 443 | /** |
| 444 | * Hide header of list view and add padding to the top of list view. |
| 445 | */ |
| 446 | protected void hideHeaderAndAddPadding(Context context, View listView, |
| 447 | View accountFilterContainer) { |
| 448 | accountFilterContainer.setVisibility(View.GONE); |
Wenyi Wang | 7cd9af3 | 2016-07-17 16:00:43 -0700 | [diff] [blame] | 449 | setListViewPaddingTop(listView, |
| 450 | /* paddingTop */ context.getResources().getDimensionPixelSize( |
| 451 | R.dimen.contact_browser_list_item_padding_top_or_bottom)); |
| 452 | } |
| 453 | |
| 454 | private void setListViewPaddingTop(View listView, int paddingTop) { |
| 455 | listView.setPadding(listView.getPaddingLeft(), paddingTop, listView.getPaddingRight(), |
| 456 | listView.getPaddingBottom()); |
Wenyi Wang | 1114fde | 2016-07-11 21:44:02 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Wenyi Wang | 7967545 | 2016-08-17 10:43:28 -0700 | [diff] [blame] | 459 | /** |
| 460 | * Returns the {@link ActionBarAdapter} object associated with list fragment. |
| 461 | */ |
| 462 | public ActionBarAdapter getActionBarAdapter() { |
| 463 | return null; |
| 464 | } |
Brian Attwell | 20510ec | 2015-02-27 16:10:45 -0800 | [diff] [blame] | 465 | } |