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;
227e1a68556c419c4b9b1eda9247e80262615fa809Yorke Leeimport android.content.BroadcastReceiver;
237e1a68556c419c4b9b1eda9247e80262615fa809Yorke Leeimport android.content.Context;
247e1a68556c419c4b9b1eda9247e80262615fa809Yorke Leeimport android.content.Intent;
250284f76fe813c4ebdf1d0d3472060719757fe394Andrew Leeimport android.content.Loader;
2638019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport android.content.pm.PackageManager;
276144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.database.Cursor;
286144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.net.Uri;
2974eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwellimport android.provider.ContactsContract.CommonDataKinds.Phone;
306144d027255568a76adf8c36b96af8e283460622Yorke Leeimport android.provider.ContactsContract.QuickContact;
318dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.LayoutInflater;
328dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.View;
338dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport android.view.ViewGroup;
34931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport android.widget.AdapterView;
358dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
36931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactEntryListAdapter;
37931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactEntryListFragment;
38931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.ContactListFilter;
39931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Leeimport com.android.contacts.common.list.DefaultContactListAdapter;
40c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Leeimport com.android.contacts.common.util.PermissionsUtil;
41781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Leeimport com.android.contacts.common.util.ViewUtil;
428dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Leeimport com.android.dialer.R;
430a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Leeimport com.android.dialer.util.DialerUtils;
4438019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.util.IntentUtil;
4538019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.widget.EmptyContentView;
4638019af70eb1ca084d36291390bbc54dc81027deYorke Leeimport com.android.dialer.widget.EmptyContentView.OnEmptyViewActionButtonClickedListener;
478dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
488dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee/**
498dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee * Fragments to show all contacts with phone numbers.
508dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee */
5138019af70eb1ca084d36291390bbc54dc81027deYorke Leepublic class AllContactsFragment extends ContactEntryListFragment<ContactEntryListAdapter>
5238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        implements OnEmptyViewActionButtonClickedListener {
5338019af70eb1ca084d36291390bbc54dc81027deYorke Lee
5438019af70eb1ca084d36291390bbc54dc81027deYorke Lee    private static final int READ_CONTACTS_PERMISSION_REQUEST_CODE = 1;
558dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
5638019af70eb1ca084d36291390bbc54dc81027deYorke Lee    private EmptyContentView mEmptyListView;
570284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
587e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    /**
597e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee     * Listen to broadcast events about permissions in order to be notified if the READ_CONTACTS
607e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee     * permission is granted via the UI in another fragment.
617e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee     */
627e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    private BroadcastReceiver mReadContactsPermissionGrantedReceiver = new BroadcastReceiver() {
637e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        @Override
647e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        public void onReceive(Context context, Intent intent) {
657e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee            reloadData();
667e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        }
677e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    };
687e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee
69931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    public AllContactsFragment() {
706144d027255568a76adf8c36b96af8e283460622Yorke Lee        setQuickContactEnabled(false);
71331f457e9ac7b41fc8c43000ed989a3c09d68f62Andrew Lee        setAdjustSelectionBoundsEnabled(true);
72931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        setPhotoLoaderEnabled(true);
73931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        setSectionHeaderDisplayEnabled(true);
748dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee        setDarkTheme(false);
7553845dc94a78d3efe4f8ea76a5e9a60dbc79aa16Yorke Lee        setVisibleScrollbarEnabled(true);
76931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
77931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
78931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
79781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    public void onViewCreated(View view, android.os.Bundle savedInstanceState) {
80781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee        super.onViewCreated(view, savedInstanceState);
810a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Lee
8238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView = (EmptyContentView) view.findViewById(R.id.empty_list_view);
8338019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setImage(R.drawable.empty_contacts);
8438019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setDescription(R.string.all_contacts_empty);
8538019af70eb1ca084d36291390bbc54dc81027deYorke Lee        mEmptyListView.setActionClickedListener(this);
860284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        getListView().setEmptyView(mEmptyListView);
870284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        mEmptyListView.setVisibility(View.GONE);
880a4327e6bfeedc23ddefb4df9f6e8041ebc87b59Andrew Lee
89781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee        ViewUtil.addBottomPaddingToListViewForFab(getListView(), getResources());
90781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    }
91781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee
92781ee24f91f6eb6a1687d803971196f2bf71c02bYorke Lee    @Override
937e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    public void onStart() {
947e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        super.onStart();
957e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        PermissionsUtil.registerPermissionReceiver(getActivity(),
967e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee                mReadContactsPermissionGrantedReceiver, READ_CONTACTS);
977e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    }
987e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee
997e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    @Override
1007e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    public void onStop() {
1017e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        PermissionsUtil.unregisterPermissionReceiver(getActivity(),
1027e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee                mReadContactsPermissionGrantedReceiver);
1037e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee        super.onStop();
1047e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    }
1057e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee
1067e1a68556c419c4b9b1eda9247e80262615fa809Yorke Lee    @Override
107c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    protected void startLoading() {
10838019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (PermissionsUtil.hasPermission(getActivity(), READ_CONTACTS)) {
109c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee            super.startLoading();
11038019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setDescription(R.string.all_contacts_empty);
11138019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setActionLabel(R.string.all_contacts_empty_add_contact_action);
11238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        } else {
11338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setDescription(R.string.permission_no_contacts);
11438019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setActionLabel(R.string.permission_single_turn_on);
11538019af70eb1ca084d36291390bbc54dc81027deYorke Lee            mEmptyListView.setVisibility(View.VISIBLE);
116c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee        }
117c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    }
118c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee
119c16ea5ad67bf454158b364dec2f26c95a879c350Yorke Lee    @Override
1200284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
1210284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        super.onLoadFinished(loader, data);
1220284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
123e35b0efae9406cd5fa5c80b487e4a4846d6e485dYorke Lee        if (data == null || data.getCount() == 0) {
1240284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee            mEmptyListView.setVisibility(View.VISIBLE);
1250284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee        }
1260284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    }
1270284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee
1280284f76fe813c4ebdf1d0d3472060719757fe394Andrew Lee    @Override
129931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    protected ContactEntryListAdapter createListAdapter() {
1306144d027255568a76adf8c36b96af8e283460622Yorke Lee        final DefaultContactListAdapter adapter = new DefaultContactListAdapter(getActivity()) {
1316144d027255568a76adf8c36b96af8e283460622Yorke Lee            @Override
1326144d027255568a76adf8c36b96af8e283460622Yorke Lee            protected void bindView(View itemView, int partition, Cursor cursor, int position) {
1336144d027255568a76adf8c36b96af8e283460622Yorke Lee                super.bindView(itemView, partition, cursor, position);
1346144d027255568a76adf8c36b96af8e283460622Yorke Lee                itemView.setTag(this.getContactUri(partition, cursor));
1356144d027255568a76adf8c36b96af8e283460622Yorke Lee            }
1366144d027255568a76adf8c36b96af8e283460622Yorke Lee        };
137931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setDisplayPhotos(true);
138931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setFilter(ContactListFilter.createFilterWithType(
1393c5b0289eaf92e0b7345321993de12e89d01b8dbYorke Lee                ContactListFilter.FILTER_TYPE_DEFAULT));
140931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        adapter.setSectionHeaderDisplayEnabled(isSectionHeaderDisplayEnabled());
141931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        return adapter;
1428dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
1438dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee
1448dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    @Override
1458dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    protected View inflateView(LayoutInflater inflater, ViewGroup container) {
14663825c8c150676a563719a00aa02b42c9775189cYorke Lee        return inflater.inflate(R.layout.all_contacts_fragment, null);
1478dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee    }
148931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
149931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
150931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
1516144d027255568a76adf8c36b96af8e283460622Yorke Lee        final Uri uri = (Uri) view.getTag();
1526144d027255568a76adf8c36b96af8e283460622Yorke Lee        if (uri != null) {
15374eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwell            QuickContact.showQuickContact(getContext(), view, uri, null,
15474eb4ecc5df272f681b427760d46deafce8ed0b2Brian Attwell                    Phone.CONTENT_ITEM_TYPE);
1556144d027255568a76adf8c36b96af8e283460622Yorke Lee        }
156931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
157931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee
158931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    @Override
159931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    protected void onItemClick(int position, long id) {
160931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee        // Do nothing. Implemented to satisfy ContactEntryListFragment.
161931075eab957d7ecd7a9e880c0650012e29f05c2Andrew Lee    }
16238019af70eb1ca084d36291390bbc54dc81027deYorke Lee
16338019af70eb1ca084d36291390bbc54dc81027deYorke Lee    @Override
164575ae388961252a771488c357e425fca191594b2Yorke Lee    public void onEmptyViewActionButtonClicked() {
16538019af70eb1ca084d36291390bbc54dc81027deYorke Lee        final Activity activity = getActivity();
16638019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (activity == null) {
16738019af70eb1ca084d36291390bbc54dc81027deYorke Lee            return;
16838019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
16938019af70eb1ca084d36291390bbc54dc81027deYorke Lee
17038019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (!PermissionsUtil.hasPermission(activity, READ_CONTACTS)) {
17138019af70eb1ca084d36291390bbc54dc81027deYorke Lee            requestPermissions(new String[] {READ_CONTACTS}, READ_CONTACTS_PERMISSION_REQUEST_CODE);
17238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        } else {
17338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            // Add new contact
17438019af70eb1ca084d36291390bbc54dc81027deYorke Lee            DialerUtils.startActivityWithErrorToast(activity, IntentUtil.getNewContactIntent(),
17538019af70eb1ca084d36291390bbc54dc81027deYorke Lee                    R.string.add_contact_not_available);
17638019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
17738019af70eb1ca084d36291390bbc54dc81027deYorke Lee    }
17838019af70eb1ca084d36291390bbc54dc81027deYorke Lee
17938019af70eb1ca084d36291390bbc54dc81027deYorke Lee    @Override
18038019af70eb1ca084d36291390bbc54dc81027deYorke Lee    public void onRequestPermissionsResult(int requestCode, String[] permissions,
18138019af70eb1ca084d36291390bbc54dc81027deYorke Lee            int[] grantResults) {
18238019af70eb1ca084d36291390bbc54dc81027deYorke Lee        if (requestCode == READ_CONTACTS_PERMISSION_REQUEST_CODE) {
18338019af70eb1ca084d36291390bbc54dc81027deYorke Lee            if (grantResults.length >= 1 && PackageManager.PERMISSION_GRANTED == grantResults[0]) {
18438019af70eb1ca084d36291390bbc54dc81027deYorke Lee                // Force a refresh of the data since we were missing the permission before this.
18538019af70eb1ca084d36291390bbc54dc81027deYorke Lee                reloadData();
18638019af70eb1ca084d36291390bbc54dc81027deYorke Lee            }
18738019af70eb1ca084d36291390bbc54dc81027deYorke Lee        }
18838019af70eb1ca084d36291390bbc54dc81027deYorke Lee    }
1898dd6200bef2eb07486f4c7e288941cfcfed35e79Yorke Lee}
190