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