Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should app check if device has calling functionality when using ACTION_DIAL intent?

I have the following code in my program:

  public static void callPhoneNumber(Context context, String clientPhoneNum) {

    if (isCallingSupported(context)) {
      Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + clientPhoneNum));
      context.startActivity(i);
    } else {
      final AlertDialog alertDialog =
          new AlertDialog.Builder(context).setMessage(context.getString(R.string.error))
              .setMessage(context.getString(R.string.no_call_functionality))
              .setPositiveButton(context.getString(R.string.ok),
                  new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                      dialog.dismiss();
                    }
                  })

              .create();

      alertDialog.show();
    }
  }

  private static boolean isCallingSupported(Context context) {
    TelephonyManager telephonyManager =
        (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    return (telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE);
  }

I am wondering if isCallingSupported() would be necessary at all? I don't remember exactly why I wrote it this way but now when I am reviewing I'm thinking the user may just call a number using his Skype or other VOIP apps. Should I do any other checking instead or is this intent safe without the isCallingSupported() (what I mean by safe is, even if user has a tablet with no calling functionality and no other apps that can handle a call, the intent doesn't cause a crash)?

like image 245
Nima G Avatar asked Nov 18 '14 15:11

Nima G


People also ask

How do you make an intent call?

Intent Object - Action to make Phone Call Intent phoneIntent = new Intent(Intent. ACTION_CALL); You can use ACTION_DIAL action instead of ACTION_CALL, in that case you will have option to modify hardcoded phone number before making a call instead of making a direct call.

What does android intent ACTION VIEW do?

A common intent action is ACTION_VIEW, which you use when you have some information that an activity can show to the user, such as a photo to view in a gallery app, or an address to view in a map app. You can specify the action for an intent in the intent constructor, or with the setAction() method.

How do you call implicit intent?

Implicit Intent with URI data Uri number = Uri. parse("tel:11061991"); Intent callIntent = new Intent(Intent. ACTION_DIAL, number);


2 Answers

From this question:

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
       //Then there is application can handle your intent
}else{
       //No Application can handle your intent
}

Start by checking if any app has registered to this intent. If they have, use it. If not, display your dialog.

You would end up just replacing your isCallingSupported function with the code above:

private static boolean isCallingSupported(Context context) {

    boolean result = true;
    PackageManager manager = context.getPackageManager();
    List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
    if (infos.size() <= 0) {
        result = false;
    }
    return result;

}

like image 69
Robin Eisenberg Avatar answered Oct 27 '22 00:10

Robin Eisenberg


I would use PackageManager and resolveActivity() to see if anything will respond to your configured ACTION_DIAL Intent. That would handle:

  • Devices that do not have native telephony and have no dialer, as resolveActivity() should return null

  • Devices that do not have native telephony but do have a dialer (e.g., VOIP), as hopefully the authors of that dialer have ACTION_DIAL support

  • Devices that do have native telephony, but the current user does not have access to the dialer (e.g., restricted profiles)

like image 36
CommonsWare Avatar answered Oct 27 '22 00:10

CommonsWare