DialerUtils.java revision 46fd712ad2896858d977274f94a08aef5bdf0e4c
1/* 2 * Copyright (C) 2014 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 */ 16package com.android.dialer.util; 17 18import android.content.ActivityNotFoundException; 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.PackageManager; 23import android.content.pm.ResolveInfo; 24import android.net.Uri; 25import android.provider.Telephony; 26import android.widget.Toast; 27 28import com.android.contacts.common.CallUtil; 29import com.android.dialer.R; 30 31import java.util.List; 32 33/** 34 * General purpose utility methods for the Dialer. 35 */ 36public class DialerUtils { 37 38 /** 39 * Attempts to start an activity and displays a toast with the default error message if the 40 * activity is not found, instead of throwing an exception. 41 * 42 * @param context to start the activity with. 43 * @param intent to start the activity with. 44 */ 45 public static void startActivityWithErrorToast(Context context, Intent intent) { 46 startActivityWithErrorToast(context, intent, R.string.activity_not_available); 47 } 48 49 /** 50 * Attempts to start an activity and displays a toast with a provided error message if the 51 * activity is not found, instead of throwing an exception. 52 * 53 * @param context to start the activity with. 54 * @param intent to start the activity with. 55 * @param msgId Resource ID of the string to display in an error message if the activity is 56 * not found. 57 */ 58 public static void startActivityWithErrorToast(Context context, Intent intent, int msgId) { 59 try { 60 context.startActivity(intent); 61 } catch (ActivityNotFoundException e) { 62 Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show(); 63 } 64 } 65 66 /** 67 * Returns the component name to use in order to send an SMS using the default SMS application, 68 * or null if none exists. 69 */ 70 public static ComponentName getSmsComponent(Context context) { 71 String smsPackage = Telephony.Sms.getDefaultSmsPackage(context); 72 if (smsPackage != null) { 73 final PackageManager packageManager = context.getPackageManager(); 74 final Intent intent = new Intent(Intent.ACTION_SENDTO, 75 Uri.fromParts(CallUtil.SCHEME_SMSTO, "", null)); 76 final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0); 77 for (ResolveInfo resolveInfo : resolveInfos) { 78 if (smsPackage.equals(resolveInfo.activityInfo.packageName)) { 79 return new ComponentName(smsPackage, resolveInfo.activityInfo.name); 80 } 81 } 82 } 83 return null; 84 } 85} 86