1 /* 2 * Copyright (C) 2012 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 */ 16 17 package com.android.dialer.util; 18 19 import android.content.Intent; 20 import android.net.Uri; 21 import android.provider.ContactsContract; 22 import android.telecom.PhoneAccount; 23 import android.telecom.PhoneAccountHandle; 24 import android.telecom.TelecomManager; 25 import android.telecom.VideoProfile; 26 27 import com.android.contacts.common.CallUtil; 28 import com.android.phone.common.PhoneConstants; 29 30 /** 31 * Utilities for creation of intents in Dialer, such as {@link Intent#ACTION_CALL}. 32 */ 33 public class IntentUtil { 34 35 public static final String CALL_ACTION = Intent.ACTION_CALL; 36 private static final String SMS_URI_PREFIX = "sms:"; 37 private static final int NO_PHONE_TYPE = -1; 38 39 /** 40 * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined 41 * automatically. 42 */ getCallIntent(String number)43 public static Intent getCallIntent(String number) { 44 return getCallIntent(number, null, null); 45 } 46 47 /** 48 * Return an Intent for making a phone call. A given Uri will be used as is (without any 49 * sanity check). 50 */ getCallIntent(Uri uri)51 public static Intent getCallIntent(Uri uri) { 52 return getCallIntent(uri, null, null); 53 } 54 55 /** 56 * A variant of {@link #getCallIntent(String)} but also accept a call origin. 57 * For more information about call origin, see comments in Phone package (PhoneApp). 58 */ getCallIntent(String number, String callOrigin)59 public static Intent getCallIntent(String number, String callOrigin) { 60 return getCallIntent(CallUtil.getCallUri(number), callOrigin, null); 61 } 62 63 /** 64 * A variant of {@link #getCallIntent(String)} but also include {@code Account}. 65 */ getCallIntent(String number, PhoneAccountHandle accountHandle)66 public static Intent getCallIntent(String number, PhoneAccountHandle accountHandle) { 67 return getCallIntent(number, null, accountHandle); 68 } 69 70 /** 71 * A variant of {@link #getCallIntent(android.net.Uri)} but also include {@code Account}. 72 */ getCallIntent(Uri uri, PhoneAccountHandle accountHandle)73 public static Intent getCallIntent(Uri uri, PhoneAccountHandle accountHandle) { 74 return getCallIntent(uri, null, accountHandle); 75 } 76 77 /** 78 * A variant of {@link #getCallIntent(String, String)} but also include {@code Account}. 79 */ getCallIntent( String number, String callOrigin, PhoneAccountHandle accountHandle)80 public static Intent getCallIntent( 81 String number, String callOrigin, PhoneAccountHandle accountHandle) { 82 return getCallIntent(CallUtil.getCallUri(number), callOrigin, accountHandle); 83 } 84 85 /** 86 * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call 87 * origin and {@code Account}. 88 * For more information about call origin, see comments in Phone package (PhoneApp). 89 */ getCallIntent( Uri uri, String callOrigin, PhoneAccountHandle accountHandle)90 public static Intent getCallIntent( 91 Uri uri, String callOrigin, PhoneAccountHandle accountHandle) { 92 return getCallIntent(uri, callOrigin, accountHandle, 93 VideoProfile.STATE_AUDIO_ONLY); 94 } 95 96 /** 97 * A variant of {@link #getCallIntent(String, String)} for starting a video call. 98 */ getVideoCallIntent(String number, String callOrigin)99 public static Intent getVideoCallIntent(String number, String callOrigin) { 100 return getCallIntent(CallUtil.getCallUri(number), callOrigin, null, 101 VideoProfile.STATE_BIDIRECTIONAL); 102 } 103 104 /** 105 * A variant of {@link #getCallIntent(String, String, android.telecom.PhoneAccountHandle)} for 106 * starting a video call. 107 */ getVideoCallIntent( String number, String callOrigin, PhoneAccountHandle accountHandle)108 public static Intent getVideoCallIntent( 109 String number, String callOrigin, PhoneAccountHandle accountHandle) { 110 return getCallIntent(CallUtil.getCallUri(number), callOrigin, accountHandle, 111 VideoProfile.STATE_BIDIRECTIONAL); 112 } 113 114 /** 115 * A variant of {@link #getCallIntent(String, String, android.telecom.PhoneAccountHandle)} for 116 * starting a video call. 117 */ getVideoCallIntent(String number, PhoneAccountHandle accountHandle)118 public static Intent getVideoCallIntent(String number, PhoneAccountHandle accountHandle) { 119 return getVideoCallIntent(number, null, accountHandle); 120 } 121 122 /** 123 * A variant of {@link #getCallIntent(android.net.Uri)} for calling Voicemail. 124 */ getVoicemailIntent()125 public static Intent getVoicemailIntent() { 126 return getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_VOICEMAIL, "", null)); 127 } 128 129 /** 130 * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call 131 * origin and {@code Account} and {@code VideoCallProfile} state. 132 * For more information about call origin, see comments in Phone package (PhoneApp). 133 */ getCallIntent( Uri uri, String callOrigin, PhoneAccountHandle accountHandle, int videoState)134 public static Intent getCallIntent( 135 Uri uri, String callOrigin, PhoneAccountHandle accountHandle, int videoState) { 136 final Intent intent = new Intent(CALL_ACTION, uri); 137 intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState); 138 if (callOrigin != null) { 139 intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin); 140 } 141 if (accountHandle != null) { 142 intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle); 143 } 144 145 return intent; 146 } 147 getSendSmsIntent(CharSequence phoneNumber)148 public static Intent getSendSmsIntent(CharSequence phoneNumber) { 149 return new Intent(Intent.ACTION_SENDTO, Uri.parse(SMS_URI_PREFIX + phoneNumber)); 150 } 151 getNewContactIntent()152 public static Intent getNewContactIntent() { 153 return new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI); 154 } 155 getNewContactIntent(CharSequence phoneNumber)156 public static Intent getNewContactIntent(CharSequence phoneNumber) { 157 return getNewContactIntent( 158 null /* name */, 159 phoneNumber /* phoneNumber */, 160 NO_PHONE_TYPE); 161 } 162 getNewContactIntent( CharSequence name, CharSequence phoneNumber, int phoneNumberType)163 public static Intent getNewContactIntent( 164 CharSequence name, CharSequence phoneNumber, int phoneNumberType) { 165 Intent intent = getNewContactIntent(); 166 populateContactIntent(intent, name, phoneNumber, phoneNumberType); 167 return intent; 168 } 169 getAddToExistingContactIntent()170 public static Intent getAddToExistingContactIntent() { 171 Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT); 172 intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); 173 return intent; 174 } 175 getAddToExistingContactIntent(CharSequence phoneNumber)176 public static Intent getAddToExistingContactIntent(CharSequence phoneNumber) { 177 return getAddToExistingContactIntent( 178 null /* name */, 179 phoneNumber /* phoneNumber */, 180 NO_PHONE_TYPE); 181 } 182 getAddToExistingContactIntent( CharSequence name, CharSequence phoneNumber, int phoneNumberType)183 public static Intent getAddToExistingContactIntent( 184 CharSequence name, CharSequence phoneNumber, int phoneNumberType) { 185 Intent intent = getAddToExistingContactIntent(); 186 populateContactIntent(intent, name, phoneNumber, phoneNumberType); 187 return intent; 188 } 189 populateContactIntent( Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType)190 private static void populateContactIntent( 191 Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType) { 192 if (phoneNumber != null) { 193 intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber); 194 } 195 if (name != null) { 196 intent.putExtra(ContactsContract.Intents.Insert.NAME, name); 197 } 198 if (phoneNumberType != NO_PHONE_TYPE) { 199 intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, phoneNumberType); 200 } 201 } 202 } 203