Home | History | Annotate | Download | only in list
      1 /*
      2  * Copyright (C) 2013 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.dialer.list;
     18 
     19 import static android.Manifest.permission.READ_CONTACTS;
     20 
     21 import android.app.Activity;
     22 import android.content.BroadcastReceiver;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.Loader;
     26 import android.content.pm.PackageManager;
     27 import android.database.Cursor;
     28 import android.net.Uri;
     29 import android.provider.ContactsContract.CommonDataKinds.Phone;
     30 import android.provider.ContactsContract.QuickContact;
     31 import android.support.v13.app.FragmentCompat;
     32 import android.view.LayoutInflater;
     33 import android.view.View;
     34 import android.view.ViewGroup;
     35 import android.widget.AdapterView;
     36 
     37 import com.android.contacts.common.compat.CompatUtils;
     38 import com.android.contacts.common.list.ContactEntryListAdapter;
     39 import com.android.contacts.common.list.ContactEntryListFragment;
     40 import com.android.contacts.common.list.ContactListFilter;
     41 import com.android.contacts.common.list.DefaultContactListAdapter;
     42 import com.android.contacts.common.util.PermissionsUtil;
     43 import com.android.contacts.common.util.ViewUtil;
     44 import com.android.dialer.R;
     45 import com.android.dialer.util.DialerUtils;
     46 import com.android.dialer.util.IntentUtil;
     47 import com.android.dialer.widget.EmptyContentView;
     48 import com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
     49 
     50 /**
     51  * Fragments to show all contacts with phone numbers.
     52  */
     53 public class AllContactsFragment extends ContactEntryListFragment<ContactEntryListAdapter>
     54         implements OnEmptyViewActionButtonClickedListener,
     55         FragmentCompat.OnRequestPermissionsResultCallback {
     56 
     57     private static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
     58 
     59     private EmptyContentView mEmptyListView;
     60 
     61     /**
     62      * Listen to broadcast events about permissions in order to be notified if the READ_CONTACTS
     63      * permission is granted via the UI in another fragment.
     64      */
     65     private BroadcastReceiver mReadContactsPermissionGrantedReceiver = new BroadcastReceiver() {
     66         @Override
     67         public void onReceive(Context context, Intent intent) {
     68             reloadData();
     69         }
     70     };
     71 
     72     public AllContactsFragment() {
     73         setQuickContactEnabled(false);
     74         setAdjustSelectionBoundsEnabled(true);
     75         setPhotoLoaderEnabled(true);
     76         setSectionHeaderDisplayEnabled(true);
     77         setDarkTheme(false);
     78         setVisibleScrollbarEnabled(true);
     79     }
     80 
     81     @Override
     82     public void onViewCreated(View view, android.os.Bundle savedInstanceState) {
     83         super.onViewCreated(view, savedInstanceState);
     84 
     85         mEmptyListView = (EmptyContentView) view.findViewById(R.id.empty_list_view);
     86         mEmptyListView.setImage(R.drawable.empty_contacts);
     87         mEmptyListView.setDescription(R.string.all_contacts_empty);
     88         mEmptyListView.setActionClickedListener(this);
     89         getListView().setEmptyView(mEmptyListView);
     90         mEmptyListView.setVisibility(View.GONE);
     91 
     92         ViewUtil.addBottomPaddingToListViewForFab(getListView(), getResources());
     93     }
     94 
     95     @Override
     96     public void onStart() {
     97         super.onStart();
     98         PermissionsUtil.registerPermissionReceiver(getActivity(),
     99                 mReadContactsPermissionGrantedReceiver, READ_CONTACTS);
    100     }
    101 
    102     @Override
    103     public void onStop() {
    104         PermissionsUtil.unregisterPermissionReceiver(getActivity(),
    105                 mReadContactsPermissionGrantedReceiver);
    106         super.onStop();
    107     }
    108 
    109     @Override
    110     protected void startLoading() {
    111         if (PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) {
    112             super.startLoading();
    113             mEmptyListView.setDescription(R.string.all_contacts_empty);
    114             mEmptyListView.setActionLabel(R.string.all_contacts_empty_add_contact_action);
    115         } else {
    116             mEmptyListView.setDescription(R.string.permission_no_contacts);
    117             mEmptyListView.setActionLabel(R.string.permission_single_turn_on);
    118             mEmptyListView.setVisibility(View.VISIBLE);
    119         }
    120     }
    121 
    122     @Override
    123     public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    124         super.onLoadFinished(loader, data);
    125 
    126         if (data == null || data.getCount() == 0) {
    127             mEmptyListView.setVisibility(View.VISIBLE);
    128         }
    129     }
    130 
    131     @Override
    132     protected ContactEntryListAdapter createListAdapter() {
    133         final DefaultContactListAdapter adapter = new DefaultContactListAdapter(getActivity()) {
    134             @Override
    135             protected void bindView(View itemView, int partition, Cursor cursor, int position) {
    136                 super.bindView(itemView, partition, cursor, position);
    137                 itemView.setTag(this.getContactUri(partition, cursor));
    138             }
    139         };
    140         adapter.setDisplayPhotos(true);
    141         adapter.setFilter(ContactListFilter.createFilterWithType(
    142                 ContactListFilter.FILTER_TYPE_DEFAULT));
    143         adapter.setSectionHeaderDisplayEnabled(isSectionHeaderDisplayEnabled());
    144         return adapter;
    145     }
    146 
    147     @Override
    148     protected View inflateView(LayoutInflater inflater, ViewGroup container) {
    149         return inflater.inflate(R.layout.all_contacts_fragment, null);
    150     }
    151 
    152     @Override
    153     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    154         final Uri uri = (Uri) view.getTag();
    155         if (uri != null) {
    156             if (CompatUtils.hasPrioritizedMimeType()) {
    157                 QuickContact.showQuickContact(getContext(), view, uri, null,
    158                         Phone.CONTENT_ITEM_TYPE);
    159             } else {
    160                 QuickContact.showQuickContact(getActivity(), view, uri, QuickContact.MODE_LARGE,
    161                         null);
    162             }
    163         }
    164     }
    165 
    166     @Override
    167     protected void onItemClick(int position, long id) {
    168         // Do nothing. Implemented to satisfy ContactEntryListFragment.
    169     }
    170 
    171     @Override
    172     public void onEmptyViewActionButtonClicked() {
    173         final Activity activity = getActivity();
    174         if (activity == null) {
    175             return;
    176         }
    177 
    178         if (!PermissionsUtil.hasPermission(activity, READ_CONTACTS)) {
    179           FragmentCompat.requestPermissions(this, new String[] {READ_CONTACTS},
    180               READ_CONTACTS_PERMISSION_REQUEST_CODE);
    181         } else {
    182             // Add new contact
    183             DialerUtils.startActivityWithErrorToast(activity, IntentUtil.getNewContactIntent(),
    184                     R.string.add_contact_not_available);
    185         }
    186     }
    187 
    188     @Override
    189     public void onRequestPermissionsResult(int requestCode, String[] permissions,
    190             int[] grantResults) {
    191         if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE) {
    192             if (grantResults.length >= 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
    193                 // Force a refresh of the data since we were missing the permission before this.
    194                 reloadData();
    195             }
    196         }
    197     }
    198 }
    199