QuickContactListFragment.java revision e128f1e6790c6efd219aca11bcfe4f5fadd594bc
1/*
2 * Copyright (C) 2011 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
17package com.android.contacts.quickcontact;
18
19import com.android.contacts.R;
20
21import android.app.Fragment;
22import android.os.Bundle;
23import android.provider.ContactsContract.CommonDataKinds.Phone;
24import android.text.TextUtils;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.ViewGroup;
29import android.widget.BaseAdapter;
30import android.widget.ImageView;
31import android.widget.LinearLayout;
32import android.widget.ListView;
33import android.widget.TextView;
34
35import java.util.List;
36
37/** A fragment that shows the list of resolve items below a tab */
38public class QuickContactListFragment extends Fragment {
39    private ListView mListView;
40    private List<Action> mActions;
41    private LinearLayout mFragmentContainer;
42    private Listener mListener;
43
44    public QuickContactListFragment() {
45        setRetainInstance(true);
46    }
47
48    @Override
49    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
50        mFragmentContainer = (LinearLayout) inflater.inflate(R.layout.quickcontact_list_fragment,
51                container, false);
52        mListView = (ListView) mFragmentContainer.findViewById(R.id.list);
53        mListView.setItemsCanFocus(true);
54
55        mFragmentContainer.setOnClickListener(mOutsideClickListener);
56        configureAdapter();
57        return mFragmentContainer;
58    }
59
60    public void setActions(List<Action> actions) {
61        mActions = actions;
62        configureAdapter();
63    }
64
65    public void setListener(Listener value) {
66        mListener = value;
67    }
68
69    private void configureAdapter() {
70        if (mActions == null || mListView == null) return;
71
72        mListView.setAdapter(new BaseAdapter() {
73            @Override
74            public int getCount() {
75                return mActions.size();
76            }
77
78            @Override
79            public Object getItem(int position) {
80                return mActions.get(position);
81            }
82
83            @Override
84            public long getItemId(int position) {
85                return position;
86            }
87
88            @Override
89            public View getView(int position, View convertView, ViewGroup parent) {
90                final View resultView = convertView != null ? convertView
91                        : getActivity().getLayoutInflater()
92                        .inflate(R.layout.quickcontact_list_item, parent, false);
93
94                // Set action title based on summary value
95                final Action action = mActions.get(position);
96
97                // TODO: Put those findViewByIds in a container
98                final TextView text1 = (TextView) resultView.findViewById(
99                        android.R.id.text1);
100                final TextView text2 = (TextView) resultView.findViewById(
101                        android.R.id.text2);
102                final View actionsContainer = resultView.findViewById(
103                        R.id.actions_view_container);
104                final ImageView alternateActionButton = (ImageView) resultView.findViewById(
105                        R.id.secondary_action_button);
106                final View alternateActionDivider = resultView.findViewById(R.id.vertical_divider);
107
108                actionsContainer.setOnClickListener(mPrimaryActionClickListener);
109                actionsContainer.setTag(action);
110                alternateActionButton.setOnClickListener(mSecondaryActionClickListener);
111                alternateActionButton.setTag(action);
112
113                final boolean hasAlternateAction = action.getAlternateIntent() != null;
114                alternateActionDivider.setVisibility(hasAlternateAction ? View.VISIBLE : View.GONE);
115                alternateActionButton.setImageDrawable(action.getAlternateIcon());
116                alternateActionButton.setVisibility(hasAlternateAction ? View.VISIBLE : View.GONE);
117
118                // Special case for phone numbers in accessibility mode
119                if (action.getMimeType().equals(Phone.CONTENT_ITEM_TYPE)) {
120                    text1.setContentDescription(getActivity().getString(
121                            R.string.description_dial_phone_number, action.getBody()));
122                    if (hasAlternateAction) {
123                        alternateActionButton.setContentDescription(getActivity()
124                                .getString(R.string.description_send_message, action.getBody()));
125                    }
126                }
127
128                text1.setText(action.getBody());
129                CharSequence subtitle = action.getSubtitle();
130                text2.setText(subtitle);
131                if (TextUtils.isEmpty(subtitle)) {
132                    text2.setVisibility(View.GONE);
133                } else {
134                    text2.setVisibility(View.VISIBLE);
135                }
136
137                return resultView;
138            }
139        });
140    }
141
142    /** A data item (e.g. phone number) was clicked */
143    protected final OnClickListener mPrimaryActionClickListener = new OnClickListener() {
144        @Override
145        public void onClick(View v) {
146            final Action action = (Action) v.getTag();
147            if (mListener != null) mListener.onItemClicked(action, false);
148        }
149    };
150
151    /** A secondary action (SMS) was clicked */
152    protected final OnClickListener mSecondaryActionClickListener = new OnClickListener() {
153        @Override
154        public void onClick(View v) {
155            final Action action = (Action) v.getTag();
156            if (mListener != null) mListener.onItemClicked(action, true);
157        }
158    };
159
160    private final OnClickListener mOutsideClickListener = new OnClickListener() {
161        @Override
162        public void onClick(View v) {
163            if (mListener != null) mListener.onOutsideClick();
164        }
165    };
166
167    public interface Listener {
168        void onOutsideClick();
169        void onItemClicked(Action action, boolean alternate);
170    }
171}
172