blob: 68b98840e4c8deda0647d55fd56d1a0f2a6afd56 [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;
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;
John Shao52dd5d72016-10-06 15:16:14 -070052import com.android.contacts.common.model.account.AccountDisplayInfo;
53import com.android.contacts.common.model.account.AccountDisplayInfoFactory;
Walter Jang13710d52016-07-08 14:36:20 -070054import com.android.contacts.common.model.account.AccountWithDataSet;
Wenyi Wangbb229242016-05-25 16:04:13 -070055import com.android.contacts.common.preference.ContactsPreferenceActivity;
56import com.android.contacts.common.util.AccountFilterUtil;
Walter Jang13710d52016-07-08 14:36:20 -070057import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
Wenyi Wangbb229242016-05-25 16:04:13 -070058import com.android.contacts.common.util.ImplicitIntentsUtil;
Wenyi Wang92d7e512016-10-03 14:57:19 -070059import com.android.contacts.common.util.MaterialColorMapUtils;
Wenyi Wangbb229242016-05-25 16:04:13 -070060import com.android.contacts.common.util.ViewUtil;
Gary Mai363af602016-09-28 10:01:23 -070061import com.android.contacts.editor.ContactEditorFragment;
Walter Jang55d0e712016-08-31 17:11:36 -070062import com.android.contacts.editor.SelectAccountDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070063import com.android.contacts.group.GroupListItem;
Wenyi Wang79675452016-08-17 10:43:28 -070064import com.android.contacts.group.GroupMembersFragment;
Walter Jang428824e2016-09-09 13:18:35 -070065import com.android.contacts.group.GroupMetaData;
Walter Jang13710d52016-07-08 14:36:20 -070066import com.android.contacts.group.GroupNameEditDialogFragment;
Wenyi Wangbb229242016-05-25 16:04:13 -070067import com.android.contacts.group.GroupUtil;
68import com.android.contacts.group.GroupsFragment;
69import com.android.contacts.group.GroupsFragment.GroupsListener;
70import com.android.contacts.interactions.AccountFiltersFragment;
71import com.android.contacts.interactions.AccountFiltersFragment.AccountFiltersListener;
Wenyi Wang79675452016-08-17 10:43:28 -070072import com.android.contacts.list.DefaultContactBrowseListFragment;
73import com.android.contacts.list.MultiSelectContactsListFragment;
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,
John Shao52dd5d72016-10-06 15:16:14 -070097 ASSISTANT,
Wenyi Wang79675452016-08-17 10:43:28 -070098 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();
Wenyi Wang92d7e512016-10-03 14:57:19 -0700138 updateStatusBarBackground();
Wenyi Wang79675452016-08-17 10:43:28 -0700139 }
140
141 private void stopSearchAndSelection() {
142 final MultiSelectContactsListFragment listFragment;
143 if (isAllContactsView() || isAccountView()) {
144 listFragment = getAllFragment();
145 } else if (isGroupView()) {
146 listFragment = getGroupFragment();
147 } else {
148 listFragment = null;
149 }
150 if (listFragment == null) {
151 return;
152 }
153 final ActionBarAdapter actionBarAdapter = listFragment.getActionBarAdapter();
154 if (actionBarAdapter == null) {
155 return;
156 }
157 if (actionBarAdapter.isSearchMode()) {
158 actionBarAdapter.setSearchMode(false);
159 } else if (actionBarAdapter.isSelectionMode()) {
160 actionBarAdapter.setSelectionMode(false);
161 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700162 }
163
164 @Override
165 public void onDrawerClosed(View view) {
166 super.onDrawerClosed(view);
167 invalidateOptionsMenu();
168 }
169
170 @Override
171 public void onDrawerStateChanged(int newState) {
172 super.onDrawerStateChanged(newState);
173 // Set transparent status bar when drawer starts to move.
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700174 if (newState != DrawerLayout.STATE_IDLE) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700175 updateStatusBarBackground();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700176 }
177 if (mRunnable != null && newState == DrawerLayout.STATE_IDLE) {
178 mRunnable.run();
179 mRunnable = null;
180 }
181 }
182
183 public void runWhenIdle(Runnable runnable) {
184 mRunnable = runnable;
185 }
186 }
187
Wenyi Wangbb229242016-05-25 16:04:13 -0700188 protected ContactListFilterController mContactListFilterController;
189 protected DrawerLayout mDrawer;
Wenyi Wang8e73c362016-06-23 18:03:53 -0700190 protected ContactsActionBarDrawerToggle mToggle;
Wenyi Wangbb229242016-05-25 16:04:13 -0700191 protected Toolbar mToolbar;
192 protected NavigationView mNavigationView;
193 protected GroupsFragment mGroupsFragment;
194 protected AccountFiltersFragment mAccountFiltersFragment;
195
Walter Jang13710d52016-07-08 14:36:20 -0700196 // The account the new group will be created under.
197 private AccountWithDataSet mNewGroupAccount;
198
Wenyi Wang79675452016-08-17 10:43:28 -0700199 // Checkable menu item lookup maps. Every map declared here should be added to
200 // clearCheckedMenus() so that they can be cleared.
201 // TODO find a better way to handle selected menu item state, when switching to fragments.
202 protected Map<Long, MenuItem> mGroupMenuMap = new HashMap<>();
203 protected Map<ContactListFilter, MenuItem> mFilterMenuMap = new HashMap<>();
204 protected Map<Integer, MenuItem> mIdMenuMap = new HashMap<>();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700205
Wenyi Wangbb229242016-05-25 16:04:13 -0700206 @Override
207 protected void onCreate(Bundle savedState) {
208 super.onCreate(savedState);
209
210 mContactListFilterController = ContactListFilterController.getInstance(this);
211 mContactListFilterController.checkFilterValidity(false);
212
213 super.setContentView(R.layout.contacts_drawer_activity);
214
215 // Set up the action bar.
216 mToolbar = getView(R.id.toolbar);
217 setSupportActionBar(mToolbar);
218
219 // Add shadow under toolbar.
220 ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
221
222 // Set up hamburger button.
223 mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700224 mToggle = new ContactsActionBarDrawerToggle(this, mDrawer, mToolbar,
Wenyi Wangbb229242016-05-25 16:04:13 -0700225 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
John Shao52dd5d72016-10-06 15:16:14 -0700226
Wenyi Wang8e73c362016-06-23 18:03:53 -0700227 mDrawer.setDrawerListener(mToggle);
John Shao52dd5d72016-10-06 15:16:14 -0700228 // Set fallback handler for when drawer is disabled.
229 mToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
230 @Override
231 public void onClick(View v) {
232 onBackPressed();
233 }
234 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700235
Wenyi Wang79675452016-08-17 10:43:28 -0700236 // Set up navigation mode.
237 if (savedState != null) {
238 mCurrentView = ContactsView.values()[savedState.getInt(KEY_CONTACTS_VIEW)];
239 } else {
240 mCurrentView = ContactsView.ALL_CONTACTS;
241 }
242
Wenyi Wangbb229242016-05-25 16:04:13 -0700243 // Set up hamburger menu items.
244 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
245 mNavigationView.setNavigationItemSelectedListener(this);
Wenyi Wang79675452016-08-17 10:43:28 -0700246 setUpMenu();
Wenyi Wangbb229242016-05-25 16:04:13 -0700247
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
John Shao52dd5d72016-10-06 15:16:14 -0700256 public void setDrawerLockMode(boolean enabled) {
257 // Prevent drawer from being opened by sliding from the start of screen.
258 mDrawer.setDrawerLockMode(enabled ? DrawerLayout.LOCK_MODE_UNLOCKED
259 : DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
260
261 // Order of these statements matter.
262 // Display back button and disable drawer indicator.
263 if (enabled) {
264 getSupportActionBar().setDisplayHomeAsUpEnabled(false);
265 mToggle.setDrawerIndicatorEnabled(true);
266 } else {
267 mToggle.setDrawerIndicatorEnabled(false);
268 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
269 }
270 }
271
Wenyi Wang79675452016-08-17 10:43:28 -0700272 private void setUpMenu() {
273 final Menu menu = mNavigationView.getMenu();
274
John Shao52dd5d72016-10-06 15:16:14 -0700275 if (ObjectFactory.getAssistantFragment() == null) {
276 menu.removeItem(R.id.nav_assistant);
Wenyi Wang79675452016-08-17 10:43:28 -0700277 } else {
John Shao52dd5d72016-10-06 15:16:14 -0700278 final MenuItem assistantMenu = menu.findItem(R.id.nav_assistant);
279 mIdMenuMap.put(R.id.nav_assistant, assistantMenu);
280 if (isAssistantView()) {
281 updateMenuSelection(assistantMenu);
Wenyi Wang79675452016-08-17 10:43:28 -0700282 }
283 }
284
285 if (!HelpUtils.isHelpAndFeedbackAvailable()) {
286 menu.removeItem(R.id.nav_help);
287 }
288
289 final MenuItem allContactsMenu = menu.findItem(R.id.nav_all_contacts);
290 mIdMenuMap.put(R.id.nav_all_contacts, allContactsMenu);
291 if (isAllContactsView()) {
292 updateMenuSelection(allContactsMenu);
293 }
Wenyi Wang6927bf32016-08-15 18:31:24 -0700294 }
295
Wenyi Wang79675452016-08-17 10:43:28 -0700296 public Toolbar getToolbar() {
297 return mToolbar;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700298 }
299
Walter Jang13710d52016-07-08 14:36:20 -0700300 @Override
301 protected void onSaveInstanceState(Bundle outState) {
302 super.onSaveInstanceState(outState);
303 if (mNewGroupAccount != null) {
304 outState.putString(KEY_NEW_GROUP_ACCOUNT, mNewGroupAccount.stringify());
305 }
Wenyi Wang79675452016-08-17 10:43:28 -0700306 outState.putInt(KEY_CONTACTS_VIEW, mCurrentView.ordinal());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700307 }
308
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700309 @Override
310 protected void onResume() {
311 super.onResume();
312 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
Wenyi Wang92d7e512016-10-03 14:57:19 -0700313 updateStatusBarBackground();
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700314 }
315 }
316
Wenyi Wang92d7e512016-10-03 14:57:19 -0700317 public void updateStatusBarBackground() {
318 updateStatusBarBackground(/* color */ -1);
319 }
320
321 public void updateStatusBarBackground(int color) {
322 if (!CompatUtils.isLollipopCompatible()) return;
323 if (color == -1) {
324 mDrawer.setStatusBarBackgroundColor(MaterialColorMapUtils.getStatusBarColor(this));
325 } else {
326 mDrawer.setStatusBarBackgroundColor(color);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700327 }
Wenyi Wang92d7e512016-10-03 14:57:19 -0700328 mDrawer.invalidate();
329 getWindow().setStatusBarColor(Color.TRANSPARENT);
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700330 }
331
John Shao52dd5d72016-10-06 15:16:14 -0700332 @Override
333 protected void onPostCreate(Bundle savedInstanceState) {
334 super.onPostCreate(savedInstanceState);
335 mToggle.syncState();
336 }
337
338 @Override
339 public void onConfigurationChanged(Configuration newConfig) {
340 super.onConfigurationChanged(newConfig);
341 mToggle.onConfigurationChanged(newConfig);
342 }
343
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700344 // Set up fragment manager to load groups and filters.
345 protected void loadGroupsAndFilters() {
Wenyi Wangbb229242016-05-25 16:04:13 -0700346 final FragmentManager fragmentManager = getFragmentManager();
347 final FragmentTransaction transaction = fragmentManager.beginTransaction();
348 addGroupsAndFiltersFragments(transaction);
349 transaction.commitAllowingStateLoss();
350 fragmentManager.executePendingTransactions();
351 }
352
353 @Override
354 public void setContentView(@LayoutRes int layoutResID) {
355 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
356 if (parent != null) {
357 parent.removeAllViews();
358 }
359 LayoutInflater.from(this).inflate(layoutResID, parent);
360 }
361
362 protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) {
363 final FragmentManager fragmentManager = getFragmentManager();
Walter Jang13710d52016-07-08 14:36:20 -0700364 mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(TAG_GROUPS);
Walter Jang060f3242016-09-07 15:23:22 -0700365 if (mGroupsFragment == null) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700366 mGroupsFragment = new GroupsFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700367 transaction.add(mGroupsFragment, TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700368 }
Walter Jang060f3242016-09-07 15:23:22 -0700369 mGroupsFragment.setListener(this);
Wenyi Wangbb229242016-05-25 16:04:13 -0700370
Walter Jang060f3242016-09-07 15:23:22 -0700371 mAccountFiltersFragment = (AccountFiltersFragment)
372 fragmentManager.findFragmentByTag(TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700373 if (mAccountFiltersFragment == null) {
374 mAccountFiltersFragment = new AccountFiltersFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700375 transaction.add(mAccountFiltersFragment, TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700376 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700377 mAccountFiltersFragment.setListener(this);
378 }
379
380 @Override
381 public void onGroupsLoaded(List<GroupListItem> groupListItems) {
382 final Menu menu = mNavigationView.getMenu();
383 final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups);
384 final SubMenu subMenu = groupsMenuItem.getSubMenu();
385 subMenu.removeGroup(R.id.nav_groups_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700386 mGroupMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700387
Walter Jang5a496962016-09-09 18:32:50 -0700388 final GroupMetaData groupMetaData = getGroupMetaData();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700389
Wenyi Wangbb229242016-05-25 16:04:13 -0700390 if (groupListItems != null) {
391 // Add each group
392 for (final GroupListItem groupListItem : groupListItems) {
393 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
394 continue;
395 }
396 final String title = groupListItem.getTitle();
397 final MenuItem menuItem =
Wenyi Wang79675452016-08-17 10:43:28 -0700398 subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, title);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700399 mGroupMenuMap.put(groupListItem.getGroupId(), menuItem);
Wenyi Wang79675452016-08-17 10:43:28 -0700400 if (isGroupView() && groupMetaData != null
401 && groupMetaData.groupId == groupListItem.getGroupId()) {
402 updateMenuSelection(menuItem);
403 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700404 menuItem.setIcon(R.drawable.ic_menu_label);
405 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
406 @Override
407 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700408 mToggle.runWhenIdle(new Runnable() {
409 @Override
410 public void run() {
Wenyi Wang86077052016-06-29 18:21:21 -0700411 onGroupMenuItemClicked(groupListItem.getGroupId(),
412 groupListItem.getTitle());
Wenyi Wang79675452016-08-17 10:43:28 -0700413 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700414 }
415 });
416 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700417 return true;
418 }
419 });
420 }
421 }
422
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700423 // Don't show "Create new..." menu if there's no group-writable accounts available.
424 if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) {
425 return;
426 }
427
Wenyi Wangbb229242016-05-25 16:04:13 -0700428 // Create a menu item in the sub menu to add new groups
Wenyi Wang45c210a2016-07-15 10:51:52 -0700429 final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700430 Menu.NONE, getString(R.string.menu_new_group_action_bar));
Wenyi Wang8cd7c5c2016-06-16 14:03:42 -0700431 menuItem.setIcon(R.drawable.ic_add);
Wenyi Wangbb229242016-05-25 16:04:13 -0700432 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
433 @Override
434 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700435 mToggle.runWhenIdle(new Runnable() {
436 @Override
437 public void run() {
438 onCreateGroupMenuItemClicked();
439 }
440 });
441 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700442 return true;
443 }
444 });
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700445
Wenyi Wang79675452016-08-17 10:43:28 -0700446 if (isGroupView() && groupMetaData != null) {
447 updateGroupMenu(groupMetaData);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700448 }
449 }
450
Walter Jang5a496962016-09-09 18:32:50 -0700451 public void updateGroupMenu(GroupMetaData groupMetaData) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700452 clearCheckedMenus();
Walter Jang428824e2016-09-09 13:18:35 -0700453 if (groupMetaData != null && mGroupMenuMap != null
454 && mGroupMenuMap.get(groupMetaData.groupId) != null) {
Walter Jang5a496962016-09-09 18:32:50 -0700455 setMenuChecked(mGroupMenuMap.get(groupMetaData.groupId), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700456 }
457 }
458
Walter Jang428824e2016-09-09 13:18:35 -0700459 protected GroupMetaData getGroupMetaData() {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700460 return null;
Wenyi Wangbb229242016-05-25 16:04:13 -0700461 }
462
Wenyi Wang92d7e512016-10-03 14:57:19 -0700463 public boolean isGroupView() {
Wenyi Wang79675452016-08-17 10:43:28 -0700464 return mCurrentView == ContactsView.GROUP_VIEW;
Wenyi Wangbb229242016-05-25 16:04:13 -0700465 }
466
John Shao52dd5d72016-10-06 15:16:14 -0700467 protected boolean isAssistantView() {
468 return mCurrentView == ContactsView.ASSISTANT;
Wenyi Wang79675452016-08-17 10:43:28 -0700469 }
470
471 protected boolean isAllContactsView() {
472 return mCurrentView == ContactsView.ALL_CONTACTS;
473 }
474
475 protected boolean isAccountView() {
476 return mCurrentView == ContactsView.ACCOUNT_VIEW;
477 }
478
479 public boolean isInSecondLevel() {
John Shao52dd5d72016-10-06 15:16:14 -0700480 return isGroupView() || isAssistantView();
Wenyi Wang79675452016-08-17 10:43:28 -0700481 }
482
483 protected abstract void onGroupMenuItemClicked(long groupId, String title);
484
Walter Jang1110d672016-07-25 17:55:12 +0000485 protected void onCreateGroupMenuItemClicked() {
Walter Jang13710d52016-07-08 14:36:20 -0700486 // Select the account to create the group
487 final Bundle extras = getIntent().getExtras();
488 final Account account = extras == null ? null :
489 (Account) extras.getParcelable(Intents.Insert.EXTRA_ACCOUNT);
490 if (account == null) {
Walter Jang552e5822016-09-10 12:03:24 -0700491 selectAccountForNewGroup();
Walter Jang13710d52016-07-08 14:36:20 -0700492 } else {
493 final String dataSet = extras == null
494 ? null : extras.getString(Intents.Insert.EXTRA_DATA_SET);
495 final AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
496 account.name, account.type, dataSet);
497 onAccountChosen(accountWithDataSet, /* extraArgs */ null);
498 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700499 }
500
501 @Override
502 public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) {
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700503 final AccountDisplayInfoFactory accountDisplayFactory = AccountDisplayInfoFactory.
504 fromListFilters(this, accountFilterItems);
505
Wenyi Wangbb229242016-05-25 16:04:13 -0700506 final Menu menu = mNavigationView.getMenu();
507 final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters);
508 final SubMenu subMenu = filtersMenuItem.getSubMenu();
509 subMenu.removeGroup(R.id.nav_filters_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700510 mFilterMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700511
512 if (accountFilterItems == null || accountFilterItems.size() < 2) {
513 return;
514 }
515
Marcus Hagerott6caf23f2016-08-18 15:02:42 -0700516
Wenyi Wangbb229242016-05-25 16:04:13 -0700517 for (int i = 0; i < accountFilterItems.size(); i++) {
518 final ContactListFilter filter = accountFilterItems.get(i);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700519 final AccountDisplayInfo displayableAccount =
520 accountDisplayFactory.getAccountDisplayInfoFor(filter);
521 final CharSequence menuName = displayableAccount.getNameLabel();
Wenyi Wang45c210a2016-07-15 10:51:52 -0700522 final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE,
Wenyi Wang79675452016-08-17 10:43:28 -0700523 Menu.NONE, menuName);
524 if (isAccountView() && filter == mContactListFilterController.getFilter()) {
525 updateMenuSelection(menuItem);
526 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700527 mFilterMenuMap.put(filter, menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700528 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000529 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700530 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
531 @Override
532 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700533 mToggle.runWhenIdle(new Runnable() {
534 @Override
535 public void run() {
Wenyi Wang79675452016-08-17 10:43:28 -0700536 onFilterMenuItemClicked(intent);
537 updateMenuSelection(menuItem);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700538 }
539 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700540 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700541 return true;
542 }
543 });
Walter Jang98d41d72016-09-15 13:12:51 -0700544 if (displayableAccount.getIcon() != null) {
545 menuItem.setIcon(displayableAccount.getIcon());
546 // Get rid of the default menu item overlay and show original account icons.
547 menuItem.getIcon().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
548 }
Gary Maif354d182016-07-25 13:59:30 -0700549 // Create a dummy action view to attach extra hidden content description to the menuItem
550 // for Talkback. We want Talkback to read out the account type but not have it be part
551 // of the menuItem title.
Gary Maif354d182016-07-25 13:59:30 -0700552 LinearLayout view = (LinearLayout) LayoutInflater.from(this)
553 .inflate(R.layout.account_type_info, null);
Marcus Hagerottfac695a2016-08-24 17:02:40 -0700554 view.setContentDescription(displayableAccount.getTypeLabel());
Gary Maif354d182016-07-25 13:59:30 -0700555 view.setVisibility(View.VISIBLE);
556 menuItem.setActionView(view);
Wenyi Wangbb229242016-05-25 16:04:13 -0700557 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700558
Wenyi Wang79675452016-08-17 10:43:28 -0700559 if (isAccountView()) {
560 updateFilterMenu(mContactListFilterController.getFilter());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700561 }
562 }
563
Wenyi Wang587b8f22016-08-17 17:40:45 -0700564 public void updateFilterMenu(ContactListFilter filter) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700565 clearCheckedMenus();
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700566 if (filter != null && filter.isContactsFilterType()) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700567 if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700568 setMenuChecked(mIdMenuMap.get(R.id.nav_all_contacts), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700569 }
570 } else {
571 if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) {
Wenyi Wang79675452016-08-17 10:43:28 -0700572 setMenuChecked(mFilterMenuMap.get(filter), true);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700573 }
574 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700575 }
576
Wenyi Wang79675452016-08-17 10:43:28 -0700577 protected void onFilterMenuItemClicked(Intent intent) {
578 AccountFilterUtil.handleAccountFilterResult(mContactListFilterController,
579 AppCompatActivity.RESULT_OK, intent);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700580 }
581
Wenyi Wangbb229242016-05-25 16:04:13 -0700582 @Override
Wenyi Wang8e73c362016-06-23 18:03:53 -0700583 public boolean onNavigationItemSelected(final MenuItem item) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700584 final int id = item.getItemId();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700585 mToggle.runWhenIdle(new Runnable() {
586 @Override
587 public void run() {
588 if (id == R.id.nav_settings) {
589 startActivity(createPreferenceIntent());
590 } else if (id == R.id.nav_help) {
591 HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this);
592 } else if (id == R.id.nav_all_contacts) {
593 switchToAllContacts();
John Shao52dd5d72016-10-06 15:16:14 -0700594 } else if (id == R.id.nav_assistant) {
595 if (!isAssistantView()) {
596 launchAssistant();
Wenyi Wang79675452016-08-17 10:43:28 -0700597 updateMenuSelection(item);
598 }
Wenyi Wang8e73c362016-06-23 18:03:53 -0700599 } else if (item.getIntent() != null) {
600 ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this,
601 item.getIntent());
602 } else {
603 Log.w(TAG, "Unhandled navigation view item selection");
604 }
605 }
606 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700607
608 mDrawer.closeDrawer(GravityCompat.START);
609 return true;
610 }
611
612 private Intent createPreferenceIntent() {
613 final Intent intent = new Intent(this, ContactsPreferenceActivity.class);
614 intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE,
Gary Mai363af602016-09-28 10:01:23 -0700615 ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE);
Wenyi Wangbb229242016-05-25 16:04:13 -0700616 return intent;
617 }
618
Wenyi Wang79675452016-08-17 10:43:28 -0700619 public void switchToAllContacts() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700620 resetFilter();
Wenyi Wang79675452016-08-17 10:43:28 -0700621
622 final Menu menu = mNavigationView.getMenu();
623 final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts);
624 updateMenuSelection(allContacts);
625
626 setTitle(getString(R.string.contactsList));
Wenyi Wangbb229242016-05-25 16:04:13 -0700627 }
628
Wenyi Wang32912eb2016-09-29 17:17:07 -0700629 private void resetFilter() {
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700630 final Intent intent = new Intent();
631 final ContactListFilter filter = AccountFilterUtil.createContactsFilter(this);
632 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
633 AccountFilterUtil.handleAccountFilterResult(
634 mContactListFilterController, AppCompatActivity.RESULT_OK, intent);
635 }
636
John Shao52dd5d72016-10-06 15:16:14 -0700637 protected abstract void launchAssistant();
Wenyi Wang79675452016-08-17 10:43:28 -0700638
639 protected abstract DefaultContactBrowseListFragment getAllFragment();
640
641 protected abstract GroupMembersFragment getGroupFragment();
642
643 public abstract void showFabWithAnimation(boolean showFab);
Wenyi Wangbb229242016-05-25 16:04:13 -0700644
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700645 private void clearCheckedMenus() {
646 clearCheckedMenu(mFilterMenuMap);
647 clearCheckedMenu(mGroupMenuMap);
648 clearCheckedMenu(mIdMenuMap);
649 }
Wenyi Wang79675452016-08-17 10:43:28 -0700650
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700651 private void clearCheckedMenu(Map<?, MenuItem> map) {
652 final Iterator it = map.entrySet().iterator();
653 while (it.hasNext()) {
Wenyi Wang79675452016-08-17 10:43:28 -0700654 Entry pair = (Entry) it.next();
655 setMenuChecked(map.get(pair.getKey()), false);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700656 }
657 }
Walter Jang13710d52016-07-08 14:36:20 -0700658
Wenyi Wang79675452016-08-17 10:43:28 -0700659 private void setMenuChecked(MenuItem menuItem, boolean checked) {
660 menuItem.setCheckable(checked);
661 menuItem.setChecked(checked);
662 }
663
Walter Jang552e5822016-09-10 12:03:24 -0700664 private void selectAccountForNewGroup() {
Walter Jang13710d52016-07-08 14:36:20 -0700665 final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this)
Walter Jang552e5822016-09-10 12:03:24 -0700666 .getGroupWritableAccounts();
Walter Jang13710d52016-07-08 14:36:20 -0700667 if (accounts.isEmpty()) {
668 // We shouldn't present the add group button if there are no writable accounts
669 // but check it since it's possible we are started with an Intent.
670 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
671 return;
672 }
673 // If there is a single writable account, use it w/o showing a dialog.
674 if (accounts.size() == 1) {
675 onAccountChosen(accounts.get(0), /* extraArgs */ null);
676 return;
677 }
678 SelectAccountDialogFragment.show(getFragmentManager(), null,
679 R.string.dialog_new_group_account, AccountListFilter.ACCOUNTS_GROUP_WRITABLE,
680 /* extraArgs */ null, TAG_SELECT_ACCOUNT_DIALOG);
681 }
682
683 @Override
684 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
685 mNewGroupAccount = account;
Wenyi Wangcc8f2722016-09-07 14:52:32 -0700686 GroupNameEditDialogFragment.newInstanceForCreation(
687 mNewGroupAccount, GroupUtil.ACTION_CREATE_GROUP)
Marcus Hagerott8d62cd22016-08-01 17:31:38 -0700688 .show(getFragmentManager(), TAG_GROUP_NAME_EDIT_DIALOG);
Walter Jang13710d52016-07-08 14:36:20 -0700689 }
690
691 @Override
692 public void onAccountSelectorCancelled() {
693 }
Wenyi Wang79675452016-08-17 10:43:28 -0700694
695 private void updateMenuSelection(MenuItem menuItem) {
696 clearCheckedMenus();
697 setMenuChecked(menuItem, true);
698 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700699}