Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.phone; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.AsyncResult; |
| 24 | import android.os.Handler; |
| 25 | import android.os.IBinder; |
| 26 | import android.os.Message; |
| 27 | import android.os.RemoteException; |
| 28 | import android.os.SystemProperties; |
| 29 | import android.util.Log; |
| 30 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 31 | import com.android.services.telephony.common.ICallHandlerService; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 32 | import com.android.services.telephony.common.ICallCommandService; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 35 | * This class is responsible for passing through call state changes to the CallHandlerService. |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 36 | */ |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 37 | public class CallHandlerServiceProxy extends Handler { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 38 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 39 | private static final String TAG = CallHandlerServiceProxy.class.getSimpleName(); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 40 | private static final boolean DBG = |
| 41 | (PhoneGlobals.DBG_LEVEL >= 1) && (SystemProperties.getInt("ro.debuggable", 0) == 1); |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame^] | 42 | |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 43 | |
| 44 | private Context mContext; |
| 45 | private CallStateMonitor mCallStateMonitor; |
| 46 | private ServiceConnection mConnection; |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 47 | private ICallHandlerService mCallHandlerService; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 48 | private CallCommandService mCallCommandService; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 49 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 50 | public CallHandlerServiceProxy(Context context, CallStateMonitor callStateMonitor, |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 51 | CallCommandService callCommandService) { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 52 | mContext = context; |
| 53 | mCallStateMonitor = callStateMonitor; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 54 | mCallCommandService = callCommandService; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 55 | |
| 56 | mCallStateMonitor.addListener(this); |
| 57 | setupServiceConnection(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void handleMessage(Message msg) { |
| 62 | switch (msg.what) { |
| 63 | case CallStateMonitor.PHONE_NEW_RINGING_CONNECTION: |
| 64 | onNewRingingConnection((AsyncResult) msg.obj); |
| 65 | break; |
| 66 | |
| 67 | default: |
| 68 | //super.handleMessage(msg); |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /** |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 74 | * Sets up the connection with ICallHandlerService |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 75 | */ |
| 76 | private void setupServiceConnection() { |
| 77 | mConnection = new ServiceConnection() { |
| 78 | @Override |
| 79 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 80 | if (DBG) { |
| 81 | Log.d(TAG, "Service Connected"); |
| 82 | } |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 83 | onCallHandlerServiceConnected(ICallHandlerService.Stub.asInterface(service)); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void onServiceDisconnected(ComponentName className) { |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame^] | 88 | // TODO(klp): handle the case where the in call ui crashed or gets destroyed. |
| 89 | // In the near term, we need to re-bind to the service when ever it's gone. |
| 90 | // Longer term, we need a way to catch the crash and allow the users to choose |
| 91 | // a different in-call screen. |
| 92 | Log.e(TAG, "Yikes! no in call ui!"); |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 93 | mCallHandlerService = null; |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 94 | } |
| 95 | }; |
| 96 | |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 97 | Intent serviceIntent = new Intent(ICallHandlerService.class.getName()); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 98 | if (!mContext.bindService(serviceIntent, mConnection, Context.BIND_AUTO_CREATE)) { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 99 | Log.e(TAG, "Cound not bind to ICallHandlerService"); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 104 | * Called when the in-call UI service is connected. Send command interface to in-call. |
| 105 | */ |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 106 | private void onCallHandlerServiceConnected(ICallHandlerService CallHandlerService) { |
| 107 | mCallHandlerService = CallHandlerService; |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 108 | |
| 109 | try { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 110 | mCallHandlerService.setCallCommandService(mCallCommandService); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 111 | } catch (RemoteException e) { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 112 | Log.e(TAG, "Remote exception calling CallHandlerService::onConnected. " + e); |
Santos Cordon | cba1b44 | 2013-07-18 12:43:58 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 117 | * Send notification of a new incoming call. |
| 118 | */ |
| 119 | private void onNewRingingConnection(AsyncResult result) { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 120 | if (mCallHandlerService != null) { |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 121 | try { |
Santos Cordon | 345350e | 2013-07-19 17:16:14 -0700 | [diff] [blame] | 122 | mCallHandlerService.onIncomingCall(0); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 123 | } catch (RemoteException e) { |
| 124 | Log.e(TAG, "Remote exception handling onIncomingCall:" + e); |
| 125 | } |
Chiao Cheng | e41661c | 2013-07-23 13:28:26 -0700 | [diff] [blame^] | 126 | } else { |
| 127 | Log.wtf(TAG, "Call handle service has not connected! Cannot accept incoming call."); |
Santos Cordon | 89647a6 | 2013-07-16 13:38:09 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | } |