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
17package com.android.dialer.util;
18
19import android.content.Intent;
20import android.net.Uri;
21import android.os.Bundle;
22import android.provider.ContactsContract;
23import android.telecom.PhoneAccountHandle;
24import android.telecom.TelecomManager;
25import android.telecom.VideoProfile;
26
27import com.android.contacts.common.CallUtil;
28
29/**
30 * Utilities for creation of intents in Dialer, such as {@link Intent#ACTION_CALL}.
31 */
32public class IntentUtil {
33
34    public static final String CALL_ACTION = Intent.ACTION_CALL;
35    private static final String SMS_URI_PREFIX = "sms:";
36    private static final int NO_PHONE_TYPE = -1;
37
38    public static final String EXTRA_CALL_INITIATION_TYPE
39            = "com.android.dialer.EXTRA_CALL_INITIATION_TYPE";
40
41    public static class CallIntentBuilder {
42        private Uri mUri;
43        private int mCallInitiationType;
44        private PhoneAccountHandle mPhoneAccountHandle;
45        private boolean mIsVideoCall = false;
46
47        public CallIntentBuilder(Uri uri) {
48            mUri = uri;
49        }
50
51        public CallIntentBuilder(String number) {
52            this(CallUtil.getCallUri(number));
53        }
54
55        public CallIntentBuilder setCallInitiationType(int initiationType) {
56            mCallInitiationType = initiationType;
57            return this;
58        }
59
60        public CallIntentBuilder setPhoneAccountHandle(PhoneAccountHandle accountHandle) {
61            mPhoneAccountHandle = accountHandle;
62            return this;
63        }
64
65        public CallIntentBuilder setIsVideoCall(boolean isVideoCall) {
66            mIsVideoCall = isVideoCall;
67            return this;
68        }
69
70        public Intent build() {
71            return getCallIntent(
72                    mUri,
73                    mPhoneAccountHandle,
74                    mIsVideoCall ? VideoProfile.STATE_BIDIRECTIONAL : VideoProfile.STATE_AUDIO_ONLY,
75                    mCallInitiationType);
76        }
77    }
78
79    /**
80     * Create a call intent that can be used to place a call.
81     *
82     * @param uri Address to place the call to.
83     * @param accountHandle {@link PhoneAccountHandle} to place the call with.
84     * @param videoState Initial video state of the call.
85     * @param callIntiationType The UI affordance the call was initiated by.
86     * @return Call intent with provided extras and data.
87     */
88    public static Intent getCallIntent(
89            Uri uri, PhoneAccountHandle accountHandle, int videoState, int callIntiationType) {
90        final Intent intent = new Intent(CALL_ACTION, uri);
91        intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState);
92
93        final Bundle b = new Bundle();
94        b.putInt(EXTRA_CALL_INITIATION_TYPE, callIntiationType);
95        intent.putExtra(TelecomManager.EXTRA_OUTGOING_CALL_EXTRAS, b);
96
97        if (accountHandle != null) {
98            intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle);
99        }
100
101        return intent;
102    }
103
104    public static Intent getSendSmsIntent(CharSequence phoneNumber) {
105        return new Intent(Intent.ACTION_SENDTO, Uri.parse(SMS_URI_PREFIX + phoneNumber));
106    }
107
108    public static Intent getNewContactIntent() {
109        return new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
110    }
111
112    public static Intent getNewContactIntent(CharSequence phoneNumber) {
113        return getNewContactIntent(
114                null /* name */,
115                phoneNumber /* phoneNumber */,
116                NO_PHONE_TYPE);
117    }
118
119    public static Intent getNewContactIntent(
120            CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
121        Intent intent = getNewContactIntent();
122        populateContactIntent(intent, name, phoneNumber, phoneNumberType);
123        return intent;
124    }
125
126    public static Intent getAddToExistingContactIntent() {
127        Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
128        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
129        return intent;
130    }
131
132    public static Intent getAddToExistingContactIntent(CharSequence phoneNumber) {
133        return getAddToExistingContactIntent(
134                null /* name */,
135                phoneNumber /* phoneNumber */,
136                NO_PHONE_TYPE);
137    }
138
139    public static Intent getAddToExistingContactIntent(
140            CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
141        Intent intent = getAddToExistingContactIntent();
142        populateContactIntent(intent, name, phoneNumber, phoneNumberType);
143        return intent;
144    }
145
146    private static void populateContactIntent(
147            Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
148        if (phoneNumber != null) {
149            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber);
150        }
151        if (name != null) {
152            intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
153        }
154        if (phoneNumberType != NO_PHONE_TYPE) {
155            intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, phoneNumberType);
156        }
157    }
158}
159