Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.telecomm; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.content.Intent; |
| 21 | import android.telecomm.CallService; |
| 22 | import android.telecomm.CallState; |
| 23 | import android.telecomm.TelecommConstants; |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 24 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 25 | import android.telephony.TelephonyManager; |
| 26 | |
| 27 | /** |
| 28 | * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state |
| 29 | * changes. |
| 30 | */ |
| 31 | final class PhoneStateBroadcaster extends CallsManagerListenerBase { |
| 32 | @Override |
| 33 | public void onCallStateChanged(Call call, CallState oldState, CallState newState) { |
| 34 | final String phoneState; |
| 35 | switch (newState) { |
| 36 | case DIALING: |
| 37 | case ACTIVE: |
| 38 | case ON_HOLD: |
| 39 | phoneState = TelephonyManager.EXTRA_STATE_OFFHOOK; |
| 40 | break; |
| 41 | case RINGING: |
| 42 | phoneState = TelephonyManager.EXTRA_STATE_RINGING; |
| 43 | break; |
| 44 | case ABORTED: |
| 45 | case DISCONNECTED: |
| 46 | phoneState = TelephonyManager.EXTRA_STATE_IDLE; |
| 47 | break; |
| 48 | default: |
| 49 | Log.w(this, "Call is in an unknown state (%s), not broadcasting: %s", |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 50 | newState, call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 51 | return; |
| 52 | } |
| 53 | sendPhoneStateChangedBroadcast(call, phoneState); |
| 54 | } |
| 55 | |
| 56 | private void sendPhoneStateChangedBroadcast(Call call, String phoneState) { |
| 57 | Log.v(this, "sendPhoneStateChangedBroadcast, call %s, phoneState: %s", call, phoneState); |
| 58 | |
| 59 | Intent intent = new Intent(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| 60 | intent.putExtra(TelephonyManager.EXTRA_STATE, phoneState); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 61 | |
| 62 | // Populate both, since the original API was needlessly complicated. |
Ihab Awad | 12de549 | 2014-06-19 10:41:41 -0700 | [diff] [blame] | 63 | if (call.getHandle() != null) { |
| 64 | String callHandle = call.getHandle().getSchemeSpecificPart(); |
| 65 | intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, callHandle); |
| 66 | // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL). |
| 67 | intent.putExtra(Intent.EXTRA_PHONE_NUMBER, callHandle); |
| 68 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 69 | |
| 70 | // TODO: Replace these with real constants once this API has been vetted. |
| 71 | CallServiceWrapper callService = call.getCallService(); |
| 72 | if (callService != null) { |
| 73 | intent.putExtra(CallService.class.getName(), callService.getComponentName()); |
| 74 | } |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 75 | |
| 76 | // TODO: Replace these with real constants once this API has been vetted. |
| 77 | int disconnectCause = call.getDisconnectCause(); |
| 78 | String disconnectMessage = call.getDisconnectMessage(); |
| 79 | if (disconnectCause != DisconnectCause.NOT_VALID) { |
| 80 | intent.putExtra(TelecommConstants.EXTRA_CALL_DISCONNECT_CAUSE, disconnectCause); |
| 81 | if (disconnectMessage == null) { |
| 82 | disconnectMessage = DisconnectCause.toString(disconnectCause); |
| 83 | } |
| 84 | } |
| 85 | if (disconnectMessage != null) { |
| 86 | intent.putExtra(TelecommConstants.EXTRA_CALL_DISCONNECT_MESSAGE, |
| 87 | call.getDisconnectMessage()); |
| 88 | } |
| 89 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 90 | TelecommApp.getInstance().sendBroadcast(intent, Manifest.permission.READ_PHONE_STATE); |
| 91 | Log.i(this, "Broadcasted state change: %s", phoneState); |
| 92 | } |
| 93 | } |