blob: 572880205dea58c0fba67d8a6ed5d711533817ef [file] [log] [blame]
Sailesh Nepal810735e2014-03-18 18:15:46 -07001/*
2 * Copyright (C) 2014 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.telecomm;
18
19import android.Manifest;
20import android.content.Intent;
21import android.telecomm.CallService;
22import android.telecomm.CallState;
23import android.telecomm.TelecommConstants;
24import android.telephony.TelephonyManager;
25
26/**
27 * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state
28 * changes.
29 */
30final class PhoneStateBroadcaster extends CallsManagerListenerBase {
31 @Override
32 public void onCallStateChanged(Call call, CallState oldState, CallState newState) {
33 final String phoneState;
34 switch (newState) {
35 case DIALING:
36 case ACTIVE:
37 case ON_HOLD:
38 phoneState = TelephonyManager.EXTRA_STATE_OFFHOOK;
39 break;
40 case RINGING:
41 phoneState = TelephonyManager.EXTRA_STATE_RINGING;
42 break;
43 case ABORTED:
44 case DISCONNECTED:
45 phoneState = TelephonyManager.EXTRA_STATE_IDLE;
46 break;
47 default:
48 Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s",
49 newState, call.getId());
50 return;
51 }
52 sendPhoneStateChangedBroadcast(call, phoneState);
53 }
54
55 private void sendPhoneStateChangedBroadcast(Call call, String phoneState) {
56 Log.v(this, "sendPhoneStateChangedBroadcast, call %s, phoneState: %s", call, phoneState);
57
58 Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
59 intent.putExtra(TelephonyManager.EXTRA_STATE, phoneState);
60 // TODO: See if we can add this (the current API doesn't have a callId).
61 intent.putExtra(TelecommConstants.EXTRA_CALL_ID, call.getId());
62
63 // Populate both, since the original API was needlessly complicated.
Evan Charlton25d4e4b2014-03-20 07:47:42 -070064 String callHandle = call.getHandle().getSchemeSpecificPart();
65 intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, callHandle);
Sailesh Nepal810735e2014-03-18 18:15:46 -070066 // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL).
Evan Charlton25d4e4b2014-03-20 07:47:42 -070067 intent.putExtra(Intent.EXTRA_PHONE_NUMBER, callHandle);
Sailesh Nepal810735e2014-03-18 18:15:46 -070068
69 // TODO: Replace these with real constants once this API has been vetted.
70 CallServiceWrapper callService = call.getCallService();
71 if (callService != null) {
72 intent.putExtra(CallService.class.getName(), callService.getComponentName());
73 }
74 TelecommApp.getInstance().sendBroadcast(intent, Manifest.permission.READ_PHONE_STATE);
75 Log.i(this, "Broadcasted state change: %s", phoneState);
76 }
77}