blob: f0bc37e3db815075ed536fd2b23d4444ddcc6c9c [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;
Wenyi Wang79ef0882016-11-16 16:45:51 -080027import android.os.Handler;
Walter Jang13710d52016-07-08 14:36:20 -070028import android.provider.ContactsContract.Intents;
Wenyi Wangbb229242016-05-25 16:04:13 -070029import android.support.annotation.LayoutRes;
30import android.support.design.widget.NavigationView;
31import android.support.v4.view.GravityCompat;
Sean Midforddfd0b0d2016-11-01 15:05:33 -070032import android.support.v4.view.MenuItemCompat;
Wenyi Wangbb229242016-05-25 16:04:13 -070033import android.support.v4.widget.DrawerLayout;
34import android.support.v7.app.ActionBarDrawerToggle;
35import android.support.v7.app.AppCompatActivity;
36import android.support.v7.widget.Toolbar;
Wenyi Wangbb229242016-05-25 16:04:13 -070037import android.util.Log;
Sean Midforddfd0b0d2016-11-01 15:05:33 -070038import android.view.Gravity;
Wenyi Wangbb229242016-05-25 16:04:13 -070039import android.view.LayoutInflater;
40import android.view.Menu;
41import android.view.MenuItem;
42import android.view.SubMenu;
Wenyi Wang8e73c362016-06-23 18:03:53 -070043import android.view.View;
Wenyi Wangbb229242016-05-25 16:04:13 -070044import android.view.ViewGroup;
Gary Maif354d182016-07-25 13:59:30 -070045import android.widget.LinearLayout;
Sean Midforddfd0b0d2016-11-01 15:05:33 -070046import android.widget.TextView;
Walter Jang13710d52016-07-08 14:36:20 -070047import android.widget.Toast;
Wenyi Wangbb229242016-05-25 16:04:13 -070048
Wenyi Wang79675452016-08-17 10:43:28 -070049import com.android.contacts.activities.ActionBarAdapter;
Gary Mai69c182a2016-12-05 13:07:03 -080050import com.android.contacts.compat.CompatUtils;
Gary Mai363af602016-09-28 10:01:23 -070051import com.android.contacts.editor.ContactEditorFragment;
Walter Jang55d0e712016-08-31 17:11:36 -070052import com.android.contacts.editor.SelectAccountDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070053import com.android.contacts.group.GroupListItem;
Wenyi Wang79675452016-08-17 10:43:28 -070054import com.android.contacts.group.GroupMembersFragment;
Walter Jang428824e2016-09-09 13:18:35 -070055import com.android.contacts.group.GroupMetaData;
Walter Jang13710d52016-07-08 14:36:20 -070056import com.android.contacts.group.GroupNameEditDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070057import com.android.contacts.group.GroupUtil;
58import com.android.contacts.group.GroupsFragment;
59import com.android.contacts.group.GroupsFragment.GroupsListener;
60import com.android.contacts.interactions.AccountFiltersFragment;
61import com.android.contacts.interactions.AccountFiltersFragment.AccountFiltersListener;
Gary Mai0a49afa2016-12-05 15:53:58 -080062import com.android.contacts.list.AccountFilterActivity;
63import com.android.contacts.list.ContactListFilter;
64import com.android.contacts.list.ContactListFilterController;
Wenyi Wang79675452016-08-17 10:43:28 -070065import com.android.contacts.list.DefaultContactBrowseListFragment;
66import com.android.contacts.list.MultiSelectContactsListFragment;
Gary Mai0a49afa2016-12-05 15:53:58 -080067import com.android.contacts.model.AccountTypeManager;
68import com.android.contacts.model.account.AccountDisplayInfo;
69import com.android.contacts.model.account.AccountDisplayInfoFactory;
70import com.android.contacts.model.account.AccountWithDataSet;
71import com.android.contacts.preference.ContactsPreferenceActivity;
72import com.android.contacts.util.AccountFilterUtil;
73import com.android.contacts.util.AccountsListAdapter.AccountListFilter;
74import com.android.contacts.util.ImplicitIntentsUtil;
75import com.android.contacts.util.MaterialColorMapUtils;
Tingting Wangac9596e2016-08-02 22:24:24 -070076import com.android.contacts.util.SharedPreferenceUtil;
Gary Mai0a49afa2016-12-05 15:53:58 -080077import com.android.contacts.util.ViewUtil;
Wenyi Wangbb229242016-05-25 16:04:13 -070078import com.android.contactsbind.HelpUtils;
Marcus Hagerott6caf23f2016-08-18 15:02:42 -070079import com.android.contactsbind.ObjectFactory;
Wenyi Wangbb229242016-05-25 16:04:13 -070080
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070081import java.util.HashMap;
82import java.util.Iterator;
Wenyi Wangbb229242016-05-25 16:04:13 -070083import java.util.List;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070084import java.util.Map;
85import java.util.Map.Entry;
Wenyi Wangbb229242016-05-25 16:04:13 -070086
87/**
88 * A common superclass for Contacts activities with a navigation drawer.
89 */
90public abstract class ContactsDrawerActivity extends AppCompatContactsActivity implements
91 AccountFiltersListener,
92 GroupsListener,
Walter Jang13710d52016-07-08 14:36:20 -070093 NavigationView.OnNavigationItemSelectedListener,
Marcus Hagerott8d62cd22016-08-01 17:31:38 -070094 SelectAccountDialogFragment.Listener {
Wenyi Wangbb229242016-05-25 16:04:13 -070095
Wenyi Wang79675452016-08-17 10:43:28 -070096 /** Possible views of Contacts app. */
97 public enum ContactsView {
98 NONE,
99 ALL_CONTACTS,
John Shao52dd5d72016-10-06 15:16:14 -0700100 ASSISTANT,
Wenyi Wang79675452016-08-17 10:43:28 -0700101 GROUP_VIEW,
102 ACCOUNT_VIEW,
103 }
104
Wenyi Wangbb229242016-05-25 16:04:13 -0700105 protected static String TAG = "ContactsDrawerActivity";
106
Walter Jang13710d52016-07-08 14:36:20 -0700107 private static final String TAG_GROUPS = "groups";
108 private static final String TAG_FILTERS = "filters";
109 private static final String TAG_SELECT_ACCOUNT_DIALOG = "selectAccountDialog";
110 private static final String TAG_GROUP_NAME_EDIT_DIALOG = "groupNameEditDialog";
111
112 private static final String KEY_NEW_GROUP_ACCOUNT = "newGroupAccount";
Wenyi Wang79675452016-08-17 10:43:28 -0700113 private static final String KEY_CONTACTS_VIEW = "contactsView";
Walter Jang13710d52016-07-08 14:36:20 -0700114
Wenyi Wang79ef0882016-11-16 16:45:51 -0800115 private static final long DRAWER_CLOSE_DELAY = 300L;
116
Wenyi Wang79675452016-08-17 10:43:28 -0700117 protected ContactsView mCurrentView;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700118
Wenyi Wang8e73c362016-06-23 18:03:53 -0700119 private class ContactsActionBarDrawerToggle extends ActionBarDrawerToggle {
Tingting Wangac9596e2016-08-02 22:24:24 -0700120 private boolean mMenuClickedBefore = SharedPreferenceUtil.getHamburgerMenuClickedBefore(
121 ContactsDrawerActivity.this);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700122
123 public ContactsActionBarDrawerToggle(AppCompatActivity activity, DrawerLayout drawerLayout,
124 Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
125 super(activity, drawerLayout, toolbar, openDrawerContentDescRes,
126 closeDrawerContentDescRes);
127 }
128
129 @Override
130 public void onDrawerOpened(View drawerView) {
131 super.onDrawerOpened(drawerView);
Tingting Wangac9596e2016-08-02 22:24:24 -0700132 if (!mMenuClickedBefore) {
133 SharedPreferenceUtil.setHamburgerMenuClickedBefore(ContactsDrawerActivity.this);
134 mMenuClickedBefore = true;
135 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700136 invalidateOptionsMenu();
Wenyi Wang79675452016-08-17 10:43:28 -0700137 // Stop search and selection mode like Gmail and Keep. Otherwise, if user switches to
138 // another fragment in navigation drawer, the current search/selection mode will be
139 // overlaid by the action bar of the newly-created fragment.
140 stopSearchAndSelection();
Wenyi Wang92d7e512016-10-03 14:57:19 -0700141 updateStatusBarBackground();
Wenyi Wang79675452016-08-17 10:43:28 -0700142 }
143
144 private void stopSearchAndSelection() {
145 final MultiSelectContactsListFragment listFragment;
146 if (isAllContactsView() || isAccountView()) {
147 listFragment = getAllFragment();
148 } else if (isGroupView()) {
149 listFragment = getGroupFragment();
150 } else {
151 listFragment = null;
152 }
153 if (listFragment == null) {
154 return;
155 }
156 final ActionBarAdapter actionBarAdapter = listFragment.getActionBarAdapter();
157 if (actionBarAdapter == null) {
158 return;
159 }
160 if (actionBarAdapter.isSearchMode()) {
161 actionBarAdapter.setSearchMode(false);
162 } else if (actionBarAdapter.isSelectionMode()) {
163 actionBarAdapter.setSelectionMode(false);
164 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700165 }
166
167 @Override
168 public void onDrawerClosed(View view) {
169 super.onDrawerClosed(view);
170 invalidateOptionsMenu();
171 }
172
173 @Override
174 public void onDrawerStateChanged(int newState) {
175 super.onDrawerStateChanged(newState);
176 // Set transparent status bar when drawer starts to move.
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700177 if (newState != DrawerLayout.STATE_IDLE) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700178 updateStatusBarBackground();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700179 }
Sean Midfordd0b21322016-11-02 14:17:26 -0700180 initializeAssistantNewBadge();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700181 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700182 }
183
Wenyi Wangbb229242016-05-25 16:04:13 -0700184 protected ContactListFilterController mContactListFilterController;
185 protected DrawerLayout mDrawer;
Wenyi Wang8e73c362016-06-23 18:03:53 -0700186 protected ContactsActionBarDrawerToggle mToggle;
Wenyi Wangbb229242016-05-25 16:04:13 -0700187 protected Toolbar mToolbar;
188 protected NavigationView mNavigationView;
189 protected GroupsFragment mGroupsFragment;
190 protected AccountFiltersFragment mAccountFiltersFragment;
191
Walter Jang13710d52016-07-08 14:36:20 -0700192 // The account the new group will be created under.
193 private AccountWithDataSet mNewGroupAccount;
194
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700195 // Recycle badge if possible
196 private TextView mAssistantNewBadge;
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);
John Shao52dd5d72016-10-06 15:16:14 -0700225
Wenyi Wang8e73c362016-06-23 18:03:53 -0700226 mDrawer.setDrawerListener(mToggle);
John Shao52dd5d72016-10-06 15:16:14 -0700227 // Set fallback handler for when drawer is disabled.
228 mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
229 @Override
230 public void onClick(View v) {
231 onBackPressed();
232 }
233 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700234
Wenyi Wang79675452016-08-17 10:43:28 -0700235 // Set up navigation mode.
236 if (savedState != null) {
237 mCurrentView = ContactsView.values()[savedState.getInt(KEY_CONTACTS_VIEW)];
238 } else {
239 mCurrentView = ContactsView.ALL_CONTACTS;
240 }
241
Wenyi Wangbb229242016-05-25 16:04:13 -0700242 // Set up hamburger menu items.
243 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
244 mNavigationView.setNavigationItemSelectedListener(this);
Wenyi Wang79675452016-08-17 10:43:28 -0700245 setUpMenu();
Wenyi Wangbb229242016-05-25 16:04:13 -0700246
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700247 initializeAssistantNewBadge();
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700248 loadGroupsAndFilters();
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700249
Walter Jang13710d52016-07-08 14:36:20 -0700250 if (savedState != null && savedState.containsKey(KEY_NEW_GROUP_ACCOUNT)) {
251 mNewGroupAccount = AccountWithDataSet.unstringify(
252 savedState.getString(KEY_NEW_GROUP_ACCOUNT));
253 }
254 }
255
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700256 private void initializeAssistantNewBadge() {
John Shao5b1ffdc2016-11-08 13:51:00 -0800257 if (mNavigationView == null) {
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700258 return;
259 }
Wenyi Wang1da57d82016-11-15 15:01:43 -0800260 final MenuItem assistantMenu = mNavigationView.getMenu().findItem(R.id.nav_assistant);
261 if (assistantMenu == null) {
262 return;
263 }
264 final LinearLayout newBadgeFrame =
265 (LinearLayout) MenuItemCompat.getActionView(assistantMenu);
Sean Midforddfd0b0d2016-11-01 15:05:33 -0700266 final boolean showWelcomeBadge = !SharedPreferenceUtil.isWelcomeCardDismissed(this);
267 if (showWelcomeBadge && newBadgeFrame.getChildCount() == 0) {
268 if (mAssistantNewBadge == null) {
269 mAssistantNewBadge = (TextView) LayoutInflater.from(this)
270 .inflate(R.layout.assistant_new_badge, null);
271 }
272 newBadgeFrame.setGravity(Gravity.CENTER_VERTICAL);
273 newBadgeFrame.addView(mAssistantNewBadge);
274 } else if (!showWelcomeBadge && newBadgeFrame.getChildCount() > 0) {
275 newBadgeFrame.removeAllViews();
276 }
277 }
278
John Shao52dd5d72016-10-06 15:16:14 -0700279 public void setDrawerLockMode(boolean enabled) {
280 // Prevent drawer from being opened by sliding from the start of screen.
281 mDrawer.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED
282 : DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
283
284 // Order of these statements matter.
285 // Display back button and disable drawer indicator.
286 if (enabled) {
287 getSupportActionBar().setDisplayHomeAsUpEnabled(false);
288 mToggle.setDrawerIndicatorEnabled(true);
289 } else {
290 mToggle.setDrawerIndicatorEnabled(false);
291 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
292 }
293 }
294
Wenyi Wang79675452016-08-17 10:43:28 -0700295 private void setUpMenu() {
296 final Menu menu = mNavigationView.getMenu();
297
John Shao5b1ffdc2016-11-08 13:51:00 -0800298 if (ObjectFactory.getAssistantFragment() == null) {
John Shao52dd5d72016-10-06 15:16:14 -0700299 menu.removeItem(R.id.nav_assistant);
Wenyi Wang79675452016-08-17 10:43:28 -0700300 } else {
John Shao5b1ffdc2016-11-08 13:51:00 -0800301 final int id = R.id.nav_assistant;
John Shaoa3ce3c72016-10-11 14:58:57 -0700302 final MenuItem assistantMenu = menu.findItem(id);
303 mIdMenuMap.put(id, assistantMenu);
John Shao52dd5d72016-10-06 15:16:14 -0700304 if (isAssistantView()) {
305 updateMenuSelection(assistantMenu);
Wenyi Wang79675452016-08-17 10:43:28 -0700306 }
307 }
308
309 if (!HelpUtils.isHelpAndFeedbackAvailable()) {
310 menu.removeItem(R.id.nav_help);
311 }
312
313 final MenuItem allContactsMenu = menu.findItem(R.id.nav_all_contacts);
314 mIdMenuMap.put(R.id.nav_all_contacts, allContactsMenu);
315 if (isAllContactsView()) {
316 updateMenuSelection(allContactsMenu);
317 }
Wenyi Wang6927bf32016-08-15 18:31:24 -0700318 }
319
Wenyi Wang79675452016-08-17 10:43:28 -0700320 public Toolbar getToolbar() {
321 return mToolbar;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700322 }
323
Walter Jang13710d52016-07-08 14:36:20 -0700324 @Override
325 protected void onSaveInstanceState(Bundle outState) {
326 super.onSaveInstanceState(outState);
327 if (mNewGroupAccount != null) {
328 outState.putString(KEY_NEW_GROUP_ACCOUNT, mNewGroupAccount.stringify());
329 }
Wenyi Wang79675452016-08-17 10:43:28 -0700330 outState.putInt(KEY_CONTACTS_VIEW, mCurrentView.ordinal());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700331 }
332
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700333 @Override
334 protected void onResume() {
335 super.onResume();
336 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700337 updateStatusBarBackground();
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700338 }
339 }
340
Wenyi Wang92d7e512016-10-03 14:57:19 -0700341 public void updateStatusBarBackground() {
342 updateStatusBarBackground(/* color */ -1);
343 }
344
345 public void updateStatusBarBackground(int color) {
346 if (!CompatUtils.isLollipopCompatible()) return;
347 if (color == -1) {
348 mDrawer.setStatusBarBackgroundColor(MaterialColorMapUtils.getStatusBarColor(this));
349 } else {
350 mDrawer.setStatusBarBackgroundColor(color);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700351 }
Wenyi Wang92d7e512016-10-03 14:57:19 -0700352 mDrawer.invalidate();
353 getWindow().setStatusBarColor(Color.TRANSPARENT);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700354 }
355
John Shao52dd5d72016-10-06 15:16:14 -0700356 @Override
357 protected void onPostCreate(Bundle savedInstanceState) {
358 super.onPostCreate(savedInstanceState);
359 mToggle.syncState();
360 }
361
362 @Override
363 public void onConfigurationChanged(Configuration newConfig) {
364 super.onConfigurationChanged(newConfig);
365 mToggle.onConfigurationChanged(newConfig);
366 }
367
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700368 // Set up fragment manager to load groups and filters.
369 protected void loadGroupsAndFilters() {
Wenyi Wangbb229242016-05-25 16:04:13 -0700370 final FragmentManager fragmentManager = getFragmentManager();
371 final FragmentTransaction transaction = fragmentManager.beginTransaction();
372 addGroupsAndFiltersFragments(transaction);
373 transaction.commitAllowingStateLoss();
374 fragmentManager.executePendingTransactions();
375 }
376
377 @Override
378 public void setContentView(@LayoutRes int layoutResID) {
379 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
380 if (parent != null) {
381 parent.removeAllViews();
382 }
383 LayoutInflater.from(this).inflate(layoutResID, parent);
384 }
385
386 protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) {
387 final FragmentManager fragmentManager = getFragmentManager();
Walter Jang13710d52016-07-08 14:36:20 -0700388 mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(TAG_GROUPS);
Walter Jang060f3242016-09-07 15:23:22 -0700389 if (mGroupsFragment == null) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700390 mGroupsFragment = new GroupsFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700391 transaction.add(mGroupsFragment, TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700392 }
Walter Jang060f3242016-09-07 15:23:22 -0700393 mGroupsFragment.setListener(this);
Wenyi Wangbb229242016-05-25 16:04:13 -0700394
Walter Jang060f3242016-09-07 15:23:22 -0700395 mAccountFiltersFragment = (AccountFiltersFragment)
396 fragmentManager.findFragmentByTag(TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700397 if (mAccountFiltersFragment == null) {
398 mAccountFiltersFragment = new AccountFiltersFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700399 transaction.add(mAccountFiltersFragment, TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700400 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700401 mAccountFiltersFragment.setListener(this);
402 }
403
404 @Override
405 public void onGroupsLoaded(List<GroupListItem> groupListItems) {
406 final Menu menu = mNavigationView.getMenu();
407 final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups);
408 final SubMenu subMenu = groupsMenuItem.getSubMenu();
409 subMenu.removeGroup(R.id.nav_groups_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700410 mGroupMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700411
Walter Jang5a496962016-09-09 18:32:50 -0700412 final GroupMetaData groupMetaData = getGroupMetaData();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700413
Wenyi Wangbb229242016-05-25 16:04:13 -0700414 if (groupListItems != null) {
415 // Add each group
416 for (final GroupListItem groupListItem : groupListItems) {
417 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
418 continue;
419 }
420 final String title = groupListItem.getTitle();
421 final MenuItem menuItem =
Wenyi Wang79675452016-08-17 10:43:28 -0700422 subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, title);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700423 mGroupMenuMap.put(groupListItem.getGroupId(), menuItem);
Wenyi Wang79675452016-08-17 10:43:28 -0700424 if (isGroupView() && groupMetaData != null
425 && groupMetaData.groupId == groupListItem.getGroupId()) {
426 updateMenuSelection(menuItem);
427 }
John Shaobd9ef3c2016-12-15 12:42:03 -0800428 menuItem.setIcon(R.drawable.quantum_ic_label_vd_theme_24);
Wenyi Wangbb229242016-05-25 16:04:13 -0700429 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
430 @Override
431 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang79ef0882016-11-16 16:45:51 -0800432 onGroupMenuItemClicked(groupListItem.getGroupId(),
433 groupListItem.getTitle());
434 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700435 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700436 return true;
437 }
438 });
Wenyi Wang5678dcb2016-11-22 11:25:50 -0800439
Wenyi Wang993c7b02016-11-29 15:45:33 -0800440 updateMenuContentDescription(menuItem,
441 getString(R.string.group_edit_field_hint_text));
Wenyi Wangbb229242016-05-25 16:04:13 -0700442 }
443 }
444
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700445 // Don't show "Create new..." menu if there's no group-writable accounts available.
446 if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) {
447 return;
448 }
449
Wenyi Wangbb229242016-05-25 16:04:13 -0700450 // Create a menu item in the sub menu to add new groups
Wenyi Wang45c210a2016-07-15 10:51:52 -0700451 final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700452 Menu.NONE, getString(R.string.menu_new_group_action_bar));
John Shaobd9ef3c2016-12-15 12:42:03 -0800453 menuItem.setIcon(R.drawable.quantum_ic_add_vd_theme_24);
Wenyi Wangbb229242016-05-25 16:04:13 -0700454 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
455 @Override
456 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang79ef0882016-11-16 16:45:51 -0800457 onCreateGroupMenuItemClicked();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700458 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700459 return true;
460 }
461 });
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700462
Wenyi Wang79675452016-08-17 10:43:28 -0700463 if (isGroupView() && groupMetaData != null) {
464 updateGroupMenu(groupMetaData);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700465 }
466 }
467
Walter Jang5a496962016-09-09 18:32:50 -0700468 public void updateGroupMenu(GroupMetaData groupMetaData) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700469 clearCheckedMenus();
Walter Jang428824e2016-09-09 13:18:35 -0700470 if (groupMetaData != null && mGroupMenuMap != null
471 && mGroupMenuMap.get(groupMetaData.groupId) != null) {
Walter Jang5a496962016-09-09 18:32:50 -0700472 setMenuChecked(mGroupMenuMap.get(groupMetaData.groupId), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700473 }
474 }
475
Walter Jang428824e2016-09-09 13:18:35 -0700476 protected GroupMetaData getGroupMetaData() {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700477 return null;
Wenyi Wangbb229242016-05-25 16:04:13 -0700478 }
479
Wenyi Wang92d7e512016-10-03 14:57:19 -0700480 public boolean isGroupView() {
Wenyi Wang79675452016-08-17 10:43:28 -0700481 return mCurrentView == ContactsView.GROUP_VIEW;
Wenyi Wangbb229242016-05-25 16:04:13 -0700482 }
483
John Shao52dd5d72016-10-06 15:16:14 -0700484 protected boolean isAssistantView() {
485 return mCurrentView == ContactsView.ASSISTANT;
Wenyi Wang79675452016-08-17 10:43:28 -0700486 }
487
488 protected boolean isAllContactsView() {
489 return mCurrentView == ContactsView.ALL_CONTACTS;
490 }
491
492 protected boolean isAccountView() {
493 return mCurrentView == ContactsView.ACCOUNT_VIEW;
494 }
495
496 public boolean isInSecondLevel() {
John Shao52dd5d72016-10-06 15:16:14 -0700497 return isGroupView() || isAssistantView();
Wenyi Wang79675452016-08-17 10:43:28 -0700498 }
499
Marcus Hagerottd6d5ad52016-10-25 19:25:38 -0700500 protected abstract void onGroupMenuItemClicked(long groupId, String title);
Wenyi Wang79675452016-08-17 10:43:28 -0700501
Walter Jang1110d672016-07-25 17:55:12 +0000502 protected void onCreateGroupMenuItemClicked() {
Walter Jang13710d52016-07-08 14:36:20 -0700503 // Select the account to create the group
504 final Bundle extras = getIntent().getExtras();
505 final Account account = extras == null ? null :
506 (Account) extras.getParcelable(Intents.Insert.EXTRA_ACCOUNT);
507 if (account == null) {
Walter Jang552e5822016-09-10 12:03:24 -0700508 selectAccountForNewGroup();
Walter Jang13710d52016-07-08 14:36:20 -0700509 } else {
510 final String dataSet = extras == null
511 ? null : extras.getString(Intents.Insert.EXTRA_DATA_SET);
512 final AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
513 account.name, account.type, dataSet);
514 onAccountChosen(accountWithDataSet, /* extraArgs */ null);
515 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700516 }
517
518 @Override
519 public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700520 final AccountDisplayInfoFactory accountDisplayFactory = AccountDisplayInfoFactory.
521 fromListFilters(this, accountFilterItems);
522
Wenyi Wangbb229242016-05-25 16:04:13 -0700523 final Menu menu = mNavigationView.getMenu();
524 final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters);
525 final SubMenu subMenu = filtersMenuItem.getSubMenu();
526 subMenu.removeGroup(R.id.nav_filters_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700527 mFilterMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700528
529 if (accountFilterItems == null || accountFilterItems.size() < 2) {
530 return;
531 }
532
533 for (int i = 0; i < accountFilterItems.size(); i++) {
534 final ContactListFilter filter = accountFilterItems.get(i);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700535 final AccountDisplayInfo displayableAccount =
536 accountDisplayFactory.getAccountDisplayInfoFor(filter);
537 final CharSequence menuName = displayableAccount.getNameLabel();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700538 final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700539 Menu.NONE, menuName);
540 if (isAccountView() && filter == mContactListFilterController.getFilter()) {
541 updateMenuSelection(menuItem);
542 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700543 mFilterMenuMap.put(filter, menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700544 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000545 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700546 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
547 @Override
548 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang79ef0882016-11-16 16:45:51 -0800549 onFilterMenuItemClicked(intent);
550 updateMenuSelection(menuItem);
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 }
Wenyi Wang5678dcb2016-11-22 11:25:50 -0800560
561 updateMenuContentDescription(menuItem, displayableAccount.getTypeLabel());
Wenyi Wangbb229242016-05-25 16:04:13 -0700562 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700563
Wenyi Wang79675452016-08-17 10:43:28 -0700564 if (isAccountView()) {
565 updateFilterMenu(mContactListFilterController.getFilter());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700566 }
567 }
568
Wenyi Wang5678dcb2016-11-22 11:25:50 -0800569 private void updateMenuContentDescription(MenuItem menuItem, CharSequence contentDescription) {
570 // Create a dummy action view to attach extra hidden content description to the menuItem
571 // for Talkback. We want Talkback to read out the account type but not have it be part
572 // of the menuItem title.
573 final LinearLayout view = (LinearLayout) LayoutInflater.from(this)
574 .inflate(R.layout.menu_item_action_view, null);
575 view.setContentDescription(contentDescription);
576 view.setVisibility(View.VISIBLE);
577 menuItem.setActionView(view);
578 }
579
Wenyi Wang587b8f22016-08-17 17:40:45 -0700580 public void updateFilterMenu(ContactListFilter filter) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700581 clearCheckedMenus();
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700582 if (filter != null && filter.isContactsFilterType()) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700583 if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700584 setMenuChecked(mIdMenuMap.get(R.id.nav_all_contacts), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700585 }
586 } else {
587 if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700588 setMenuChecked(mFilterMenuMap.get(filter), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700589 }
590 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700591 }
592
Wenyi Wang79675452016-08-17 10:43:28 -0700593 protected void onFilterMenuItemClicked(Intent intent) {
594 AccountFilterUtil.handleAccountFilterResult(mContactListFilterController,
595 AppCompatActivity.RESULT_OK, intent);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700596 }
597
Wenyi Wangbb229242016-05-25 16:04:13 -0700598 @Override
Wenyi Wang8e73c362016-06-23 18:03:53 -0700599 public boolean onNavigationItemSelected(final MenuItem item) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700600 final int id = item.getItemId();
Wenyi Wang79ef0882016-11-16 16:45:51 -0800601
602 if (id == R.id.nav_settings) {
603 new Handler().postDelayed(new Runnable() {
604 @Override
605 public void run() {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700606 startActivity(createPreferenceIntent());
Wenyi Wang8e73c362016-06-23 18:03:53 -0700607 }
Wenyi Wang79ef0882016-11-16 16:45:51 -0800608 }, DRAWER_CLOSE_DELAY);
609 } else if (id == R.id.nav_help) {
610 HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this);
611 } else if (id == R.id.nav_all_contacts) {
612 switchToAllContacts();
613 } else if (id == R.id.nav_assistant) {
614 if (!isAssistantView()) {
615 launchAssistant();
616 updateMenuSelection(item);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700617 }
Wenyi Wang79ef0882016-11-16 16:45:51 -0800618 } else if (item.getIntent() != null) {
619 ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this,
620 item.getIntent());
621 } else {
622 Log.w(TAG, "Unhandled navigation view item selection");
623 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700624
625 mDrawer.closeDrawer(GravityCompat.START);
626 return true;
627 }
628
629 private Intent createPreferenceIntent() {
630 final Intent intent = new Intent(this, ContactsPreferenceActivity.class);
631 intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE,
Gary Mai363af602016-09-28 10:01:23 -0700632 ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE);
Wenyi Wangbb229242016-05-25 16:04:13 -0700633 return intent;
634 }
635
Wenyi Wang79675452016-08-17 10:43:28 -0700636 public void switchToAllContacts() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700637 resetFilter();
Wenyi Wang79675452016-08-17 10:43:28 -0700638
639 final Menu menu = mNavigationView.getMenu();
640 final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts);
641 updateMenuSelection(allContacts);
642
643 setTitle(getString(R.string.contactsList));
Wenyi Wangbb229242016-05-25 16:04:13 -0700644 }
645
Wenyi Wang32912eb2016-09-29 17:17:07 -0700646 private void resetFilter() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700647 final Intent intent = new Intent();
648 final ContactListFilter filter = AccountFilterUtil.createContactsFilter(this);
649 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
650 AccountFilterUtil.handleAccountFilterResult(
651 mContactListFilterController, AppCompatActivity.RESULT_OK, intent);
652 }
653
John Shao52dd5d72016-10-06 15:16:14 -0700654 protected abstract void launchAssistant();
Wenyi Wang79675452016-08-17 10:43:28 -0700655
656 protected abstract DefaultContactBrowseListFragment getAllFragment();
657
658 protected abstract GroupMembersFragment getGroupFragment();
659
660 public abstract void showFabWithAnimation(boolean showFab);
Wenyi Wangbb229242016-05-25 16:04:13 -0700661
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700662 private void clearCheckedMenus() {
663 clearCheckedMenu(mFilterMenuMap);
664 clearCheckedMenu(mGroupMenuMap);
665 clearCheckedMenu(mIdMenuMap);
666 }
Wenyi Wang79675452016-08-17 10:43:28 -0700667
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700668 private void clearCheckedMenu(Map<?, MenuItem> map) {
669 final Iterator it = map.entrySet().iterator();
670 while (it.hasNext()) {
Wenyi Wang79675452016-08-17 10:43:28 -0700671 Entry pair = (Entry) it.next();
672 setMenuChecked(map.get(pair.getKey()), false);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700673 }
674 }
Walter Jang13710d52016-07-08 14:36:20 -0700675
Wenyi Wang79675452016-08-17 10:43:28 -0700676 private void setMenuChecked(MenuItem menuItem, boolean checked) {
677 menuItem.setCheckable(checked);
678 menuItem.setChecked(checked);
679 }
680
Walter Jang552e5822016-09-10 12:03:24 -0700681 private void selectAccountForNewGroup() {
Walter Jang13710d52016-07-08 14:36:20 -0700682 final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this)
Walter Jang552e5822016-09-10 12:03:24 -0700683 .getGroupWritableAccounts();
Walter Jang13710d52016-07-08 14:36:20 -0700684 if (accounts.isEmpty()) {
685 // We shouldn't present the add group button if there are no writable accounts
686 // but check it since it's possible we are started with an Intent.
687 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
688 return;
689 }
690 // If there is a single writable account, use it w/o showing a dialog.
691 if (accounts.size() == 1) {
692 onAccountChosen(accounts.get(0), /* extraArgs */ null);
693 return;
694 }
Wenyi Wangc3322282016-11-11 14:53:29 -0800695 SelectAccountDialogFragment.show(getFragmentManager(), R.string.dialog_new_group_account,
696 AccountListFilter.ACCOUNTS_GROUP_WRITABLE, /* extraArgs */ null,
697 TAG_SELECT_ACCOUNT_DIALOG);
Walter Jang13710d52016-07-08 14:36:20 -0700698 }
699
700 @Override
701 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
702 mNewGroupAccount = account;
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700703 GroupNameEditDialogFragment.newInstanceForCreation(
704 mNewGroupAccount, GroupUtil.ACTION_CREATE_GROUP)
Marcus Hagerott8d62cd22016-08-01 17:31:38 -0700705 .show(getFragmentManager(), TAG_GROUP_NAME_EDIT_DIALOG);
Walter Jang13710d52016-07-08 14:36:20 -0700706 }
707
708 @Override
709 public void onAccountSelectorCancelled() {
710 }
Wenyi Wang79675452016-08-17 10:43:28 -0700711
712 private void updateMenuSelection(MenuItem menuItem) {
713 clearCheckedMenus();
714 setMenuChecked(menuItem, true);
715 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700716}