package com.android.contacts.ui;
+import com.android.contacts.Collapser;
+import com.android.contacts.ContactPresenceIconUtil;
import com.android.contacts.ContactsUtils;
import com.android.contacts.R;
import com.android.contacts.model.ContactsSource;
import com.android.internal.policy.PolicyManager;
import com.google.android.collect.Sets;
-import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
-import android.content.EntityIterator;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.Photo;
+import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
+import android.provider.ContactsContract.CommonDataKinds.Website;
import android.text.TextUtils;
-import android.text.format.DateUtils;
+import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.ListView;
+import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.lang.ref.SoftReference;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
public void onDismiss(QuickContactWindow dialog);
}
+ /**
+ * Custom layout the sole purpose of which is to intercept the BACK key and
+ * close QC even when the soft keyboard is open.
+ */
+ public static class RootLayout extends RelativeLayout {
+
+ QuickContactWindow mQuickContactWindow;
+
+ public RootLayout(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ /**
+ * Intercepts the BACK key event and dismisses QuickContact window.
+ */
+ @Override
+ public boolean dispatchKeyEventPreIme(KeyEvent event) {
+ if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
+ mQuickContactWindow.onBackPressed();
+ return true;
+ } else {
+ return super.dispatchKeyEventPreIme(event);
+ }
+ }
+ }
+
private final Context mContext;
private final LayoutInflater mInflater;
private final WindowManager mWindowManager;
private ImageView mArrowDown;
private int mMode;
+ private RootLayout mRootView;
private View mHeader;
private HorizontalScrollView mTrackScroll;
private ViewGroup mTrack;
private String[] mExcludeMimes;
/**
- * Specific MIME-types that should be bumped to the front of the dialog.
- * Other MIME-types not appearing in this list follow in alphabetic order.
+ * {@link #PRECEDING_MIMETYPES} and {@link #FOLLOWING_MIMETYPES} are used to sort MIME-types.
+ *
+ * <p>The MIME-types in {@link #PRECEDING_MIMETYPES} appear in the front of the dialog,
+ * in the order in the array.
+ *
+ * <p>The ones in {@link #FOLLOWING_MIMETYPES} appear in the end of the dialog, in alphabetical
+ * order.
+ *
+ * <p>The rest go between them, in the order in the array.
*/
- private static final String[] ORDERED_MIMETYPES = new String[] {
+ private static final String[] PRECEDING_MIMETYPES = new String[] {
Phone.CONTENT_ITEM_TYPE,
Contacts.CONTENT_ITEM_TYPE,
Constants.MIME_SMS_ADDRESS,
};
/**
+ * See {@link #PRECEDING_MIMETYPES}.
+ */
+ private static final String[] FOLLOWING_MIMETYPES = new String[] {
+ StructuredPostal.CONTENT_ITEM_TYPE,
+ Website.CONTENT_ITEM_TYPE,
+ };
+
+ /**
* Specific list {@link ApplicationInfo#packageName} of apps that are
* prefered <strong>only</strong> for the purposes of default icons when
* multiple {@link ResolveInfo} are found to match. This only happens when
* the user has not selected a default app yet, and they will still be
* presented with the system disambiguation dialog.
*/
- private static final HashSet<String> sPreferResolve = Sets.newHashSet(new String[] {
+ private static final HashSet<String> sPreferResolve = Sets.newHashSet(
"com.android.email",
"com.android.calendar",
"com.android.contacts",
"com.android.mms",
"com.android.phone",
- });
+ "com.android.browser");
private static final int TOKEN_DATA = 1;
mWindow = PolicyManager.makeNewWindow(mContext);
mWindow.setCallback(this);
mWindow.setWindowManager(mWindowManager, null, null);
+ mWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
mWindow.setContentView(R.layout.quickcontact);
+ mRootView = (RootLayout)mWindow.findViewById(R.id.root);
+ mRootView.mQuickContactWindow = this;
+ mRootView.setFocusable(true);
+ mRootView.setFocusableInTouchMode(true);
+ mRootView.setDescendantFocusability(RootLayout.FOCUS_AFTER_DESCENDANTS);
+
mArrowUp = (ImageView)mWindow.findViewById(R.id.arrow_up);
mArrowDown = (ImageView)mWindow.findViewById(R.id.arrow_down);
// Inflate actual header if we picked a stub
final ViewStub stub = (ViewStub)header;
header = stub.inflate();
- } else {
+ } else if (header != null) {
header.setVisibility(View.VISIBLE);
}
android.os.Debug.startMethodTracing(TRACE_TAG);
}
+ // Validate incoming parameters
+ final boolean validMode = (mode == QuickContact.MODE_SMALL
+ || mode == QuickContact.MODE_MEDIUM || mode == QuickContact.MODE_LARGE);
+ if (!validMode) {
+ throw new IllegalArgumentException("Invalid mode, expecting MODE_LARGE, "
+ + "MODE_MEDIUM, or MODE_SMALL");
+ }
+
+ if (anchor == null) {
+ throw new IllegalArgumentException("Missing anchor rectangle");
+ }
+
// Prepare header view for requested mode
mLookupUri = lookupUri;
mAnchor = new Rect(anchor);
resetTrack();
+ // We need to have a focused view inside the QuickContact window so
+ // that the BACK key event can be intercepted
+ mRootView.requestFocus();
+
mHasValidSocial = false;
mDismissed = false;
mQuerying = true;
}
/** Assign this image to the view, if found in {@link #mHeader}. */
- private void setHeaderImage(int id, int resId) {
- setHeaderImage(id, mContext.getResources().getDrawable(resId));
- }
-
- /** Assign this image to the view, if found in {@link #mHeader}. */
private void setHeaderImage(int id, Drawable drawable) {
final View view = mHeader.findViewById(id);
if (view instanceof ImageView) {
}
/**
- * Find the presence icon for showing in summary header.
- */
- private Drawable getPresenceIcon(int status) {
- int resId = -1;
- switch (status) {
- case StatusUpdates.AVAILABLE:
- resId = android.R.drawable.presence_online;
- break;
- case StatusUpdates.IDLE:
- case StatusUpdates.AWAY:
- resId = android.R.drawable.presence_away;
- break;
- case StatusUpdates.DO_NOT_DISTURB:
- resId = android.R.drawable.presence_busy;
- break;
- }
- if (resId != -1) {
- return mContext.getResources().getDrawable(resId);
- } else {
- return null;
- }
- }
-
- /**
* Find the QuickContact-specific presence icon for showing in chiclets.
*/
private Drawable getTrackPresenceIcon(int status) {
- int resId = -1;
+ int resId;
switch (status) {
case StatusUpdates.AVAILABLE:
resId = R.drawable.quickcontact_slider_presence_active;
* Abstract definition of an action that could be performed, along with
* string description and icon.
*/
- private interface Action {
+ private interface Action extends Collapser.Collapsible<Action> {
public CharSequence getHeader();
public CharSequence getBody();
mIntent = new Intent(Intent.ACTION_SENDTO, mailUri);
}
+ } else if (Website.CONTENT_ITEM_TYPE.equals(mimeType)) {
+ final String url = getAsString(cursor, Website.URL);
+ if (!TextUtils.isEmpty(url)) {
+ mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
+ }
+
} else if (Im.CONTENT_ITEM_TYPE.equals(mimeType)) {
final boolean isEmail = Email.CONTENT_ITEM_TYPE.equals(
getAsString(cursor, Data.MIMETYPE));
- final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK :
- getAsInt(cursor, Im.PROTOCOL);
-
- if (isEmail) {
- // Use Google Talk string when using Email, and clear data
- // Uri so we don't try saving Email as primary.
- mHeader = context.getText(R.string.chat_gtalk);
- mDataUri = null;
- }
+ if (isEmail || isProtocolValid(cursor)) {
+ final int protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK :
+ getAsInt(cursor, Im.PROTOCOL);
+
+ if (isEmail) {
+ // Use Google Talk string when using Email, and clear data
+ // Uri so we don't try saving Email as primary.
+ mHeader = context.getText(R.string.chat_gtalk);
+ mDataUri = null;
+ }
- String host = getAsString(cursor, Im.CUSTOM_PROTOCOL);
- String data = getAsString(cursor, isEmail ? Email.DATA : Im.DATA);
- if (protocol != Im.PROTOCOL_CUSTOM) {
- // Try bringing in a well-known host for specific protocols
- host = ContactsUtils.lookupProviderNameFromId(protocol);
- }
+ String host = getAsString(cursor, Im.CUSTOM_PROTOCOL);
+ String data = getAsString(cursor, isEmail ? Email.DATA : Im.DATA);
+ if (protocol != Im.PROTOCOL_CUSTOM) {
+ // Try bringing in a well-known host for specific protocols
+ host = ContactsUtils.lookupProviderNameFromId(protocol);
+ }
- if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(data)) {
- final String authority = host.toLowerCase();
- final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
- authority).appendPath(data).build();
- mIntent = new Intent(Intent.ACTION_SENDTO, imUri);
+ if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(data)) {
+ final String authority = host.toLowerCase();
+ final Uri imUri = new Uri.Builder().scheme(Constants.SCHEME_IMTO).authority(
+ authority).appendPath(data).build();
+ mIntent = new Intent(Intent.ACTION_SENDTO, imUri);
+ }
}
}
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
+ private boolean isProtocolValid(Cursor cursor) {
+ final int columnIndex = cursor.getColumnIndex(Im.PROTOCOL);
+ if (cursor.isNull(columnIndex)) {
+ return false;
+ }
+ try {
+ Integer.valueOf(cursor.getString(columnIndex));
+ } catch (NumberFormatException e) {
+ return false;
+ }
+ return true;
+ }
+
/** {@inheritDoc} */
public CharSequence getHeader() {
return mHeader;
public Intent getIntent() {
return mIntent;
}
+
+ /** {@inheritDoc} */
+ public boolean collapseWith(Action other) {
+ if (!shouldCollapseWith(other)) {
+ return false;
+ }
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ public boolean shouldCollapseWith(Action t) {
+ if (t == null) {
+ return false;
+ }
+ if (!(t instanceof DataAction)) {
+ Log.e(TAG, "t must be DataAction");
+ return false;
+ }
+ DataAction other = (DataAction)t;
+ if (!ContactsUtils.areObjectsEqual(mKind, other.mKind)) {
+ return false;
+ }
+ if (!ContactsUtils.areDataEqual(mContext, mMimeType, mBody, other.mMimeType,
+ other.mBody)) {
+ return false;
+ }
+ if (!TextUtils.equals(mMimeType, other.mMimeType)
+ || !ContactsUtils.areIntentActionEqual(mIntent, other.mIntent)
+ ) {
+ return false;
+ }
+ return true;
+ }
}
/**
return null;
}
+ /** {@inheritDoc} */
+ public boolean collapseWith(Action t) {
+ return false; // Never dup.
+ }
+
+ /** {@inheritDoc} */
+ public boolean shouldCollapseWith(Action t) {
+ return false; // Never dup.
+ }
}
/**
* queries, keyed internally on MIME-type.
*/
private static class ResolveCache {
- private Context mContext;
private PackageManager mPackageManager;
/**
private HashMap<String, Entry> mCache = new HashMap<String, Entry>();
public ResolveCache(Context context) {
- mContext = context;
mPackageManager = context.getPackageManager();
}
* Provide a strongly-typed {@link LinkedList} that holds a list of
* {@link Action} objects.
*/
- private class ActionList extends LinkedList<Action> {
+ private class ActionList extends ArrayList<Action> {
}
/**
while (cursor.moveToNext()) {
final long dataId = cursor.getLong(DataQuery._ID);
final String accountType = cursor.getString(DataQuery.ACCOUNT_TYPE);
- final String resPackage = cursor.getString(DataQuery.RES_PACKAGE);
final String mimeType = cursor.getString(DataQuery.MIMETYPE);
// Handle any social status updates from this row
// Read contact information from last data row
final String name = cursor.getString(DataQuery.DISPLAY_NAME);
final int presence = cursor.getInt(DataQuery.CONTACT_PRESENCE);
- final Drawable statusIcon = getPresenceIcon(presence);
+ final Drawable statusIcon = ContactPresenceIconUtil.getPresenceIcon(mContext, presence);
setHeaderText(R.id.name, name);
setHeaderImage(R.id.presence, statusIcon);
setHeaderText(R.id.timestamp, status.getTimestampLabel(mContext));
}
- // Turn our list of actions into UI elements, starting with common types
- final Set<String> containedTypes = mActions.keySet();
- for (String mimeType : ORDERED_MIMETYPES) {
+ // Turn our list of actions into UI elements
+
+ // Index where we start adding child views.
+ int index = mTrack.getChildCount() - 1;
+
+ // All the mime-types to add.
+ final Set<String> containedTypes = new HashSet<String>(mActions.keySet());
+
+ // First, add PRECEDING_MIMETYPES, which are most common.
+ for (String mimeType : PRECEDING_MIMETYPES) {
if (containedTypes.contains(mimeType)) {
- final int index = mTrack.getChildCount() - 1;
- mTrack.addView(inflateAction(mimeType), index);
+ mTrack.addView(inflateAction(mimeType), index++);
containedTypes.remove(mimeType);
}
}
- // Then continue with remaining MIME-types in alphabetical order
+ // Keep the current index to append non PRECEDING/FOLLOWING items.
+ final int indexAfterPreceding = index;
+
+ // Then, add FOLLOWING_MIMETYPES, which are least common.
+ for (String mimeType : FOLLOWING_MIMETYPES) {
+ if (containedTypes.contains(mimeType)) {
+ mTrack.addView(inflateAction(mimeType), index++);
+ containedTypes.remove(mimeType);
+ }
+ }
+
+ // Go back to just after PRECEDING_MIMETYPES, and append the rest.
+ index = indexAfterPreceding;
final String[] remainingTypes = containedTypes.toArray(new String[containedTypes.size()]);
Arrays.sort(remainingTypes);
for (String mimeType : remainingTypes) {
- final int index = mTrack.getChildCount() - 1;
- mTrack.addView(inflateAction(mimeType), index);
+ mTrack.addView(inflateAction(mimeType), index++);
}
}
}
/**
- * Inflate the in-track view for the action of the given MIME-type. Will use
- * the icon provided by the {@link DataKind}.
+ * Inflate the in-track view for the action of the given MIME-type, collapsing duplicate values.
+ * Will use the icon provided by the {@link DataKind}.
*/
private View inflateAction(String mimeType) {
final CheckableImageView view = (CheckableImageView)obtainView();
// Add direct intent if single child, otherwise flag for multiple
ActionList children = mActions.get(mimeType);
+ if (children.size() > 1) {
+ Collapser.collapseList(children);
+ }
Action firstInfo = children.get(0);
if (children.size() == 1) {
view.setTag(firstInfo);
}
// Hide the resolution list, if present
- setResolveVisible(false, actionView);
+ setResolveVisible(false, null);
this.dismiss();
if (makePrimary) {
// Set action title based on summary value
final Action action = (Action)getItem(position);
- final Drawable icon = mResolveCache.getIcon(action);
TextView text1 = (TextView)convertView.findViewById(android.R.id.text1);
TextView text2 = (TextView)convertView.findViewById(android.R.id.text2);
* window, which usually means we should dismiss.
*/
protected void detectEventOutside(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
+ if (event.getAction() == MotionEvent.ACTION_DOWN && mDecor != null) {
// Only try detecting outside events on down-press
mDecor.getHitRect(mRect);
mRect.top = mRect.top + mShadowTouch;
}
/** {@inheritDoc} */
- public void onQueryEntitiesComplete(int token, Object cookie, EntityIterator iterator) {
- // No actions
- }
-
- /** {@inheritDoc} */
public void onAttachedToWindow() {
// No actions
}