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; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 21 | import android.telecomm.CallState; |
Evan Charlton | b35fc31 | 2014-07-19 15:02:55 -0700 | [diff] [blame] | 22 | import android.telecomm.TelecommManager; |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 23 | import android.telephony.DisconnectCause; |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 24 | import android.telephony.TelephonyManager; |
| 25 | |
| 26 | /** |
| 27 | * Send a {@link TelephonyManager#ACTION_PHONE_STATE_CHANGED} broadcast when the call state |
| 28 | * changes. |
| 29 | */ |
| 30 | final 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", |
Sailesh Nepal | e59bb19 | 2014-04-01 18:33:59 -0700 | [diff] [blame] | 49 | newState, call); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 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); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 60 | |
| 61 | // Populate both, since the original API was needlessly complicated. |
Ihab Awad | 12de549 | 2014-06-19 10:41:41 -0700 | [diff] [blame] | 62 | if (call.getHandle() != null) { |
| 63 | String callHandle = call.getHandle().getSchemeSpecificPart(); |
| 64 | intent.putExtra(TelephonyManager.EXTRA_INCOMING_NUMBER, callHandle); |
| 65 | // TODO: See if we can add this (the current API only sets this on NEW_OUTGOING_CALL). |
| 66 | intent.putExtra(Intent.EXTRA_PHONE_NUMBER, callHandle); |
| 67 | } |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 68 | |
| 69 | // TODO: Replace these with real constants once this API has been vetted. |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 70 | ConnectionServiceWrapper connectionService = call.getConnectionService(); |
| 71 | if (connectionService != null) { |
| 72 | intent.putExtra( |
Evan Charlton | b35fc31 | 2014-07-19 15:02:55 -0700 | [diff] [blame] | 73 | TelecommManager.EXTRA_CONNECTION_SERVICE, |
Sailesh Nepal | c92c436 | 2014-07-04 18:33:21 -0700 | [diff] [blame] | 74 | connectionService.getComponentName()); |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 75 | } |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 76 | |
| 77 | // TODO: Replace these with real constants once this API has been vetted. |
| 78 | int disconnectCause = call.getDisconnectCause(); |
| 79 | String disconnectMessage = call.getDisconnectMessage(); |
| 80 | if (disconnectCause != DisconnectCause.NOT_VALID) { |
Evan Charlton | b35fc31 | 2014-07-19 15:02:55 -0700 | [diff] [blame] | 81 | intent.putExtra(TelecommManager.EXTRA_CALL_DISCONNECT_CAUSE, disconnectCause); |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 82 | if (disconnectMessage == null) { |
| 83 | disconnectMessage = DisconnectCause.toString(disconnectCause); |
| 84 | } |
| 85 | } |
| 86 | if (disconnectMessage != null) { |
Evan Charlton | b35fc31 | 2014-07-19 15:02:55 -0700 | [diff] [blame] | 87 | intent.putExtra(TelecommManager.EXTRA_CALL_DISCONNECT_MESSAGE, |
Sailesh Nepal | 53fb8c1 | 2014-04-01 23:38:39 -0700 | [diff] [blame] | 88 | call.getDisconnectMessage()); |
| 89 | } |
| 90 | |
Sailesh Nepal | 810735e | 2014-03-18 18:15:46 -0700 | [diff] [blame] | 91 | TelecommApp.getInstance().sendBroadcast(intent, Manifest.permission.READ_PHONE_STATE); |
| 92 | Log.i(this, "Broadcasted state change: %s", phoneState); |
| 93 | } |
| 94 | } |