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