Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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; |
| 18 | |
| 19 | import com.android.contacts.NotifyingAsyncQueryHandler.QueryCompleteListener; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 20 | import com.android.contacts.SocialStreamActivity.Mapping; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 21 | import com.android.contacts.SocialStreamActivity.MappingCache; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 22 | import com.android.internal.policy.PolicyManager; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 23 | |
| 24 | import android.content.ActivityNotFoundException; |
| 25 | import android.content.ContentUris; |
| 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.content.res.Resources; |
| 29 | import android.database.Cursor; |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 30 | import android.graphics.Bitmap; |
| 31 | import android.graphics.BitmapFactory; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 32 | import android.graphics.Color; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 36 | import android.provider.SocialContract; |
| 37 | import android.provider.Contacts.Phones; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 38 | import android.provider.ContactsContract.Aggregates; |
| 39 | import android.provider.ContactsContract.CommonDataKinds; |
Jeff Sharkey | 06bd5a8 | 2009-06-15 20:06:48 -0700 | [diff] [blame] | 40 | import android.provider.ContactsContract.Contacts; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 41 | import android.provider.ContactsContract.Data; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 42 | import android.provider.ContactsContract.Presence; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 43 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Evan Millar | 66388be | 2009-05-28 15:41:07 -0700 | [diff] [blame] | 44 | import android.provider.ContactsContract.CommonDataKinds.Phone; |
| 45 | import android.provider.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 46 | import android.provider.Im.PresenceColumns; |
| 47 | import android.provider.SocialContract.Activities; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 48 | import android.text.SpannableStringBuilder; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 49 | import android.text.format.DateUtils; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 50 | import android.text.style.CharacterStyle; |
| 51 | import android.text.style.ForegroundColorSpan; |
| 52 | import android.text.style.StyleSpan; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 53 | import android.util.Log; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 54 | import android.view.ContextThemeWrapper; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 55 | import android.view.Gravity; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 56 | import android.view.KeyEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 57 | import android.view.LayoutInflater; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 58 | import android.view.Menu; |
| 59 | import android.view.MenuItem; |
| 60 | import android.view.MotionEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 61 | import android.view.View; |
| 62 | import android.view.ViewGroup; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 63 | import android.view.Window; |
| 64 | import android.view.WindowManager; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 65 | import android.view.View.OnClickListener; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 66 | import android.view.accessibility.AccessibilityEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 67 | import android.widget.AbsListView; |
Jeff Sharkey | 51ca544 | 2009-05-21 16:12:18 -0700 | [diff] [blame] | 68 | import android.widget.AdapterView; |
| 69 | import android.widget.BaseAdapter; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 70 | import android.widget.HorizontalScrollView; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 71 | import android.widget.ImageView; |
| 72 | import android.widget.LinearLayout; |
| 73 | import android.widget.ListView; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 74 | import android.widget.RemoteViews; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 75 | import android.widget.TextView; |
| 76 | import android.widget.Toast; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 77 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 78 | import java.util.Arrays; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 79 | import java.util.HashMap; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 80 | import java.util.LinkedList; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 81 | import java.util.Set; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 84 | * Window that shows fast-track contact details for a specific |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 85 | * {@link Aggregates#_ID}. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 86 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 87 | public class FastTrackWindow implements Window.Callback, QueryCompleteListener, OnClickListener, |
| 88 | AbsListView.OnItemClickListener { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 89 | private static final String TAG = "FastTrackWindow"; |
| 90 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Interface used to allow the person showing a {@link FastTrackWindow} to |
| 93 | * know when the window has been dismissed. |
| 94 | */ |
| 95 | interface OnDismissListener { |
| 96 | public void onDismiss(FastTrackWindow dialog); |
| 97 | } |
| 98 | |
| 99 | final Context mContext; |
| 100 | final LayoutInflater mInflater; |
| 101 | final WindowManager mWindowManager; |
| 102 | Window mWindow; |
| 103 | View mDecor; |
| 104 | |
| 105 | private boolean mQuerying = false; |
| 106 | private boolean mShowing = false; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 107 | |
| 108 | /** Mapping cache from mime-type to icons and actions */ |
| 109 | private MappingCache mMappingCache; |
| 110 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 111 | private NotifyingAsyncQueryHandler mHandler; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 112 | private OnDismissListener mDismissListener; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 113 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 114 | private long mAggId; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 115 | private Rect mAnchor; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 116 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 117 | private boolean mHasSummary = false; |
| 118 | private boolean mHasSocial = false; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 119 | private boolean mHasActions = false; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 120 | |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 121 | private View mArrowUp; |
| 122 | private View mArrowDown; |
| 123 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 124 | private ImageView mPhoto; |
| 125 | private ImageView mPresence; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 126 | private TextView mContent; |
| 127 | private TextView mPublished; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 128 | private HorizontalScrollView mTrackScroll; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 129 | private ViewGroup mTrack; |
Jeff Sharkey | 51ca544 | 2009-05-21 16:12:18 -0700 | [diff] [blame] | 130 | private ListView mResolveList; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 131 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 132 | private String mDisplayName = null; |
| 133 | private String mSocialTitle = null; |
| 134 | |
| 135 | private SpannableStringBuilder mBuilder = new SpannableStringBuilder(); |
| 136 | private CharacterStyle mStyleBold = new StyleSpan(android.graphics.Typeface.BOLD); |
| 137 | private CharacterStyle mStyleBlack = new ForegroundColorSpan(Color.BLACK); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Set of {@link ActionInfo} that are associated with the aggregate |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 141 | * currently displayed by this fast-track window, represented as a map from |
| 142 | * {@link String} mimetype to {@link ActionList}. |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 143 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 144 | private ActionMap mActions = new ActionMap(); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 145 | |
Evan Millar | 54a5c9f | 2009-06-23 17:41:09 -0700 | [diff] [blame^] | 146 | // TODO We should move this to someplace more general as it is needed in a few places in the app |
| 147 | // code. |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Specific mime-type for {@link Phone#CONTENT_ITEM_TYPE} entries that |
| 150 | * distinguishes actions that should initiate a text message. |
| 151 | */ |
| 152 | public static final String MIME_SMS_ADDRESS = "vnd.android.cursor.item/sms-address"; |
| 153 | |
| 154 | /** |
| 155 | * Specific mime-types that should be bumped to the front of the fast-track. |
| 156 | * Other mime-types not appearing in this list follow in alphabetic order. |
| 157 | */ |
| 158 | private static final String[] ORDERED_MIMETYPES = new String[] { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 159 | Phones.CONTENT_ITEM_TYPE, |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 160 | Aggregates.CONTENT_ITEM_TYPE, |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 161 | MIME_SMS_ADDRESS, |
| 162 | Email.CONTENT_ITEM_TYPE, |
| 163 | }; |
| 164 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 165 | private static final boolean INCLUDE_PROFILE_ACTION = true; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 166 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 167 | private static final int TOKEN_SUMMARY = 1; |
| 168 | private static final int TOKEN_SOCIAL = 2; |
| 169 | private static final int TOKEN_DATA = 3; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 170 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 171 | /** Message to show when no activity is found to perform an action */ |
| 172 | // TODO: move this value into a resources string |
| 173 | private static final String NOT_FOUND = "Couldn't find an app to handle this action"; |
| 174 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 175 | /** |
| 176 | * Prepare a fast-track window to show in the given {@link Context}. |
| 177 | */ |
| 178 | public FastTrackWindow(Context context) { |
| 179 | mContext = new ContextThemeWrapper(context, R.style.FastTrack); |
| 180 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 181 | mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 182 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 183 | mWindow = PolicyManager.makeNewWindow(mContext); |
| 184 | mWindow.setCallback(this); |
| 185 | mWindow.setWindowManager(mWindowManager, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 186 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 187 | mWindow.setContentView(R.layout.fasttrack); |
| 188 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 189 | mArrowUp = mWindow.findViewById(R.id.arrow_up); |
| 190 | mArrowDown = mWindow.findViewById(R.id.arrow_down); |
| 191 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 192 | mPhoto = (ImageView)mWindow.findViewById(R.id.photo); |
| 193 | mPresence = (ImageView)mWindow.findViewById(R.id.presence); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 194 | mContent = (TextView)mWindow.findViewById(R.id.content); |
| 195 | mPublished = (TextView)mWindow.findViewById(R.id.published); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 196 | mTrack = (ViewGroup)mWindow.findViewById(R.id.fasttrack); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 197 | mTrackScroll = (HorizontalScrollView)mWindow.findViewById(R.id.scroll); |
Jeff Sharkey | 51ca544 | 2009-05-21 16:12:18 -0700 | [diff] [blame] | 198 | mResolveList = (ListView)mWindow.findViewById(android.R.id.list); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 199 | |
| 200 | // TODO: move generation of mime-type cache to more-efficient place |
| 201 | generateMappingCache(); |
| 202 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 206 | * Prepare a fast-track window to show in the given {@link Context}, and |
| 207 | * notify the given {@link OnDismissListener} each time this dialog is |
| 208 | * dismissed. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 209 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 210 | public FastTrackWindow(Context context, OnDismissListener dismissListener) { |
| 211 | this(context); |
| 212 | mDismissListener = dismissListener; |
| 213 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 214 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 215 | /** |
| 216 | * Generate {@link MappingCache} specifically for fast-track windows. This |
| 217 | * cache knows how to display {@link CommonDataKinds#PACKAGE_COMMON} data |
| 218 | * types using generic icons. |
| 219 | */ |
| 220 | private void generateMappingCache() { |
| 221 | mMappingCache = MappingCache.createAndFill(mContext); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 222 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 223 | Resources res = mContext.getResources(); |
| 224 | Mapping mapping; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 226 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Aggregates.CONTENT_ITEM_TYPE); |
| 227 | mapping.icon = BitmapFactory.decodeResource(res, R.drawable.ic_contacts_details); |
| 228 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 229 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 230 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Phone.CONTENT_ITEM_TYPE); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 231 | mapping.summaryColumn = Phone.TYPE; |
| 232 | mapping.detailColumn = Phone.NUMBER; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 233 | mapping.icon = BitmapFactory.decodeResource(res, android.R.drawable.sym_action_call); |
| 234 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 235 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 236 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, MIME_SMS_ADDRESS); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 237 | mapping.summaryColumn = Phone.TYPE; |
| 238 | mapping.detailColumn = Phone.NUMBER; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 239 | mapping.icon = BitmapFactory.decodeResource(res, R.drawable.sym_action_sms); |
| 240 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 241 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 242 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Email.CONTENT_ITEM_TYPE); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 243 | mapping.summaryColumn = Email.TYPE; |
| 244 | mapping.detailColumn = Email.DATA; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 245 | mapping.icon = BitmapFactory.decodeResource(res, android.R.drawable.sym_action_email); |
| 246 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 247 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 248 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 249 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Start showing a fast-track window for the given {@link Aggregate#_ID} |
| 252 | * pointing towards the given location. |
| 253 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 254 | public void show(Uri aggUri, Rect anchor) { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 255 | if (mShowing || mQuerying) { |
| 256 | Log.w(TAG, "already in process of showing"); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | mAggId = ContentUris.parseId(aggUri); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 261 | mAnchor = new Rect(anchor); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 262 | mQuerying = true; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 263 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 264 | Uri aggSummary = ContentUris.withAppendedId( |
| 265 | ContactsContract.Aggregates.CONTENT_SUMMARY_URI, mAggId); |
| 266 | Uri aggSocial = ContentUris.withAppendedId( |
| 267 | SocialContract.Activities.CONTENT_AGGREGATE_STATUS_URI, mAggId); |
| 268 | Uri aggData = Uri.withAppendedPath(aggUri, |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 269 | ContactsContract.Aggregates.Data.CONTENT_DIRECTORY); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 270 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 271 | // Start data query in background |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 272 | mHandler = new NotifyingAsyncQueryHandler(mContext, this); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 273 | mHandler.startQuery(TOKEN_SUMMARY, null, aggSummary, null, null, null, null); |
| 274 | mHandler.startQuery(TOKEN_SOCIAL, null, aggSocial, null, null, null, null); |
| 275 | mHandler.startQuery(TOKEN_DATA, null, aggData, null, null, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 276 | |
| 277 | } |
| 278 | |
| 279 | /** |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 280 | * Show the correct callout arrow based on a {@link R.id} reference. |
| 281 | */ |
| 282 | private void showArrow(int whichArrow, int requestedX) { |
| 283 | final View showArrow = (whichArrow == R.id.arrow_up) ? mArrowUp : mArrowDown; |
| 284 | final View hideArrow = (whichArrow == R.id.arrow_up) ? mArrowDown : mArrowUp; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 285 | |
| 286 | final int arrowWidth = mArrowUp.getMeasuredWidth(); |
| 287 | |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 288 | showArrow.setVisibility(View.VISIBLE); |
| 289 | LinearLayout.LayoutParams param = (LinearLayout.LayoutParams)showArrow.getLayoutParams(); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 290 | param.leftMargin = requestedX - arrowWidth / 2; |
| 291 | |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 292 | hideArrow.setVisibility(View.INVISIBLE); |
| 293 | } |
| 294 | |
| 295 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 296 | * Actual internal method to show this fast-track window. Called only by |
| 297 | * {@link #considerShowing()} when all data requirements have been met. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 298 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 299 | private void showInternal() { |
| 300 | mDecor = mWindow.getDecorView(); |
| 301 | WindowManager.LayoutParams l = mWindow.getAttributes(); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 302 | |
| 303 | l.width = WindowManager.LayoutParams.FILL_PARENT; |
| 304 | l.height = WindowManager.LayoutParams.WRAP_CONTENT; |
| 305 | |
| 306 | // Force layout measuring pass so we have baseline numbers |
| 307 | mDecor.measure(l.width, l.height); |
| 308 | |
| 309 | final int blockHeight = mDecor.getMeasuredHeight(); |
| 310 | final int arrowHeight = mArrowUp.getHeight(); |
| 311 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 312 | l.gravity = Gravity.TOP | Gravity.LEFT; |
| 313 | l.x = 0; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 314 | |
| 315 | if (mAnchor.top > blockHeight) { |
| 316 | // Show downwards callout when enough room, aligning bottom block |
| 317 | // edge with top of anchor area, and adjusting to inset arrow. |
| 318 | showArrow(R.id.arrow_down, mAnchor.centerX()); |
| 319 | l.y = mAnchor.top - blockHeight + arrowHeight; |
| 320 | |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 321 | } else { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 322 | // Otherwise show upwards callout, aligning block top with bottom of |
| 323 | // anchor area, and adjusting to inset arrow. |
| 324 | showArrow(R.id.arrow_up, mAnchor.centerX()); |
| 325 | l.y = mAnchor.bottom - arrowHeight; |
| 326 | |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 327 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 328 | |
| 329 | l.dimAmount = 0.6f; |
| 330 | l.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 331 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 332 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 333 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 334 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 335 | |
| 336 | mWindowManager.addView(mDecor, l); |
| 337 | mShowing = true; |
| 338 | mQuerying = false; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Dismiss this fast-track window if showing. |
| 343 | */ |
| 344 | public void dismiss() { |
| 345 | if (!mQuerying && !mShowing) { |
| 346 | Log.d(TAG, "not visible, ignore"); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | // Cancel any pending queries |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 351 | mHandler.cancelOperation(TOKEN_SUMMARY); |
| 352 | mHandler.cancelOperation(TOKEN_SOCIAL); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 353 | mHandler.cancelOperation(TOKEN_DATA); |
| 354 | |
| 355 | // Reset all views to prepare for possible recycling |
| 356 | mPhoto.setImageResource(R.drawable.ic_contact_list_picture); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 357 | mPresence.setImageDrawable(null); |
| 358 | mPublished.setText(null); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 359 | mContent.setText(null); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 360 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 361 | mDisplayName = null; |
| 362 | mSocialTitle = null; |
| 363 | |
| 364 | // Clear track actions and scroll to hard left |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 365 | mActions.clear(); |
| 366 | mTrack.removeAllViews(); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 367 | mTrackScroll.fullScroll(View.FOCUS_LEFT); |
| 368 | mWasDownArrow = false; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 369 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 370 | showResolveList(View.GONE); |
| 371 | |
| 372 | mHasSocial = false; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 373 | mHasActions = false; |
| 374 | |
| 375 | if (mDecor == null || !mShowing) { |
| 376 | Log.d(TAG, "not showing, ignore"); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | mWindowManager.removeView(mDecor); |
| 381 | mDecor = null; |
| 382 | mWindow.closeAllPanels(); |
| 383 | mShowing = false; |
| 384 | |
| 385 | // Notify any listeners that we've been dismissed |
| 386 | if (mDismissListener != null) { |
| 387 | mDismissListener.onDismiss(this); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Returns true if this fast-track window is showing or querying. |
| 393 | */ |
| 394 | public boolean isShowing() { |
| 395 | return mShowing || mQuerying; |
| 396 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 397 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 398 | /** |
| 399 | * Consider showing this window, which will only call through to |
| 400 | * {@link #showInternal()} when all data items are present. |
| 401 | */ |
| 402 | private synchronized void considerShowing() { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 403 | if (mHasSummary && mHasSocial && mHasActions && !mShowing) { |
| 404 | // Now that all queries have been finished, build summary string. |
| 405 | mBuilder.clear(); |
| 406 | mBuilder.append(mDisplayName); |
| 407 | mBuilder.append(" "); |
| 408 | mBuilder.append(mSocialTitle); |
| 409 | mBuilder.setSpan(mStyleBold, 0, mDisplayName.length(), 0); |
| 410 | mBuilder.setSpan(mStyleBlack, 0, mDisplayName.length(), 0); |
| 411 | mContent.setText(mBuilder); |
| 412 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 413 | showInternal(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
| 417 | /** {@inheritDoc} */ |
| 418 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 419 | if (cursor == null) { |
| 420 | return; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 421 | } else if (token == TOKEN_SUMMARY) { |
| 422 | handleSummary(cursor); |
| 423 | } else if (token == TOKEN_SOCIAL) { |
| 424 | handleSocial(cursor); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 425 | } else if (token == TOKEN_DATA) { |
| 426 | handleData(cursor); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 427 | } |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 431 | * Handle the result from the {@link TOKEN_SUMMARY} query. |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 432 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 433 | private void handleSummary(Cursor cursor) { |
| 434 | final int colDisplayName = cursor.getColumnIndex(Aggregates.DISPLAY_NAME); |
| 435 | final int colStatus = cursor.getColumnIndex(PresenceColumns.PRESENCE_STATUS); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 436 | |
| 437 | if (cursor.moveToNext()) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 438 | mDisplayName = cursor.getString(colDisplayName); |
| 439 | |
| 440 | int status = cursor.getInt(colStatus); |
| 441 | int statusRes = Presence.getPresenceIconResourceId(status); |
| 442 | mPresence.setImageResource(statusRes); |
| 443 | } |
| 444 | |
| 445 | mHasSummary = true; |
| 446 | considerShowing(); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Handle the result from the {@link TOKEN_SOCIAL} query. |
| 451 | */ |
| 452 | private void handleSocial(Cursor cursor) { |
| 453 | final int colTitle = cursor.getColumnIndex(Activities.TITLE); |
| 454 | final int colPublished = cursor.getColumnIndex(Activities.PUBLISHED); |
| 455 | |
| 456 | if (cursor.moveToNext()) { |
| 457 | mSocialTitle = cursor.getString(colTitle); |
| 458 | |
| 459 | long published = cursor.getLong(colPublished); |
| 460 | CharSequence relativePublished = DateUtils.getRelativeTimeSpanString(published, |
| 461 | System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS); |
| 462 | mPublished.setText(relativePublished); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame] | 463 | |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 466 | mHasSocial = true; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 467 | considerShowing(); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Description of a specific, actionable {@link Data#_ID} item. May have a |
| 472 | * {@link Mapping} associated with it to find {@link RemoteViews} or icon, |
| 473 | * and may have built a summary of itself for UI display. |
| 474 | */ |
| 475 | private class ActionInfo { |
| 476 | long dataId; |
| 477 | String packageName; |
| 478 | String mimeType; |
| 479 | |
| 480 | Mapping mapping; |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 481 | CharSequence summaryValue; |
| 482 | String detailValue; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * Create an action from common {@link Data} elements. |
| 486 | */ |
| 487 | public ActionInfo(long dataId, String packageName, String mimeType) { |
| 488 | this.dataId = dataId; |
| 489 | this.packageName = packageName; |
| 490 | this.mimeType = mimeType; |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Attempt to find a {@link Mapping} for the package and mime-type |
| 495 | * defined by this action. Returns true if one was found. |
| 496 | */ |
| 497 | public boolean findMapping(MappingCache cache) { |
| 498 | mapping = cache.findMapping(packageName, mimeType); |
| 499 | return (mapping != null); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Given a {@link Cursor} pointed at the {@link Data} row associated |
| 504 | * with this action, use the {@link Mapping} to build a text summary. |
| 505 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 506 | public void buildDetails(Cursor cursor) { |
| 507 | if (mapping == null) return; |
| 508 | |
| 509 | // Try finding common display label for this item, otherwise fall |
| 510 | // back to reading from defined summary column. |
| 511 | summaryValue = ContactsUtils.getDisplayLabel(mContext, mimeType, cursor); |
| 512 | if (summaryValue == null && mapping.summaryColumn != null) { |
| 513 | summaryValue = cursor.getString(cursor.getColumnIndex(mapping.summaryColumn)); |
| 514 | } |
| 515 | |
| 516 | // Read detailed value, if mapping was defined |
| 517 | if (mapping.detailColumn != null) { |
| 518 | detailValue = cursor.getString(cursor.getColumnIndex(mapping.detailColumn)); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Build an {@link Intent} that will perform this action. |
| 524 | */ |
| 525 | public Intent buildIntent() { |
| 526 | // Handle well-known mime-types with special care |
| 527 | if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 528 | Uri callUri = Uri.parse("tel:" + Uri.encode(detailValue)); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 529 | return new Intent(Intent.ACTION_DIAL, callUri); |
| 530 | |
| 531 | } else if (MIME_SMS_ADDRESS.equals(mimeType)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 532 | Uri smsUri = Uri.fromParts("smsto", detailValue, null); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 533 | return new Intent(Intent.ACTION_SENDTO, smsUri); |
| 534 | |
| 535 | } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 536 | Uri mailUri = Uri.fromParts("mailto", detailValue, null); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 537 | return new Intent(Intent.ACTION_SENDTO, mailUri); |
| 538 | |
| 539 | } |
| 540 | |
| 541 | // Otherwise fall back to default VIEW action |
| 542 | Uri dataUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, dataId); |
| 543 | |
| 544 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 545 | intent.setData(dataUri); |
| 546 | |
| 547 | return intent; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | /** |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 552 | * Provide a strongly-typed {@link LinkedList} that holds a list of |
| 553 | * {@link ActionInfo} objects. |
| 554 | */ |
| 555 | private class ActionList extends LinkedList<ActionInfo> { |
| 556 | } |
| 557 | |
| 558 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 559 | * Provide a simple way of collecting one or more {@link ActionInfo} objects |
| 560 | * under a mime-type key. |
| 561 | */ |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 562 | private class ActionMap extends HashMap<String, ActionList> { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 563 | private void collect(String mimeType, ActionInfo info) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 564 | // Create list for this mimetype when needed |
| 565 | ActionList collectList = get(mimeType); |
| 566 | if (collectList == null) { |
| 567 | collectList = new ActionList(); |
| 568 | put(mimeType, collectList); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 569 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 570 | collectList.add(info); |
| 571 | } |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Handle the result from the {@link TOKEN_DATA} query. |
| 576 | */ |
| 577 | private void handleData(Cursor cursor) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 578 | final int colId = cursor.getColumnIndex(Data._ID); |
Jeff Sharkey | 06bd5a8 | 2009-06-15 20:06:48 -0700 | [diff] [blame] | 579 | final int colPackage = cursor.getColumnIndex(Contacts.PACKAGE); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 580 | final int colMimeType = cursor.getColumnIndex(Data.MIMETYPE); |
| 581 | final int colPhoto = cursor.getColumnIndex(Photo.PHOTO); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 582 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 583 | ActionInfo info; |
| 584 | |
| 585 | // Add the profile shortcut action if requested |
| 586 | if (INCLUDE_PROFILE_ACTION) { |
| 587 | final String mimeType = Aggregates.CONTENT_ITEM_TYPE; |
| 588 | info = new ActionInfo(mAggId, CommonDataKinds.PACKAGE_COMMON, mimeType); |
| 589 | if (info.findMapping(mMappingCache)) { |
| 590 | mActions.collect(mimeType, info); |
| 591 | } |
| 592 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 593 | |
| 594 | while (cursor.moveToNext()) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 595 | final long dataId = cursor.getLong(colId); |
| 596 | final String packageName = cursor.getString(colPackage); |
| 597 | final String mimeType = cursor.getString(colMimeType); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 598 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 599 | // Handle when a photo appears in the various data items |
| 600 | // TODO: accept a photo only if its marked as primary |
| 601 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 602 | byte[] photoBlob = cursor.getBlob(colPhoto); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 603 | Bitmap photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 604 | mPhoto.setImageBitmap(photoBitmap); |
Jeff Sharkey | 6912668 | 2009-05-18 21:33:41 -0700 | [diff] [blame] | 605 | continue; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 608 | // Build an action for this data entry, find a mapping to a UI |
| 609 | // element, build its summary from the cursor, and collect it along |
| 610 | // with all others of this mime-type. |
| 611 | info = new ActionInfo(dataId, packageName, mimeType); |
| 612 | if (info.findMapping(mMappingCache)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 613 | info.buildDetails(cursor); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 614 | mActions.collect(info.mimeType, info); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 615 | } |
| 616 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 617 | // If phone number, also insert as text message action |
| 618 | if (Phones.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 619 | info = new ActionInfo(dataId, packageName, MIME_SMS_ADDRESS); |
| 620 | if (info.findMapping(mMappingCache)) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 621 | info.buildDetails(cursor); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 622 | mActions.collect(info.mimeType, info); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 623 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 624 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | cursor.close(); |
| 628 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 629 | // Turn our list of actions into UI elements, starting with common types |
| 630 | Set<String> containedTypes = mActions.keySet(); |
| 631 | for (String mimeType : ORDERED_MIMETYPES) { |
| 632 | if (containedTypes.contains(mimeType)) { |
| 633 | mTrack.addView(inflateAction(mimeType)); |
| 634 | containedTypes.remove(mimeType); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 635 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 638 | // Then continue with remaining mime-types in alphabetical order |
| 639 | String[] remainingTypes = containedTypes.toArray(new String[containedTypes.size()]); |
| 640 | Arrays.sort(remainingTypes); |
| 641 | for (String mimeType : remainingTypes) { |
| 642 | mTrack.addView(inflateAction(mimeType)); |
| 643 | } |
| 644 | |
| 645 | mHasActions = true; |
| 646 | considerShowing(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 650 | * Inflate the in-track view for the action of the given mime-type. Will use |
| 651 | * the icon provided by the {@link Mapping}. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 652 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 653 | private View inflateAction(String mimeType) { |
| 654 | ImageView view = (ImageView)mInflater.inflate(R.layout.fasttrack_item, mTrack, false); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 655 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 656 | // Add direct intent if single child, otherwise flag for multiple |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 657 | ActionList children = mActions.get(mimeType); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 658 | ActionInfo firstInfo = children.get(0); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 659 | if (children.size() == 1) { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 660 | view.setTag(firstInfo.buildIntent()); |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 661 | } else { |
| 662 | view.setTag(children); |
| 663 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 664 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 665 | // Set icon and listen for clicks |
| 666 | view.setImageBitmap(firstInfo.mapping.icon); |
| 667 | view.setOnClickListener(this); |
| 668 | return view; |
| 669 | } |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 670 | |
Jeff Sharkey | 51ca544 | 2009-05-21 16:12:18 -0700 | [diff] [blame] | 671 | /** {@inheritDoc} */ |
| 672 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 673 | // Pass list item clicks along so that Intents are handled uniformly |
| 674 | onClick(view); |
| 675 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 676 | |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 677 | /** |
| 678 | * Flag indicating if {@link #mArrowDown} was visible during the last call |
| 679 | * to {@link #showResolveList(int)}. Used to decide during a later call if |
| 680 | * the arrow should be restored. |
| 681 | */ |
| 682 | private boolean mWasDownArrow = false; |
| 683 | |
| 684 | /** |
| 685 | * Helper for showing and hiding {@link #mResolveList}, which will correctly |
| 686 | * manage {@link #mArrowDown} as needed. |
| 687 | */ |
| 688 | private void showResolveList(int visibility) { |
| 689 | // Show or hide the resolve list if needed |
| 690 | if (mResolveList.getVisibility() != visibility) { |
| 691 | mResolveList.setVisibility(visibility); |
| 692 | } |
| 693 | |
| 694 | if (visibility == View.VISIBLE) { |
| 695 | // If showing list, then hide and save state of down arrow |
| 696 | mWasDownArrow = mWasDownArrow || (mArrowDown.getVisibility() == View.VISIBLE); |
| 697 | mArrowDown.setVisibility(View.INVISIBLE); |
| 698 | } else { |
| 699 | // If hiding list, restore any down arrow state |
| 700 | mArrowDown.setVisibility(mWasDownArrow ? View.VISIBLE : View.INVISIBLE); |
| 701 | } |
| 702 | |
| 703 | } |
| 704 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 705 | /** {@inheritDoc} */ |
| 706 | public void onClick(View v) { |
| 707 | final Object tag = v.getTag(); |
| 708 | if (tag instanceof Intent) { |
Jeff Sharkey | 51ca544 | 2009-05-21 16:12:18 -0700 | [diff] [blame] | 709 | // Hide the resolution list, if present |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 710 | showResolveList(View.GONE); |
| 711 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 712 | // Incoming tag is concrete intent, so launch |
| 713 | try { |
| 714 | mContext.startActivity((Intent)tag); |
| 715 | } catch (ActivityNotFoundException e) { |
| 716 | Log.w(TAG, NOT_FOUND); |
| 717 | Toast.makeText(mContext, NOT_FOUND, Toast.LENGTH_SHORT).show(); |
| 718 | } |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 719 | } else if (tag instanceof ActionList) { |
| 720 | // Incoming tag is a mime-type, so show resolution list |
| 721 | final ActionList children = (ActionList)tag; |
| 722 | |
| 723 | // Show resolution list and set adapter |
| 724 | showResolveList(View.VISIBLE); |
| 725 | |
| 726 | mResolveList.setOnItemClickListener(this); |
| 727 | mResolveList.setAdapter(new BaseAdapter() { |
| 728 | public int getCount() { |
| 729 | return children.size(); |
| 730 | } |
| 731 | |
| 732 | public Object getItem(int position) { |
| 733 | return children.get(position); |
| 734 | } |
| 735 | |
| 736 | public long getItemId(int position) { |
| 737 | return position; |
| 738 | } |
| 739 | |
| 740 | public View getView(int position, View convertView, ViewGroup parent) { |
| 741 | if (convertView == null) { |
| 742 | convertView = mInflater.inflate(R.layout.fasttrack_resolve_item, parent, false); |
| 743 | } |
| 744 | |
| 745 | // Set action title based on summary value |
| 746 | ActionInfo info = (ActionInfo)getItem(position); |
| 747 | |
| 748 | ImageView icon1 = (ImageView)convertView.findViewById(android.R.id.icon1); |
| 749 | TextView text1 = (TextView)convertView.findViewById(android.R.id.text1); |
| 750 | TextView text2 = (TextView)convertView.findViewById(android.R.id.text2); |
| 751 | |
| 752 | icon1.setImageBitmap(info.mapping.icon); |
| 753 | text1.setText(info.summaryValue); |
| 754 | text2.setText(info.detailValue); |
| 755 | |
| 756 | convertView.setTag(info.buildIntent()); |
| 757 | return convertView; |
| 758 | } |
| 759 | }); |
| 760 | |
| 761 | // Make sure we resize to make room for ListView |
| 762 | onWindowAttributesChanged(mWindow.getAttributes()); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 763 | |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** {@inheritDoc} */ |
| 768 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Sharkey | 3926127 | 2009-06-03 19:15:09 -0700 | [diff] [blame] | 769 | if (event.getAction() == KeyEvent.ACTION_DOWN |
| 770 | && event.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 771 | // Back key will first dismiss any expanded resolve list, otherwise |
| 772 | // it will close the entire dialog. |
| 773 | if (mResolveList.getVisibility() == View.VISIBLE) { |
| 774 | showResolveList(View.GONE); |
| 775 | } else { |
| 776 | dismiss(); |
| 777 | } |
| 778 | return true; |
| 779 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 780 | return mWindow.superDispatchKeyEvent(event); |
| 781 | } |
| 782 | |
| 783 | /** {@inheritDoc} */ |
| 784 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 785 | // TODO: make this window accessible |
| 786 | return false; |
| 787 | } |
| 788 | |
| 789 | /** {@inheritDoc} */ |
| 790 | public boolean dispatchTouchEvent(MotionEvent event) { |
| 791 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
| 792 | dismiss(); |
| 793 | return true; |
| 794 | } |
| 795 | return mWindow.superDispatchTouchEvent(event); |
| 796 | } |
| 797 | |
| 798 | /** {@inheritDoc} */ |
| 799 | public boolean dispatchTrackballEvent(MotionEvent event) { |
| 800 | return mWindow.superDispatchTrackballEvent(event); |
| 801 | } |
| 802 | |
| 803 | /** {@inheritDoc} */ |
| 804 | public void onContentChanged() { |
| 805 | } |
| 806 | |
| 807 | /** {@inheritDoc} */ |
| 808 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 809 | return false; |
| 810 | } |
| 811 | |
| 812 | /** {@inheritDoc} */ |
| 813 | public View onCreatePanelView(int featureId) { |
| 814 | return null; |
| 815 | } |
| 816 | |
| 817 | /** {@inheritDoc} */ |
| 818 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | /** {@inheritDoc} */ |
| 823 | public boolean onMenuOpened(int featureId, Menu menu) { |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | /** {@inheritDoc} */ |
| 828 | public void onPanelClosed(int featureId, Menu menu) { |
| 829 | } |
| 830 | |
| 831 | /** {@inheritDoc} */ |
| 832 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 833 | return false; |
| 834 | } |
| 835 | |
| 836 | /** {@inheritDoc} */ |
| 837 | public boolean onSearchRequested() { |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | /** {@inheritDoc} */ |
| 842 | public void onWindowAttributesChanged(android.view.WindowManager.LayoutParams attrs) { |
| 843 | if (mDecor != null) { |
| 844 | mWindowManager.updateViewLayout(mDecor, attrs); |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /** {@inheritDoc} */ |
| 849 | public void onWindowFocusChanged(boolean hasFocus) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 850 | } |
| 851 | } |