Cleaning up the implementation of Intents.SEARCH_SUGGESTION_CLICKED

Change-Id: Ib29aec1eb31bee61c594e3fd9d745ff073358bd7
diff --git a/src/com/android/contacts/CallContactActivity.java b/src/com/android/contacts/CallContactActivity.java
new file mode 100644
index 0000000..181e9b7
--- /dev/null
+++ b/src/com/android/contacts/CallContactActivity.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2009 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts;
+
+import com.android.contacts.list.CallOrSmsInitiator;
+
+import android.app.Activity;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.content.DialogInterface.OnDismissListener;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract.Contacts;
+
+/**
+ * An interstitial activity used when the user selects a QSB search suggestion using
+ * a call button.
+ */
+public class CallContactActivity extends Activity implements OnDismissListener {
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        Uri data = getIntent().getData();
+        if (data == null) {
+            finish();
+        }
+
+        if (Contacts.CONTENT_ITEM_TYPE.equals(getContentResolver().getType(data))) {
+            CallOrSmsInitiator initiator = new CallOrSmsInitiator(this);
+            initiator.setOnDismissListener(this);
+            initiator.initiateCall(data);
+        } else {
+            startActivity(new Intent(Intent.ACTION_CALL_PRIVILEGED, data));
+            finish();
+        }
+    }
+
+    public void onDismiss(DialogInterface dialog) {
+        finish();
+    }
+}
diff --git a/src/com/android/contacts/PhoneDisambigDialog.java b/src/com/android/contacts/PhoneDisambigDialog.java
index 391ded0..3b32b57 100644
--- a/src/com/android/contacts/PhoneDisambigDialog.java
+++ b/src/com/android/contacts/PhoneDisambigDialog.java
@@ -89,6 +89,10 @@
         mDialog = dialogBuilder.create();
     }
 
+    public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
+        mDialog.setOnDismissListener(dismissListener);
+    }
+
     /**
      * Show the dialog.
      */
diff --git a/src/com/android/contacts/list/CallOrSmsInitiator.java b/src/com/android/contacts/list/CallOrSmsInitiator.java
index 1511931..b5aabee 100644
--- a/src/com/android/contacts/list/CallOrSmsInitiator.java
+++ b/src/com/android/contacts/list/CallOrSmsInitiator.java
@@ -17,9 +17,12 @@
 
 import com.android.contacts.ContactsUtils;
 import com.android.contacts.PhoneDisambigDialog;
+import com.android.internal.widget.RotarySelector.OnDialTriggerListener;
 
 import android.content.AsyncQueryHandler;
 import android.content.Context;
+import android.content.DialogInterface;
+import android.content.DialogInterface.OnDismissListener;
 import android.database.Cursor;
 import android.net.Uri;
 import android.provider.ContactsContract.Contacts;
@@ -48,6 +51,7 @@
 
     private static final String PHONE_NUMBER_SELECTION = Data.MIMETYPE + "='"
             + Phone.CONTENT_ITEM_TYPE + "' AND " + Phone.NUMBER + " NOT NULL";
+    private OnDismissListener mDismissListener;
 
     public CallOrSmsInitiator(Context context) {
         this.mContext = context;
@@ -59,6 +63,10 @@
         };
     }
 
