blob: d6d03a52b38d35415f863d80bbb1a2d03a2286c7 [file] [log] [blame]
Wenyi Wangbb229242016-05-25 16:04:13 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.contacts;
18
Walter Jang13710d52016-07-08 14:36:20 -070019import android.accounts.Account;
Wenyi Wangbb229242016-05-25 16:04:13 -070020import android.app.FragmentManager;
21import android.app.FragmentTransaction;
Wenyi Wangbb229242016-05-25 16:04:13 -070022import android.content.Intent;
23import android.graphics.Color;
24import android.graphics.PorterDuff;
Walter Jang13710d52016-07-08 14:36:20 -070025import android.net.Uri;
Wenyi Wangbb229242016-05-25 16:04:13 -070026import 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;
Wenyi Wang8e73c362016-06-23 18:03:53 -070030import android.support.v4.content.ContextCompat;
Wenyi Wangbb229242016-05-25 16:04:13 -070031import android.support.v4.view.GravityCompat;
32import android.support.v4.widget.DrawerLayout;
33import android.support.v7.app.ActionBarDrawerToggle;
34import android.support.v7.app.AppCompatActivity;
Wenyi Wang45c210a2016-07-15 10:51:52 -070035import android.support.v7.widget.LinearLayoutManager;
36import android.support.v7.widget.RecyclerView;
Wenyi Wangbb229242016-05-25 16:04:13 -070037import android.support.v7.widget.Toolbar;
Wenyi Wang45c210a2016-07-15 10:51:52 -070038import android.util.DisplayMetrics;
Wenyi Wangbb229242016-05-25 16:04:13 -070039import android.util.Log;
40import android.view.LayoutInflater;
41import android.view.Menu;
42import android.view.MenuItem;
43import android.view.SubMenu;
Wenyi Wang8e73c362016-06-23 18:03:53 -070044import android.view.View;
Wenyi Wangbb229242016-05-25 16:04:13 -070045import android.view.ViewGroup;
Gary Maif354d182016-07-25 13:59:30 -070046import android.widget.LinearLayout;
Walter Jang13710d52016-07-08 14:36:20 -070047import android.widget.Toast;
Wenyi Wangbb229242016-05-25 16:04:13 -070048
Walter Jang13710d52016-07-08 14:36:20 -070049import com.android.contacts.activities.GroupMembersActivity;
Wenyi Wangbb229242016-05-25 16:04:13 -070050import com.android.contacts.common.ContactsUtils;
Wenyi Wang8e73c362016-06-23 18:03:53 -070051import com.android.contacts.common.compat.CompatUtils;
Walter Jang13710d52016-07-08 14:36:20 -070052import com.android.contacts.common.editor.SelectAccountDialogFragment;
Walter Jang92942632016-07-14 19:49:32 +000053import com.android.contacts.common.list.AccountFilterActivity;
Wenyi Wangbb229242016-05-25 16:04:13 -070054import com.android.contacts.common.list.ContactListFilter;
55import com.android.contacts.common.list.ContactListFilterController;
Walter Jang13710d52016-07-08 14:36:20 -070056import com.android.contacts.common.model.AccountTypeManager;
Gary Maif354d182016-07-25 13:59:30 -070057import com.android.contacts.common.model.account.AccountType;
Walter Jang13710d52016-07-08 14:36:20 -070058import com.android.contacts.common.model.account.AccountWithDataSet;
Wenyi Wangbb229242016-05-25 16:04:13 -070059import com.android.contacts.common.preference.ContactsPreferenceActivity;
60import com.android.contacts.common.util.AccountFilterUtil;
Walter Jang13710d52016-07-08 14:36:20 -070061import com.android.contacts.common.util.AccountsListAdapter.AccountListFilter;
Wenyi Wangbb229242016-05-25 16:04:13 -070062import com.android.contacts.common.util.ImplicitIntentsUtil;
63import com.android.contacts.common.util.ViewUtil;
64import com.android.contacts.editor.ContactEditorFragment;
65import com.android.contacts.group.GroupListItem;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -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;
73import com.android.contacts.quickcontact.QuickContactActivity;
Wenyi Wangbb229242016-05-25 16:04:13 -070074import com.android.contactsbind.Assistants;
75import com.android.contactsbind.HelpUtils;
76
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070077import java.util.HashMap;
78import java.util.Iterator;
Wenyi Wangbb229242016-05-25 16:04:13 -070079import java.util.List;
Wenyi Wangf8a5ab42016-05-25 16:04:13 -070080import java.util.Map;
81import java.util.Map.Entry;
Wenyi Wangbb229242016-05-25 16:04:13 -070082
83/**
84 * A common superclass for Contacts activities with a navigation drawer.
85 */
86public abstract class ContactsDrawerActivity extends AppCompatContactsActivity implements
87 AccountFiltersListener,
88 GroupsListener,
Walter Jang13710d52016-07-08 14:36:20 -070089 NavigationView.OnNavigationItemSelectedListener,
90 SelectAccountDialogFragment.Listener,
91 GroupNameEditDialogFragment.Listener {
Wenyi Wangbb229242016-05-25 16:04:13 -070092
93 protected static String TAG = "ContactsDrawerActivity";
94
Walter Jang13710d52016-07-08 14:36:20 -070095 private static final String TAG_GROUPS = "groups";
96 private static final String TAG_FILTERS = "filters";
97 private static final String TAG_SELECT_ACCOUNT_DIALOG = "selectAccountDialog";
98 private static final String TAG_GROUP_NAME_EDIT_DIALOG = "groupNameEditDialog";
99
100 private static final String KEY_NEW_GROUP_ACCOUNT = "newGroupAccount";
101
102 protected static final String ACTION_CREATE_GROUP = "createGroup";
Wenyi Wangbb229242016-05-25 16:04:13 -0700103
Wenyi Wang45c210a2016-07-15 10:51:52 -0700104 // TODO(wenyiw): remove all the code related to these constants after switching to fragments.
105 // Positions of "all contacts" and "duplicates" in navigation drawer.
106 private static final int ALL_CONTACTS_POSITION = 1;
107 private static final int DUPLICATES_POSITION = 2;
108 // Gap between two menu groups, including a separator, a menu group header.
109 private static final int GAP_BETWEEN_TWO_MENU_GROUPS = 2;
110
Wenyi Wang8e73c362016-06-23 18:03:53 -0700111 private class ContactsActionBarDrawerToggle extends ActionBarDrawerToggle {
112
113 private Runnable mRunnable;
114
115 public ContactsActionBarDrawerToggle(AppCompatActivity activity, DrawerLayout drawerLayout,
116 Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) {
117 super(activity, drawerLayout, toolbar, openDrawerContentDescRes,
118 closeDrawerContentDescRes);
119 }
120
121 @Override
122 public void onDrawerOpened(View drawerView) {
123 super.onDrawerOpened(drawerView);
124 invalidateOptionsMenu();
125 }
126
127 @Override
128 public void onDrawerClosed(View view) {
129 super.onDrawerClosed(view);
130 invalidateOptionsMenu();
131 }
132
133 @Override
134 public void onDrawerStateChanged(int newState) {
135 super.onDrawerStateChanged(newState);
136 // Set transparent status bar when drawer starts to move.
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700137 if (newState != DrawerLayout.STATE_IDLE) {
138 makeStatusBarTransparent();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700139 }
140 if (mRunnable != null && newState == DrawerLayout.STATE_IDLE) {
141 mRunnable.run();
142 mRunnable = null;
143 }
144 }
145
146 public void runWhenIdle(Runnable runnable) {
147 mRunnable = runnable;
148 }
149 }
150
Wenyi Wangbb229242016-05-25 16:04:13 -0700151 protected ContactListFilterController mContactListFilterController;
152 protected DrawerLayout mDrawer;
Wenyi Wang8e73c362016-06-23 18:03:53 -0700153 protected ContactsActionBarDrawerToggle mToggle;
Wenyi Wangbb229242016-05-25 16:04:13 -0700154 protected Toolbar mToolbar;
155 protected NavigationView mNavigationView;
156 protected GroupsFragment mGroupsFragment;
157 protected AccountFiltersFragment mAccountFiltersFragment;
158
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700159 // Checkable menu item lookup maps. Every map declared here should be added to
160 // clearCheckedMenus() so that they can be cleared.
161 // TODO find a better way to handle selected menu item state, when swicthing to fragments.
162 protected Map<Long, MenuItem> mGroupMenuMap = new HashMap<>();
163 protected Map<ContactListFilter, MenuItem> mFilterMenuMap = new HashMap<>();
164 protected Map<Integer, MenuItem> mIdMenuMap = new HashMap<>();
165
Walter Jang13710d52016-07-08 14:36:20 -0700166 // The account the new group will be created under.
167 private AccountWithDataSet mNewGroupAccount;
168
Wenyi Wang45c210a2016-07-15 10:51:52 -0700169 private int mPositionOfLastGroup;
170
Wenyi Wangbb229242016-05-25 16:04:13 -0700171 @Override
172 protected void onCreate(Bundle savedState) {
173 super.onCreate(savedState);
174
175 mContactListFilterController = ContactListFilterController.getInstance(this);
176 mContactListFilterController.checkFilterValidity(false);
177
178 super.setContentView(R.layout.contacts_drawer_activity);
179
180 // Set up the action bar.
181 mToolbar = getView(R.id.toolbar);
182 setSupportActionBar(mToolbar);
183
184 // Add shadow under toolbar.
185 ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());
186
187 // Set up hamburger button.
188 mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700189 mToggle = new ContactsActionBarDrawerToggle(this, mDrawer, mToolbar,
Wenyi Wangbb229242016-05-25 16:04:13 -0700190 R.string.navigation_drawer_open, R.string.navigation_drawer_close);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700191 mDrawer.setDrawerListener(mToggle);
192 mToggle.syncState();
Wenyi Wangbb229242016-05-25 16:04:13 -0700193
194 // Set up hamburger menu items.
195 mNavigationView = (NavigationView) findViewById(R.id.nav_view);
196 mNavigationView.setNavigationItemSelectedListener(this);
197
198 final Menu menu = mNavigationView.getMenu();
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700199
200 final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts);
201 mIdMenuMap.put(R.id.nav_all_contacts, allContacts);
202
Wenyi Wangbb229242016-05-25 16:04:13 -0700203 if (Assistants.getDuplicatesActivityIntent(this) == null) {
204 menu.removeItem(R.id.nav_find_duplicates);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700205 } else {
206 final MenuItem findDup = menu.findItem(R.id.nav_find_duplicates);
207 mIdMenuMap.put(R.id.nav_find_duplicates, findDup);
Wenyi Wangbb229242016-05-25 16:04:13 -0700208 }
209
210 if (!HelpUtils.isHelpAndFeedbackAvailable()) {
211 menu.removeItem(R.id.nav_help);
212 }
213
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700214 loadGroupsAndFilters();
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700215
216 if (isDuplicatesActivity()) {
217 clearCheckedMenus();
218 mIdMenuMap.get(R.id.nav_find_duplicates).setCheckable(true);
219 mIdMenuMap.get(R.id.nav_find_duplicates).setChecked(true);
Wenyi Wang7b334362016-08-03 15:24:06 -0700220 maybeUpdateScrollPosition(DUPLICATES_POSITION);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700221 }
Walter Jang13710d52016-07-08 14:36:20 -0700222
223 if (savedState != null && savedState.containsKey(KEY_NEW_GROUP_ACCOUNT)) {
224 mNewGroupAccount = AccountWithDataSet.unstringify(
225 savedState.getString(KEY_NEW_GROUP_ACCOUNT));
226 }
227 }
228
Wenyi Wang7b334362016-08-03 15:24:06 -0700229 private void maybeUpdateScrollPosition(int position) {
230 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
231 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "Don't scroll menu when drawer open");
232 return;
233 }
Wenyi Wang45c210a2016-07-15 10:51:52 -0700234 final RecyclerView recyclerView = (RecyclerView) mNavigationView.getChildAt(0);
235 final LinearLayoutManager layoutManager =
236 (LinearLayoutManager) recyclerView.getLayoutManager();
237
238 // Get screen height
239 final DisplayMetrics metrics = getResources().getDisplayMetrics();
240 final int height = metrics.heightPixels;
241
242 // Set 1/3 screen height as offset if possible.
243 layoutManager.scrollToPositionWithOffset(position, height / 3);
244 recyclerView.requestLayout();
245 }
246
Walter Jang13710d52016-07-08 14:36:20 -0700247 @Override
248 protected void onSaveInstanceState(Bundle outState) {
249 super.onSaveInstanceState(outState);
250 if (mNewGroupAccount != null) {
251 outState.putString(KEY_NEW_GROUP_ACCOUNT, mNewGroupAccount.stringify());
252 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700253 }
254
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700255 @Override
256 protected void onResume() {
257 super.onResume();
258 if (mDrawer.isDrawerOpen(GravityCompat.START)) {
259 makeStatusBarTransparent();
260 }
261 }
262
263 private void makeStatusBarTransparent() {
Wenyi Wang45c210a2016-07-15 10:51:52 -0700264 // Avoid making status bar transparent when action bar's selection mode is on.
265 if (getWindow().getStatusBarColor() !=
266 ContextCompat.getColor(this, R.color.contextual_selection_bar_status_bar_color)
267 && CompatUtils.isLollipopCompatible()) {
Wenyi Wangf9289ec2016-06-28 19:40:38 -0700268 getWindow().setStatusBarColor(Color.TRANSPARENT);
269 }
270 }
271
Walter Jang13710d52016-07-08 14:36:20 -0700272 @Override
273 protected void onNewIntent(Intent newIntent) {
274 if (ACTION_CREATE_GROUP.equals(newIntent.getAction())) {
275 final Uri groupUri = newIntent.getData();
276 if (groupUri == null) {
277 Toast.makeText(this, R.string.groupSavedErrorToast, Toast.LENGTH_SHORT).show();
278 return;
279 }
280 if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "Received group URI " + groupUri);
281 Toast.makeText(this, R.string.groupCreatedToast, Toast.LENGTH_SHORT).show();
282 startActivity(GroupUtil.createViewGroupIntent(this, groupUri, /* title */ null));
Walter Jang92d68782016-07-09 15:31:24 -0700283 if (this instanceof GroupMembersActivity) {
284 // If we created a group while viewing the members of an existing group (i.e.
285 // while on GroupMembersActivity), finish the current GroupMembersActivity so that
286 // hitting back from the new GroupMembersActivity that was just stared will open
287 // the all contacts list. See b/30047708.
288 finish();
289 }
Walter Jang13710d52016-07-08 14:36:20 -0700290 } else {
291 super.onNewIntent(newIntent);
292 }
293 }
294
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700295 /**
296 * Returns true if child class is DuplicatesActivity
297 */
298 protected boolean isDuplicatesActivity() {
299 return false;
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700300 }
301
302 // Set up fragment manager to load groups and filters.
303 protected void loadGroupsAndFilters() {
Wenyi Wangbb229242016-05-25 16:04:13 -0700304 final FragmentManager fragmentManager = getFragmentManager();
305 final FragmentTransaction transaction = fragmentManager.beginTransaction();
306 addGroupsAndFiltersFragments(transaction);
307 transaction.commitAllowingStateLoss();
308 fragmentManager.executePendingTransactions();
309 }
310
311 @Override
312 public void setContentView(@LayoutRes int layoutResID) {
313 final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame);
314 if (parent != null) {
315 parent.removeAllViews();
316 }
317 LayoutInflater.from(this).inflate(layoutResID, parent);
318 }
319
320 protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) {
321 final FragmentManager fragmentManager = getFragmentManager();
Walter Jang13710d52016-07-08 14:36:20 -0700322 mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700323 mAccountFiltersFragment = (AccountFiltersFragment)
Walter Jang13710d52016-07-08 14:36:20 -0700324 fragmentManager.findFragmentByTag(TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700325
326 if (mGroupsFragment == null && ContactsUtils.areGroupWritableAccountsAvailable(this)) {
327 mGroupsFragment = new GroupsFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700328 transaction.add(mGroupsFragment, TAG_GROUPS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700329 }
330
331 if (mAccountFiltersFragment == null) {
332 mAccountFiltersFragment = new AccountFiltersFragment();
Walter Jang13710d52016-07-08 14:36:20 -0700333 transaction.add(mAccountFiltersFragment, TAG_FILTERS);
Wenyi Wangbb229242016-05-25 16:04:13 -0700334 }
335
336 if (ContactsUtils.areGroupWritableAccountsAvailable(this) && mGroupsFragment != null) {
337 mGroupsFragment.setListener(this);
338 }
339 mAccountFiltersFragment.setListener(this);
340 }
341
342 @Override
343 public void onGroupsLoaded(List<GroupListItem> groupListItems) {
344 final Menu menu = mNavigationView.getMenu();
345 final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups);
346 final SubMenu subMenu = groupsMenuItem.getSubMenu();
347 subMenu.removeGroup(R.id.nav_groups_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700348 mGroupMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700349
Wenyi Wang45c210a2016-07-15 10:51:52 -0700350 mPositionOfLastGroup = DUPLICATES_POSITION + GAP_BETWEEN_TWO_MENU_GROUPS;
351
Wenyi Wangbb229242016-05-25 16:04:13 -0700352 if (groupListItems != null) {
353 // Add each group
354 for (final GroupListItem groupListItem : groupListItems) {
355 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
356 continue;
357 }
Wenyi Wang45c210a2016-07-15 10:51:52 -0700358 mPositionOfLastGroup++;
Wenyi Wangbb229242016-05-25 16:04:13 -0700359 final String title = groupListItem.getTitle();
360 final MenuItem menuItem =
Wenyi Wang45c210a2016-07-15 10:51:52 -0700361 subMenu.add(R.id.nav_groups_items, Menu.NONE, mPositionOfLastGroup, title);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700362 mGroupMenuMap.put(groupListItem.getGroupId(), menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700363 menuItem.setIcon(R.drawable.ic_menu_label);
364 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
365 @Override
366 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700367 mToggle.runWhenIdle(new Runnable() {
368 @Override
369 public void run() {
Wenyi Wang86077052016-06-29 18:21:21 -0700370 onGroupMenuItemClicked(groupListItem.getGroupId(),
371 groupListItem.getTitle());
Wenyi Wang8e73c362016-06-23 18:03:53 -0700372 }
373 });
374 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700375 return true;
376 }
377 });
378 }
379 }
380
Wenyi Wangcb117bd2016-06-17 13:26:57 -0700381 // Don't show "Create new..." menu if there's no group-writable accounts available.
382 if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) {
383 return;
384 }
385
Wenyi Wang45c210a2016-07-15 10:51:52 -0700386 mPositionOfLastGroup++;
Wenyi Wangbb229242016-05-25 16:04:13 -0700387 // Create a menu item in the sub menu to add new groups
Wenyi Wang45c210a2016-07-15 10:51:52 -0700388 final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE,
389 mPositionOfLastGroup, getString(R.string.menu_new_group_action_bar));
Wenyi Wang8cd7c5c2016-06-16 14:03:42 -0700390 menuItem.setIcon(R.drawable.ic_add);
Wenyi Wangbb229242016-05-25 16:04:13 -0700391 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
392 @Override
393 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700394 mToggle.runWhenIdle(new Runnable() {
395 @Override
396 public void run() {
397 onCreateGroupMenuItemClicked();
398 }
399 });
400 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700401 return true;
402 }
403 });
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700404
405 if (getGroupMetadata() != null) {
406 updateGroupMenu(getGroupMetadata());
407 }
408 }
409
410 protected void updateGroupMenu(GroupMetadata groupMetadata) {
411 clearCheckedMenus();
412 if (groupMetadata != null && mGroupMenuMap != null
413 && mGroupMenuMap.get(groupMetadata.groupId) != null) {
414 mGroupMenuMap.get(groupMetadata.groupId).setCheckable(true);
415 mGroupMenuMap.get(groupMetadata.groupId).setChecked(true);
Wenyi Wang7b334362016-08-03 15:24:06 -0700416 maybeUpdateScrollPosition(mGroupMenuMap.get(groupMetadata.groupId).getOrder());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700417 }
418 }
419
420 /**
421 * Returns group metadata if the child class is {@link GroupMembersActivity}, and null
422 * otherwise.
423 */
424 protected GroupMetadata getGroupMetadata() {
425 return null;
Wenyi Wangbb229242016-05-25 16:04:13 -0700426 }
427
Wenyi Wang86077052016-06-29 18:21:21 -0700428 protected void onGroupMenuItemClicked(long groupId, String title) {
429 startActivity(GroupUtil.createViewGroupIntent(this, groupId, title));
Wenyi Wang8e73c362016-06-23 18:03:53 -0700430 if (shouldFinish()) {
431 finish();
432 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700433 }
434
Walter Jang1110d672016-07-25 17:55:12 +0000435 protected void onCreateGroupMenuItemClicked() {
Walter Jang13710d52016-07-08 14:36:20 -0700436 // Select the account to create the group
437 final Bundle extras = getIntent().getExtras();
438 final Account account = extras == null ? null :
439 (Account) extras.getParcelable(Intents.Insert.EXTRA_ACCOUNT);
440 if (account == null) {
441 selectAccount();
442 } else {
443 final String dataSet = extras == null
444 ? null : extras.getString(Intents.Insert.EXTRA_DATA_SET);
445 final AccountWithDataSet accountWithDataSet = new AccountWithDataSet(
446 account.name, account.type, dataSet);
447 onAccountChosen(accountWithDataSet, /* extraArgs */ null);
448 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700449 }
450
451 @Override
452 public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) {
453 final Menu menu = mNavigationView.getMenu();
454 final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters);
455 final SubMenu subMenu = filtersMenuItem.getSubMenu();
456 subMenu.removeGroup(R.id.nav_filters_items);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700457 mFilterMenuMap = new HashMap<>();
Wenyi Wangbb229242016-05-25 16:04:13 -0700458
459 if (accountFilterItems == null || accountFilterItems.size() < 2) {
460 return;
461 }
462
Wenyi Wang45c210a2016-07-15 10:51:52 -0700463 int positionOfLastFilter = mPositionOfLastGroup + GAP_BETWEEN_TWO_MENU_GROUPS;
464
Wenyi Wangbb229242016-05-25 16:04:13 -0700465 for (int i = 0; i < accountFilterItems.size(); i++) {
Wenyi Wang45c210a2016-07-15 10:51:52 -0700466 positionOfLastFilter++;
Wenyi Wangbb229242016-05-25 16:04:13 -0700467 final ContactListFilter filter = accountFilterItems.get(i);
Tingting Wang2dc340c2016-07-05 22:31:58 -0700468 final String menuName =
469 filter.filterType == ContactListFilter.FILTER_TYPE_DEVICE_CONTACTS
470 ? getString(R.string.account_phone) : filter.accountName;
Wenyi Wang45c210a2016-07-15 10:51:52 -0700471 final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE,
472 positionOfLastFilter, menuName);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700473 mFilterMenuMap.put(filter, menuItem);
Wenyi Wangbb229242016-05-25 16:04:13 -0700474 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000475 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700476 menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
477 @Override
478 public boolean onMenuItemClick(MenuItem item) {
Wenyi Wang8e73c362016-06-23 18:03:53 -0700479 mToggle.runWhenIdle(new Runnable() {
480 @Override
481 public void run() {
482 AccountFilterUtil.handleAccountFilterResult(
483 mContactListFilterController, AppCompatActivity.RESULT_OK,
484 intent);
485 if (shouldFinish()) {
486 finish();
487 }
488 }
489 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700490 mDrawer.closeDrawer(GravityCompat.START);
Wenyi Wangbb229242016-05-25 16:04:13 -0700491 return true;
492 }
493 });
494 menuItem.setIcon(filter.icon);
Wenyi Wang45c210a2016-07-15 10:51:52 -0700495 // Get rid of the default menu item overlay and show original account icons.
Wenyi Wangbb229242016-05-25 16:04:13 -0700496 menuItem.getIcon().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP);
Gary Maif354d182016-07-25 13:59:30 -0700497 // Create a dummy action view to attach extra hidden content description to the menuItem
498 // for Talkback. We want Talkback to read out the account type but not have it be part
499 // of the menuItem title.
500 final AccountType account = AccountTypeManager.getInstance(this)
501 .getAccountType(filter.accountType, filter.dataSet);
502 LinearLayout view = (LinearLayout) LayoutInflater.from(this)
503 .inflate(R.layout.account_type_info, null);
504 view.setContentDescription(account.getDisplayLabel(this));
505 view.setVisibility(View.VISIBLE);
506 menuItem.setActionView(view);
Wenyi Wangbb229242016-05-25 16:04:13 -0700507 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700508
509 if (getContactListFilter() != null) {
510 updateFilterMenu(getContactListFilter());
511 }
512 }
513
514 protected void updateFilterMenu(ContactListFilter filter) {
515 clearCheckedMenus();
Walter Jangb8e90f12016-07-18 18:25:32 -0700516 if (filter != null && filter.isContactsFilterType()) {
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700517 if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) {
518 mIdMenuMap.get(R.id.nav_all_contacts).setCheckable(true);
519 mIdMenuMap.get(R.id.nav_all_contacts).setChecked(true);
Wenyi Wang7b334362016-08-03 15:24:06 -0700520 maybeUpdateScrollPosition(ALL_CONTACTS_POSITION);
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700521 }
522 } else {
523 if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) {
524 mFilterMenuMap.get(filter).setCheckable(true);
525 mFilterMenuMap.get(filter).setChecked(true);
Wenyi Wang7b334362016-08-03 15:24:06 -0700526 maybeUpdateScrollPosition(mFilterMenuMap.get(filter).getOrder());
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700527 }
528 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700529 }
530
531 /**
Walter Jang92942632016-07-14 19:49:32 +0000532 * Returns the current filter if the child class is PeopleActivity, and null otherwise.
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700533 */
534 protected ContactListFilter getContactListFilter() {
535 return null;
536 }
537
538 /**
539 * Returns true if the child activity should finish after launching another activity.
Wenyi Wangbb229242016-05-25 16:04:13 -0700540 */
541 protected abstract boolean shouldFinish();
542
543 @Override
Wenyi Wang8e73c362016-06-23 18:03:53 -0700544 public boolean onNavigationItemSelected(final MenuItem item) {
Wenyi Wangbb229242016-05-25 16:04:13 -0700545 final int id = item.getItemId();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700546 mToggle.runWhenIdle(new Runnable() {
547 @Override
548 public void run() {
549 if (id == R.id.nav_settings) {
550 startActivity(createPreferenceIntent());
551 } else if (id == R.id.nav_help) {
552 HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this);
553 } else if (id == R.id.nav_all_contacts) {
554 switchToAllContacts();
Wenyi Wang8e73c362016-06-23 18:03:53 -0700555 } else if (id == R.id.nav_find_duplicates) {
556 launchFindDuplicates();
557 } else if (item.getIntent() != null) {
558 ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this,
559 item.getIntent());
560 } else {
561 Log.w(TAG, "Unhandled navigation view item selection");
562 }
563 }
564 });
Wenyi Wangbb229242016-05-25 16:04:13 -0700565
566 mDrawer.closeDrawer(GravityCompat.START);
567 return true;
568 }
569
570 private Intent createPreferenceIntent() {
571 final Intent intent = new Intent(this, ContactsPreferenceActivity.class);
572 intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE,
573 ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE);
574 intent.putExtra(ContactsPreferenceActivity.EXTRA_MODE_FULLY_EXPANDED,
575 QuickContactActivity.MODE_FULLY_EXPANDED);
576 intent.putExtra(ContactsPreferenceActivity.EXTRA_PREVIOUS_SCREEN_TYPE,
577 QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE);
578 return intent;
579 }
580
581 protected void switchToAllContacts() {
582 final Intent intent = new Intent();
Walter Jang92942632016-07-14 19:49:32 +0000583 final ContactListFilter filter = createContactsFilter();
584 intent.putExtra(AccountFilterActivity.EXTRA_CONTACT_LIST_FILTER, filter);
Wenyi Wangbb229242016-05-25 16:04:13 -0700585 AccountFilterUtil.handleAccountFilterResult(
586 mContactListFilterController, AppCompatActivity.RESULT_OK, intent);
Wenyi Wang8e73c362016-06-23 18:03:53 -0700587 if (shouldFinish()) {
588 finish();
589 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700590 }
591
592 protected void launchFindDuplicates() {
593 ImplicitIntentsUtil.startActivityInAppIfPossible(this,
594 Assistants.getDuplicatesActivityIntent(this));
595 }
596
Walter Jang92942632016-07-14 19:49:32 +0000597 /**
598 * Returns a {@link ContactListFilter} of type
599 * {@link ContactListFilter#FILTER_TYPE_ALL_ACCOUNTS}, or if a custom "Contacts to display"
600 * filter has been set, then one of type {@link ContactListFilter#FILTER_TYPE_CUSTOM}.
601 */
602 protected ContactListFilter createContactsFilter() {
603 final int filterType =
604 ContactListFilterController.getInstance(this).isCustomFilterPersisted()
605 ? ContactListFilter.FILTER_TYPE_CUSTOM
606 : ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS;
607 return ContactListFilter.createFilterWithType(filterType);
Wenyi Wangbb229242016-05-25 16:04:13 -0700608 }
609
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700610 private void clearCheckedMenus() {
611 clearCheckedMenu(mFilterMenuMap);
612 clearCheckedMenu(mGroupMenuMap);
613 clearCheckedMenu(mIdMenuMap);
614 }
Wenyi Wangf8a5ab42016-05-25 16:04:13 -0700615 private void clearCheckedMenu(Map<?, MenuItem> map) {
616 final Iterator it = map.entrySet().iterator();
617 while (it.hasNext()) {
618 Entry pair = (Entry)it.next();
619 map.get(pair.getKey()).setCheckable(false);
620 map.get(pair.getKey()).setChecked(false);
621 }
622 }
Walter Jang13710d52016-07-08 14:36:20 -0700623
624 private void selectAccount() {
625 final List<AccountWithDataSet> accounts = AccountTypeManager.getInstance(this)
626 .getAccounts(/* writable */ true);
627 if (accounts.isEmpty()) {
628 // We shouldn't present the add group button if there are no writable accounts
629 // but check it since it's possible we are started with an Intent.
630 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
631 return;
632 }
633 // If there is a single writable account, use it w/o showing a dialog.
634 if (accounts.size() == 1) {
635 onAccountChosen(accounts.get(0), /* extraArgs */ null);
636 return;
637 }
638 SelectAccountDialogFragment.show(getFragmentManager(), null,
639 R.string.dialog_new_group_account, AccountListFilter.ACCOUNTS_GROUP_WRITABLE,
640 /* extraArgs */ null, TAG_SELECT_ACCOUNT_DIALOG);
641 }
642
643 @Override
644 public void onAccountChosen(AccountWithDataSet account, Bundle extraArgs) {
645 mNewGroupAccount = account;
646 GroupNameEditDialogFragment.showInsertDialog(
647 getFragmentManager(), TAG_GROUP_NAME_EDIT_DIALOG);
648 }
649
650 @Override
651 public void onAccountSelectorCancelled() {
652 }
653
654 @Override
655 public void onGroupNameEdit(String groupName, boolean isInsert) {
656 if (mNewGroupAccount == null) {
657 Toast.makeText(this, R.string.groupCreateFailedToast, Toast.LENGTH_SHORT).show();
658 return;
659 }
660 startService(ContactSaveService.createNewGroupIntent(this,
661 mNewGroupAccount, groupName, /* rawContactsToAdd */ null, getClass(),
662 ACTION_CREATE_GROUP));
663 }
664
665 @Override
666 public void onGroupNameEditCancelled() {
667 }
Wenyi Wangbb229242016-05-25 16:04:13 -0700668}