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