AllContactsFragment.java revision 63825c8c150676a563719a00aa02b42c9775189c
18dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee/*
28dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * Copyright (C) 2013 The Android Open Source Project
38dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee *
48dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
58dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * you may not use this file except in compliance with the License.
68dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * You may obtain a copy of the License at
78dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee *
88dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
98dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee *
108dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * Unless required by applicable law or agreed to in writing, software
118dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
128dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * See the License for the specific language governing permissions and
148dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * limitations under the License.
158dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee */
168dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
178dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leepackage com.android.dialer.list;
188dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
1938019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport static android.Manifest.permission.READ_CONTACTS;
2038019af70eb1ca084d36291390bbc54dc81027deYorke Lee
2138019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport android.app.Activity;
220284f76fe813c4ebdf1d0d3472060719757fe394Andrew Leeimport android.content.Loader;
2338019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport android.content.pm.PackageManager;
246144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.database.Cursor;
256144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.net.Uri;
2674eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwellimport android.provider.ContactsContract.CommonDataKinds.Phone;
276144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.provider.ContactsContract.QuickContact;
288dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.LayoutInflater;
298dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.View;
308dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.ViewGroup;
31931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport android.widget.AdapterView;
328dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
33931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactEntryListAdapter;
34931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactEntryListFragment;
35931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactListFilter;
36931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.DefaultContactListAdapter;
37c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Leeimport com.android.contacts.common.util.PermissionsUtil;
38781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Leeimport com.android.contacts.common.util.ViewUtil;
398dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.R;
400a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Leeimport com.android.dialer.util.DialerUtils;
4138019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.util.IntentUtil;
4238019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.widget.EmptyContentView;
4338019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
448dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
458dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee/**
468dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * Fragments to show all contacts with phone numbers.
478dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee */
4838019af70eb1ca084d36291390bbc54dc81027deYorke Leepublic class AllContactsFragment extends ContactEntryListFragment<ContactEntryListAdapter>
4938019af70eb1ca084d36291390bbc54dc81027deYorke Lee        implements OnEmptyViewActionButtonClickedListener {
5038019af70eb1ca084d36291390bbc54dc81027deYorke Lee
5138019af70eb1ca084d36291390bbc54dc81027deYorke Lee    private static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
528dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
5338019af70eb1ca084d36291390bbc54dc81027deYorke Lee    private EmptyContentView mEmptyListView;
540284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
55931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    public AllContactsFragment() {
566144d027255568a76adf8c36b96af8e283460622Yorke Lee        setQuickContactEnabled(false);
57331f457e9ac7b41fc8c43000ed989a3c09d68f62Andrew Lee        setAdjustSelectionBoundsEnabled(true);
58931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        setPhotoLoaderEnabled(true);
59931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        setSectionHeaderDisplayEnabled(true);
608dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        setDarkTheme(false);
6153845dc94a78d3efe4f8ea76a5e9a60dbc79aa16Yorke Lee        setVisibleScrollbarEnabled(true);
62931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
63931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
64931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
65781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    public void onViewCreated(View view, android.os.Bundle savedInstanceState) {
66781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee        super.onViewCreated(view, savedInstanceState);
670a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Lee
6838019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView = (EmptyContentView) view.findViewById(R.id.empty_list_view);
6938019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setImage(R.drawable.empty_contacts);
7038019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setDescription(R.string.all_contacts_empty);
7138019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setActionClickedListener(this);
720284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        getListView().setEmptyView(mEmptyListView);
730284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        mEmptyListView.setVisibility(View.GONE);
740a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Lee
75781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee        ViewUtil.addBottomPaddingToListViewForFab(getListView(), getResources());
76781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    }
77781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee
78781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    @Override
79c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    protected void startLoading() {
8038019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) {
81c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee            super.startLoading();
8238019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setDescription(R.string.all_contacts_empty);
8338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setActionLabel(R.string.all_contacts_empty_add_contact_action);
8438019af70eb1ca084d36291390bbc54dc81027deYorke Lee        } else {
8538019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setDescription(R.string.permission_no_contacts);
8638019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setActionLabel(R.string.permission_single_turn_on);
8738019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setVisibility(View.VISIBLE);
88c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee        }
89c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    }
90c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee
91c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    @Override
920284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
930284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        super.onLoadFinished(loader, data);
940284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
95e35b0efae9406cd5fa5c80b487e4a4846d6e485dYorke Lee        if (data == null || data.getCount() == 0) {
960284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee            mEmptyListView.setVisibility(View.VISIBLE);
970284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        }
980284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    }
990284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
1000284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    @Override
101931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    protected ContactEntryListAdapter createListAdapter() {
1026144d027255568a76adf8c36b96af8e283460622Yorke Lee        final DefaultContactListAdapter adapter = new DefaultContactListAdapter(getActivity()) {
1036144d027255568a76adf8c36b96af8e283460622Yorke Lee            @Override
1046144d027255568a76adf8c36b96af8e283460622Yorke Lee            protected void bindView(View itemView, int partition, Cursor cursor, int position) {
1056144d027255568a76adf8c36b96af8e283460622Yorke Lee                super.bindView(itemView, partition, cursor, position);
1066144d027255568a76adf8c36b96af8e283460622Yorke Lee                itemView.setTag(this.getContactUri(partition, cursor));
1076144d027255568a76adf8c36b96af8e283460622Yorke Lee            }
1086144d027255568a76adf8c36b96af8e283460622Yorke Lee        };
109931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setDisplayPhotos(true);
110931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setFilter(ContactListFilter.createFilterWithType(
1113c5b0289eaf92e0b7345321993de12e89d01b8dbYorke Lee                ContactListFilter.FILTER_TYPE_DEFAULT));
112931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setSectionHeaderDisplayEnabled(isSectionHeaderDisplayEnabled());
113931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        return adapter;
1148dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
1158dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
1168dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
1178dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
11863825c8c150676a563719a00aa02b42c9775189cYorke Lee        return inflater.inflate(R.layout.all_contacts_fragment, null);
1198dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
120931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
121931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
122931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1236144d027255568a76adf8c36b96af8e283460622Yorke Lee        final Uri uri = (Uri) view.getTag();
1246144d027255568a76adf8c36b96af8e283460622Yorke Lee        if (uri != null) {
12574eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwell            QuickContact.showQuickContact(getContext(), view, uri, null,
12674eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwell                    Phone.CONTENT_ITEM_TYPE);
1276144d027255568a76adf8c36b96af8e283460622Yorke Lee        }
128931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
129931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
130931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
131931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    protected void onItemClick(int position, long id) {
132931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        // Do nothing. Implemented to satisfy ContactEntryListFragment.
133931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
13438019af70eb1ca084d36291390bbc54dc81027deYorke Lee
13538019af70eb1ca084d36291390bbc54dc81027deYorke Lee    @Override
13638019af70eb1ca084d36291390bbc54dc81027deYorke Lee    public void onEmptyViewActionButtonClicked(String[] permissions) {
13738019af70eb1ca084d36291390bbc54dc81027deYorke Lee        final Activity activity = getActivity();
13838019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (activity == null) {
13938019af70eb1ca084d36291390bbc54dc81027deYorke Lee            return;
14038019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
14138019af70eb1ca084d36291390bbc54dc81027deYorke Lee
14238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (!PermissionsUtil.hasPermission(activity, READ_CONTACTS)) {
14338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            requestPermissions(new String[] {READ_CONTACTS}, READ_CONTACTS_PERMISSION_REQUEST_CODE);
14438019af70eb1ca084d36291390bbc54dc81027deYorke Lee        } else {
14538019af70eb1ca084d36291390bbc54dc81027deYorke Lee            // Add new contact
14638019af70eb1ca084d36291390bbc54dc81027deYorke Lee            DialerUtils.startActivityWithErrorToast(activity, IntentUtil.getNewContactIntent(),
14738019af70eb1ca084d36291390bbc54dc81027deYorke Lee                    R.string.add_contact_not_available);
14838019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
14938019af70eb1ca084d36291390bbc54dc81027deYorke Lee    }
15038019af70eb1ca084d36291390bbc54dc81027deYorke Lee
15138019af70eb1ca084d36291390bbc54dc81027deYorke Lee    @Override
15238019af70eb1ca084d36291390bbc54dc81027deYorke Lee    public void onRequestPermissionsResult(int requestCode, String[] permissions,
15338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            int[] grantResults) {
15438019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE) {
15538019af70eb1ca084d36291390bbc54dc81027deYorke Lee            if (grantResults.length >= 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
15638019af70eb1ca084d36291390bbc54dc81027deYorke Lee                // Force a refresh of the data since we were missing the permission before this.
15738019af70eb1ca084d36291390bbc54dc81027deYorke Lee                reloadData();
15838019af70eb1ca084d36291390bbc54dc81027deYorke Lee            }
15938019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
16038019af70eb1ca084d36291390bbc54dc81027deYorke Lee    }
1618dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee}
162