blob: 492de760664f4d69a28984bbcb30ad88086f483c [file] [log] [blame]
Wenyi Wangbb229242016-05-25 16:04:13 -07001/*
2 * Copyright (C) 2016 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
17package com.android.contacts;
18
Walter Jang13710d52016-07-08 14:36:20 -070019import android.accounts.Account;
Wenyi Wangbb229242016-05-25 16:04:13 -070020import android.app.FragmentManager;
21import android.app.FragmentTransaction;
Wenyi Wangbb229242016-05-25 16:04:13 -070022import android.content.Intent;
23import android.graphics.Color;
24import android.graphics.PorterDuff;
25import android.os.Bundle;
Walter Jang13710d52016-07-08 14:36:20 -070026import android.provider.ContactsContract.Intents;
Wenyi Wangbb229242016-05-25 16:04:13 -070027import android.support.annotation.LayoutRes;
28import android.support.design.widget.NavigationView;
Wenyi Wang8e73c362016-06-23 18:03:53 -070029import android.support.v4.content.ContextCompat;
Wenyi Wangbb229242016-05-25 16:04:13 -070030import android.support.v4.view.GravityCompat;
31import android.support.v4.widget.DrawerLayout;
32import android.support.v7.app.ActionBarDrawerToggle;
33import android.support.v7.app.AppCompatActivity;
34import android.support.v7.widget.Toolbar;
Wenyi Wangbb229242016-05-25 16:04:13 -070035import android.util.Log;
36import android.view.LayoutInflater;
37import android.view.Menu;
38import android.view.MenuItem;
39import android.view.SubMenu;
Wenyi Wang8e73c362016-06-23 18:03:53 -070040import android.view.View;
Wenyi Wangbb229242016-05-25 16:04:13 -070041import android.view.ViewGroup;
Gary Maif354d182016-07-25 13:59:30 -070042import android.widget.LinearLayout;
Walter Jang13710d52016-07-08 14:36:20 -070043import android.widget.Toast;
Wenyi Wangbb229242016-05-25 16:04:13 -070044
Wenyi Wang79675452016-08-17 10:43:28 -070045import com.android.contacts.activities.ActionBarAdapter;
Wenyi Wangbb229242016-05-25 16:04:13 -070046import com.android.contacts.common.ContactsUtils;
Wenyi Wang8e73c362016-06-23 18:03:53 -070047import com.android.contacts.common.compat.CompatUtils;
Walter Jang92942632016-07-14 19:49:32 +000048import com.android.contacts.common.list.AccountFilterActivity;
Wenyi Wangbb229242016-05-25 16:04:13 -070049import com.android.contacts.common.list.ContactListFilter;
50import com.android.contacts.common.list.ContactListFilterController;
Walter Jang13710d52016-07-08 14:36:20 -070051import com.android.contacts.common.model.AccountTypeManager;
52import com.android.contacts.common.model.account.AccountWithDataSet;
Wenyi Wangbb229242016-05-25 16:04:13 -070053import com.android.contacts.common.preference.ContactsPreferenceActivity;
54import com.android.contacts.common.util.AccountFilterUtil;
Walter Jang13710d52016-07-08 14:36:20 -070055import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
Wenyi Wangbb229242016-05-25 16:04:13 -070056import com.android.contacts.common.util.ImplicitIntentsUtil;
57import com.android.contacts.common.util.ViewUtil;
Walter Jang7b0970f2016-09-01 10:40:19 -070058import com.android.contacts.editor.CompactContactEditorFragment;
Walter Jang55d0e712016-08-31 17:11:36 -070059import com.android.contacts.editor.SelectAccountDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070060import com.android.contacts.group.GroupListItem;
Wenyi Wang79675452016-08-17 10:43:28 -070061import com.android.contacts.group.GroupMembersFragment;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070062import com.android.contacts.group.GroupMetadata;
Walter Jang13710d52016-07-08 14:36:20 -070063import com.android.contacts.group.GroupNameEditDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070064import com.android.contacts.group.GroupUtil;
65import com.android.contacts.group.GroupsFragment;
66import com.android.contacts.group.GroupsFragment.GroupsListener;
67import com.android.contacts.interactions.AccountFiltersFragment;
68import com.android.contacts.interactions.AccountFiltersFragment.AccountFiltersListener;
Wenyi Wang79675452016-08-17 10:43:28 -070069import com.android.contacts.list.DefaultContactBrowseListFragment;
70import com.android.contacts.list.MultiSelectContactsListFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070071import com.android.contacts.quickcontact.QuickContactActivity;
Marcus Hagerottfac695a2016-08-24 17:02:40 -070072import com.android.contacts.common.model.account.AccountDisplayInfo;
73import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Tingting Wangac9596e2016-08-02 22:24:24 -070074import com.android.contacts.util.SharedPreferenceUtil;
Wenyi Wangbb229242016-05-25 16:04:13 -070075import com.android.contactsbind.HelpUtils;
Marcus Hagerott6caf23f2016-08-18 15:02:42 -070076import com.android.contactsbind.ObjectFactory;
Wenyi Wangbb229242016-05-25 16:04:13 -070077
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070078import java.util.HashMap;
79import java.util.Iterator;
Wenyi Wangbb229242016-05-25 16:04:13 -070080import java.util.List;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070081import java.util.Map;
82import java.util.Map.Entry;
Wenyi Wangbb229242016-05-25 16:04:13 -070083
84/**
85 * A common superclass for Contacts activities with a navigation drawer.
86 */
87public abstract class ContactsDrawerActivity extends AppCompatContactsActivity implements
88 AccountFiltersListener,
89 GroupsListener,
Walter Jang13710d52016-07-08 14:36:20 -070090 NavigationView.OnNavigationItemSelectedListener,
Marcus Hagerott8d62cd22016-08-01 17:31:38 -070091 SelectAccountDialogFragment.Listener {
Wenyi Wangbb229242016-05-25 16:04:13 -070092
Wenyi Wang79675452016-08-17 10:43:28 -070093 /** Possible views of Contacts app. */
94 public enum ContactsView {
95 NONE,
96 ALL_CONTACTS,
97 DUPLICATES,
98 GROUP_VIEW,
99 ACCOUNT_VIEW,
100 }
101
Wenyi Wangbb229242016-05-25 16:04:13 -0700102 protected static String TAG = "ContactsDrawerActivity";
103
Walter Jang13710d52016-07-08 14:36:20 -0700104 private static final String TAG_GROUPS = "groups";
105 private static final String TAG_FILTERS = "filters";
106 private static final String TAG_SELECT_ACCOUNT_DIALOG = "selectAccountDialog";
107 private static final String TAG_GROUP_NAME_EDIT_DIALOG = "groupNameEditDialog";
108
109 private static final String KEY_NEW_GROUP_ACCOUNT = "newGroupAccount";
Wenyi Wang79675452016-08-17 10:43:28 -0700110 private static final String KEY_CONTACTS_VIEW = "contactsView";
Walter Jang13710d52016-07-08 14:36:20 -0700111
Wenyi Wang79675452016-08-17 10:43:28 -0700112 protected ContactsView mCurrentView;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700113
Wenyi Wang8e73c362016-06-23 18:03:53 -0700114 private class ContactsActionBarDrawerToggle extends ActionBarDrawerToggle {
115
116 private Runnable mRunnable;
Tingting Wangac9596e2016-08-02 22:24:24 -0700117 private boolean mMenuClickedBefore = SharedPreferenceUtil.getHamburgerMenuClickedBefore(
118 ContactsDrawerActivity.this);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700119
120 public ContactsActionBarDrawerToggle(AppCompatActivity activity, DrawerLayout drawerLayout,
121 Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
122 super(activity, drawerLayout, toolbar, openDrawerContentDescRes,
123 closeDrawerContentDescRes);
124 }
125
126 @Override
127 public void onDrawerOpened(View drawerView) {
128 super.onDrawerOpened(drawerView);
Tingting Wangac9596e2016-08-02 22:24:24 -0700129 if (!mMenuClickedBefore) {
130 SharedPreferenceUtil.setHamburgerMenuClickedBefore(ContactsDrawerActivity.this);
131 mMenuClickedBefore = true;
132 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700133 invalidateOptionsMenu();
Wenyi Wang79675452016-08-17 10:43:28 -0700134 // Stop search and selection mode like Gmail and Keep. Otherwise, if user switches to
135 // another fragment in navigation drawer, the current search/selection mode will be
136 // overlaid by the action bar of the newly-created fragment.
137 stopSearchAndSelection();
138 }
139
140 private void stopSearchAndSelection() {
141 final MultiSelectContactsListFragment listFragment;
142 if (isAllContactsView() || isAccountView()) {
143 listFragment = getAllFragment();
144 } else if (isGroupView()) {
145 listFragment = getGroupFragment();
146 } else {
147 listFragment = null;
148 }
149 if (listFragment == null) {
150 return;
151 }
152 final ActionBarAdapter actionBarAdapter = listFragment.getActionBarAdapter();
153 if (actionBarAdapter == null) {
154 return;
155 }
156 if (actionBarAdapter.isSearchMode()) {
157 actionBarAdapter.setSearchMode(false);
158 } else if (actionBarAdapter.isSelectionMode()) {
159 actionBarAdapter.setSelectionMode(false);
160 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700161 }
162
163 @Override
164 public void onDrawerClosed(View view) {
165 super.onDrawerClosed(view);
166 invalidateOptionsMenu();
167 }
168
169 @Override
170 public void onDrawerStateChanged(int newState) {
171 super.onDrawerStateChanged(newState);
172 // Set transparent status bar when drawer starts to move.
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700173 if (newState != DrawerLayout.STATE_IDLE) {
174 makeStatusBarTransparent();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700175 }
176 if (mRunnable != null && newState == DrawerLayout.STATE_IDLE) {
177 mRunnable.run();
178 mRunnable = null;
179 }
180 }
181
182 public void runWhenIdle(Runnable runnable) {
183 mRunnable = runnable;
184 }
185 }
186
Wenyi Wangbb229242016-05-25 16:04:13 -0700187 protected ContactListFilterController mContactListFilterController;
188 protected DrawerLayout mDrawer;
Wenyi Wang8e73c362016-06-23 18:03:53 -0700189 protected ContactsActionBarDrawerToggle mToggle;
Wenyi Wangbb229242016-05-25 16:04:13 -0700190 protected Toolbar mToolbar;
191 protected NavigationView mNavigationView;
192 protected GroupsFragment mGroupsFragment;
193 protected AccountFiltersFragment mAccountFiltersFragment;
194
Walter Jang13710d52016-07-08 14:36:20 -0700195 // The account the new group will be created under.
196 private AccountWithDataSet mNewGroupAccount;
197
Wenyi Wang79675452016-08-17 10:43:28 -0700198 // Checkable menu item lookup maps. Every map declared here should be added to
199 // clearCheckedMenus() so that they can be cleared.
200 // TODO find a better way to handle selected menu item state, when switching to fragments.
201 protected Map<Long, MenuItem> mGroupMenuMap = new HashMap<>();
202 protected Map<ContactListFilter, MenuItem> mFilterMenuMap = new HashMap<>();
203 protected Map<Integer, MenuItem> mIdMenuMap = new HashMap<>();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700204
Wenyi Wangbb229242016-05-25 16:04:13 -0700205 @Override
206 protected void onCreate(Bundle savedState) {
207 super.onCreate(savedState);
208
209 mContactListFilterController = ContactListFilterController.getInstance(this);
210 mContactListFilterController.checkFilterValidity(false);
211
212 super.setContentView(R.layout.contacts_drawer_activity);
213
214 // Set up the action bar.
215 mToolbar = getView(R.id.toolbar);
216 setSupportActionBar(mToolbar);
217
218 // Add shadow under toolbar.
219 ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
220
221 // Set up hamburger button.
222 mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700223 mToggle = new ContactsActionBarDrawerToggle(this, mDrawer, mToolbar,
Wenyi Wangbb229242016-05-25 16:04:13 -0700224 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700225 mDrawer.setDrawerListener(mToggle);
226 mToggle.syncState();
Wenyi Wangbb229242016-05-25 16:04:13 -0700227
Wenyi Wang79675452016-08-17 10:43:28 -0700228 // Set up navigation mode.
229 if (savedState != null) {
230 mCurrentView = ContactsView.values()[savedState.getInt(KEY_CONTACTS_VIEW)];
231 } else {
232 mCurrentView = ContactsView.ALL_CONTACTS;
233 }
234
Wenyi Wangbb229242016-05-25 16:04:13 -0700235 // Set up hamburger menu items.
236 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
237 mNavigationView.setNavigationItemSelectedListener(this);
Wenyi Wang79675452016-08-17 10:43:28 -0700238 setUpMenu();
Wenyi Wangbb229242016-05-25 16:04:13 -0700239
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700240 loadGroupsAndFilters();
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700241
Walter Jang13710d52016-07-08 14:36:20 -0700242 if (savedState != null && savedState.containsKey(KEY_NEW_GROUP_ACCOUNT)) {
243 mNewGroupAccount = AccountWithDataSet.unstringify(
244 savedState.getString(KEY_NEW_GROUP_ACCOUNT));
245 }
246 }
247
Wenyi Wang79675452016-08-17 10:43:28 -0700248 private void setUpMenu() {
249 final Menu menu = mNavigationView.getMenu();
250
251 if (ObjectFactory.getDuplicatesFragment() == null) {
252 menu.removeItem(R.id.nav_find_duplicates);
253 } else {
254 final MenuItem findDupMenu = menu.findItem(R.id.nav_find_duplicates);
255 mIdMenuMap.put(R.id.nav_find_duplicates, findDupMenu);
256 if (isDuplicatesView()) {
257 updateMenuSelection(findDupMenu);
258 }
259 }
260
261 if (!HelpUtils.isHelpAndFeedbackAvailable()) {
262 menu.removeItem(R.id.nav_help);
263 }
264
265 final MenuItem allContactsMenu = menu.findItem(R.id.nav_all_contacts);
266 mIdMenuMap.put(R.id.nav_all_contacts, allContactsMenu);
267 if (isAllContactsView()) {
268 updateMenuSelection(allContactsMenu);
269 }
Wenyi Wang6927bf32016-08-15 18:31:24 -0700270 }
271
Wenyi Wang79675452016-08-17 10:43:28 -0700272 public Toolbar getToolbar() {
273 return mToolbar;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700274 }
275
Walter Jang13710d52016-07-08 14:36:20 -0700276 @Override
277 protected void onSaveInstanceState(Bundle outState) {
278 super.onSaveInstanceState(outState);
279 if (mNewGroupAccount != null) {
280 outState.putString(KEY_NEW_GROUP_ACCOUNT, mNewGroupAccount.stringify());
281 }
Wenyi Wang79675452016-08-17 10:43:28 -0700282 outState.putInt(KEY_CONTACTS_VIEW, mCurrentView.ordinal());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700283 }
284
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700285 @Override
286 protected void onResume() {
287 super.onResume();
288 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
289 makeStatusBarTransparent();
290 }
291 }
292
293 private void makeStatusBarTransparent() {
Wenyi Wang45c210a2016-07-15 10:51:52 -0700294 // Avoid making status bar transparent when action bar's selection mode is on.
295 if (getWindow().getStatusBarColor() !=
296 ContextCompat.getColor(this, R.color.contextual_selection_bar_status_bar_color)
297 && CompatUtils.isLollipopCompatible()) {
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700298 getWindow().setStatusBarColor(Color.TRANSPARENT);
299 }
300 }
301
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700302 // Set up fragment manager to load groups and filters.
303 protected void loadGroupsAndFilters() {
Wenyi Wangbb229242016-05-25 16:04:13 -0700304 final FragmentManager fragmentManager = getFragmentManager();
305 final FragmentTransaction transaction = fragmentManager.beginTransaction();
306 addGroupsAndFiltersFragments(transaction);
307 transaction.commitAllowingStateLoss();
308 fragmentManager.executePendingTransactions();
309 }
310
311 @Override
312 public void setContentView(@LayoutRes int layoutResID) {
313 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
314 if (parent != null) {
315 parent.removeAllViews();
316 }
317 LayoutInflater.from(this).inflate(layoutResID, parent);
318 }
319
320 protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) {
321 final FragmentManager fragmentManager = getFragmentManager();
Walter Jang13710d52016-07-08 14:36:20 -0700322 mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700323 mAccountFiltersFragment = (AccountFiltersFragment)
Walter Jang13710d52016-07-08 14:36:20 -0700324 fragmentManager.findFragmentByTag(TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700325
326 if (mGroupsFragment == null && ContactsUtils.areGroupWritableAccountsAvailable(this)) {
327 mGroupsFragment = new GroupsFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700328 transaction.add(mGroupsFragment, TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700329 }
330
331 if (mAccountFiltersFragment == null) {
332 mAccountFiltersFragment = new AccountFiltersFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700333 transaction.add(mAccountFiltersFragment, TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700334 }
335
336 if (ContactsUtils.areGroupWritableAccountsAvailable(this) && mGroupsFragment != null) {
337 mGroupsFragment.setListener(this);
338 }
339 mAccountFiltersFragment.setListener(this);
340 }
341
342 @Override
343 public void onGroupsLoaded(List<GroupListItem> groupListItems) {
344 final Menu menu = mNavigationView.getMenu();
345 final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups);
346 final SubMenu subMenu = groupsMenuItem.getSubMenu();
347 subMenu.removeGroup(R.id.nav_groups_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700348 mGroupMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700349
Wenyi Wang79675452016-08-17 10:43:28 -0700350 final GroupMetadata groupMetaData = getGroupMetadata();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700351
Wenyi Wangbb229242016-05-25 16:04:13 -0700352 if (groupListItems != null) {
353 // Add each group
354 for (final GroupListItem groupListItem : groupListItems) {
355 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
356 continue;
357 }
358 final String title = groupListItem.getTitle();
359 final MenuItem menuItem =
Wenyi Wang79675452016-08-17 10:43:28 -0700360 subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, title);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700361 mGroupMenuMap.put(groupListItem.getGroupId(), menuItem);
Wenyi Wang79675452016-08-17 10:43:28 -0700362 if (isGroupView() && groupMetaData != null
363 && groupMetaData.groupId == groupListItem.getGroupId()) {
364 updateMenuSelection(menuItem);
365 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700366 menuItem.setIcon(R.drawable.ic_menu_label);
367 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
368 @Override
369 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700370 mToggle.runWhenIdle(new Runnable() {
371 @Override
372 public void run() {
Wenyi Wang86077052016-06-29 18:21:21 -0700373 onGroupMenuItemClicked(groupListItem.getGroupId(),
374 groupListItem.getTitle());
Wenyi Wang79675452016-08-17 10:43:28 -0700375 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700376 }
377 });
378 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700379 return true;
380 }
381 });
382 }
383 }
384
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700385 // Don't show "Create new..." menu if there's no group-writable accounts available.
386 if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) {
387 return;
388 }
389
Wenyi Wangbb229242016-05-25 16:04:13 -0700390 // Create a menu item in the sub menu to add new groups
Wenyi Wang45c210a2016-07-15 10:51:52 -0700391 final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700392 Menu.NONE, getString(R.string.menu_new_group_action_bar));
Wenyi Wang8cd7c5c2016-06-16 14:03:42 -0700393 menuItem.setIcon(R.drawable.ic_add);
Wenyi Wangbb229242016-05-25 16:04:13 -0700394 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
395 @Override
396 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700397 mToggle.runWhenIdle(new Runnable() {
398 @Override
399 public void run() {
400 onCreateGroupMenuItemClicked();
401 }
402 });
403 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700404 return true;
405 }
406 });
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700407
Wenyi Wang79675452016-08-17 10:43:28 -0700408 if (isGroupView() && groupMetaData != null) {
409 updateGroupMenu(groupMetaData);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700410 }
411 }
412
Wenyi Wang6927bf32016-08-15 18:31:24 -0700413 public void updateGroupMenu(GroupMetadata groupMetadata) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700414 clearCheckedMenus();
415 if (groupMetadata != null && mGroupMenuMap != null
416 && mGroupMenuMap.get(groupMetadata.groupId) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700417 setMenuChecked(mGroupMenuMap.get(groupMetadata.groupId), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700418 }
419 }
420
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700421 protected GroupMetadata getGroupMetadata() {
422 return null;
Wenyi Wangbb229242016-05-25 16:04:13 -0700423 }
424
Wenyi Wang79675452016-08-17 10:43:28 -0700425 protected boolean isGroupView() {
426 return mCurrentView == ContactsView.GROUP_VIEW;
Wenyi Wangbb229242016-05-25 16:04:13 -0700427 }
428
Wenyi Wang79675452016-08-17 10:43:28 -0700429 protected boolean isDuplicatesView() {
430 return mCurrentView == ContactsView.DUPLICATES;
431 }
432
433 protected boolean isAllContactsView() {
434 return mCurrentView == ContactsView.ALL_CONTACTS;
435 }
436
437 protected boolean isAccountView() {
438 return mCurrentView == ContactsView.ACCOUNT_VIEW;
439 }
440
441 public boolean isInSecondLevel() {
442 return isGroupView() || isDuplicatesView();
443 }
444
445 protected abstract void onGroupMenuItemClicked(long groupId, String title);
446
Walter Jang1110d672016-07-25 17:55:12 +0000447 protected void onCreateGroupMenuItemClicked() {
Walter Jang13710d52016-07-08 14:36:20 -0700448 // Select the account to create the group
449 final Bundle extras = getIntent().getExtras();
450 final Account account = extras == null ? null :
451 (Account) extras.getParcelable(Intents.Insert.EXTRA_ACCOUNT);
452 if (account == null) {
453 selectAccount();
454 } else {
455 final String dataSet = extras == null
456 ? null : extras.getString(Intents.Insert.EXTRA_DATA_SET);
457 final AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
458 account.name, account.type, dataSet);
459 onAccountChosen(accountWithDataSet, /* extraArgs */ null);
460 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700461 }
462
463 @Override
464 public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700465 final AccountDisplayInfoFactory accountDisplayFactory = AccountDisplayInfoFactory.
466 fromListFilters(this, accountFilterItems);
467
Wenyi Wangbb229242016-05-25 16:04:13 -0700468 final Menu menu = mNavigationView.getMenu();
469 final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters);
470 final SubMenu subMenu = filtersMenuItem.getSubMenu();
471 subMenu.removeGroup(R.id.nav_filters_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700472 mFilterMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700473
474 if (accountFilterItems == null || accountFilterItems.size() < 2) {
475 return;
476 }
477
Marcus Hagerott6caf23f2016-08-18 15:02:42 -0700478
Wenyi Wangbb229242016-05-25 16:04:13 -0700479 for (int i = 0; i < accountFilterItems.size(); i++) {
480 final ContactListFilter filter = accountFilterItems.get(i);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700481 final AccountDisplayInfo displayableAccount =
482 accountDisplayFactory.getAccountDisplayInfoFor(filter);
483 final CharSequence menuName = displayableAccount.getNameLabel();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700484 final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700485 Menu.NONE, menuName);
486 if (isAccountView() && filter == mContactListFilterController.getFilter()) {
487 updateMenuSelection(menuItem);
488 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700489 mFilterMenuMap.put(filter, menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700490 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000491 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700492 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
493 @Override
494 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700495 mToggle.runWhenIdle(new Runnable() {
496 @Override
497 public void run() {
Wenyi Wang79675452016-08-17 10:43:28 -0700498 onFilterMenuItemClicked(intent);
499 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700500 }
501 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700502 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700503 return true;
504 }
505 });
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700506 menuItem.setIcon(displayableAccount.getIcon());
Wenyi Wang45c210a2016-07-15 10:51:52 -0700507 // Get rid of the default menu item overlay and show original account icons.
Wenyi Wangbb229242016-05-25 16:04:13 -0700508 menuItem.getIcon().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
Gary Maif354d182016-07-25 13:59:30 -0700509 // Create a dummy action view to attach extra hidden content description to the menuItem
510 // for Talkback. We want Talkback to read out the account type but not have it be part
511 // of the menuItem title.
Gary Maif354d182016-07-25 13:59:30 -0700512 LinearLayout view = (LinearLayout) LayoutInflater.from(this)
513 .inflate(R.layout.account_type_info, null);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700514 view.setContentDescription(displayableAccount.getTypeLabel());
Gary Maif354d182016-07-25 13:59:30 -0700515 view.setVisibility(View.VISIBLE);
516 menuItem.setActionView(view);
Wenyi Wangbb229242016-05-25 16:04:13 -0700517 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700518
Wenyi Wang79675452016-08-17 10:43:28 -0700519 if (isAccountView()) {
520 updateFilterMenu(mContactListFilterController.getFilter());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700521 }
522 }
523
Wenyi Wang587b8f22016-08-17 17:40:45 -0700524 public void updateFilterMenu(ContactListFilter filter) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700525 clearCheckedMenus();
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700526 if (filter != null && filter.isContactsFilterType()) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700527 if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700528 setMenuChecked(mIdMenuMap.get(R.id.nav_all_contacts), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700529 }
530 } else {
531 if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700532 setMenuChecked(mFilterMenuMap.get(filter), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700533 }
534 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700535 }
536
Wenyi Wang79675452016-08-17 10:43:28 -0700537 protected void onFilterMenuItemClicked(Intent intent) {
538 AccountFilterUtil.handleAccountFilterResult(mContactListFilterController,
539 AppCompatActivity.RESULT_OK, intent);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700540 }
541
Wenyi Wangbb229242016-05-25 16:04:13 -0700542 @Override
Wenyi Wang8e73c362016-06-23 18:03:53 -0700543 public boolean onNavigationItemSelected(final MenuItem item) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700544 final int id = item.getItemId();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700545 mToggle.runWhenIdle(new Runnable() {
546 @Override
547 public void run() {
548 if (id == R.id.nav_settings) {
549 startActivity(createPreferenceIntent());
550 } else if (id == R.id.nav_help) {
551 HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this);
552 } else if (id == R.id.nav_all_contacts) {
553 switchToAllContacts();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700554 } else if (id == R.id.nav_find_duplicates) {
Wenyi Wang79675452016-08-17 10:43:28 -0700555 if (!isDuplicatesView()) {
556 launchFindDuplicates();
557 updateMenuSelection(item);
558 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700559 } else if (item.getIntent() != null) {
560 ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this,
561 item.getIntent());
562 } else {
563 Log.w(TAG, "Unhandled navigation view item selection");
564 }
565 }
566 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700567
568 mDrawer.closeDrawer(GravityCompat.START);
569 return true;
570 }
571
572 private Intent createPreferenceIntent() {
573 final Intent intent = new Intent(this, ContactsPreferenceActivity.class);
574 intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE,
Walter Jang7b0970f2016-09-01 10:40:19 -0700575 CompactContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE);
Wenyi Wangbb229242016-05-25 16:04:13 -0700576 intent.putExtra(ContactsPreferenceActivity.EXTRA_MODE_FULLY_EXPANDED,
577 QuickContactActivity.MODE_FULLY_EXPANDED);
578 intent.putExtra(ContactsPreferenceActivity.EXTRA_PREVIOUS_SCREEN_TYPE,
579 QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE);
580 return intent;
581 }
582
Wenyi Wang79675452016-08-17 10:43:28 -0700583 public void switchToAllContacts() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700584 resetFilter();
Wenyi Wang79675452016-08-17 10:43:28 -0700585
586 final Menu menu = mNavigationView.getMenu();
587 final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts);
588 updateMenuSelection(allContacts);
589
590 setTitle(getString(R.string.contactsList));
Wenyi Wangbb229242016-05-25 16:04:13 -0700591 }
592
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700593 protected void resetFilter() {
594 final Intent intent = new Intent();
595 final ContactListFilter filter = AccountFilterUtil.createContactsFilter(this);
596 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
597 AccountFilterUtil.handleAccountFilterResult(
598 mContactListFilterController, AppCompatActivity.RESULT_OK, intent);
599 }
600
Wenyi Wang79675452016-08-17 10:43:28 -0700601 protected abstract void launchFindDuplicates();
602
603 protected abstract DefaultContactBrowseListFragment getAllFragment();
604
605 protected abstract GroupMembersFragment getGroupFragment();
606
607 public abstract void showFabWithAnimation(boolean showFab);
Wenyi Wangbb229242016-05-25 16:04:13 -0700608
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700609 private void clearCheckedMenus() {
610 clearCheckedMenu(mFilterMenuMap);
611 clearCheckedMenu(mGroupMenuMap);
612 clearCheckedMenu(mIdMenuMap);
613 }
Wenyi Wang79675452016-08-17 10:43:28 -0700614
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700615 private void clearCheckedMenu(Map<?, MenuItem> map) {
616 final Iterator it = map.entrySet().iterator();
617 while (it.hasNext()) {
Wenyi Wang79675452016-08-17 10:43:28 -0700618 Entry pair = (Entry) it.next();
619 setMenuChecked(map.get(pair.getKey()), false);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700620 }
621 }
Walter Jang13710d52016-07-08 14:36:20 -0700622
Wenyi Wang79675452016-08-17 10:43:28 -0700623 private void setMenuChecked(MenuItem menuItem, boolean checked) {
624 menuItem.setCheckable(checked);
625 menuItem.setChecked(checked);
626 }
627
Walter Jang13710d52016-07-08 14:36:20 -0700628 private void selectAccount() {
629 final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this)
630 .getAccounts(/* writable */ true);
631 if (accounts.isEmpty()) {
632 // We shouldn't present the add group button if there are no writable accounts
633 // but check it since it's possible we are started with an Intent.
634 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
635 return;
636 }
637 // If there is a single writable account, use it w/o showing a dialog.
638 if (accounts.size() == 1) {
639 onAccountChosen(accounts.get(0), /* extraArgs */ null);
640 return;
641 }
642 SelectAccountDialogFragment.show(getFragmentManager(), null,
643 R.string.dialog_new_group_account, AccountListFilter.ACCOUNTS_GROUP_WRITABLE,
644 /* extraArgs */ null, TAG_SELECT_ACCOUNT_DIALOG);
645 }
646
647 @Override
648 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
649 mNewGroupAccount = account;
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700650 GroupNameEditDialogFragment.newInstanceForCreation(
651 mNewGroupAccount, GroupUtil.ACTION_CREATE_GROUP)
Marcus Hagerott8d62cd22016-08-01 17:31:38 -0700652 .show(getFragmentManager(), TAG_GROUP_NAME_EDIT_DIALOG);
Walter Jang13710d52016-07-08 14:36:20 -0700653 }
654
655 @Override
656 public void onAccountSelectorCancelled() {
657 }
Wenyi Wang79675452016-08-17 10:43:28 -0700658
659 private void updateMenuSelection(MenuItem menuItem) {
660 clearCheckedMenus();
661 setMenuChecked(menuItem, true);
662 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700663}