+    public void setOnDismissListener(DialogInterface.OnDismissListener dismissListener) {
+        this.mDismissListener = dismissListener;
+    }
+
     protected void onPhoneNumberQueryComplete(int token, Object cookie, Cursor cursor) {
         if (cursor == null || cursor.getCount() == 0) {
             cursor.close();
@@ -89,6 +97,9 @@
         if (phone == null) {
             // Display dialog to choose a number to call.
             PhoneDisambigDialog phoneDialog = new PhoneDisambigDialog(mContext, cursor, mSendSms);
+            if (mDismissListener != null) {
+                phoneDialog.setOnDismissListener(mDismissListener);
+            }
             phoneDialog.show();
         } else {
             if (mSendSms) {
@@ -96,6 +107,9 @@
             } else {
                 ContactsUtils.initiateCall(mContext, phone);
             }
+            if (mDismissListener != null) {
+                mDismissListener.onDismiss(null);
+            }
         }
     }
 
diff --git a/src/com/android/contacts/list/ContactsIntentResolver.java b/src/com/android/contacts/list/ContactsIntentResolver.java
index beab168..62d56b5 100644
--- a/src/com/android/contacts/list/ContactsIntentResolver.java
+++ b/src/com/android/contacts/list/ContactsIntentResolver.java
@@ -16,25 +16,20 @@
 
 package com.android.contacts.list;
 
+import com.android.contacts.CallContactActivity;
 import com.android.contacts.ContactsSearchManager;
 import com.android.contacts.R;
 
 import android.app.Activity;
 import android.app.SearchManager;
-import android.content.ContentUris;
 import android.content.Intent;
-import android.content.UriMatcher;
-import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
-import android.provider.ContactsContract;
 import android.provider.Contacts.ContactMethods;
 import android.provider.Contacts.People;
 import android.provider.Contacts.Phones;
 import android.provider.ContactsContract.Contacts;
-import android.provider.ContactsContract.Data;
 import android.provider.ContactsContract.Intents;
-import android.provider.ContactsContract.RawContacts;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
 import android.provider.ContactsContract.Intents.UI;
@@ -50,14 +45,6 @@
 
     private static final String TAG = "ContactsListActivity";
 
-    // Uri matcher for contact id
-    private static final int CONTACTS_ID = 1001;
-    private static final UriMatcher sContactsIdMatcher;
-    static {
-        sContactsIdMatcher = new UriMatcher(UriMatcher.NO_MATCH);
-        sContactsIdMatcher.addURI(ContactsContract.AUTHORITY, "contacts/#", CONTACTS_ID);
-    }
-
     private final Activity mContext;
 
     public ContactsIntentResolver(Activity context) {
@@ -69,8 +56,6 @@
         request.setDisplayOnlyVisible(true);
 
         String action = intent.getAction();
-        String component = intent.getComponent().getClassName();
-        String type = intent.getType();
 
         Log.i(TAG, "Called with action: " + action);
 
@@ -96,11 +81,11 @@
         } else if (UI.LIST_GROUP_ACTION.equals(action)) {
             request.setActionCode(ContactsRequest.ACTION_GROUP);
             String groupName = intent.getStringExtra(UI.GROUP_NAME_EXTRA_KEY);
-            if (TextUtils.isEmpty(groupName)) {
+            if (!TextUtils.isEmpty(groupName)) {
+                request.setGroupName(groupName);
+            } else {
                 Log.e(TAG, "Intent missing a required extra: " + UI.GROUP_NAME_EXTRA_KEY);
                 request.setValid(false);
-            } else {
-                request.setGroupName(groupName);
             }
         } else if (Intent.ACTION_PICK.equals(action)) {
             final String resolvedType = intent.resolveType(mContext);
@@ -121,6 +106,7 @@
                 request.setLegacyCompatibilityMode(true);
             }
         } else if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
+            String component = intent.getComponent().getClassName();
             if (component.equals("alias.DialShortcut")) {
                 request.setActionCode(ContactsRequest.ACTION_CREATE_SHORTCUT_CALL);
                 request.setActivityTitle(mContext.getString(R.string.callShortcutActivityTitle));
@@ -132,6 +118,7 @@
                 request.setActivityTitle(mContext.getString(R.string.shortcutActivityTitle));
             }
         } else if (Intent.ACTION_GET_CONTENT.equals(action)) {
+            String type = intent.getType();
             if (Contacts.CONTENT_ITEM_TYPE.equals(type)) {
                 request.setActionCode(ContactsRequest.ACTION_PICK_OR_CREATE_CONTACT);
             } else if (Phone.CONTENT_ITEM_TYPE.equals(type)) {
@@ -211,40 +198,25 @@
         // dispatched from the SearchManager for security reasons
         // so we need to re-dispatch from here to the intended target.
         } else if (Intents.SEARCH_SUGGESTION_CLICKED.equals(action)) {
-            // TODO show the disambig dialog instead of guessing the number
             Uri data = intent.getData();
-            Uri telUri = null;
-            if (sContactsIdMatcher.match(data) == CONTACTS_ID) {
-                long contactId = Long.valueOf(data.getLastPathSegment());
-                final Cursor cursor = queryPhoneNumbers(contactId);
-                if (cursor != null) {
-                    if (cursor.getCount() == 1 && cursor.moveToFirst()) {
-                        int phoneNumberIndex = cursor.getColumnIndex(Phone.NUMBER);
-                        String phoneNumber = cursor.getString(phoneNumberIndex);
-                        telUri = Uri.parse("tel:" + phoneNumber);
-                    }
-                    cursor.close();
-                }
-            }
             // See if the suggestion was clicked with a search action key (call button)
-            Intent newIntent;
-            if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG)) && telUri != null) {
-                newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, telUri);
+            if ("call".equals(intent.getStringExtra(SearchManager.ACTION_MSG))) {
+                Intent newIntent = new Intent(mContext, CallContactActivity.class);
+                newIntent.setData(data);
+                request.setRedirectIntent(newIntent);
             } else {
-                newIntent = new Intent(Intent.ACTION_VIEW, data);
+                request.setRedirectIntent(new Intent(Intent.ACTION_VIEW, data));
             }
-            request.setRedirectIntent(newIntent);
         } else if (Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED.equals(action)) {
-            Intent newIntent = new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData());
-            request.setRedirectIntent(newIntent);
+            request.setRedirectIntent(new Intent(Intent.ACTION_CALL_PRIVILEGED, intent.getData()));
         } else if (Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED.equals(action)) {
             // TODO actually support this in EditContactActivity.
             String number = intent.getData().getSchemeSpecificPart();
             Intent newIntent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
             newIntent.putExtra(Intents.Insert.PHONE, number);
             request.setRedirectIntent(newIntent);
-        }
 
+        }
         // Allow the title to be set to a custom String using an extra on the intent
         String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
         if (title != null) {
@@ -253,20 +225,4 @@
 
         return request;
     }
-
-    // TODO replace with a disabmig dialog
-    @Deprecated
-    private Cursor queryPhoneNumbers(long contactId) {
-        Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
-        Uri dataUri = Uri.withAppendedPath(baseUri, Contacts.Data.CONTENT_DIRECTORY);
-
-        Cursor c = mContext.getContentResolver().query(dataUri,
-                new String[] {Phone._ID, Phone.NUMBER, Phone.IS_SUPER_PRIMARY,
-                RawContacts.ACCOUNT_TYPE, Phone.TYPE, Phone.LABEL},
-                Data.MIMETYPE + "=?", new String[] {Phone.CONTENT_ITEM_TYPE}, null);
-        if (c != null && c.moveToFirst()) {
-            return c;
-        }
-        return null;
-    }
 }