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 | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 20 | import com.android.contacts.SocialStreamActivity.MappingCache; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 21 | import com.android.contacts.SocialStreamActivity.Mapping; |
| 22 | import com.android.internal.policy.PolicyManager; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 23 | import com.android.providers.contacts2.ContactsContract; |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 24 | import com.android.providers.contacts2.ContactsContract.Aggregates; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 25 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds; |
| 26 | import com.android.providers.contacts2.ContactsContract.Data; |
| 27 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Email; |
| 28 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Im; |
| 29 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Phone; |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 30 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 31 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Postal; |
| 32 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 33 | import android.app.Activity; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 34 | import android.content.ActivityNotFoundException; |
| 35 | import android.content.ContentUris; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
| 38 | import android.content.res.Resources; |
| 39 | import android.database.Cursor; |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 40 | import android.graphics.Bitmap; |
| 41 | import android.graphics.BitmapFactory; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 42 | import android.net.Uri; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 43 | import android.provider.Contacts.Phones; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 44 | import android.util.Log; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 45 | import android.view.ContextThemeWrapper; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 46 | import android.view.Gravity; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 47 | import android.view.KeyEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 48 | import android.view.LayoutInflater; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 49 | import android.view.Menu; |
| 50 | import android.view.MenuItem; |
| 51 | import android.view.MotionEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 52 | import android.view.View; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 53 | import android.view.ViewConfiguration; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 54 | import android.view.ViewGroup; |
| 55 | import android.view.ViewTreeObserver; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 56 | import android.view.Window; |
| 57 | import android.view.WindowManager; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 58 | import android.view.View.OnClickListener; |
| 59 | import android.view.ViewTreeObserver.OnScrollChangedListener; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 60 | import android.view.accessibility.AccessibilityEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 61 | import android.widget.AbsListView; |
| 62 | import android.widget.ImageView; |
| 63 | import android.widget.LinearLayout; |
| 64 | import android.widget.ListView; |
| 65 | import android.widget.PopupWindow; |
| 66 | import android.widget.TextView; |
| 67 | import android.widget.Toast; |
| 68 | import android.widget.AbsListView.OnScrollListener; |
| 69 | import android.widget.Gallery.LayoutParams; |
| 70 | |
| 71 | import java.lang.ref.WeakReference; |
| 72 | import java.util.ArrayList; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 73 | import java.util.Arrays; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 74 | import java.util.Collections; |
| 75 | import java.util.Comparator; |
| 76 | import java.util.HashMap; |
| 77 | import java.util.Iterator; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 78 | import java.util.LinkedList; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 79 | import java.util.PriorityQueue; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 80 | import java.util.Set; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 83 | * Window that shows fast-track contact details for a specific |
| 84 | * {@link Aggregate#_ID}. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 85 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 86 | public class FastTrackWindow implements Window.Callback, QueryCompleteListener, OnClickListener { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 87 | private static final String TAG = "FastTrackWindow"; |
| 88 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 89 | /** |
| 90 | * Interface used to allow the person showing a {@link FastTrackWindow} to |
| 91 | * know when the window has been dismissed. |
| 92 | */ |
| 93 | interface OnDismissListener { |
| 94 | public void onDismiss(FastTrackWindow dialog); |
| 95 | } |
| 96 | |
| 97 | final Context mContext; |
| 98 | final LayoutInflater mInflater; |
| 99 | final WindowManager mWindowManager; |
| 100 | Window mWindow; |
| 101 | View mDecor; |
| 102 | |
| 103 | private boolean mQuerying = false; |
| 104 | private boolean mShowing = false; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 105 | |
| 106 | /** Mapping cache from mime-type to icons and actions */ |
| 107 | private MappingCache mMappingCache; |
| 108 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 109 | private NotifyingAsyncQueryHandler mHandler; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 110 | private OnDismissListener mDismissListener; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 111 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 112 | private long mAggId; |
| 113 | private int mRequestedY; |
| 114 | private int mSlop; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 115 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 116 | private boolean mHasProfile = false; |
| 117 | private boolean mHasActions = false; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 118 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 119 | private ImageView mPhoto; |
| 120 | private ImageView mPresence; |
| 121 | private TextView mDisplayName; |
| 122 | private TextView mStatus; |
| 123 | private ViewGroup mTrack; |
| 124 | |
| 125 | // TODO: read from a resource somewhere |
| 126 | private static final int mHeight = 150; |
| 127 | private static final int mAnchorHeight = 20; |
| 128 | |
| 129 | /** |
| 130 | * Set of {@link ActionInfo} that are associated with the aggregate |
| 131 | * currently displayed by this fast-track window. |
| 132 | */ |
| 133 | private ActionSet mActions = new ActionSet(); |
| 134 | |
| 135 | /** |
| 136 | * Specific mime-type for {@link Phone#CONTENT_ITEM_TYPE} entries that |
| 137 | * distinguishes actions that should initiate a text message. |
| 138 | */ |
| 139 | public static final String MIME_SMS_ADDRESS = "vnd.android.cursor.item/sms-address"; |
| 140 | |
| 141 | /** |
| 142 | * Specific mime-types that should be bumped to the front of the fast-track. |
| 143 | * Other mime-types not appearing in this list follow in alphabetic order. |
| 144 | */ |
| 145 | private static final String[] ORDERED_MIMETYPES = new String[] { |
| 146 | Aggregates.CONTENT_ITEM_TYPE, |
| 147 | Phones.CONTENT_ITEM_TYPE, |
| 148 | MIME_SMS_ADDRESS, |
| 149 | Email.CONTENT_ITEM_TYPE, |
| 150 | }; |
| 151 | |
| 152 | // public static final int ICON_SIZE = 42; |
| 153 | // public static final int ICON_PADDING = 3; |
| 154 | |
| 155 | // TODO: read this status from actual query |
| 156 | private static final String STUB_STATUS = "has a really long random status message that would be far too long to read on a single device without the need for tiny reading glasses"; |
| 157 | |
| 158 | private static final boolean INCLUDE_PROFILE_ACTION = true; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 159 | |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 160 | private static final int TOKEN_DISPLAY_NAME = 1; |
| 161 | private static final int TOKEN_DATA = 2; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 163 | /** Message to show when no activity is found to perform an action */ |
| 164 | // TODO: move this value into a resources string |
| 165 | private static final String NOT_FOUND = "Couldn't find an app to handle this action"; |
| 166 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Prepare a fast-track window to show in the given {@link Context}. |
| 169 | */ |
| 170 | public FastTrackWindow(Context context) { |
| 171 | mContext = new ContextThemeWrapper(context, R.style.FastTrack); |
| 172 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 173 | mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 174 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 175 | mSlop = ViewConfiguration.get(mContext).getScaledWindowTouchSlop(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 177 | mWindow = PolicyManager.makeNewWindow(mContext); |
| 178 | mWindow.setCallback(this); |
| 179 | mWindow.setWindowManager(mWindowManager, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 180 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 181 | mWindow.setContentView(R.layout.fasttrack); |
| 182 | |
| 183 | mPhoto = (ImageView)mWindow.findViewById(R.id.photo); |
| 184 | mPresence = (ImageView)mWindow.findViewById(R.id.presence); |
| 185 | mDisplayName = (TextView)mWindow.findViewById(R.id.displayname); |
| 186 | mStatus = (TextView)mWindow.findViewById(R.id.status); |
| 187 | mTrack = (ViewGroup)mWindow.findViewById(R.id.fasttrack); |
| 188 | |
| 189 | // TODO: move generation of mime-type cache to more-efficient place |
| 190 | generateMappingCache(); |
| 191 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 195 | * Prepare a fast-track window to show in the given {@link Context}, and |
| 196 | * notify the given {@link OnDismissListener} each time this dialog is |
| 197 | * dismissed. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 198 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 199 | public FastTrackWindow(Context context, OnDismissListener dismissListener) { |
| 200 | this(context); |
| 201 | mDismissListener = dismissListener; |
| 202 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 203 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Generate {@link MappingCache} specifically for fast-track windows. This |
| 206 | * cache knows how to display {@link CommonDataKinds#PACKAGE_COMMON} data |
| 207 | * types using generic icons. |
| 208 | */ |
| 209 | private void generateMappingCache() { |
| 210 | mMappingCache = MappingCache.createAndFill(mContext); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 211 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 212 | Resources res = mContext.getResources(); |
| 213 | Mapping mapping; |
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 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Aggregates.CONTENT_ITEM_TYPE); |
| 216 | mapping.icon = BitmapFactory.decodeResource(res, R.drawable.ic_contacts_details); |
| 217 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 218 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 219 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Phone.CONTENT_ITEM_TYPE); |
| 220 | mapping.summaryColumn = Phone.NUMBER; |
| 221 | mapping.icon = BitmapFactory.decodeResource(res, android.R.drawable.sym_action_call); |
| 222 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 223 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 224 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, MIME_SMS_ADDRESS); |
| 225 | mapping.summaryColumn = Phone.NUMBER; |
| 226 | mapping.icon = BitmapFactory.decodeResource(res, R.drawable.sym_action_sms); |
| 227 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 228 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 229 | mapping = new Mapping(CommonDataKinds.PACKAGE_COMMON, Email.CONTENT_ITEM_TYPE); |
| 230 | mapping.summaryColumn = Email.DATA; |
| 231 | mapping.icon = BitmapFactory.decodeResource(res, android.R.drawable.sym_action_email); |
| 232 | mMappingCache.addMapping(mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 233 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 234 | } |
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 | /** |
| 237 | * Start showing a fast-track window for the given {@link Aggregate#_ID} |
| 238 | * pointing towards the given location. |
| 239 | */ |
| 240 | public void show(Uri aggUri, int y) { |
| 241 | if (mShowing || mQuerying) { |
| 242 | Log.w(TAG, "already in process of showing"); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | mAggId = ContentUris.parseId(aggUri); |
| 247 | mRequestedY = y; |
| 248 | mQuerying = true; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 249 | |
| 250 | // Start data query in background |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 251 | Uri dataUri = Uri.withAppendedPath(aggUri, |
| 252 | ContactsContract.Aggregates.Data.CONTENT_DIRECTORY); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 253 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 254 | // TODO: also query for latest status message |
| 255 | mHandler = new NotifyingAsyncQueryHandler(mContext, this); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 256 | mHandler.startQuery(TOKEN_DISPLAY_NAME, null, aggUri, null, null, null, null); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 257 | mHandler.startQuery(TOKEN_DATA, null, dataUri, null, null, null, null); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 258 | |
| 259 | } |
| 260 | |
| 261 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 262 | * Actual internal method to show this fast-track window. Called only by |
| 263 | * {@link #considerShowing()} when all data requirements have been met. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 264 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 265 | private void showInternal() { |
| 266 | mDecor = mWindow.getDecorView(); |
| 267 | WindowManager.LayoutParams l = mWindow.getAttributes(); |
| 268 | |
| 269 | l.gravity = Gravity.TOP | Gravity.LEFT; |
| 270 | l.x = 0; |
| 271 | l.y = mRequestedY - mHeight; |
| 272 | |
| 273 | l.width = WindowManager.LayoutParams.FILL_PARENT; |
| 274 | l.height = mHeight + mAnchorHeight; |
| 275 | |
| 276 | l.dimAmount = 0.6f; |
| 277 | l.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 278 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 279 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 280 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 281 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 282 | |
| 283 | mWindowManager.addView(mDecor, l); |
| 284 | mShowing = true; |
| 285 | mQuerying = false; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Dismiss this fast-track window if showing. |
| 290 | */ |
| 291 | public void dismiss() { |
| 292 | if (!mQuerying && !mShowing) { |
| 293 | Log.d(TAG, "not visible, ignore"); |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | // Cancel any pending queries |
| 298 | mHandler.cancelOperation(TOKEN_DISPLAY_NAME); |
| 299 | mHandler.cancelOperation(TOKEN_DATA); |
| 300 | |
| 301 | // Reset all views to prepare for possible recycling |
| 302 | mPhoto.setImageResource(R.drawable.ic_contact_list_picture); |
| 303 | mPresence.setImageDrawable(null); |
| 304 | mDisplayName.setText(null); |
| 305 | mStatus.setText(null); |
| 306 | |
| 307 | mActions.clear(); |
| 308 | mTrack.removeAllViews(); |
| 309 | |
| 310 | mHasProfile = false; |
| 311 | mHasActions = false; |
| 312 | |
| 313 | if (mDecor == null || !mShowing) { |
| 314 | Log.d(TAG, "not showing, ignore"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | mWindowManager.removeView(mDecor); |
| 319 | mDecor = null; |
| 320 | mWindow.closeAllPanels(); |
| 321 | mShowing = false; |
| 322 | |
| 323 | // Notify any listeners that we've been dismissed |
| 324 | if (mDismissListener != null) { |
| 325 | mDismissListener.onDismiss(this); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 329 | /** |
| 330 | * Returns true if this fast-track window is showing or querying. |
| 331 | */ |
| 332 | public boolean isShowing() { |
| 333 | return mShowing || mQuerying; |
| 334 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 335 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Consider showing this window, which will only call through to |
| 338 | * {@link #showInternal()} when all data items are present. |
| 339 | */ |
| 340 | private synchronized void considerShowing() { |
| 341 | if (mHasActions && mHasProfile && !mShowing) { |
| 342 | showInternal(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | /** {@inheritDoc} */ |
| 347 | public void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 348 | if (cursor == null) { |
| 349 | return; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 350 | } else if (token == TOKEN_DISPLAY_NAME) { |
| 351 | handleDisplayName(cursor); |
| 352 | } else if (token == TOKEN_DATA) { |
| 353 | handleData(cursor); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 354 | } |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Handle the result from the {@link TOKEN_DISPLAY_NAME} query. |
| 359 | */ |
| 360 | private void handleDisplayName(Cursor cursor) { |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 361 | final int COL_DISPLAY_NAME = cursor.getColumnIndex(Aggregates.DISPLAY_NAME); |
| 362 | |
| 363 | if (cursor.moveToNext()) { |
| 364 | String foundName = cursor.getString(COL_DISPLAY_NAME); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 365 | mDisplayName.setText(foundName); |
| 366 | mStatus.setText(STUB_STATUS); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 369 | mHasProfile = true; |
| 370 | considerShowing(); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Description of a specific, actionable {@link Data#_ID} item. May have a |
| 375 | * {@link Mapping} associated with it to find {@link RemoteViews} or icon, |
| 376 | * and may have built a summary of itself for UI display. |
| 377 | */ |
| 378 | private class ActionInfo { |
| 379 | long dataId; |
| 380 | String packageName; |
| 381 | String mimeType; |
| 382 | |
| 383 | Mapping mapping; |
| 384 | String summaryValue; |
| 385 | |
| 386 | /** |
| 387 | * Create an action from common {@link Data} elements. |
| 388 | */ |
| 389 | public ActionInfo(long dataId, String packageName, String mimeType) { |
| 390 | this.dataId = dataId; |
| 391 | this.packageName = packageName; |
| 392 | this.mimeType = mimeType; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * Attempt to find a {@link Mapping} for the package and mime-type |
| 397 | * defined by this action. Returns true if one was found. |
| 398 | */ |
| 399 | public boolean findMapping(MappingCache cache) { |
| 400 | mapping = cache.findMapping(packageName, mimeType); |
| 401 | return (mapping != null); |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Given a {@link Cursor} pointed at the {@link Data} row associated |
| 406 | * with this action, use the {@link Mapping} to build a text summary. |
| 407 | */ |
| 408 | public void buildSummary(Cursor cursor) { |
| 409 | if (mapping == null || mapping.summaryColumn == null) return; |
| 410 | int index = cursor.getColumnIndex(mapping.summaryColumn); |
| 411 | if (index != -1) { |
| 412 | summaryValue = cursor.getString(index); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Build an {@link Intent} that will perform this action. |
| 418 | */ |
| 419 | public Intent buildIntent() { |
| 420 | // Handle well-known mime-types with special care |
| 421 | if (CommonDataKinds.Phone.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 422 | Uri callUri = Uri.parse("tel:" + Uri.encode(summaryValue)); |
| 423 | return new Intent(Intent.ACTION_DIAL, callUri); |
| 424 | |
| 425 | } else if (MIME_SMS_ADDRESS.equals(mimeType)) { |
| 426 | Uri smsUri = Uri.fromParts("smsto", summaryValue, null); |
| 427 | return new Intent(Intent.ACTION_SENDTO, smsUri); |
| 428 | |
| 429 | } else if (CommonDataKinds.Email.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 430 | Uri mailUri = Uri.fromParts("mailto", summaryValue, null); |
| 431 | return new Intent(Intent.ACTION_SENDTO, mailUri); |
| 432 | |
| 433 | } |
| 434 | |
| 435 | // Otherwise fall back to default VIEW action |
| 436 | Uri dataUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, dataId); |
| 437 | |
| 438 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 439 | intent.setData(dataUri); |
| 440 | |
| 441 | return intent; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Provide a simple way of collecting one or more {@link ActionInfo} objects |
| 447 | * under a mime-type key. |
| 448 | */ |
| 449 | private class ActionSet extends HashMap<String, LinkedList<ActionInfo>> { |
| 450 | private void collect(String mimeType, ActionInfo info) { |
| 451 | // Create mime-type set if needed |
| 452 | if (!containsKey(mimeType)) { |
| 453 | put(mimeType, new LinkedList<ActionInfo>()); |
| 454 | } |
| 455 | LinkedList<ActionInfo> collectList = get(mimeType); |
| 456 | collectList.add(info); |
| 457 | } |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Handle the result from the {@link TOKEN_DATA} query. |
| 462 | */ |
| 463 | private void handleData(Cursor cursor) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 464 | final int COL_ID = cursor.getColumnIndex(Data._ID); |
| 465 | final int COL_PACKAGE = cursor.getColumnIndex(Data.PACKAGE); |
| 466 | final int COL_MIMETYPE = cursor.getColumnIndex(Data.MIMETYPE); |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 467 | final int COL_PHOTO = cursor.getColumnIndex(Photo.PHOTO); |
| 468 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 469 | ActionInfo info; |
| 470 | |
| 471 | // Add the profile shortcut action if requested |
| 472 | if (INCLUDE_PROFILE_ACTION) { |
| 473 | final String mimeType = Aggregates.CONTENT_ITEM_TYPE; |
| 474 | info = new ActionInfo(mAggId, CommonDataKinds.PACKAGE_COMMON, mimeType); |
| 475 | if (info.findMapping(mMappingCache)) { |
| 476 | mActions.collect(mimeType, info); |
| 477 | } |
| 478 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 479 | |
| 480 | while (cursor.moveToNext()) { |
| 481 | final long dataId = cursor.getLong(COL_ID); |
| 482 | final String packageName = cursor.getString(COL_PACKAGE); |
| 483 | final String mimeType = cursor.getString(COL_MIMETYPE); |
| 484 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 485 | // Handle when a photo appears in the various data items |
| 486 | // TODO: accept a photo only if its marked as primary |
| 487 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
Jeff Sharkey | ecedf75 | 2009-05-18 22:07:40 -0700 | [diff] [blame] | 488 | byte[] photoBlob = cursor.getBlob(COL_PHOTO); |
| 489 | Bitmap photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 490 | mPhoto.setImageBitmap(photoBitmap); |
Jeff Sharkey | 6912668 | 2009-05-18 21:33:41 -0700 | [diff] [blame] | 491 | continue; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 494 | // Build an action for this data entry, find a mapping to a UI |
| 495 | // element, build its summary from the cursor, and collect it along |
| 496 | // with all others of this mime-type. |
| 497 | info = new ActionInfo(dataId, packageName, mimeType); |
| 498 | if (info.findMapping(mMappingCache)) { |
| 499 | info.buildSummary(cursor); |
| 500 | mActions.collect(info.mimeType, info); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 503 | // If phone number, also insert as text message action |
| 504 | if (Phones.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 505 | info = new ActionInfo(dataId, packageName, MIME_SMS_ADDRESS); |
| 506 | if (info.findMapping(mMappingCache)) { |
| 507 | info.buildSummary(cursor); |
| 508 | mActions.collect(info.mimeType, info); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 509 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 510 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | cursor.close(); |
| 514 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 515 | // Turn our list of actions into UI elements, starting with common types |
| 516 | Set<String> containedTypes = mActions.keySet(); |
| 517 | for (String mimeType : ORDERED_MIMETYPES) { |
| 518 | if (containedTypes.contains(mimeType)) { |
| 519 | mTrack.addView(inflateAction(mimeType)); |
| 520 | containedTypes.remove(mimeType); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 521 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 524 | // Then continue with remaining mime-types in alphabetical order |
| 525 | String[] remainingTypes = containedTypes.toArray(new String[containedTypes.size()]); |
| 526 | Arrays.sort(remainingTypes); |
| 527 | for (String mimeType : remainingTypes) { |
| 528 | mTrack.addView(inflateAction(mimeType)); |
| 529 | } |
| 530 | |
| 531 | mHasActions = true; |
| 532 | considerShowing(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 536 | * Inflate the in-track view for the action of the given mime-type. Will use |
| 537 | * the icon provided by the {@link Mapping}. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 538 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 539 | private View inflateAction(String mimeType) { |
| 540 | ImageView view = (ImageView)mInflater.inflate(R.layout.fasttrack_item, mTrack, false); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 541 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 542 | // Add direct intent if single child, otherwise flag for multiple |
| 543 | LinkedList<ActionInfo> children = mActions.get(mimeType); |
| 544 | ActionInfo firstInfo = children.get(0); |
| 545 | if (children.size() == 1) { |
| 546 | view.setTag(firstInfo.buildIntent()); |
| 547 | } else { |
| 548 | view.setTag(mimeType); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 551 | // Set icon and listen for clicks |
| 552 | view.setImageBitmap(firstInfo.mapping.icon); |
| 553 | view.setOnClickListener(this); |
| 554 | return view; |
| 555 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 556 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 557 | /** {@inheritDoc} */ |
| 558 | public void onClick(View v) { |
| 559 | final Object tag = v.getTag(); |
| 560 | if (tag instanceof Intent) { |
| 561 | // Incoming tag is concrete intent, so launch |
| 562 | try { |
| 563 | mContext.startActivity((Intent)tag); |
| 564 | } catch (ActivityNotFoundException e) { |
| 565 | Log.w(TAG, NOT_FOUND); |
| 566 | Toast.makeText(mContext, NOT_FOUND, Toast.LENGTH_SHORT).show(); |
| 567 | } |
| 568 | } else if (tag instanceof String) { |
| 569 | // Incoming tag is a mime-type, so show resolution list |
| 570 | LinkedList<ActionInfo> children = mActions.get(tag); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 571 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 572 | // TODO: show drop-down resolution list |
| 573 | Log.d(TAG, "would show list between several options here"); |
| 574 | |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | /** {@inheritDoc} */ |
| 579 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 580 | return mWindow.superDispatchKeyEvent(event); |
| 581 | } |
| 582 | |
| 583 | /** {@inheritDoc} */ |
| 584 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 585 | // TODO: make this window accessible |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | /** {@inheritDoc} */ |
| 590 | public boolean dispatchTouchEvent(MotionEvent event) { |
| 591 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
| 592 | dismiss(); |
| 593 | return true; |
| 594 | } |
| 595 | return mWindow.superDispatchTouchEvent(event); |
| 596 | } |
| 597 | |
| 598 | /** {@inheritDoc} */ |
| 599 | public boolean dispatchTrackballEvent(MotionEvent event) { |
| 600 | return mWindow.superDispatchTrackballEvent(event); |
| 601 | } |
| 602 | |
| 603 | /** {@inheritDoc} */ |
| 604 | public void onContentChanged() { |
| 605 | } |
| 606 | |
| 607 | /** {@inheritDoc} */ |
| 608 | public boolean onCreatePanelMenu(int featureId, Menu menu) { |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | /** {@inheritDoc} */ |
| 613 | public View onCreatePanelView(int featureId) { |
| 614 | return null; |
| 615 | } |
| 616 | |
| 617 | /** {@inheritDoc} */ |
| 618 | public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 619 | return false; |
| 620 | } |
| 621 | |
| 622 | /** {@inheritDoc} */ |
| 623 | public boolean onMenuOpened(int featureId, Menu menu) { |
| 624 | return false; |
| 625 | } |
| 626 | |
| 627 | /** {@inheritDoc} */ |
| 628 | public void onPanelClosed(int featureId, Menu menu) { |
| 629 | } |
| 630 | |
| 631 | /** {@inheritDoc} */ |
| 632 | public boolean onPreparePanel(int featureId, View view, Menu menu) { |
| 633 | return false; |
| 634 | } |
| 635 | |
| 636 | /** {@inheritDoc} */ |
| 637 | public boolean onSearchRequested() { |
| 638 | return false; |
| 639 | } |
| 640 | |
| 641 | /** {@inheritDoc} */ |
| 642 | public void onWindowAttributesChanged(android.view.WindowManager.LayoutParams attrs) { |
| 643 | if (mDecor != null) { |
| 644 | mWindowManager.updateViewLayout(mDecor, attrs); |
| 645 | } |
| 646 | } |
| 647 | |
| 648 | /** {@inheritDoc} */ |
| 649 | public void onWindowFocusChanged(boolean hasFocus) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 650 | } |
| 651 | } |