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 
23 /** Utilities for creation of intents in Dialer. */
24 public class IntentUtil {
25 
26   private static final String SMS_URI_PREFIX = "sms:";
27   private static final int NO_PHONE_TYPE = -1;
28 
getSendSmsIntent(CharSequence phoneNumber)29   public static Intent getSendSmsIntent(CharSequence phoneNumber) {
30     return new Intent(Intent.ACTION_SENDTO, Uri.parse(SMS_URI_PREFIX + phoneNumber));
31   }
32 
getNewContactIntent()33   public static Intent getNewContactIntent() {
34     return new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI)
35         .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
36   }
37 
getNewContactIntent(CharSequence phoneNumber)38   public static Intent getNewContactIntent(CharSequence phoneNumber) {
39     return getNewContactIntent(null /* name */, phoneNumber /* phoneNumber */, NO_PHONE_TYPE);
40   }
41 
getNewContactIntent( CharSequence name, CharSequence phoneNumber, int phoneNumberType)42   public static Intent getNewContactIntent(
43       CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
44     Intent intent = getNewContactIntent();
45     populateContactIntent(intent, name, phoneNumber, phoneNumberType);
46     return intent;
47   }
48 
getAddToExistingContactIntent()49   public static Intent getAddToExistingContactIntent() {
50     Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
51     intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
52     return intent;
53   }
54 
getAddToExistingContactIntent(CharSequence phoneNumber)55   public static Intent getAddToExistingContactIntent(CharSequence phoneNumber) {
56     return getAddToExistingContactIntent(
57         null /* name */, phoneNumber /* phoneNumber */, NO_PHONE_TYPE);
58   }
59 
getAddToExistingContactIntent( CharSequence name, CharSequence phoneNumber, int phoneNumberType)60   public static Intent getAddToExistingContactIntent(
61       CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
62     Intent intent = getAddToExistingContactIntent();
63     populateContactIntent(intent, name, phoneNumber, phoneNumberType);
64     return intent;
65   }
66 
populateContactIntent( Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType)67   private static void populateContactIntent(
68       Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
69     if (phoneNumber != null) {
70       intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber);
71     }
72     if (name != null) {
73       intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
74     }
75     if (phoneNumberType != NO_PHONE_TYPE) {
76       intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, phoneNumberType);
77     }
78   }
79 }
80