From 46fd712ad2896858d977274f94a08aef5bdf0e4c Mon Sep 17 00:00:00 2001 From: Yorke Lee Date: Sun, 18 May 2014 17:37:32 -0700 Subject: Add send message option to dialpad Bug: 14052383 Change-Id: I45e9ffc829664af0010eb99be453160bcab2ac3d --- res/menu/dialpad_options.xml | 12 +++++----- .../android/dialer/dialpad/DialpadFragment.java | 25 +++++++++++++++---- src/com/android/dialer/util/DialerUtils.java | 28 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/res/menu/dialpad_options.xml b/res/menu/dialpad_options.xml index c6b5dda80..6f0b5ddce 100644 --- a/res/menu/dialpad_options.xml +++ b/res/menu/dialpad_options.xml @@ -17,16 +17,16 @@ + android:showAsAction="withText" /> + android:showAsAction="withText" /> + android:showAsAction="withText" /> + diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java index 418d54a20..1adfeb84b 100644 --- a/src/com/android/dialer/dialpad/DialpadFragment.java +++ b/src/com/android/dialer/dialpad/DialpadFragment.java @@ -21,6 +21,7 @@ import android.app.AlertDialog; import android.app.Dialog; import android.app.DialogFragment; import android.app.Fragment; +import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; @@ -260,6 +261,8 @@ public class DialpadFragment extends Fragment private boolean mFirstLaunch = false; private boolean mAnimate = false; + private ComponentName mSmsPackageComponentName; + private static final String PREF_DIGITS_FILLED_BY_INTENT = "pref_digits_filled_by_intent"; /** @@ -392,8 +395,6 @@ public class DialpadFragment extends Fragment mOverflowMenuButton = mDialpadView.getOverflowMenuButton(); mOverflowMenuButton.setOnClickListener(this); - final PopupMenu overflowMenu = buildOptionsMenu(mOverflowMenuButton); - mOverflowMenuButton.setOnTouchListener(overflowMenu.getDragToOpenListener()); return fragmentView; } @@ -654,6 +655,11 @@ public class DialpadFragment extends Fragment stopWatch.lap("bes"); stopWatch.stopAndLog(TAG, 50); + + mSmsPackageComponentName = DialerUtils.getSmsComponent(activity); + + final PopupMenu overflowMenu = buildOptionsMenu(mOverflowMenuButton); + mOverflowMenuButton.setOnTouchListener(overflowMenu.getDragToOpenListener()); } @Override @@ -845,9 +851,12 @@ public class DialpadFragment extends Fragment * * @param invoker the View that invoked the options menu, to act as an anchor location. */ - public PopupMenu buildOptionsMenu(View invoker) { + private PopupMenu buildOptionsMenu(View invoker) { final PopupMenu popupMenu = new PopupMenu(getActivity(), invoker); popupMenu.inflate(R.menu.dialpad_options); + final Menu menu = popupMenu.getMenu(); + final MenuItem sendMessage = menu.findItem(R.id.menu_send_message); + sendMessage.setVisible(mSmsPackageComponentName != null); popupMenu.setOnMenuItemClickListener(this); return popupMenu; } @@ -1407,17 +1416,25 @@ public class DialpadFragment extends Fragment @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { - case R.id.menu_add_contact: + case R.id.menu_add_contact: { final CharSequence digits = mDigits.getText(); DialerUtils.startActivityWithErrorToast(getActivity(), DialtactsActivity.getAddNumberToContactIntent(digits)); return true; + } case R.id.menu_2s_pause: updateDialString(PAUSE); return true; case R.id.menu_add_wait: updateDialString(WAIT); return true; + case R.id.menu_send_message: { + final CharSequence digits = mDigits.getText(); + final Intent smsIntent = new Intent(Intent.ACTION_SENDTO, + Uri.fromParts(CallUtil.SCHEME_SMSTO, digits.toString(), null)); + smsIntent.setComponent(mSmsPackageComponentName); + DialerUtils.startActivityWithErrorToast(getActivity(), smsIntent); + } default: return false; } diff --git a/src/com/android/dialer/util/DialerUtils.java b/src/com/android/dialer/util/DialerUtils.java index 8b0c4c6dd..584caa9a1 100644 --- a/src/com/android/dialer/util/DialerUtils.java +++ b/src/com/android/dialer/util/DialerUtils.java @@ -16,12 +16,20 @@ package com.android.dialer.util; import android.content.ActivityNotFoundException; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.net.Uri; +import android.provider.Telephony; import android.widget.Toast; +import com.android.contacts.common.CallUtil; import com.android.dialer.R; +import java.util.List; + /** * General purpose utility methods for the Dialer. */ @@ -54,4 +62,24 @@ public class DialerUtils { Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show(); } } + + /** + * Returns the component name to use in order to send an SMS using the default SMS application, + * or null if none exists. + */ + public static ComponentName getSmsComponent(Context context) { + String smsPackage = Telephony.Sms.getDefaultSmsPackage(context); + if (smsPackage != null) { + final PackageManager packageManager = context.getPackageManager(); + final Intent intent = new Intent(Intent.ACTION_SENDTO, + Uri.fromParts(CallUtil.SCHEME_SMSTO, "", null)); + final List resolveInfos = packageManager.queryIntentActivities(intent, 0); + for (ResolveInfo resolveInfo : resolveInfos) { + if (smsPackage.equals(resolveInfo.activityInfo.packageName)) { + return new ComponentName(smsPackage, resolveInfo.activityInfo.name); + } + } + } + return null; + } } -- cgit v1.2.3