Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import android.app.FragmentManager; |
| 20 | import android.app.FragmentTransaction; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 21 | import android.content.Intent; |
| 22 | import android.graphics.Color; |
| 23 | import android.graphics.PorterDuff; |
| 24 | import android.os.Bundle; |
| 25 | import android.support.annotation.LayoutRes; |
| 26 | import android.support.design.widget.NavigationView; |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 27 | import android.support.v4.content.ContextCompat; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 28 | import android.support.v4.view.GravityCompat; |
| 29 | import android.support.v4.widget.DrawerLayout; |
| 30 | import android.support.v7.app.ActionBarDrawerToggle; |
| 31 | import android.support.v7.app.AppCompatActivity; |
| 32 | import android.support.v7.widget.Toolbar; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | import android.view.LayoutInflater; |
| 35 | import android.view.Menu; |
| 36 | import android.view.MenuItem; |
| 37 | import android.view.SubMenu; |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 38 | import android.view.View; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 39 | import android.view.ViewGroup; |
| 40 | |
| 41 | import com.android.contacts.common.ContactsUtils; |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 42 | import com.android.contacts.common.compat.CompatUtils; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 43 | import com.android.contacts.common.list.ContactListFilter; |
| 44 | import com.android.contacts.common.list.ContactListFilterController; |
| 45 | import com.android.contacts.common.preference.ContactsPreferenceActivity; |
| 46 | import com.android.contacts.common.util.AccountFilterUtil; |
| 47 | import com.android.contacts.common.util.ImplicitIntentsUtil; |
| 48 | import com.android.contacts.common.util.ViewUtil; |
| 49 | import com.android.contacts.editor.ContactEditorFragment; |
| 50 | import com.android.contacts.group.GroupListItem; |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 51 | import com.android.contacts.group.GroupMetadata; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 52 | import com.android.contacts.group.GroupUtil; |
| 53 | import com.android.contacts.group.GroupsFragment; |
| 54 | import com.android.contacts.group.GroupsFragment.GroupsListener; |
| 55 | import com.android.contacts.interactions.AccountFiltersFragment; |
| 56 | import com.android.contacts.interactions.AccountFiltersFragment.AccountFiltersListener; |
| 57 | import com.android.contacts.quickcontact.QuickContactActivity; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 58 | import com.android.contactsbind.Assistants; |
| 59 | import com.android.contactsbind.HelpUtils; |
| 60 | |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 61 | import java.util.HashMap; |
| 62 | import java.util.Iterator; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 63 | import java.util.List; |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 64 | import java.util.Map; |
| 65 | import java.util.Map.Entry; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * A common superclass for Contacts activities with a navigation drawer. |
| 69 | */ |
| 70 | public abstract class ContactsDrawerActivity extends AppCompatContactsActivity implements |
| 71 | AccountFiltersListener, |
| 72 | GroupsListener, |
| 73 | NavigationView.OnNavigationItemSelectedListener { |
| 74 | |
| 75 | protected static String TAG = "ContactsDrawerActivity"; |
| 76 | |
| 77 | protected static final String GROUPS_TAG = "groups"; |
| 78 | protected static final String FILTERS_TAG = "filters"; |
| 79 | |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 80 | private class ContactsActionBarDrawerToggle extends ActionBarDrawerToggle { |
| 81 | |
| 82 | private Runnable mRunnable; |
| 83 | |
| 84 | public ContactsActionBarDrawerToggle(AppCompatActivity activity, DrawerLayout drawerLayout, |
| 85 | Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes) { |
| 86 | super(activity, drawerLayout, toolbar, openDrawerContentDescRes, |
| 87 | closeDrawerContentDescRes); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public void onDrawerOpened(View drawerView) { |
| 92 | super.onDrawerOpened(drawerView); |
| 93 | invalidateOptionsMenu(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onDrawerClosed(View view) { |
| 98 | super.onDrawerClosed(view); |
| 99 | invalidateOptionsMenu(); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void onDrawerStateChanged(int newState) { |
| 104 | super.onDrawerStateChanged(newState); |
| 105 | // Set transparent status bar when drawer starts to move. |
Wenyi Wang | f9289ec | 2016-06-28 19:40:38 -0700 | [diff] [blame] | 106 | if (newState != DrawerLayout.STATE_IDLE) { |
| 107 | makeStatusBarTransparent(); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 108 | } |
| 109 | if (mRunnable != null && newState == DrawerLayout.STATE_IDLE) { |
| 110 | mRunnable.run(); |
| 111 | mRunnable = null; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | public void runWhenIdle(Runnable runnable) { |
| 116 | mRunnable = runnable; |
| 117 | } |
| 118 | } |
| 119 | |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 120 | protected ContactListFilterController mContactListFilterController; |
| 121 | protected DrawerLayout mDrawer; |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 122 | protected ContactsActionBarDrawerToggle mToggle; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 123 | protected Toolbar mToolbar; |
| 124 | protected NavigationView mNavigationView; |
| 125 | protected GroupsFragment mGroupsFragment; |
| 126 | protected AccountFiltersFragment mAccountFiltersFragment; |
| 127 | |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 128 | // Checkable menu item lookup maps. Every map declared here should be added to |
| 129 | // clearCheckedMenus() so that they can be cleared. |
| 130 | // TODO find a better way to handle selected menu item state, when swicthing to fragments. |
| 131 | protected Map<Long, MenuItem> mGroupMenuMap = new HashMap<>(); |
| 132 | protected Map<ContactListFilter, MenuItem> mFilterMenuMap = new HashMap<>(); |
| 133 | protected Map<Integer, MenuItem> mIdMenuMap = new HashMap<>(); |
| 134 | |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 135 | @Override |
| 136 | protected void onCreate(Bundle savedState) { |
| 137 | super.onCreate(savedState); |
| 138 | |
| 139 | mContactListFilterController = ContactListFilterController.getInstance(this); |
| 140 | mContactListFilterController.checkFilterValidity(false); |
| 141 | |
| 142 | super.setContentView(R.layout.contacts_drawer_activity); |
| 143 | |
| 144 | // Set up the action bar. |
| 145 | mToolbar = getView(R.id.toolbar); |
| 146 | setSupportActionBar(mToolbar); |
| 147 | |
| 148 | // Add shadow under toolbar. |
| 149 | ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources()); |
| 150 | |
| 151 | // Set up hamburger button. |
| 152 | mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 153 | mToggle = new ContactsActionBarDrawerToggle(this, mDrawer, mToolbar, |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 154 | R.string.navigation_drawer_open, R.string.navigation_drawer_close); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 155 | mDrawer.setDrawerListener(mToggle); |
| 156 | mToggle.syncState(); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 157 | |
| 158 | // Set up hamburger menu items. |
| 159 | mNavigationView = (NavigationView) findViewById(R.id.nav_view); |
| 160 | mNavigationView.setNavigationItemSelectedListener(this); |
| 161 | |
| 162 | final Menu menu = mNavigationView.getMenu(); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 163 | |
| 164 | final MenuItem allContacts = menu.findItem(R.id.nav_all_contacts); |
| 165 | mIdMenuMap.put(R.id.nav_all_contacts, allContacts); |
| 166 | |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 167 | if (Assistants.getDuplicatesActivityIntent(this) == null) { |
| 168 | menu.removeItem(R.id.nav_find_duplicates); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 169 | } else { |
| 170 | final MenuItem findDup = menu.findItem(R.id.nav_find_duplicates); |
| 171 | mIdMenuMap.put(R.id.nav_find_duplicates, findDup); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | if (!HelpUtils.isHelpAndFeedbackAvailable()) { |
| 175 | menu.removeItem(R.id.nav_help); |
| 176 | } |
| 177 | |
Wenyi Wang | cb117bd | 2016-06-17 13:26:57 -0700 | [diff] [blame] | 178 | loadGroupsAndFilters(); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 179 | |
| 180 | if (isDuplicatesActivity()) { |
| 181 | clearCheckedMenus(); |
| 182 | mIdMenuMap.get(R.id.nav_find_duplicates).setCheckable(true); |
| 183 | mIdMenuMap.get(R.id.nav_find_duplicates).setChecked(true); |
| 184 | } |
| 185 | } |
| 186 | |
Wenyi Wang | f9289ec | 2016-06-28 19:40:38 -0700 | [diff] [blame] | 187 | @Override |
| 188 | protected void onResume() { |
| 189 | super.onResume(); |
| 190 | if (mDrawer.isDrawerOpen(GravityCompat.START)) { |
| 191 | makeStatusBarTransparent(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | private void makeStatusBarTransparent() { |
| 196 | if (CompatUtils.isLollipopCompatible() |
| 197 | && getWindow().getStatusBarColor() == |
| 198 | ContextCompat.getColor(this, R.color.primary_color_dark)) { |
| 199 | getWindow().setStatusBarColor(Color.TRANSPARENT); |
| 200 | } |
| 201 | } |
| 202 | |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 203 | /** |
| 204 | * Returns true if child class is DuplicatesActivity |
| 205 | */ |
| 206 | protected boolean isDuplicatesActivity() { |
| 207 | return false; |
Wenyi Wang | cb117bd | 2016-06-17 13:26:57 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | // Set up fragment manager to load groups and filters. |
| 211 | protected void loadGroupsAndFilters() { |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 212 | final FragmentManager fragmentManager = getFragmentManager(); |
| 213 | final FragmentTransaction transaction = fragmentManager.beginTransaction(); |
| 214 | addGroupsAndFiltersFragments(transaction); |
| 215 | transaction.commitAllowingStateLoss(); |
| 216 | fragmentManager.executePendingTransactions(); |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void setContentView(@LayoutRes int layoutResID) { |
| 221 | final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame); |
| 222 | if (parent != null) { |
| 223 | parent.removeAllViews(); |
| 224 | } |
| 225 | LayoutInflater.from(this).inflate(layoutResID, parent); |
| 226 | } |
| 227 | |
| 228 | protected void addGroupsAndFiltersFragments(FragmentTransaction transaction) { |
| 229 | final FragmentManager fragmentManager = getFragmentManager(); |
| 230 | mGroupsFragment = (GroupsFragment) fragmentManager.findFragmentByTag(GROUPS_TAG); |
| 231 | mAccountFiltersFragment = (AccountFiltersFragment) |
| 232 | fragmentManager.findFragmentByTag(FILTERS_TAG); |
| 233 | |
| 234 | if (mGroupsFragment == null && ContactsUtils.areGroupWritableAccountsAvailable(this)) { |
| 235 | mGroupsFragment = new GroupsFragment(); |
| 236 | transaction.add(mGroupsFragment, GROUPS_TAG); |
| 237 | } |
| 238 | |
| 239 | if (mAccountFiltersFragment == null) { |
| 240 | mAccountFiltersFragment = new AccountFiltersFragment(); |
| 241 | transaction.add(mAccountFiltersFragment, FILTERS_TAG); |
| 242 | } |
| 243 | |
| 244 | if (ContactsUtils.areGroupWritableAccountsAvailable(this) && mGroupsFragment != null) { |
| 245 | mGroupsFragment.setListener(this); |
| 246 | } |
| 247 | mAccountFiltersFragment.setListener(this); |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void onGroupsLoaded(List<GroupListItem> groupListItems) { |
| 252 | final Menu menu = mNavigationView.getMenu(); |
| 253 | final MenuItem groupsMenuItem = menu.findItem(R.id.nav_groups); |
| 254 | final SubMenu subMenu = groupsMenuItem.getSubMenu(); |
| 255 | subMenu.removeGroup(R.id.nav_groups_items); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 256 | mGroupMenuMap = new HashMap<>(); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 257 | |
| 258 | if (groupListItems != null) { |
| 259 | // Add each group |
| 260 | for (final GroupListItem groupListItem : groupListItems) { |
| 261 | if (GroupUtil.isEmptyFFCGroup(groupListItem)) { |
| 262 | continue; |
| 263 | } |
| 264 | final String title = groupListItem.getTitle(); |
| 265 | final MenuItem menuItem = |
| 266 | subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, title); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 267 | mGroupMenuMap.put(groupListItem.getGroupId(), menuItem); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 268 | menuItem.setIcon(R.drawable.ic_menu_label); |
| 269 | menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 270 | @Override |
| 271 | public boolean onMenuItemClick(MenuItem item) { |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 272 | mToggle.runWhenIdle(new Runnable() { |
| 273 | @Override |
| 274 | public void run() { |
Wenyi Wang | 8607705 | 2016-06-29 18:21:21 -0700 | [diff] [blame] | 275 | onGroupMenuItemClicked(groupListItem.getGroupId(), |
| 276 | groupListItem.getTitle()); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 277 | } |
| 278 | }); |
| 279 | mDrawer.closeDrawer(GravityCompat.START); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | }); |
| 283 | } |
| 284 | } |
| 285 | |
Wenyi Wang | cb117bd | 2016-06-17 13:26:57 -0700 | [diff] [blame] | 286 | // Don't show "Create new..." menu if there's no group-writable accounts available. |
| 287 | if (!ContactsUtils.areGroupWritableAccountsAvailable(this)) { |
| 288 | return; |
| 289 | } |
| 290 | |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 291 | // Create a menu item in the sub menu to add new groups |
| 292 | final MenuItem menuItem = subMenu.add(R.id.nav_groups_items, Menu.NONE, Menu.NONE, |
| 293 | getString(R.string.menu_new_group_action_bar)); |
Wenyi Wang | 8cd7c5c | 2016-06-16 14:03:42 -0700 | [diff] [blame] | 294 | menuItem.setIcon(R.drawable.ic_add); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 295 | menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 296 | @Override |
| 297 | public boolean onMenuItemClick(MenuItem item) { |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 298 | mToggle.runWhenIdle(new Runnable() { |
| 299 | @Override |
| 300 | public void run() { |
| 301 | onCreateGroupMenuItemClicked(); |
| 302 | } |
| 303 | }); |
| 304 | mDrawer.closeDrawer(GravityCompat.START); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 305 | return true; |
| 306 | } |
| 307 | }); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 308 | |
| 309 | if (getGroupMetadata() != null) { |
| 310 | updateGroupMenu(getGroupMetadata()); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | protected void updateGroupMenu(GroupMetadata groupMetadata) { |
| 315 | clearCheckedMenus(); |
| 316 | if (groupMetadata != null && mGroupMenuMap != null |
| 317 | && mGroupMenuMap.get(groupMetadata.groupId) != null) { |
| 318 | mGroupMenuMap.get(groupMetadata.groupId).setCheckable(true); |
| 319 | mGroupMenuMap.get(groupMetadata.groupId).setChecked(true); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Returns group metadata if the child class is {@link GroupMembersActivity}, and null |
| 325 | * otherwise. |
| 326 | */ |
| 327 | protected GroupMetadata getGroupMetadata() { |
| 328 | return null; |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Wenyi Wang | 8607705 | 2016-06-29 18:21:21 -0700 | [diff] [blame] | 331 | protected void onGroupMenuItemClicked(long groupId, String title) { |
| 332 | startActivity(GroupUtil.createViewGroupIntent(this, groupId, title)); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 333 | if (shouldFinish()) { |
| 334 | finish(); |
| 335 | } |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | protected void onCreateGroupMenuItemClicked() { |
| 339 | startActivity(GroupUtil.createAddGroupIntent(this)); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void onFiltersLoaded(List<ContactListFilter> accountFilterItems) { |
| 344 | final Menu menu = mNavigationView.getMenu(); |
| 345 | final MenuItem filtersMenuItem = menu.findItem(R.id.nav_filters); |
| 346 | final SubMenu subMenu = filtersMenuItem.getSubMenu(); |
| 347 | subMenu.removeGroup(R.id.nav_filters_items); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 348 | mFilterMenuMap = new HashMap<>(); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 349 | |
| 350 | if (accountFilterItems == null || accountFilterItems.size() < 2) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | for (int i = 0; i < accountFilterItems.size(); i++) { |
| 355 | final ContactListFilter filter = accountFilterItems.get(i); |
| 356 | final String accountName = filter.accountName; |
| 357 | final MenuItem menuItem = subMenu.add(R.id.nav_filters_items, Menu.NONE, Menu.NONE, |
| 358 | accountName); |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 359 | mFilterMenuMap.put(filter, menuItem); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 360 | final Intent intent = new Intent(); |
| 361 | intent.putExtra(AccountFilterUtil.EXTRA_CONTACT_LIST_FILTER, filter); |
| 362 | menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() { |
| 363 | @Override |
| 364 | public boolean onMenuItemClick(MenuItem item) { |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 365 | mToggle.runWhenIdle(new Runnable() { |
| 366 | @Override |
| 367 | public void run() { |
| 368 | AccountFilterUtil.handleAccountFilterResult( |
| 369 | mContactListFilterController, AppCompatActivity.RESULT_OK, |
| 370 | intent); |
| 371 | if (shouldFinish()) { |
| 372 | finish(); |
| 373 | } |
| 374 | } |
| 375 | }); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 376 | mDrawer.closeDrawer(GravityCompat.START); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | }); |
| 380 | menuItem.setIcon(filter.icon); |
| 381 | // Get rid of the default memu item overlay and show original account icons. |
| 382 | menuItem.getIcon().setColorFilter(Color.TRANSPARENT, PorterDuff.Mode.SRC_ATOP); |
| 383 | } |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 384 | |
| 385 | if (getContactListFilter() != null) { |
| 386 | updateFilterMenu(getContactListFilter()); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | protected void updateFilterMenu(ContactListFilter filter) { |
| 391 | clearCheckedMenus(); |
| 392 | if (filter.filterType == ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS) { |
| 393 | if (mIdMenuMap != null && mIdMenuMap.get(R.id.nav_all_contacts) != null) { |
| 394 | mIdMenuMap.get(R.id.nav_all_contacts).setCheckable(true); |
| 395 | mIdMenuMap.get(R.id.nav_all_contacts).setChecked(true); |
| 396 | } |
| 397 | } else { |
| 398 | if (mFilterMenuMap != null && mFilterMenuMap.get(filter) != null) { |
| 399 | mFilterMenuMap.get(filter).setCheckable(true); |
| 400 | mFilterMenuMap.get(filter).setChecked(true); |
| 401 | } |
| 402 | } |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /** |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 406 | * Returns the current filter if the child class is {@link PeopleActivity}, and null otherwise. |
| 407 | */ |
| 408 | protected ContactListFilter getContactListFilter() { |
| 409 | return null; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Returns true if the child activity should finish after launching another activity. |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 414 | */ |
| 415 | protected abstract boolean shouldFinish(); |
| 416 | |
| 417 | @Override |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 418 | public boolean onNavigationItemSelected(final MenuItem item) { |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 419 | final int id = item.getItemId(); |
| 420 | |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 421 | mToggle.runWhenIdle(new Runnable() { |
| 422 | @Override |
| 423 | public void run() { |
| 424 | if (id == R.id.nav_settings) { |
| 425 | startActivity(createPreferenceIntent()); |
| 426 | } else if (id == R.id.nav_help) { |
| 427 | HelpUtils.launchHelpAndFeedbackForMainScreen(ContactsDrawerActivity.this); |
| 428 | } else if (id == R.id.nav_all_contacts) { |
| 429 | switchToAllContacts(); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 430 | } else if (id == R.id.nav_find_duplicates) { |
| 431 | launchFindDuplicates(); |
| 432 | } else if (item.getIntent() != null) { |
| 433 | ImplicitIntentsUtil.startActivityInApp(ContactsDrawerActivity.this, |
| 434 | item.getIntent()); |
| 435 | } else { |
| 436 | Log.w(TAG, "Unhandled navigation view item selection"); |
| 437 | } |
| 438 | } |
| 439 | }); |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 440 | |
| 441 | mDrawer.closeDrawer(GravityCompat.START); |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | private Intent createPreferenceIntent() { |
| 446 | final Intent intent = new Intent(this, ContactsPreferenceActivity.class); |
| 447 | intent.putExtra(ContactsPreferenceActivity.EXTRA_NEW_LOCAL_PROFILE, |
| 448 | ContactEditorFragment.INTENT_EXTRA_NEW_LOCAL_PROFILE); |
| 449 | intent.putExtra(ContactsPreferenceActivity.EXTRA_MODE_FULLY_EXPANDED, |
| 450 | QuickContactActivity.MODE_FULLY_EXPANDED); |
| 451 | intent.putExtra(ContactsPreferenceActivity.EXTRA_PREVIOUS_SCREEN_TYPE, |
| 452 | QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE); |
| 453 | return intent; |
| 454 | } |
| 455 | |
| 456 | protected void switchToAllContacts() { |
| 457 | final Intent intent = new Intent(); |
| 458 | final ContactListFilter filter = createAllAccountsFilter(); |
| 459 | intent.putExtra(AccountFilterUtil.EXTRA_CONTACT_LIST_FILTER, filter); |
| 460 | AccountFilterUtil.handleAccountFilterResult( |
| 461 | mContactListFilterController, AppCompatActivity.RESULT_OK, intent); |
Wenyi Wang | 8e73c36 | 2016-06-23 18:03:53 -0700 | [diff] [blame] | 462 | if (shouldFinish()) { |
| 463 | finish(); |
| 464 | } |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | protected void launchFindDuplicates() { |
| 468 | ImplicitIntentsUtil.startActivityInAppIfPossible(this, |
| 469 | Assistants.getDuplicatesActivityIntent(this)); |
| 470 | } |
| 471 | |
| 472 | protected ContactListFilter createAllAccountsFilter() { |
| 473 | return ContactListFilter.createFilterWithType(ContactListFilter.FILTER_TYPE_ALL_ACCOUNTS); |
| 474 | } |
| 475 | |
Wenyi Wang | f8a5ab4 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 476 | private void clearCheckedMenus() { |
| 477 | clearCheckedMenu(mFilterMenuMap); |
| 478 | clearCheckedMenu(mGroupMenuMap); |
| 479 | clearCheckedMenu(mIdMenuMap); |
| 480 | } |
| 481 | |
| 482 | private void clearCheckedMenu(Map<?, MenuItem> map) { |
| 483 | final Iterator it = map.entrySet().iterator(); |
| 484 | while (it.hasNext()) { |
| 485 | Entry pair = (Entry)it.next(); |
| 486 | map.get(pair.getKey()).setCheckable(false); |
| 487 | map.get(pair.getKey()).setChecked(false); |
| 488 | } |
| 489 | } |
Wenyi Wang | bb22924 | 2016-05-25 16:04:13 -0700 | [diff] [blame] | 490 | } |