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