1dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee/*
2dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Copyright (C) 2013 The Android Open Source Project
3dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
4dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
5dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * you may not use this file except in compliance with the License.
6dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * You may obtain a copy of the License at
7dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
8dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
9dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
10dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Unless required by applicable law or agreed to in writing, software
11dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
12dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * See the License for the specific language governing permissions and
14dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * limitations under the License.
15dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee */
162e5b34b14181f367ec90198f5cbcd39f261b7bf5Jay Shraunerpackage com.android.dialer.list;
17dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
18dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.app.Activity;
19e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.content.ActivityNotFoundException;
20e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.content.Intent;
21dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.widget.AbsListView;
22dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.widget.AbsListView.OnScrollListener;
23e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport android.widget.Toast;
24dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
25ef2b7388569a94432278a617b528f628385fbb8cYorke Leeimport com.android.contacts.common.list.ContactEntryListAdapter;
2605de701306a4986e91a9b2b2eed3bce71d6d9e6fAlon Albertimport com.android.contacts.common.list.ContactListItemView;
27e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport com.android.contacts.common.list.OnPhoneNumberPickerActionListener;
28dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.list.PhoneNumberPickerFragment;
29e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport com.android.dialer.DialtactsActivity;
30e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport com.android.dialer.R;
31e142481570d7fbda5d035555fe217314e396ae90Yorke Leeimport com.android.dialer.dialpad.DialpadFragment;
32dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.dialer.list.OnListFragmentScrolledListener;
33dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
348898cd0577d41cfa4e6acf785c9e904bd71fbdfdYorke Leepublic class SearchFragment extends PhoneNumberPickerFragment {
35dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
36dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private OnListFragmentScrolledListener mActivityScrollListener;
37dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
38dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
39dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void onAttach(Activity activity) {
40dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        super.onAttach(activity);
41dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
42dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        setQuickContactEnabled(true);
43dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        setDarkTheme(false);
44dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        setPhotoPosition(ContactListItemView.getDefaultPhotoPosition(true /* opposite */));
45dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        setUseCallableUri(true);
46dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
47dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        try {
48dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            mActivityScrollListener = (OnListFragmentScrolledListener) activity;
49dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        } catch (ClassCastException e) {
50dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            throw new ClassCastException(activity.toString()
51dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    + " must implement OnListFragmentScrolledListener");
52dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
53dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
54dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
55dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
56dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void onStart() {
57dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        super.onStart();
58ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (isSearchMode()) {
59ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            getAdapter().setHasHeader(0, false);
60ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
61dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        getListView().setOnScrollListener(new OnScrollListener() {
62dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            @Override
63dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            public void onScrollStateChanged(AbsListView view, int scrollState) {
64dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                mActivityScrollListener.onListFragmentScrollStateChange(scrollState);
65dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
66dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
67dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            @Override
68dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
69dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    int totalItemCount) {
70dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
71dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        });
72dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
73dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
74dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
75dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected void setSearchMode(boolean flag) {
76dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        super.setSearchMode(flag);
77dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        // This hides the "All contacts with phone numbers" header in the search fragment
78ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        final ContactEntryListAdapter adapter = getAdapter();
79ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        if (adapter != null) {
80ef2b7388569a94432278a617b528f628385fbb8cYorke Lee            adapter.setHasHeader(0, false);
81ef2b7388569a94432278a617b528f628385fbb8cYorke Lee        }
82dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
83e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
84e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    @Override
85e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    protected ContactEntryListAdapter createListAdapter() {
86e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        DialerPhoneNumberListAdapter adapter = new DialerPhoneNumberListAdapter(getActivity());
87e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        adapter.setDisplayPhotos(true);
88e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        adapter.setUseCallableUri(super.usesCallableUri());
89e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        return adapter;
90e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
91e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
92e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    @Override
93e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    protected void onItemClick(int position, long id) {
94e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final DialerPhoneNumberListAdapter adapter = (DialerPhoneNumberListAdapter) getAdapter();
95e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        final int shortcutType = adapter.getShortcutTypeFromPosition(position);
96e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
97e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_INVALID) {
98e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            super.onItemClick(position, id);
99e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        } else if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_DIRECT_CALL) {
100e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            final OnPhoneNumberPickerActionListener listener =
101e142481570d7fbda5d035555fe217314e396ae90Yorke Lee                    getOnPhoneNumberPickerListener();
102e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            if (listener != null) {
103e142481570d7fbda5d035555fe217314e396ae90Yorke Lee                listener.onCallNumberDirectly(getQueryString());
104e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            }
105e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        } else if (shortcutType == DialerPhoneNumberListAdapter.SHORTCUT_ADD_NUMBER_TO_CONTACTS) {
106e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            final String number = adapter.getFormattedQueryString();
107e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            final Intent intent = DialtactsActivity.getAddNumberToContactIntent(number);
108e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            startActivityWithErrorToast(intent);
109e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        }
110e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
111e142481570d7fbda5d035555fe217314e396ae90Yorke Lee
112e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    private void startActivityWithErrorToast(Intent intent) {
113e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        try {
114e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            startActivity(intent);
115e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        } catch (ActivityNotFoundException e) {
116e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            Toast toast = Toast.makeText(getActivity(), R.string.add_contact_not_available,
117e142481570d7fbda5d035555fe217314e396ae90Yorke Lee                    Toast.LENGTH_SHORT);
118e142481570d7fbda5d035555fe217314e396ae90Yorke Lee            toast.show();
119e142481570d7fbda5d035555fe217314e396ae90Yorke Lee        }
120e142481570d7fbda5d035555fe217314e396ae90Yorke Lee    }
121dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee}
122