• 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.R;
20 
21 import android.app.Fragment;
22 import android.os.Bundle;
23 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
24 import android.provider.ContactsContract.CommonDataKinds.Email;
25 import android.provider.ContactsContract.CommonDataKinds.Phone;
26 import android.provider.ContactsContract.CommonDataKinds.Website;
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.LinearLayout;
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 LinearLayout 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 = (LinearLayout) 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 
115                 actionsContainer.setOnClickListener(mPrimaryActionClickListener);
116                 actionsContainer.setTag(action);
117                 alternateActionButton.setOnClickListener(mSecondaryActionClickListener);
118                 alternateActionButton.setTag(action);
119 
120                 final boolean hasAlternateAction = action.getAlternateIntent() != null;
121                 alternateActionDivider.setVisibility(hasAlternateAction ? View.VISIBLE : View.GONE);
122                 alternateActionButton.setImageDrawable(action.getAlternateIcon());
123                 alternateActionButton.setContentDescription(action.getAlternateIconDescription());
124                 alternateActionButton.setVisibility(hasAlternateAction ? View.VISIBLE : View.GONE);
125 
126                 // Special case for phone numbers in accessibility mode
127                 if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
128                     text1.setContentDescription(getActivity().getString(
129                             R.string.description_dial_phone_number, action.getBody()));
130                     if (hasAlternateAction) {
131                         alternateActionButton.setContentDescription(getActivity()
132                                 .getString(R.string.description_send_message, action.getBody()));
133                     }
134                 }
135 
136                 text1.setText(action.getBody());
137                 if (text2 != null) {
138                     CharSequence subtitle = action.getSubtitle();
139                     text2.setText(subtitle);
140                     if (TextUtils.isEmpty(subtitle)) {
141                         text2.setVisibility(View.GONE);
142                     } else {
143                         text2.setVisibility(View.VISIBLE);
144                     }
145                 }
146                 return resultView;
147             }
148         });
149     }
150 
151     /** A data item (e.g. phone number) was clicked */
152     protected final OnClickListener mPrimaryActionClickListener = new OnClickListener() {
153         @Override
154         public void onClick(View v) {
155             final Action action = (Action) v.getTag();
156             if (mListener != null) mListener.onItemClicked(action, false);
157         }
158     };
159 
160     /** A secondary action (SMS) was clicked */
161     protected final OnClickListener mSecondaryActionClickListener = new OnClickListener() {
162         @Override
163         public void onClick(View v) {
164             final Action action = (Action) v.getTag();
165             if (mListener != null) mListener.onItemClicked(action, true);
166         }
167     };
168 
169     private final OnClickListener mOutsideClickListener = new OnClickListener() {
170         @Override
171         public void onClick(View v) {
172             if (mListener != null) mListener.onOutsideClick();
173         }
174     };
175 
176     public interface Listener {
onOutsideClick()177         void onOutsideClick();
onItemClicked(Action action, boolean alternate)178         void onItemClicked(Action action, boolean alternate);
179     }
180 }
181