CallUtil.java revision 7e558dcdb3af3f8a1ef57f0c445d4ef5172ee935
1cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng/*
2cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * Copyright (C) 2012 The Android Open Source Project
3cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng *
4cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
5cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * you may not use this file except in compliance with the License.
6cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * You may obtain a copy of the License at
7cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng *
8cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
9cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng *
10cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * Unless required by applicable law or agreed to in writing, software
11cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
12cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * See the License for the specific language governing permissions and
14cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * limitations under the License
15cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng */
16cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
17cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Chengpackage com.android.contacts.common;
18cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
197e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Leeimport android.content.Context;
20cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Chengimport android.content.Intent;
21cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Chengimport android.net.Uri;
227e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Leeimport android.phone.PhoneManager;
230dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charltonimport android.telecomm.PhoneAccountHandle;
241227b2a7a6d0ae8bfa4cf2065bed4d5f9e341e37Evan Charltonimport android.telecomm.TelecommManager;
250a221fc115a95ebb131eb15e081e163baf9c10a2Ihab Awadimport android.telecomm.VideoProfile;
26cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
279e8e7cb9ec53b46ff476336e1b40f7dfa6ccb61cYorke Leeimport com.android.contacts.common.util.PhoneNumberHelper;
28cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Chengimport com.android.phone.common.PhoneConstants;
29cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
30cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng/**
31cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng * Utilities related to calls.
32cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng */
33cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Chengpublic class CallUtil {
34cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
35cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static final String SCHEME_TEL = "tel";
36cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static final String SCHEME_SMSTO = "smsto";
37cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static final String SCHEME_MAILTO = "mailto";
38cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static final String SCHEME_IMTO = "imto";
39cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static final String SCHEME_SIP = "sip";
40cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
41cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    /**
42cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
43cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * automatically.
44cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     */
45cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static Intent getCallIntent(String number) {
46ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen        return getCallIntent(number, null, null);
47cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    }
48cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
49cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    /**
50cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * Return an Intent for making a phone call. A given Uri will be used as is (without any
51cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * sanity check).
52cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     */
53cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static Intent getCallIntent(Uri uri) {
54ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen        return getCallIntent(uri, null, null);
55cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    }
56cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
57cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    /**
58ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     * A variant of {@link #getCallIntent(String)} but also accept a call origin.
59ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     * For more information about call origin, see comments in Phone package (PhoneApp).
60cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     */
61cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static Intent getCallIntent(String number, String callOrigin) {
62ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen        return getCallIntent(getCallUri(number), callOrigin, null);
63cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    }
64cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
65cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    /**
66e0a273a872611569b1e04cb2204febaca65acea9Ihab Awad     * A variant of {@link #getCallIntent(String)} but also include {@code Account}.
67cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     */
688fe111fccff65b89a5d3d8a01cfdf5c28e9becd9Evan Charlton    public static Intent getCallIntent(String number, PhoneAccountHandle accountHandle) {
698fe111fccff65b89a5d3d8a01cfdf5c28e9becd9Evan Charlton        return getCallIntent(number, null, accountHandle);
70ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    }
71ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen
72ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    /**
73e0a273a872611569b1e04cb2204febaca65acea9Ihab Awad     * A variant of {@link #getCallIntent(android.net.Uri)} but also include {@code Account}.
74ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     */
750dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton    public static Intent getCallIntent(Uri uri, PhoneAccountHandle accountHandle) {
760dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        return getCallIntent(uri, null, accountHandle);
77ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    }
78ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen
79ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    /**
80e0a273a872611569b1e04cb2204febaca65acea9Ihab Awad     * A variant of {@link #getCallIntent(String, String)} but also include {@code Account}.
81ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     */
820dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton    public static Intent getCallIntent(
830dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton            String number, String callOrigin, PhoneAccountHandle accountHandle) {
840dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        return getCallIntent(getCallUri(number), callOrigin, accountHandle);
85ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    }
86ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen
87ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen    /**
88ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call
89e0a273a872611569b1e04cb2204febaca65acea9Ihab Awad     * origin and {@code Account}.
90ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     * For more information about call origin, see comments in Phone package (PhoneApp).
91ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen     */
920dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton    public static Intent getCallIntent(
930dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton            Uri uri, String callOrigin, PhoneAccountHandle accountHandle) {
940dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        return getCallIntent(uri, callOrigin, accountHandle,
950a221fc115a95ebb131eb15e081e163baf9c10a2Ihab Awad                VideoProfile.VideoState.AUDIO_ONLY);
96c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    }
97c18a7548cc07635f52575309d28a1f465748b717Andrew Lee
98c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    /**
99c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     * A variant of {@link #getCallIntent(String, String)} for starting a video call.
100c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     */
101c095aee097899239c8ec427cc640f9870edad1eeNancy Chen    public static Intent getVideoCallIntent(String number, String callOrigin) {
102c095aee097899239c8ec427cc640f9870edad1eeNancy Chen        return getCallIntent(getCallUri(number), callOrigin, null,
1030a221fc115a95ebb131eb15e081e163baf9c10a2Ihab Awad                VideoProfile.VideoState.BIDIRECTIONAL);
104c095aee097899239c8ec427cc640f9870edad1eeNancy Chen    }
105c095aee097899239c8ec427cc640f9870edad1eeNancy Chen
106c095aee097899239c8ec427cc640f9870edad1eeNancy Chen    /**
1070dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton     * A variant of {@link #getCallIntent(String, String, android.telecomm.PhoneAccountHandle)} for
1080dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton     * starting a video call.
109c095aee097899239c8ec427cc640f9870edad1eeNancy Chen     */
110c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    public static Intent getVideoCallIntent(
1110dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton            String number, String callOrigin, PhoneAccountHandle accountHandle) {
1120dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        return getCallIntent(getCallUri(number), callOrigin, accountHandle,
1130a221fc115a95ebb131eb15e081e163baf9c10a2Ihab Awad                VideoProfile.VideoState.BIDIRECTIONAL);
114c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    }
115c18a7548cc07635f52575309d28a1f465748b717Andrew Lee
116c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    /**
1170dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton     * A variant of {@link #getCallIntent(String, String, android.telecomm.PhoneAccountHandle)} for
1180dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton     * starting a video call.
119a186d040e3d998381beb9d107fd4ab72929b01e2Tyler Gunn     */
1200dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton    public static Intent getVideoCallIntent(String number, PhoneAccountHandle accountHandle) {
1210dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        return getVideoCallIntent(number, null, accountHandle);
122a186d040e3d998381beb9d107fd4ab72929b01e2Tyler Gunn    }
123a186d040e3d998381beb9d107fd4ab72929b01e2Tyler Gunn
124a186d040e3d998381beb9d107fd4ab72929b01e2Tyler Gunn    /**
125c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     * A variant of {@link #getCallIntent(android.net.Uri)} but also accept a call
126c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     * origin and {@code Account} and {@code VideoCallProfile} state.
127c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     * For more information about call origin, see comments in Phone package (PhoneApp).
128c18a7548cc07635f52575309d28a1f465748b717Andrew Lee     */
129c18a7548cc07635f52575309d28a1f465748b717Andrew Lee    public static Intent getCallIntent(
1300dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton            Uri uri, String callOrigin, PhoneAccountHandle accountHandle, int videoState) {
131a80db7afac980c6c2d0253702c6cad7ceeefab0eYorke Lee        final Intent intent = new Intent(Intent.ACTION_CALL, uri);
1321227b2a7a6d0ae8bfa4cf2065bed4d5f9e341e37Evan Charlton        intent.putExtra(TelecommManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState);
133cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng        if (callOrigin != null) {
134cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng            intent.putExtra(PhoneConstants.EXTRA_CALL_ORIGIN, callOrigin);
135cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng        }
1360dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton        if (accountHandle != null) {
1370dc7d4b5d2951ee2787b878bd924b385968c5f35Evan Charlton            intent.putExtra(TelecommManager.EXTRA_PHONE_ACCOUNT_HANDLE, accountHandle);
138ac51a8788f5ecf755784d00e1ef40a1a87b437c1Nancy Chen        }
139dd084b0b99d42835d79ea931fbee3a9a84a5f60eSantos Cordon
140cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng        return intent;
141cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    }
142cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng
143cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    /**
144cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * Return Uri with an appropriate scheme, accepting Voicemail, SIP, and usual phone call
145cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     * numbers.
146cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     */
147cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng    public static Uri getCallUri(String number) {
1489e8e7cb9ec53b46ff476336e1b40f7dfa6ccb61cYorke Lee        if (PhoneNumberHelper.isUriNumber(number)) {
149cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng             return Uri.fromParts(SCHEME_SIP, number, null);
150cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng        }
151cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng        return Uri.fromParts(SCHEME_TEL, number, null);
152cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng     }
15356046089a707f905482ea01eac697d36fcde4556Paul Soulos
1547e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee    public static boolean isVideoEnabled(Context context) {
1557e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee        PhoneManager phoneMgr = (PhoneManager) context.getSystemService(Context.PHONE_SERVICE);
1567e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee        if (phoneMgr == null) {
1577e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee            return false;
1587e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee        }
1597e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee
1607e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee        // TODO: Check phoneManager for value instead.
1617e558dcdb3af3f8a1ef57f0c445d4ef5172ee935Andrew Lee        // return phoneMgr.isVideoEnabled();
16256046089a707f905482ea01eac697d36fcde4556Paul Soulos        return false;
16356046089a707f905482ea01eac697d36fcde4556Paul Soulos    }
164cb5b270471f8cd27ba666268d11c60ffd9819b95Chiao Cheng}
165