1 /* 2 * Copyright (C) 2017 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.drawer; 18 19 import android.app.Activity; 20 import android.app.Fragment; 21 import android.app.LoaderManager; 22 import android.content.CursorLoader; 23 import android.content.Loader; 24 import android.database.ContentObserver; 25 import android.database.Cursor; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.view.Gravity; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 import android.view.ViewGroup; 33 import android.view.WindowInsets; 34 import android.widget.AdapterView; 35 import android.widget.AdapterView.OnItemClickListener; 36 import android.widget.FrameLayout; 37 import android.widget.ListView; 38 39 import com.android.contacts.GroupListLoader; 40 import com.android.contacts.R; 41 import com.android.contacts.activities.PeopleActivity.ContactsView; 42 import com.android.contacts.group.GroupListItem; 43 import com.android.contacts.group.GroupUtil; 44 import com.android.contacts.list.ContactListFilter; 45 import com.android.contacts.model.AccountTypeManager; 46 import com.android.contacts.model.account.AccountInfo; 47 import com.android.contacts.model.account.AccountsLoader; 48 import com.android.contacts.model.account.AccountsLoader.AccountsListener; 49 import com.android.contacts.util.AccountFilterUtil; 50 import com.android.contactsbind.ObjectFactory; 51 52 import java.util.ArrayList; 53 import java.util.Iterator; 54 import java.util.List; 55 56 public class DrawerFragment extends Fragment implements AccountsListener { 57 58 private static final int LOADER_GROUPS = 1; 59 private static final int LOADER_ACCOUNTS = 2; 60 private static final int LOADER_FILTERS = 3; 61 62 private static final String KEY_CONTACTS_VIEW = "contactsView"; 63 private static final String KEY_SELECTED_GROUP = "selectedGroup"; 64 private static final String KEY_SELECTED_ACCOUNT = "selectedAccount"; 65 66 private WelcomeContentObserver mObserver; 67 private ListView mDrawerListView; 68 private DrawerAdapter mDrawerAdapter; 69 private ContactsView mCurrentContactsView; 70 private DrawerFragmentListener mListener; 71 // Transparent scrim drawn at the top of the drawer fragment. 72 private ScrimDrawable mScrimDrawable; 73 74 private List<GroupListItem> mGroupListItems = new ArrayList<>(); 75 private boolean mGroupsLoaded; 76 private boolean mAccountsLoaded; 77 private boolean mHasGroupWritableAccounts; 78 79 private final class WelcomeContentObserver extends ContentObserver { WelcomeContentObserver(Handler handler)80 private WelcomeContentObserver(Handler handler) { 81 super(handler); 82 } 83 84 @Override onChange(boolean selfChange)85 public void onChange(boolean selfChange) { 86 mDrawerAdapter.notifyDataSetChanged(); 87 } 88 } 89 90 private final LoaderManager.LoaderCallbacks<List<ContactListFilter>> mFiltersLoaderListener = 91 new LoaderManager.LoaderCallbacks<List<ContactListFilter>> () { 92 @Override 93 public Loader<List<ContactListFilter>> onCreateLoader(int id, Bundle args) { 94 return new AccountFilterUtil.FilterLoader(getActivity()); 95 } 96 97 @Override 98 public void onLoadFinished( 99 Loader<List<ContactListFilter>> loader, List<ContactListFilter> data) { 100 if (data != null) { 101 if (data == null || data.size() < 2) { 102 mDrawerAdapter.setAccounts(new ArrayList<ContactListFilter>()); 103 } else { 104 mDrawerAdapter.setAccounts(data); 105 } 106 } 107 } 108 109 public void onLoaderReset(Loader<List<ContactListFilter>> loader) { 110 } 111 }; 112 113 private final LoaderManager.LoaderCallbacks<Cursor> mGroupListLoaderListener = 114 new LoaderManager.LoaderCallbacks<Cursor>() { 115 @Override 116 public CursorLoader onCreateLoader(int id, Bundle args) { 117 return new GroupListLoader(getActivity()); 118 } 119 120 @Override 121 public void onLoadFinished(Loader<Cursor> loader, Cursor data) { 122 if (data == null) { 123 return; 124 } 125 mGroupListItems.clear(); 126 for (int i = 0; i < data.getCount(); i++) { 127 if (data.moveToNext()) { 128 mGroupListItems.add(GroupUtil.getGroupListItem(data, i)); 129 } 130 } 131 mGroupsLoaded = true; 132 notifyIfReady(); 133 } 134 135 public void onLoaderReset(Loader<Cursor> loader) { 136 } 137 }; 138 DrawerFragment()139 public DrawerFragment() {} 140 141 @Override onAttach(Activity activity)142 public void onAttach(Activity activity) { 143 super.onAttach(activity); 144 if (activity instanceof DrawerFragmentListener) { 145 mListener = (DrawerFragmentListener) activity; 146 } else { 147 throw new IllegalArgumentException( 148 "Activity must implement " + DrawerFragmentListener.class.getName()); 149 } 150 } 151 152 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)153 public View onCreateView(LayoutInflater inflater, ViewGroup container, 154 Bundle savedInstanceState) { 155 final View contentView = inflater.inflate(R.layout.drawer_fragment, null); 156 mDrawerListView = (ListView) contentView.findViewById(R.id.list); 157 mDrawerAdapter = new DrawerAdapter(getActivity()); 158 mDrawerAdapter.setSelectedContactsView(mCurrentContactsView); 159 loadGroupsAndFilters(); 160 mDrawerListView.setAdapter(mDrawerAdapter); 161 mDrawerListView.setOnItemClickListener(mOnDrawerItemClickListener); 162 163 if (savedInstanceState != null) { 164 final ContactsView contactsView = 165 ContactsView.values()[savedInstanceState.getInt(KEY_CONTACTS_VIEW)]; 166 setNavigationItemChecked(contactsView); 167 final long groupId = savedInstanceState.getLong(KEY_SELECTED_GROUP); 168 mDrawerAdapter.setSelectedGroupId(groupId); 169 final ContactListFilter filter = savedInstanceState.getParcelable(KEY_SELECTED_ACCOUNT); 170 mDrawerAdapter.setSelectedAccount(filter); 171 } else { 172 setNavigationItemChecked(ContactsView.ALL_CONTACTS); 173 } 174 175 final FrameLayout root = (FrameLayout) contentView.findViewById(R.id.drawer_fragment_root); 176 root.setFitsSystemWindows(true); 177 root.setOnApplyWindowInsetsListener(new WindowInsetsListener()); 178 root.setForegroundGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL); 179 180 mScrimDrawable = new ScrimDrawable(); 181 root.setForeground(mScrimDrawable); 182 183 return contentView; 184 } 185 186 @Override onResume()187 public void onResume() { 188 super.onResume(); 189 // todo double check on the new Handler() thing 190 final Uri uri = ObjectFactory.getWelcomeUri(); 191 if (uri != null) { 192 mObserver = new WelcomeContentObserver(new Handler()); 193 getActivity().getContentResolver().registerContentObserver(uri, false, mObserver); 194 } 195 } 196 197 @Override onSaveInstanceState(Bundle outState)198 public void onSaveInstanceState(Bundle outState) { 199 super.onSaveInstanceState(outState); 200 outState.putInt(KEY_CONTACTS_VIEW, mCurrentContactsView.ordinal()); 201 outState.putLong(KEY_SELECTED_GROUP, mDrawerAdapter.getSelectedGroupId()); 202 outState.putParcelable(KEY_SELECTED_ACCOUNT, mDrawerAdapter.getSelectedAccount()); 203 } 204 205 @Override onPause()206 public void onPause() { 207 super.onPause(); 208 if (mObserver != null) { 209 getActivity().getContentResolver().unregisterContentObserver(mObserver); 210 } 211 } 212 loadGroupsAndFilters()213 private void loadGroupsAndFilters() { 214 getLoaderManager().initLoader(LOADER_FILTERS, null, mFiltersLoaderListener); 215 AccountsLoader.loadAccounts(this, LOADER_ACCOUNTS, 216 AccountTypeManager.AccountFilter.GROUPS_WRITABLE); 217 getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupListLoaderListener); 218 } 219 220 @Override onDetach()221 public void onDetach() { 222 super.onDetach(); 223 mListener = null; 224 } 225 226 private final OnItemClickListener mOnDrawerItemClickListener = new OnItemClickListener() { 227 @Override 228 public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 229 if (mListener == null) { 230 return; 231 } 232 final int viewId = v.getId(); 233 if (viewId == R.id.nav_all_contacts) { 234 mListener.onContactsViewSelected(ContactsView.ALL_CONTACTS); 235 setNavigationItemChecked(ContactsView.ALL_CONTACTS); 236 } else if (viewId == R.id.nav_assistant) { 237 mListener.onContactsViewSelected(ContactsView.ASSISTANT); 238 setNavigationItemChecked(ContactsView.ASSISTANT); 239 } else if (viewId == R.id.nav_group) { 240 final GroupListItem groupListItem = (GroupListItem) v.getTag(); 241 mListener.onGroupViewSelected(groupListItem); 242 mDrawerAdapter.setSelectedGroupId(groupListItem.getGroupId()); 243 setNavigationItemChecked(ContactsView.GROUP_VIEW); 244 } else if (viewId == R.id.nav_filter) { 245 final ContactListFilter filter = (ContactListFilter) v.getTag(); 246 mListener.onAccountViewSelected(filter); 247 mDrawerAdapter.setSelectedAccount(filter); 248 setNavigationItemChecked(ContactsView.ACCOUNT_VIEW); 249 } else if (viewId == R.id.nav_create_label) { 250 mListener.onCreateLabelButtonClicked(); 251 } else if (viewId == R.id.nav_settings) { 252 mListener.onOpenSettings(); 253 } else if (viewId == R.id.nav_help) { 254 mListener.onLaunchHelpFeedback(); 255 } else { 256 return; 257 } 258 mListener.onDrawerItemClicked(); 259 } 260 }; 261 setNavigationItemChecked(ContactsView contactsView)262 public void setNavigationItemChecked(ContactsView contactsView) { 263 mCurrentContactsView = contactsView; 264 if (mDrawerAdapter != null) { 265 mDrawerAdapter.setSelectedContactsView(contactsView); 266 } 267 } 268 updateGroupMenu(long groupId)269 public void updateGroupMenu(long groupId) { 270 mDrawerAdapter.setSelectedGroupId(groupId); 271 setNavigationItemChecked(ContactsView.GROUP_VIEW); 272 } 273 274 @Override onAccountsLoaded(List<AccountInfo> accounts)275 public void onAccountsLoaded(List<AccountInfo> accounts) { 276 mHasGroupWritableAccounts = !accounts.isEmpty(); 277 mAccountsLoaded = true; 278 notifyIfReady(); 279 } 280 notifyIfReady()281 private void notifyIfReady() { 282 if (mAccountsLoaded && mGroupsLoaded) { 283 final Iterator<GroupListItem> iterator = mGroupListItems.iterator(); 284 while (iterator.hasNext()) { 285 final GroupListItem groupListItem = iterator.next(); 286 if (GroupUtil.isEmptyFFCGroup(groupListItem)) { 287 iterator.remove(); 288 } 289 } 290 mDrawerAdapter.setGroups(mGroupListItems, mHasGroupWritableAccounts); 291 } 292 } 293 294 public interface DrawerFragmentListener { onDrawerItemClicked()295 void onDrawerItemClicked(); onContactsViewSelected(ContactsView mode)296 void onContactsViewSelected(ContactsView mode); onGroupViewSelected(GroupListItem groupListItem)297 void onGroupViewSelected(GroupListItem groupListItem); onAccountViewSelected(ContactListFilter filter)298 void onAccountViewSelected(ContactListFilter filter); onCreateLabelButtonClicked()299 void onCreateLabelButtonClicked(); onOpenSettings()300 void onOpenSettings(); onLaunchHelpFeedback()301 void onLaunchHelpFeedback(); 302 } 303 304 private class WindowInsetsListener implements View.OnApplyWindowInsetsListener { 305 @Override onApplyWindowInsets(View v, WindowInsets insets)306 public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { 307 final int insetTop = insets.getSystemWindowInsetTop(); 308 // set height of the scrim 309 mScrimDrawable.setIntrinsicHeight(insetTop); 310 return insets; 311 } 312 } 313 } 314