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                     // Initialize cursor's position. If Activity relaunched by orientation change,
127                     // only onLoadFinished is called. OnCreateLoader is not called.
128                     // The cursor's position is remain end position by moveToNext when the last
129                     // onLoadFinished was called.
130                     // Therefore, if cursor position was not initialized mGroupListItems is empty.
131                     data.moveToPosition(-1);
132                     for (int i = 0; i < data.getCount(); i++) {
133                         if (data.moveToNext()) {
134                             mGroupListItems.add(GroupUtil.getGroupListItem(data, i));
135                         }
136                     }
137                     mGroupsLoaded = true;
138                     notifyIfReady();
139                 }
140 
141                 public void onLoaderReset(Loader<Cursor> loader) {
142                 }
143             };
144 
DrawerFragment()145     public DrawerFragment() {}
146 
147     @Override
onAttach(Activity activity)148     public void onAttach(Activity activity) {
149         super.onAttach(activity);
150         if (activity instanceof DrawerFragmentListener) {
151             mListener = (DrawerFragmentListener) activity;
152         } else {
153             throw new IllegalArgumentException(
154                     "Activity must implement " + DrawerFragmentListener.class.getName());
155         }
156     }
157 
158     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)159     public View onCreateView(LayoutInflater inflater, ViewGroup container,
160             Bundle savedInstanceState) {
161         final View contentView = inflater.inflate(R.layout.drawer_fragment, null);
162         mDrawerListView = (ListView) contentView.findViewById(R.id.list);
163         mDrawerAdapter = new DrawerAdapter(getActivity());
164         mDrawerAdapter.setSelectedContactsView(mCurrentContactsView);
165         loadGroupsAndFilters();
166         mDrawerListView.setAdapter(mDrawerAdapter);
167         mDrawerListView.setOnItemClickListener(mOnDrawerItemClickListener);
168 
169         if (savedInstanceState != null) {
170             final ContactsView contactsView =
171                     ContactsView.values()[savedInstanceState.getInt(KEY_CONTACTS_VIEW)];
172             setNavigationItemChecked(contactsView);
173             final long groupId = savedInstanceState.getLong(KEY_SELECTED_GROUP);
174             mDrawerAdapter.setSelectedGroupId(groupId);
175             final ContactListFilter filter = savedInstanceState.getParcelable(KEY_SELECTED_ACCOUNT);
176             mDrawerAdapter.setSelectedAccount(filter);
177         } else {
178             setNavigationItemChecked(ContactsView.ALL_CONTACTS);
179         }
180 
181         final FrameLayout root = (FrameLayout) contentView.findViewById(R.id.drawer_fragment_root);
182         root.setFitsSystemWindows(true);
183         root.setOnApplyWindowInsetsListener(new WindowInsetsListener());
184         root.setForegroundGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL);
185 
186         mScrimDrawable = new ScrimDrawable();
187         root.setForeground(mScrimDrawable);
188 
189         return contentView;
190     }
191 
192     @Override
onResume()193     public void onResume() {
194         super.onResume();
195         // todo double check on the new Handler() thing
196         final Uri uri = ObjectFactory.getWelcomeUri();
197         if (uri != null) {
198             mObserver = new WelcomeContentObserver(new Handler());
199             getActivity().getContentResolver().registerContentObserver(uri, false, mObserver);
200         }
201     }
202 
203     @Override
onSaveInstanceState(Bundle outState)204     public void onSaveInstanceState(Bundle outState) {
205         super.onSaveInstanceState(outState);
206         outState.putInt(KEY_CONTACTS_VIEW, mCurrentContactsView.ordinal());
207         outState.putLong(KEY_SELECTED_GROUP, mDrawerAdapter.getSelectedGroupId());
208         outState.putParcelable(KEY_SELECTED_ACCOUNT, mDrawerAdapter.getSelectedAccount());
209     }
210 
211     @Override
onPause()212     public void onPause() {
213         super.onPause();
214         if (mObserver != null) {
215             getActivity().getContentResolver().unregisterContentObserver(mObserver);
216         }
217     }
218 
loadGroupsAndFilters()219     private void loadGroupsAndFilters() {
220         getLoaderManager().initLoader(LOADER_FILTERS, null, mFiltersLoaderListener);
221         AccountsLoader.loadAccounts(this, LOADER_ACCOUNTS,
222                 AccountTypeManager.AccountFilter.GROUPS_WRITABLE);
223         getLoaderManager().initLoader(LOADER_GROUPS, null, mGroupListLoaderListener);
224     }
225 
226     @Override
onDetach()227     public void onDetach() {
228         super.onDetach();
229         mListener = null;
230     }
231 
232     private final OnItemClickListener mOnDrawerItemClickListener = new OnItemClickListener() {
233         @Override
234         public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
235             if (mListener == null) {
236                 return;
237             }
238             final int viewId = v.getId();
239             if (viewId == R.id.nav_all_contacts) {
240                 mListener.onContactsViewSelected(ContactsView.ALL_CONTACTS);
241                 setNavigationItemChecked(ContactsView.ALL_CONTACTS);
242             } else if (viewId == R.id.nav_assistant) {
243                 mListener.onContactsViewSelected(ContactsView.ASSISTANT);
244                 setNavigationItemChecked(ContactsView.ASSISTANT);
245             } else if (viewId == R.id.nav_group) {
246                 final GroupListItem groupListItem = (GroupListItem) v.getTag();
247                 mListener.onGroupViewSelected(groupListItem);
248                 mDrawerAdapter.setSelectedGroupId(groupListItem.getGroupId());
249                 setNavigationItemChecked(ContactsView.GROUP_VIEW);
250             } else if (viewId == R.id.nav_filter) {
251                 final ContactListFilter filter = (ContactListFilter) v.getTag();
252                 mListener.onAccountViewSelected(filter);
253                 mDrawerAdapter.setSelectedAccount(filter);
254                 setNavigationItemChecked(ContactsView.ACCOUNT_VIEW);
255             } else if (viewId == R.id.nav_create_label) {
256                 mListener.onCreateLabelButtonClicked();
257             } else if (viewId == R.id.nav_settings) {
258                 mListener.onOpenSettings();
259             } else if (viewId == R.id.nav_help) {
260                 mListener.onLaunchHelpFeedback();
261             } else {
262                 return;
263             }
264             mListener.onDrawerItemClicked();
265         }
266     };
267 
setNavigationItemChecked(ContactsView contactsView)268     public void setNavigationItemChecked(ContactsView contactsView) {
269         mCurrentContactsView = contactsView;
270         if (mDrawerAdapter != null) {
271             mDrawerAdapter.setSelectedContactsView(contactsView);
272         }
273     }
274 
updateGroupMenu(long groupId)275     public void updateGroupMenu(long groupId) {
276         mDrawerAdapter.setSelectedGroupId(groupId);
277         setNavigationItemChecked(ContactsView.GROUP_VIEW);
278     }
279 
280     @Override
onAccountsLoaded(List<AccountInfo> accounts)281     public void onAccountsLoaded(List<AccountInfo> accounts) {
282         mHasGroupWritableAccounts = !accounts.isEmpty();
283         mAccountsLoaded = true;
284         notifyIfReady();
285     }
286 
notifyIfReady()287     private void notifyIfReady() {
288         if (mAccountsLoaded && mGroupsLoaded) {
289             final Iterator<GroupListItem> iterator = mGroupListItems.iterator();
290             while (iterator.hasNext()) {
291                 final GroupListItem groupListItem = iterator.next();
292                 if (GroupUtil.isEmptyFFCGroup(groupListItem)) {
293                     iterator.remove();
294                 }
295             }
296             mDrawerAdapter.setGroups(mGroupListItems, mHasGroupWritableAccounts);
297         }
298     }
299 
applyTopInset(int insetTop)300     private void applyTopInset(int insetTop) {
301         // set height of the scrim
302         mScrimDrawable.setIntrinsicHeight(insetTop);
303         mDrawerListView.setPadding(mDrawerListView.getPaddingLeft(),
304                 insetTop, mDrawerListView.getPaddingRight(),
305                 mDrawerListView.getPaddingBottom());
306     }
307 
308     public interface DrawerFragmentListener {
onDrawerItemClicked()309         void onDrawerItemClicked();
onContactsViewSelected(ContactsView mode)310         void onContactsViewSelected(ContactsView mode);
onGroupViewSelected(GroupListItem groupListItem)311         void onGroupViewSelected(GroupListItem groupListItem);
onAccountViewSelected(ContactListFilter filter)312         void onAccountViewSelected(ContactListFilter filter);
onCreateLabelButtonClicked()313         void onCreateLabelButtonClicked();
onOpenSettings()314         void onOpenSettings();
onLaunchHelpFeedback()315         void onLaunchHelpFeedback();
316     }
317 
318     private class WindowInsetsListener implements View.OnApplyWindowInsetsListener {
319         @Override
onApplyWindowInsets(View v, WindowInsets insets)320         public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
321             final int insetTop = insets.getSystemWindowInsetTop();
322             applyTopInset(insetTop);
323             return insets;
324         }
325     }
326 }
327