blob: 497295d462e7ff3de47d6784ce5da0f128477426 [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;
John Shao52dd5d72016-10-06 15:16:14 -070023import android.content.res.Configuration;
Wenyi Wangbb229242016-05-25 16:04:13 -070024import android.graphics.Color;
25import android.graphics.PorterDuff;
26import android.os.Bundle;
Walter Jang13710d52016-07-08 14:36:20 -070027import android.provider.ContactsContract.Intents;
Wenyi Wangbb229242016-05-25 16:04:13 -070028import android.support.annotation.LayoutRes;
29import android.support.design.widget.NavigationView;
30import 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;
John Shaoa3ce3c72016-10-11 14:58:57 -070047import com.android.contacts.common.Experiments;
Wenyi Wang8e73c362016-06-23 18:03:53 -070048import com.android.contacts.common.compat.CompatUtils;
Walter Jang92942632016-07-14 19:49:32 +000049import com.android.contacts.common.list.AccountFilterActivity;
Wenyi Wangbb229242016-05-25 16:04:13 -070050import com.android.contacts.common.list.ContactListFilter;
51import com.android.contacts.common.list.ContactListFilterController;
Walter Jang13710d52016-07-08 14:36:20 -070052import com.android.contacts.common.model.AccountTypeManager;
John Shao52dd5d72016-10-06 15:16:14 -070053import com.android.contacts.common.model.account.AccountDisplayInfo;
54import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Walter Jang13710d52016-07-08 14:36:20 -070055import com.android.contacts.common.model.account.AccountWithDataSet;
Wenyi Wangbb229242016-05-25 16:04:13 -070056import com.android.contacts.common.preference.ContactsPreferenceActivity;
57import com.android.contacts.common.util.AccountFilterUtil;
Walter Jang13710d52016-07-08 14:36:20 -070058import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
Wenyi Wangbb229242016-05-25 16:04:13 -070059import com.android.contacts.common.util.ImplicitIntentsUtil;
Wenyi Wang92d7e512016-10-03 14:57:19 -070060import com.android.contacts.common.util.MaterialColorMapUtils;
Wenyi Wangbb229242016-05-25 16:04:13 -070061import com.android.contacts.common.util.ViewUtil;
Gary Mai363af602016-09-28 10:01:23 -070062import com.android.contacts.editor.ContactEditorFragment;
Walter Jang55d0e712016-08-31 17:11:36 -070063import com.android.contacts.editor.SelectAccountDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070064import com.android.contacts.group.GroupListItem;
Wenyi Wang79675452016-08-17 10:43:28 -070065import com.android.contacts.group.GroupMembersFragment;
Walter Jang428824e2016-09-09 13:18:35 -070066import com.android.contacts.group.GroupMetaData;
Walter Jang13710d52016-07-08 14:36:20 -070067import com.android.contacts.group.GroupNameEditDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070068import com.android.contacts.group.GroupUtil;
69import com.android.contacts.group.GroupsFragment;
70import com.android.contacts.group.GroupsFragment.GroupsListener;
71import com.android.contacts.interactions.AccountFiltersFragment;
72import com.android.contacts.interactions.AccountFiltersFragment.AccountFiltersListener;
Wenyi Wang79675452016-08-17 10:43:28 -070073import com.android.contacts.list.DefaultContactBrowseListFragment;
74import com.android.contacts.list.MultiSelectContactsListFragment;
Tingting Wangac9596e2016-08-02 22:24:24 -070075import com.android.contacts.util.SharedPreferenceUtil;
Wenyi Wangbb229242016-05-25 16:04:13 -070076import com.android.contactsbind.HelpUtils;
Marcus Hagerott6caf23f2016-08-18 15:02:42 -070077import com.android.contactsbind.ObjectFactory;
John Shaoa3ce3c72016-10-11 14:58:57 -070078import com.android.contactsbind.experiments.Flags;
Wenyi Wangbb229242016-05-25 16:04:13 -070079
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070080import java.util.HashMap;
81import java.util.Iterator;
Wenyi Wangbb229242016-05-25 16:04:13 -070082import java.util.List;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070083import java.util.Map;
84import java.util.Map.Entry;
Wenyi Wangbb229242016-05-25 16:04:13 -070085
86/**
87 * A common superclass for Contacts activities with a navigation drawer.
88 */
89public abstract class ContactsDrawerActivity extends AppCompatContactsActivity implements
90 AccountFiltersListener,
91 GroupsListener,
Walter Jang13710d52016-07-08 14:36:20 -070092 NavigationView.OnNavigationItemSelectedListener,
Marcus Hagerott8d62cd22016-08-01 17:31:38 -070093 SelectAccountDialogFragment.Listener {
Wenyi Wangbb229242016-05-25 16:04:13 -070094
Wenyi Wang79675452016-08-17 10:43:28 -070095 /** Possible views of Contacts app. */
96 public enum ContactsView {
97 NONE,
98 ALL_CONTACTS,
John Shao52dd5d72016-10-06 15:16:14 -070099 ASSISTANT,
Wenyi Wang79675452016-08-17 10:43:28 -0700100 GROUP_VIEW,
101 ACCOUNT_VIEW,
102 }
103
Wenyi Wangbb229242016-05-25 16:04:13 -0700104 protected static String TAG = "ContactsDrawerActivity";
105
Walter Jang13710d52016-07-08 14:36:20 -0700106 private static final String TAG_GROUPS = "groups";
107 private static final String TAG_FILTERS = "filters";
108 private static final String TAG_SELECT_ACCOUNT_DIALOG = "selectAccountDialog";
109 private static final String TAG_GROUP_NAME_EDIT_DIALOG = "groupNameEditDialog";
110
111 private static final String KEY_NEW_GROUP_ACCOUNT = "newGroupAccount";
Wenyi Wang79675452016-08-17 10:43:28 -0700112 private static final String KEY_CONTACTS_VIEW = "contactsView";
Walter Jang13710d52016-07-08 14:36:20 -0700113
Wenyi Wang79675452016-08-17 10:43:28 -0700114 protected ContactsView mCurrentView;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700115
Wenyi Wang8e73c362016-06-23 18:03:53 -0700116 private class ContactsActionBarDrawerToggle extends ActionBarDrawerToggle {
117
118 private Runnable mRunnable;
Tingting Wangac9596e2016-08-02 22:24:24 -0700119 private boolean mMenuClickedBefore = SharedPreferenceUtil.getHamburgerMenuClickedBefore(
120 ContactsDrawerActivity.this);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700121
122 public ContactsActionBarDrawerToggle(AppCompatActivity activity, DrawerLayout drawerLayout,
123 Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
124 super(activity, drawerLayout, toolbar, openDrawerContentDescRes,
125 closeDrawerContentDescRes);
126 }
127
128 @Override
129 public void onDrawerOpened(View drawerView) {
130 super.onDrawerOpened(drawerView);
Tingting Wangac9596e2016-08-02 22:24:24 -0700131 if (!mMenuClickedBefore) {
132 SharedPreferenceUtil.setHamburgerMenuClickedBefore(ContactsDrawerActivity.this);
133 mMenuClickedBefore = true;
134 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700135 invalidateOptionsMenu();
Wenyi Wang79675452016-08-17 10:43:28 -0700136 // Stop search and selection mode like Gmail and Keep. Otherwise, if user switches to
137 // another fragment in navigation drawer, the current search/selection mode will be
138 // overlaid by the action bar of the newly-created fragment.
139 stopSearchAndSelection();
Wenyi Wang92d7e512016-10-03 14:57:19 -0700140 updateStatusBarBackground();
Wenyi Wang79675452016-08-17 10:43:28 -0700141 }
142
143 private void stopSearchAndSelection() {
144 final MultiSelectContactsListFragment listFragment;
145 if (isAllContactsView() || isAccountView()) {
146 listFragment = getAllFragment();
147 } else if (isGroupView()) {
148 listFragment = getGroupFragment();
149 } else {
150 listFragment = null;
151 }
152 if (listFragment == null) {
153 return;
154 }
155 final ActionBarAdapter actionBarAdapter = listFragment.getActionBarAdapter();
156 if (actionBarAdapter == null) {
157 return;
158 }
159 if (actionBarAdapter.isSearchMode()) {
160 actionBarAdapter.setSearchMode(false);
161 } else if (actionBarAdapter.isSelectionMode()) {
162 actionBarAdapter.setSelectionMode(false);
163 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700164 }
165
166 @Override
167 public void onDrawerClosed(View view) {
168 super.onDrawerClosed(view);
169 invalidateOptionsMenu();
170 }
171
172 @Override
173 public void onDrawerStateChanged(int newState) {
174 super.onDrawerStateChanged(newState);
175 // Set transparent status bar when drawer starts to move.
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700176 if (newState != DrawerLayout.STATE_IDLE) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700177 updateStatusBarBackground();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700178 }
179 if (mRunnable != null && newState == DrawerLayout.STATE_IDLE) {
180 mRunnable.run();
181 mRunnable = null;
182 }
183 }
184
185 public void runWhenIdle(Runnable runnable) {
186 mRunnable = runnable;
187 }
188 }
189
Wenyi Wangbb229242016-05-25 16:04:13 -0700190 protected ContactListFilterController mContactListFilterController;
191 protected DrawerLayout mDrawer;
Wenyi Wang8e73c362016-06-23 18:03:53 -0700192 protected ContactsActionBarDrawerToggle mToggle;
Wenyi Wangbb229242016-05-25 16:04:13 -0700193 protected Toolbar mToolbar;
194 protected NavigationView mNavigationView;
195 protected GroupsFragment mGroupsFragment;
196 protected AccountFiltersFragment mAccountFiltersFragment;
197
Walter Jang13710d52016-07-08 14:36:20 -0700198 // The account the new group will be created under.
199 private AccountWithDataSet mNewGroupAccount;
200
Wenyi Wang79675452016-08-17 10:43:28 -0700201 // Checkable menu item lookup maps. Every map declared here should be added to
202 // clearCheckedMenus() so that they can be cleared.
203 // TODO find a better way to handle selected menu item state, when switching to fragments.
204 protected Map<Long, MenuItem> mGroupMenuMap = new HashMap<>();
205 protected Map<ContactListFilter, MenuItem> mFilterMenuMap = new HashMap<>();
206 protected Map<Integer, MenuItem> mIdMenuMap = new HashMap<>();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700207
Wenyi Wangbb229242016-05-25 16:04:13 -0700208 @Override
209 protected void onCreate(Bundle savedState) {
210 super.onCreate(savedState);
211
212 mContactListFilterController = ContactListFilterController.getInstance(this);
213 mContactListFilterController.checkFilterValidity(false);
214
215 super.setContentView(R.layout.contacts_drawer_activity);
216
217 // Set up the action bar.
218 mToolbar = getView(R.id.toolbar);
219 setSupportActionBar(mToolbar);
220
221 // Add shadow under toolbar.
222 ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
223
224 // Set up hamburger button.
225 mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700226 mToggle = new ContactsActionBarDrawerToggle(this, mDrawer, mToolbar,
Wenyi Wangbb229242016-05-25 16:04:13 -0700227 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
John Shao52dd5d72016-10-06 15:16:14 -0700228
Wenyi Wang8e73c362016-06-23 18:03:53 -0700229 mDrawer.setDrawerListener(mToggle);
John Shao52dd5d72016-10-06 15:16:14 -0700230 // Set fallback handler for when drawer is disabled.
231 mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
232 @Override
233 public void onClick(View v) {
234 onBackPressed();
235 }
236 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700237
Wenyi Wang79675452016-08-17 10:43:28 -0700238 // Set up navigation mode.
239 if (savedState != null) {
240 mCurrentView = ContactsView.values()[savedState.getInt(KEY_CONTACTS_VIEW)];
241 } else {
242 mCurrentView = ContactsView.ALL_CONTACTS;
243 }
244
Wenyi Wangbb229242016-05-25 16:04:13 -0700245 // Set up hamburger menu items.
246 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
247 mNavigationView.setNavigationItemSelectedListener(this);
Wenyi Wang79675452016-08-17 10:43:28 -0700248 setUpMenu();
Wenyi Wangbb229242016-05-25 16:04:13 -0700249
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700250 loadGroupsAndFilters();
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700251
Walter Jang13710d52016-07-08 14:36:20 -0700252 if (savedState != null && savedState.containsKey(KEY_NEW_GROUP_ACCOUNT)) {
253 mNewGroupAccount = AccountWithDataSet.unstringify(
254 savedState.getString(KEY_NEW_GROUP_ACCOUNT));
255 }
256 }
257
John Shao52dd5d72016-10-06 15:16:14 -0700258 public void setDrawerLockMode(boolean enabled) {
259 // Prevent drawer from being opened by sliding from the start of screen.
260 mDrawer.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED
261 : DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
262
263 // Order of these statements matter.
264 // Display back button and disable drawer indicator.
265 if (enabled) {
266 getSupportActionBar().setDisplayHomeAsUpEnabled(false);
267 mToggle.setDrawerIndicatorEnabled(true);
268 } else {
269 mToggle.setDrawerIndicatorEnabled(false);
270 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
271 }
272 }
273
Wenyi Wang79675452016-08-17 10:43:28 -0700274 private void setUpMenu() {
275 final Menu menu = mNavigationView.getMenu();
276
John Shaoa3ce3c72016-10-11 14:58:57 -0700277 if (ObjectFactory.getDuplicatesUtilFragment() == null) {
John Shao52dd5d72016-10-06 15:16:14 -0700278 menu.removeItem(R.id.nav_assistant);
John Shaoa3ce3c72016-10-11 14:58:57 -0700279 menu.removeItem(R.id.nav_find_duplicates);
Wenyi Wang79675452016-08-17 10:43:28 -0700280 } else {
John Shaoa3ce3c72016-10-11 14:58:57 -0700281 int id;
Walter Jangdf86ede2016-10-19 09:48:29 -0700282 if (Flags.getInstance().getBoolean(Experiments.ASSISTANT)) {
John Shaoa3ce3c72016-10-11 14:58:57 -0700283 id = R.id.nav_assistant;
284 menu.removeItem(R.id.nav_find_duplicates);
285 } else {
286 id = R.id.nav_find_duplicates;
287 menu.removeItem(R.id.nav_assistant);
288 }
289
290 final MenuItem assistantMenu = menu.findItem(id);
291 mIdMenuMap.put(id, assistantMenu);
John Shao52dd5d72016-10-06 15:16:14 -0700292 if (isAssistantView()) {
293 updateMenuSelection(assistantMenu);
Wenyi Wang79675452016-08-17 10:43:28 -0700294 }
295 }
296
297 if (!HelpUtils.isHelpAndFeedbackAvailable()) {
298 menu.removeItem(R.id.nav_help);
299 }
300
301 final MenuItem allContactsMenu = menu.findItem(R.id.nav_all_contacts);
302 mIdMenuMap.put(R.id.nav_all_contacts, allContactsMenu);
303 if (isAllContactsView()) {
304 updateMenuSelection(allContactsMenu);
305 }
Wenyi Wang6927bf32016-08-15 18:31:24 -0700306 }
307
Wenyi Wang79675452016-08-17 10:43:28 -0700308 public Toolbar getToolbar() {
309 return mToolbar;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700310 }
311
Walter Jang13710d52016-07-08 14:36:20 -0700312 @Override
313 protected void onSaveInstanceState(Bundle outState) {
314 super.onSaveInstanceState(outState);
315 if (mNewGroupAccount != null) {
316 outState.putString(KEY_NEW_GROUP_ACCOUNT, mNewGroupAccount.stringify());
317 }
Wenyi Wang79675452016-08-17 10:43:28 -0700318 outState.putInt(KEY_CONTACTS_VIEW, mCurrentView.ordinal());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700319 }
320
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700321 @Override
322 protected void onResume() {
323 super.onResume();
324 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700325 updateStatusBarBackground();
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700326 }
327 }
328
Wenyi Wang92d7e512016-10-03 14:57:19 -0700329 public void updateStatusBarBackground() {
330 updateStatusBarBackground(/* color */ -1);
331 }
332
333 public void updateStatusBarBackground(int color) {
334 if (!CompatUtils.isLollipopCompatible()) return;
335 if (color == -1) {
336 mDrawer.setStatusBarBackgroundColor(MaterialColorMapUtils.getStatusBarColor(this));
337 } else {
338 mDrawer.setStatusBarBackgroundColor(color);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700339 }
Wenyi Wang92d7e512016-10-03 14:57:19 -0700340 mDrawer.invalidate();
341 getWindow().setStatusBarColor(Color.TRANSPARENT);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700342 }
343
John Shao52dd5d72016-10-06 15:16:14 -0700344 @Override
345 protected void onPostCreate(Bundle savedInstanceState) {
346 super.onPostCreate(savedInstanceState);
347 mToggle.syncState();
348 }
349
350 @Override
351 public void onConfigurationChanged(Configuration newConfig) {
352 super.onConfigurationChanged(newConfig);
353 mToggle.onConfigurationChanged(newConfig);
354 }
355
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700356 // Set up fragment manager to load groups and filters.
357 protected void loadGroupsAndFilters() {
Wenyi Wangbb229242016-05-25 16:04:13 -0700358 final FragmentManager fragmentManager = getFragmentManager();
359 final FragmentTransaction transaction = fragmentManager.beginTransaction();
360 addGroupsAndFiltersFragments(transaction);
361 transaction.commitAllowingStateLoss();
362 fragmentManager.executePendingTransactions();
363 }
364
365 @Override
366 public void setContentView(@LayoutRes int layoutResID) {
367 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
368 if (parent != null) {
369 parent.removeAllViews();
370 }
371 LayoutInflater.from(this).inflate(layoutResID, parent);
372 }
373
374 protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) {
375 final FragmentManager fragmentManager = getFragmentManager();
Walter Jang13710d52016-07-08 14:36:20 -0700376 mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(TAG_GROUPS);
Walter Jang060f3242016-09-07 15:23:22 -0700377 if (mGroupsFragment == null) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700378 mGroupsFragment = new GroupsFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700379 transaction.add(mGroupsFragment, TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700380 }
Walter Jang060f3242016-09-07 15:23:22 -0700381 mGroupsFragment.setListener(this);
Wenyi Wangbb229242016-05-25 16:04:13 -0700382
Walter Jang060f3242016-09-07 15:23:22 -0700383 mAccountFiltersFragment = (AccountFiltersFragment)
384 fragmentManager.findFragmentByTag(TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700385 if (mAccountFiltersFragment == null) {
386 mAccountFiltersFragment = new AccountFiltersFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700387 transaction.add(mAccountFiltersFragment, TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700388 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700389 mAccountFiltersFragment.setListener(this);
390 }
391
392 @Override
393 public void onGroupsLoaded(List<GroupListItem> groupListItems) {
394 final Menu menu = mNavigationView.getMenu();
395 final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups);
396 final SubMenu subMenu = groupsMenuItem.getSubMenu();
397 subMenu.removeGroup(R.id.nav_groups_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700398 mGroupMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700399
Walter Jang5a496962016-09-09 18:32:50 -0700400 final GroupMetaData groupMetaData = getGroupMetaData();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700401
Wenyi Wangbb229242016-05-25 16:04:13 -0700402 if (groupListItems != null) {
403 // Add each group
404 for (final GroupListItem groupListItem : groupListItems) {
405 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
406 continue;
407 }
408 final String title = groupListItem.getTitle();
409 final MenuItem menuItem =
Wenyi Wang79675452016-08-17 10:43:28 -0700410 subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, title);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700411 mGroupMenuMap.put(groupListItem.getGroupId(), menuItem);
Wenyi Wang79675452016-08-17 10:43:28 -0700412 if (isGroupView() && groupMetaData != null
413 && groupMetaData.groupId == groupListItem.getGroupId()) {
414 updateMenuSelection(menuItem);
415 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700416 menuItem.setIcon(R.drawable.ic_menu_label);
417 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
418 @Override
419 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700420 mToggle.runWhenIdle(new Runnable() {
421 @Override
422 public void run() {
Marcus Hagerottc1698712016-10-20 09:14:06 -0700423 onGroupMenuItemClicked(groupListItem);
Wenyi Wang79675452016-08-17 10:43:28 -0700424 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700425 }
426 });
427 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700428 return true;
429 }
430 });
431 }
432 }
433
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700434 // Don't show "Create new..." menu if there's no group-writable accounts available.
435 if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) {
436 return;
437 }
438
Wenyi Wangbb229242016-05-25 16:04:13 -0700439 // Create a menu item in the sub menu to add new groups
Wenyi Wang45c210a2016-07-15 10:51:52 -0700440 final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700441 Menu.NONE, getString(R.string.menu_new_group_action_bar));
Wenyi Wang8cd7c5c2016-06-16 14:03:42 -0700442 menuItem.setIcon(R.drawable.ic_add);
Wenyi Wangbb229242016-05-25 16:04:13 -0700443 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
444 @Override
445 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700446 mToggle.runWhenIdle(new Runnable() {
447 @Override
448 public void run() {
449 onCreateGroupMenuItemClicked();
450 }
451 });
452 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700453 return true;
454 }
455 });
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700456
Wenyi Wang79675452016-08-17 10:43:28 -0700457 if (isGroupView() && groupMetaData != null) {
458 updateGroupMenu(groupMetaData);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700459 }
460 }
461
Walter Jang5a496962016-09-09 18:32:50 -0700462 public void updateGroupMenu(GroupMetaData groupMetaData) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700463 clearCheckedMenus();
Walter Jang428824e2016-09-09 13:18:35 -0700464 if (groupMetaData != null && mGroupMenuMap != null
465 && mGroupMenuMap.get(groupMetaData.groupId) != null) {
Walter Jang5a496962016-09-09 18:32:50 -0700466 setMenuChecked(mGroupMenuMap.get(groupMetaData.groupId), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700467 }
468 }
469
Walter Jang428824e2016-09-09 13:18:35 -0700470 protected GroupMetaData getGroupMetaData() {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700471 return null;
Wenyi Wangbb229242016-05-25 16:04:13 -0700472 }
473
Wenyi Wang92d7e512016-10-03 14:57:19 -0700474 public boolean isGroupView() {
Wenyi Wang79675452016-08-17 10:43:28 -0700475 return mCurrentView == ContactsView.GROUP_VIEW;
Wenyi Wangbb229242016-05-25 16:04:13 -0700476 }
477
John Shao52dd5d72016-10-06 15:16:14 -0700478 protected boolean isAssistantView() {
479 return mCurrentView == ContactsView.ASSISTANT;
Wenyi Wang79675452016-08-17 10:43:28 -0700480 }
481
482 protected boolean isAllContactsView() {
483 return mCurrentView == ContactsView.ALL_CONTACTS;
484 }
485
486 protected boolean isAccountView() {
487 return mCurrentView == ContactsView.ACCOUNT_VIEW;
488 }
489
490 public boolean isInSecondLevel() {
John Shao52dd5d72016-10-06 15:16:14 -0700491 return isGroupView() || isAssistantView();
Wenyi Wang79675452016-08-17 10:43:28 -0700492 }
493
Marcus Hagerottc1698712016-10-20 09:14:06 -0700494 protected abstract void onGroupMenuItemClicked(GroupListItem group);
Wenyi Wang79675452016-08-17 10:43:28 -0700495
Walter Jang1110d672016-07-25 17:55:12 +0000496 protected void onCreateGroupMenuItemClicked() {
Walter Jang13710d52016-07-08 14:36:20 -0700497 // Select the account to create the group
498 final Bundle extras = getIntent().getExtras();
499 final Account account = extras == null ? null :
500 (Account) extras.getParcelable(Intents.Insert.EXTRA_ACCOUNT);
501 if (account == null) {
Walter Jang552e5822016-09-10 12:03:24 -0700502 selectAccountForNewGroup();
Walter Jang13710d52016-07-08 14:36:20 -0700503 } else {
504 final String dataSet = extras == null
505 ? null : extras.getString(Intents.Insert.EXTRA_DATA_SET);
506 final AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
507 account.name, account.type, dataSet);
508 onAccountChosen(accountWithDataSet, /* extraArgs */ null);
509 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700510 }
511
512 @Override
513 public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700514 final AccountDisplayInfoFactory accountDisplayFactory = AccountDisplayInfoFactory.
515 fromListFilters(this, accountFilterItems);
516
Wenyi Wangbb229242016-05-25 16:04:13 -0700517 final Menu menu = mNavigationView.getMenu();
518 final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters);
519 final SubMenu subMenu = filtersMenuItem.getSubMenu();
520 subMenu.removeGroup(R.id.nav_filters_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700521 mFilterMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700522
523 if (accountFilterItems == null || accountFilterItems.size() < 2) {
524 return;
525 }
526
Marcus Hagerott6caf23f2016-08-18 15:02:42 -0700527
Wenyi Wangbb229242016-05-25 16:04:13 -0700528 for (int i = 0; i < accountFilterItems.size(); i++) {
529 final ContactListFilter filter = accountFilterItems.get(i);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700530 final AccountDisplayInfo displayableAccount =
531 accountDisplayFactory.getAccountDisplayInfoFor(filter);
532 final CharSequence menuName = displayableAccount.getNameLabel();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700533 final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700534 Menu.NONE, menuName);
535 if (isAccountView() && filter == mContactListFilterController.getFilter()) {
536 updateMenuSelection(menuItem);
537 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700538 mFilterMenuMap.put(filter, menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700539 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000540 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700541 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
542 @Override
543 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700544 mToggle.runWhenIdle(new Runnable() {
545 @Override
546 public void run() {
Wenyi Wang79675452016-08-17 10:43:28 -0700547 onFilterMenuItemClicked(intent);
548 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700549 }
550 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700551 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700552 return true;
553 }
554 });
Walter Jang98d41d72016-09-15 13:12:51 -0700555 if (displayableAccount.getIcon() != null) {
556 menuItem.setIcon(displayableAccount.getIcon());
557 // Get rid of the default menu item overlay and show original account icons.
558 menuItem.getIcon().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
559 }
Gary Maif354d182016-07-25 13:59:30 -0700560 // Create a dummy action view to attach extra hidden content description to the menuItem
561 // for Talkback. We want Talkback to read out the account type but not have it be part
562 // of the menuItem title.
Gary Maif354d182016-07-25 13:59:30 -0700563 LinearLayout view = (LinearLayout) LayoutInflater.from(this)
564 .inflate(R.layout.account_type_info, null);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700565 view.setContentDescription(displayableAccount.getTypeLabel());
Gary Maif354d182016-07-25 13:59:30 -0700566 view.setVisibility(View.VISIBLE);
567 menuItem.setActionView(view);
Wenyi Wangbb229242016-05-25 16:04:13 -0700568 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700569
Wenyi Wang79675452016-08-17 10:43:28 -0700570 if (isAccountView()) {
571 updateFilterMenu(mContactListFilterController.getFilter());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700572 }
573 }
574
Wenyi Wang587b8f22016-08-17 17:40:45 -0700575 public void updateFilterMenu(ContactListFilter filter) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700576 clearCheckedMenus();
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700577 if (filter != null && filter.isContactsFilterType()) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700578 if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700579 setMenuChecked(mIdMenuMap.get(R.id.nav_all_contacts), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700580 }
581 } else {
582 if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700583 setMenuChecked(mFilterMenuMap.get(filter), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700584 }
585 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700586 }
587
Wenyi Wang79675452016-08-17 10:43:28 -0700588 protected void onFilterMenuItemClicked(Intent intent) {
Marcus Hagerottc1698712016-10-20 09:14:06 -0700589 resetFilter();
Wenyi Wang79675452016-08-17 10:43:28 -0700590 AccountFilterUtil.handleAccountFilterResult(mContactListFilterController,
591 AppCompatActivity.RESULT_OK, intent);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700592 }
593
Wenyi Wangbb229242016-05-25 16:04:13 -0700594 @Override
Wenyi Wang8e73c362016-06-23 18:03:53 -0700595 public boolean onNavigationItemSelected(final MenuItem item) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700596 final int id = item.getItemId();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700597 mToggle.runWhenIdle(new Runnable() {
598 @Override
599 public void run() {
600 if (id == R.id.nav_settings) {
601 startActivity(createPreferenceIntent());
602 } else if (id == R.id.nav_help) {
603 HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this);
604 } else if (id == R.id.nav_all_contacts) {
605 switchToAllContacts();
John Shaoa3ce3c72016-10-11 14:58:57 -0700606 } else if (id == R.id.nav_assistant || id == R.id.nav_find_duplicates) {
John Shao52dd5d72016-10-06 15:16:14 -0700607 if (!isAssistantView()) {
608 launchAssistant();
Wenyi Wang79675452016-08-17 10:43:28 -0700609 updateMenuSelection(item);
610 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700611 } else if (item.getIntent() != null) {
612 ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this,
613 item.getIntent());
614 } else {
615 Log.w(TAG, "Unhandled navigation view item selection");
616 }
617 }
618 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700619
620 mDrawer.closeDrawer(GravityCompat.START);
621 return true;
622 }
623
624 private Intent createPreferenceIntent() {
625 final Intent intent = new Intent(this, ContactsPreferenceActivity.class);
626 intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE,
Gary Mai363af602016-09-28 10:01:23 -0700627 ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE);
Wenyi Wangbb229242016-05-25 16:04:13 -0700628 return intent;
629 }
630
Wenyi Wang79675452016-08-17 10:43:28 -0700631 public void switchToAllContacts() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700632 resetFilter();
Wenyi Wang79675452016-08-17 10:43:28 -0700633
634 final Menu menu = mNavigationView.getMenu();
635 final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts);
636 updateMenuSelection(allContacts);
637
638 setTitle(getString(R.string.contactsList));
Wenyi Wangbb229242016-05-25 16:04:13 -0700639 }
640
Wenyi Wang32912eb2016-09-29 17:17:07 -0700641 private void resetFilter() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700642 final Intent intent = new Intent();
643 final ContactListFilter filter = AccountFilterUtil.createContactsFilter(this);
644 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
645 AccountFilterUtil.handleAccountFilterResult(
646 mContactListFilterController, AppCompatActivity.RESULT_OK, intent);
647 }
648
John Shao52dd5d72016-10-06 15:16:14 -0700649 protected abstract void launchAssistant();
Wenyi Wang79675452016-08-17 10:43:28 -0700650
651 protected abstract DefaultContactBrowseListFragment getAllFragment();
652
653 protected abstract GroupMembersFragment getGroupFragment();
654
655 public abstract void showFabWithAnimation(boolean showFab);
Wenyi Wangbb229242016-05-25 16:04:13 -0700656
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700657 private void clearCheckedMenus() {
658 clearCheckedMenu(mFilterMenuMap);
659 clearCheckedMenu(mGroupMenuMap);
660 clearCheckedMenu(mIdMenuMap);
661 }
Wenyi Wang79675452016-08-17 10:43:28 -0700662
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700663 private void clearCheckedMenu(Map<?, MenuItem> map) {
664 final Iterator it = map.entrySet().iterator();
665 while (it.hasNext()) {
Wenyi Wang79675452016-08-17 10:43:28 -0700666 Entry pair = (Entry) it.next();
667 setMenuChecked(map.get(pair.getKey()), false);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700668 }
669 }
Walter Jang13710d52016-07-08 14:36:20 -0700670
Wenyi Wang79675452016-08-17 10:43:28 -0700671 private void setMenuChecked(MenuItem menuItem, boolean checked) {
672 menuItem.setCheckable(checked);
673 menuItem.setChecked(checked);
674 }
675
Walter Jang552e5822016-09-10 12:03:24 -0700676 private void selectAccountForNewGroup() {
Walter Jang13710d52016-07-08 14:36:20 -0700677 final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this)
Walter Jang552e5822016-09-10 12:03:24 -0700678 .getGroupWritableAccounts();
Walter Jang13710d52016-07-08 14:36:20 -0700679 if (accounts.isEmpty()) {
680 // We shouldn't present the add group button if there are no writable accounts
681 // but check it since it's possible we are started with an Intent.
682 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
683 return;
684 }
685 // If there is a single writable account, use it w/o showing a dialog.
686 if (accounts.size() == 1) {
687 onAccountChosen(accounts.get(0), /* extraArgs */ null);
688 return;
689 }
690 SelectAccountDialogFragment.show(getFragmentManager(), null,
691 R.string.dialog_new_group_account, AccountListFilter.ACCOUNTS_GROUP_WRITABLE,
692 /* extraArgs */ null, TAG_SELECT_ACCOUNT_DIALOG);
693 }
694
695 @Override
696 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
697 mNewGroupAccount = account;
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700698 GroupNameEditDialogFragment.newInstanceForCreation(
699 mNewGroupAccount, GroupUtil.ACTION_CREATE_GROUP)
Marcus Hagerott8d62cd22016-08-01 17:31:38 -0700700 .show(getFragmentManager(), TAG_GROUP_NAME_EDIT_DIALOG);
Walter Jang13710d52016-07-08 14:36:20 -0700701 }
702
703 @Override
704 public void onAccountSelectorCancelled() {
705 }
Wenyi Wang79675452016-08-17 10:43:28 -0700706
707 private void updateMenuSelection(MenuItem menuItem) {
708 clearCheckedMenus();
709 setMenuChecked(menuItem, true);
710 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700711}