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