DialerUtils.java revision 46fd712ad2896858d977274f94a08aef5bdf0e4c
13921359f3f01938768f0b0e731941542f0385787Yorke Lee/* 23921359f3f01938768f0b0e731941542f0385787Yorke Lee * Copyright (C) 2014 The Android Open Source Project 33921359f3f01938768f0b0e731941542f0385787Yorke Lee * 43921359f3f01938768f0b0e731941542f0385787Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License"); 53921359f3f01938768f0b0e731941542f0385787Yorke Lee * you may not use this file except in compliance with the License. 63921359f3f01938768f0b0e731941542f0385787Yorke Lee * You may obtain a copy of the License at 73921359f3f01938768f0b0e731941542f0385787Yorke Lee * 83921359f3f01938768f0b0e731941542f0385787Yorke Lee * http://www.apache.org/licenses/LICENSE-2.0 93921359f3f01938768f0b0e731941542f0385787Yorke Lee * 103921359f3f01938768f0b0e731941542f0385787Yorke Lee * Unless required by applicable law or agreed to in writing, software 113921359f3f01938768f0b0e731941542f0385787Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS, 123921359f3f01938768f0b0e731941542f0385787Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 133921359f3f01938768f0b0e731941542f0385787Yorke Lee * See the License for the specific language governing permissions and 143921359f3f01938768f0b0e731941542f0385787Yorke Lee * limitations under the License. 153921359f3f01938768f0b0e731941542f0385787Yorke Lee */ 163921359f3f01938768f0b0e731941542f0385787Yorke Leepackage com.android.dialer.util; 173921359f3f01938768f0b0e731941542f0385787Yorke Lee 183921359f3f01938768f0b0e731941542f0385787Yorke Leeimport android.content.ActivityNotFoundException; 1946fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport android.content.ComponentName; 203921359f3f01938768f0b0e731941542f0385787Yorke Leeimport android.content.Context; 213921359f3f01938768f0b0e731941542f0385787Yorke Leeimport android.content.Intent; 2246fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport android.content.pm.PackageManager; 2346fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport android.content.pm.ResolveInfo; 2446fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport android.net.Uri; 2546fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport android.provider.Telephony; 263921359f3f01938768f0b0e731941542f0385787Yorke Leeimport android.widget.Toast; 273921359f3f01938768f0b0e731941542f0385787Yorke Lee 2846fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport com.android.contacts.common.CallUtil; 293921359f3f01938768f0b0e731941542f0385787Yorke Leeimport com.android.dialer.R; 303921359f3f01938768f0b0e731941542f0385787Yorke Lee 3146fd712ad2896858d977274f94a08aef5bdf0e4cYorke Leeimport java.util.List; 3246fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee 333921359f3f01938768f0b0e731941542f0385787Yorke Lee/** 343921359f3f01938768f0b0e731941542f0385787Yorke Lee * General purpose utility methods for the Dialer. 353921359f3f01938768f0b0e731941542f0385787Yorke Lee */ 363921359f3f01938768f0b0e731941542f0385787Yorke Leepublic class DialerUtils { 373921359f3f01938768f0b0e731941542f0385787Yorke Lee 383921359f3f01938768f0b0e731941542f0385787Yorke Lee /** 393921359f3f01938768f0b0e731941542f0385787Yorke Lee * Attempts to start an activity and displays a toast with the default error message if the 403921359f3f01938768f0b0e731941542f0385787Yorke Lee * activity is not found, instead of throwing an exception. 413921359f3f01938768f0b0e731941542f0385787Yorke Lee * 423921359f3f01938768f0b0e731941542f0385787Yorke Lee * @param context to start the activity with. 433921359f3f01938768f0b0e731941542f0385787Yorke Lee * @param intent to start the activity with. 443921359f3f01938768f0b0e731941542f0385787Yorke Lee */ 453921359f3f01938768f0b0e731941542f0385787Yorke Lee public static void startActivityWithErrorToast(Context context, Intent intent) { 463921359f3f01938768f0b0e731941542f0385787Yorke Lee startActivityWithErrorToast(context, intent, R.string.activity_not_available); 473921359f3f01938768f0b0e731941542f0385787Yorke Lee } 483921359f3f01938768f0b0e731941542f0385787Yorke Lee 493921359f3f01938768f0b0e731941542f0385787Yorke Lee /** 503921359f3f01938768f0b0e731941542f0385787Yorke Lee * Attempts to start an activity and displays a toast with a provided error message if the 513921359f3f01938768f0b0e731941542f0385787Yorke Lee * activity is not found, instead of throwing an exception. 523921359f3f01938768f0b0e731941542f0385787Yorke Lee * 533921359f3f01938768f0b0e731941542f0385787Yorke Lee * @param context to start the activity with. 543921359f3f01938768f0b0e731941542f0385787Yorke Lee * @param intent to start the activity with. 553921359f3f01938768f0b0e731941542f0385787Yorke Lee * @param msgId Resource ID of the string to display in an error message if the activity is 563921359f3f01938768f0b0e731941542f0385787Yorke Lee * not found. 573921359f3f01938768f0b0e731941542f0385787Yorke Lee */ 583921359f3f01938768f0b0e731941542f0385787Yorke Lee public static void startActivityWithErrorToast(Context context, Intent intent, int msgId) { 593921359f3f01938768f0b0e731941542f0385787Yorke Lee try { 603921359f3f01938768f0b0e731941542f0385787Yorke Lee context.startActivity(intent); 613921359f3f01938768f0b0e731941542f0385787Yorke Lee } catch (ActivityNotFoundException e) { 623921359f3f01938768f0b0e731941542f0385787Yorke Lee Toast.makeText(context, msgId, Toast.LENGTH_SHORT).show(); 633921359f3f01938768f0b0e731941542f0385787Yorke Lee } 643921359f3f01938768f0b0e731941542f0385787Yorke Lee } 6546fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee 6646fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee /** 6746fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee * Returns the component name to use in order to send an SMS using the default SMS application, 6846fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee * or null if none exists. 6946fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee */ 7046fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee public static ComponentName getSmsComponent(Context context) { 7146fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee String smsPackage = Telephony.Sms.getDefaultSmsPackage(context); 7246fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee if (smsPackage != null) { 7346fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee final PackageManager packageManager = context.getPackageManager(); 7446fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee final Intent intent = new Intent(Intent.ACTION_SENDTO, 7546fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee Uri.fromParts(CallUtil.SCHEME_SMSTO, "", null)); 7646fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0); 7746fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee for (ResolveInfo resolveInfo : resolveInfos) { 7846fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee if (smsPackage.equals(resolveInfo.activityInfo.packageName)) { 7946fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee return new ComponentName(smsPackage, resolveInfo.activityInfo.name); 8046fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee } 8146fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee } 8246fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee } 8346fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee return null; 8446fd712ad2896858d977274f94a08aef5bdf0e4cYorke Lee } 853921359f3f01938768f0b0e731941542f0385787Yorke Lee} 86