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.contacts.common;
18
19import android.content.ComponentName;
20import android.content.Intent;
21import android.net.Uri;
22import android.telephony.PhoneNumberUtils;
23
24import com.android.phone.common.PhoneConstants;
25
26/**
27 * Utilities related to calls.
28 */
29public class CallUtil {
30
31    public static final String SCHEME_TEL = "tel";
32    public static final String SCHEME_SMSTO = "smsto";
33    public static final String SCHEME_MAILTO = "mailto";
34    public static final String SCHEME_IMTO = "imto";
35    public static final String SCHEME_SIP = "sip";
36
37    public static final ComponentName CALL_INTENT_DESTINATION = new ComponentName(
38            "com.android.phone", "com.android.phone.PrivilegedOutgoingCallBroadcaster");
39
40    /**
41     * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
42     * automatically.
43     */
44    public static Intent getCallIntent(String number) {
45        return getCallIntent(number, null);
46    }
47
48    /**
49     * Return an Intent for making a phone call. A given Uri will be used as is (without any
50     * sanity check).
51     */
52    public static Intent getCallIntent(Uri uri) {
53        return getCallIntent(uri, null);
54    }
55
56    /**
57     * A variant of {@link #getCallIntent(String)} but also accept a call origin. For more
58     * information about call origin, see comments in Phone package (PhoneApp).
59     */
60    public static Intent getCallIntent(String number, String callOrigin) {
61        return getCallIntent(getCallUri(number), callOrigin);
62    }
63
64    /**
65     * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call origin. For more
66     * information about call origin, see comments in Phone package (PhoneApp).
67     */
68    public static Intent getCallIntent(Uri uri, String callOrigin) {
69        final Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, uri);
70        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
71        if (callOrigin != null) {
72            intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
73        }
74
75        // Set phone as an explicit component of CALL_PRIVILEGED intent.
76        // Setting destination explicitly prevents other apps from capturing this Intent since,
77        // unlike SendBroadcast, there is no API for specifying a permission on startActivity.
78        intent.setComponent(CALL_INTENT_DESTINATION);
79
80        return intent;
81    }
82
83    /**
84     * Return Uri with an appropriate scheme, accepting Voicemail, SIP, and usual phone call
85     * numbers.
86     */
87    public static Uri getCallUri(String number) {
88        if (PhoneNumberUtils.isUriNumber(number)) {
89             return Uri.fromParts(SCHEME_SIP, number, null);
90        }
91        return Uri.fromParts(SCHEME_TEL, number, null);
92     }
93}
94