Santos Cordon | b64c150 | 2014-05-21 21:21:49 -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 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 19 | import android.app.AppOpsManager; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 23 | import android.net.Uri; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 24 | import android.os.Binder; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 26 | import android.os.Looper; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 27 | import android.os.Message; |
| 28 | import android.os.ServiceManager; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 29 | import android.phone.PhoneManager; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 30 | import android.telecomm.CallState; |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 31 | import android.telecomm.PhoneAccount; |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 32 | import android.telecomm.PhoneAccountMetadata; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 33 | import android.telecomm.TelecommManager; |
| 34 | import android.telephony.TelephonyManager; |
| 35 | |
| 36 | import com.android.internal.telecomm.ITelecommService; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 37 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
| 39 | import java.util.HashMap; |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 40 | import java.util.List; |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 41 | import java.util.Map; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Implementation of the ITelecomm interface. |
| 45 | */ |
| 46 | public class TelecommServiceImpl extends ITelecommService.Stub { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 47 | /** |
| 48 | * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the |
| 49 | * request after sending. The main thread will notify the request when it is complete. |
| 50 | */ |
| 51 | private static final class MainThreadRequest { |
| 52 | /** The result of the request that is run on the main thread */ |
| 53 | public Object result; |
| 54 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * A handler that processes messages on the main thread in the phone process. Since many |
| 58 | * of the Phone calls are not thread safe this is needed to shuttle the requests from the |
| 59 | * inbound binder threads to the main thread in the phone process. |
| 60 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 61 | private final class MainThreadHandler extends Handler { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public void handleMessage(Message msg) { |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 64 | if (msg.obj instanceof MainThreadRequest) { |
| 65 | MainThreadRequest request = (MainThreadRequest) msg.obj; |
| 66 | Object result = null; |
| 67 | switch (msg.what) { |
| 68 | case MSG_SILENCE_RINGER: |
| 69 | mCallsManager.getRinger().silence(); |
| 70 | break; |
| 71 | case MSG_SHOW_CALL_SCREEN: |
| 72 | mCallsManager.getInCallController().bringToForeground(msg.arg1 == 1); |
| 73 | break; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 74 | case MSG_END_CALL: |
| 75 | result = endCallInternal(); |
| 76 | break; |
| 77 | case MSG_ACCEPT_RINGING_CALL: |
| 78 | acceptRingingCallInternal(); |
| 79 | break; |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 80 | case MSG_CANCEL_MISSED_CALLS_NOTIFICATION: |
| 81 | mMissedCallNotifier.clearMissedCalls(); |
| 82 | break; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 83 | case MSG_IS_TTY_SUPPORTED: |
| 84 | result = mCallsManager.isTtySupported(); |
| 85 | break; |
| 86 | case MSG_GET_CURRENT_TTY_MODE: |
| 87 | result = mCallsManager.getCurrentTtyMode(); |
| 88 | break; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | if (result != null) { |
| 92 | request.result = result; |
| 93 | synchronized(request) { |
| 94 | request.notifyAll(); |
| 95 | } |
| 96 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 99 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 100 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 101 | /** Private constructor; @see init() */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 102 | private static final String TAG = TelecommServiceImpl.class.getSimpleName(); |
| 103 | |
| 104 | private static final String SERVICE_NAME = "telecomm"; |
| 105 | |
| 106 | private static final int MSG_SILENCE_RINGER = 1; |
| 107 | private static final int MSG_SHOW_CALL_SCREEN = 2; |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 108 | private static final int MSG_END_CALL = 3; |
| 109 | private static final int MSG_ACCEPT_RINGING_CALL = 4; |
| 110 | private static final int MSG_CANCEL_MISSED_CALLS_NOTIFICATION = 5; |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 111 | private static final int MSG_IS_TTY_SUPPORTED = 6; |
| 112 | private static final int MSG_GET_CURRENT_TTY_MODE = 7; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 113 | |
| 114 | /** The singleton instance. */ |
| 115 | private static TelecommServiceImpl sInstance; |
| 116 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 117 | private final MainThreadHandler mMainThreadHandler = new MainThreadHandler(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 118 | private final CallsManager mCallsManager = CallsManager.getInstance(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 119 | private final MissedCallNotifier mMissedCallNotifier; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 120 | private final AppOpsManager mAppOpsManager; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 121 | |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 122 | private TelecommServiceImpl(MissedCallNotifier missedCallNotifier) { |
| 123 | mMissedCallNotifier = missedCallNotifier; |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 124 | mAppOpsManager = |
| 125 | (AppOpsManager) TelecommApp.getInstance().getSystemService(Context.APP_OPS_SERVICE); |
| 126 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 127 | publish(); |
| 128 | } |
| 129 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Initialize the singleton TelecommServiceImpl instance. |
| 132 | * This is only done once, at startup, from TelecommApp.onCreate(). |
| 133 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 134 | static TelecommServiceImpl init(MissedCallNotifier missedCallNotifier) { |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 135 | synchronized (TelecommServiceImpl.class) { |
| 136 | if (sInstance == null) { |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 137 | sInstance = new TelecommServiceImpl(missedCallNotifier); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 138 | } else { |
| 139 | Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance); |
| 140 | } |
| 141 | return sInstance; |
| 142 | } |
| 143 | } |
| 144 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 145 | // |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 146 | // Implementation of the ITelecommService interface. |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 147 | // |
| 148 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 149 | private static Map<PhoneAccount, PhoneAccountMetadata> sMetadataByAccount = new HashMap<>(); |
| 150 | |
| 151 | static { |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 152 | // TODO (STOPSHIP): Static list of Accounts for testing and UX work only. |
| 153 | ComponentName componentName = new ComponentName( |
| 154 | "com.android.telecomm", |
| 155 | TelecommServiceImpl.class.getName()); // This field is a no-op |
| 156 | Context app = TelecommApp.getInstance(); |
| 157 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 158 | PhoneAccount[] accounts = new PhoneAccount[] { |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 159 | new PhoneAccount( |
| 160 | componentName, |
| 161 | "account0", |
| 162 | Uri.parse("tel:999-555-1212"), |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 163 | 0), |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 164 | new PhoneAccount( |
| 165 | componentName, |
| 166 | "account1", |
| 167 | Uri.parse("tel:333-111-2222"), |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 168 | 0), |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 169 | new PhoneAccount( |
| 170 | componentName, |
| 171 | "account2", |
| 172 | Uri.parse("mailto:two@example.com"), |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 173 | 0), |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 174 | new PhoneAccount( |
| 175 | componentName, |
| 176 | "account3", |
| 177 | Uri.parse("mailto:three@example.com"), |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 178 | 0) |
| 179 | }; |
| 180 | |
| 181 | sMetadataByAccount.put( |
| 182 | accounts[0], |
| 183 | new PhoneAccountMetadata( |
| 184 | accounts[0], |
| 185 | 0, |
| 186 | app.getString(R.string.test_account_0_label), |
| 187 | app.getString(R.string.test_account_0_short_description))); |
| 188 | sMetadataByAccount.put( |
| 189 | accounts[1], |
| 190 | new PhoneAccountMetadata( |
| 191 | accounts[1], |
| 192 | 0, |
| 193 | app.getString(R.string.test_account_1_label), |
| 194 | app.getString(R.string.test_account_1_short_description))); |
| 195 | sMetadataByAccount.put( |
| 196 | accounts[2], |
| 197 | new PhoneAccountMetadata( |
| 198 | accounts[2], |
| 199 | 0, |
| 200 | app.getString(R.string.test_account_2_label), |
| 201 | app.getString(R.string.test_account_2_short_description))); |
| 202 | sMetadataByAccount.put( |
| 203 | accounts[3], |
| 204 | new PhoneAccountMetadata( |
| 205 | accounts[3], |
| 206 | 0, |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 207 | app.getString(R.string.test_account_3_label), |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 208 | app.getString(R.string.test_account_3_short_description))); |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | @Override |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 212 | public List<PhoneAccount> getEnabledPhoneAccounts() { |
| 213 | return new ArrayList<>(sMetadataByAccount.keySet()); |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | @Override |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 217 | public PhoneAccountMetadata getPhoneAccountMetadata(PhoneAccount account) { |
| 218 | return sMetadataByAccount.get(account); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void registerPhoneAccount(PhoneAccount account, PhoneAccountMetadata metadata) { |
| 223 | enforceModifyPermissionOrCallingPackage(account.getComponentName().getPackageName()); |
| 224 | // TODO(santoscordon) -- IMPLEMENT ... |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void unregisterPhoneAccount(PhoneAccount account) { |
| 229 | enforceModifyPermissionOrCallingPackage(account.getComponentName().getPackageName()); |
| 230 | // TODO(santoscordon) -- IMPLEMENT ... |
| 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void clearAccounts(String packageName) { |
| 235 | enforceModifyPermissionOrCallingPackage(packageName); |
| 236 | // TODO(santoscordon) -- IMPLEMENT ... |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 239 | /** |
| 240 | * @see TelecommManager#silenceringer |
| 241 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 242 | @Override |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 243 | public void silenceRinger() { |
| 244 | Log.d(this, "silenceRinger"); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 245 | enforceModifyPermission(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 246 | sendRequestAsync(MSG_SILENCE_RINGER, 0); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 249 | /** |
| 250 | * @see TelecommManager#getDefaultPhoneApp |
| 251 | */ |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 252 | @Override |
| 253 | public ComponentName getDefaultPhoneApp() { |
| 254 | Resources resources = TelecommApp.getInstance().getResources(); |
| 255 | return new ComponentName( |
| 256 | resources.getString(R.string.ui_default_package), |
| 257 | resources.getString(R.string.dialer_default_class)); |
| 258 | } |
| 259 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 260 | /** |
| 261 | * @see TelecommManager#isInAPhoneCall |
| 262 | */ |
| 263 | @Override |
| 264 | public boolean isInAPhoneCall() { |
| 265 | enforceReadPermission(); |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 266 | // Do not use sendRequest() with this method since it could cause a deadlock with |
| 267 | // audio service, which we call into from the main thread: AudioManager.setMode(). |
| 268 | return mCallsManager.hasAnyCalls(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * @see TelecommManager#isRinging |
| 273 | */ |
| 274 | @Override |
| 275 | public boolean isRinging() { |
| 276 | enforceReadPermission(); |
Santos Cordon | 626697a | 2014-07-10 22:36:37 -0700 | [diff] [blame] | 277 | return mCallsManager.hasRingingCall(); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @see TelecommManager#endCall |
| 282 | */ |
| 283 | @Override |
| 284 | public boolean endCall() { |
| 285 | enforceModifyPermission(); |
| 286 | return (boolean) sendRequest(MSG_END_CALL); |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @see TelecommManager#acceptRingingCall |
| 291 | */ |
| 292 | @Override |
| 293 | public void acceptRingingCall() { |
| 294 | enforceModifyPermission(); |
| 295 | sendRequestAsync(MSG_ACCEPT_RINGING_CALL, 0); |
| 296 | } |
| 297 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 298 | /** |
| 299 | * @see PhoneManager#showCallScreen |
| 300 | */ |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 301 | @Override |
| 302 | public void showCallScreen(boolean showDialpad) { |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 303 | enforceReadPermissionOrDefaultDialer(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 304 | sendRequestAsync(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0); |
| 305 | } |
| 306 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 307 | /** |
| 308 | * @see PhoneManager#cancelMissedCallsNotification |
| 309 | */ |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 310 | @Override |
| 311 | public void cancelMissedCallsNotification() { |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 312 | enforceModifyPermissionOrDefaultDialer(); |
Santos Cordon | 64c7e96 | 2014-07-02 15:15:27 -0700 | [diff] [blame] | 313 | sendRequestAsync(MSG_CANCEL_MISSED_CALLS_NOTIFICATION, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 316 | /** |
| 317 | * @see PhoneManager#handlePinMmi |
| 318 | */ |
| 319 | @Override |
| 320 | public boolean handlePinMmi(String dialString) { |
| 321 | enforceModifyPermissionOrDefaultDialer(); |
| 322 | |
| 323 | // Switch identity so that TelephonyManager checks Telecomm's permissions instead. |
| 324 | long token = Binder.clearCallingIdentity(); |
| 325 | boolean retval = getTelephonyManager().handlePinMmi(dialString); |
| 326 | Binder.restoreCallingIdentity(token); |
| 327 | |
| 328 | return retval; |
| 329 | } |
| 330 | |
Sailesh Nepal | b88795a | 2014-07-15 14:53:27 -0700 | [diff] [blame] | 331 | /** |
| 332 | * @see TelecommManager#isTtySupported |
| 333 | */ |
| 334 | @Override |
| 335 | public boolean isTtySupported() { |
| 336 | enforceReadPermission(); |
| 337 | return (boolean) sendRequest(MSG_IS_TTY_SUPPORTED); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @see TelecommManager#getCurrentTtyMode |
| 342 | */ |
| 343 | @Override |
| 344 | public int getCurrentTtyMode() { |
| 345 | enforceReadPermission(); |
| 346 | return (int) sendRequest(MSG_GET_CURRENT_TTY_MODE); |
| 347 | } |
| 348 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 349 | // |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 350 | // Supporting methods for the ITelecommService interface implementation. |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 351 | // |
| 352 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 353 | private void acceptRingingCallInternal() { |
| 354 | Call call = mCallsManager.getFirstCallWithState(CallState.RINGING); |
| 355 | if (call != null) { |
| 356 | call.answer(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | private boolean endCallInternal() { |
| 361 | // Always operate on the foreground call if one exists, otherwise get the first call in |
| 362 | // priority order by call-state. |
| 363 | Call call = mCallsManager.getForegroundCall(); |
| 364 | if (call == null) { |
| 365 | call = mCallsManager.getFirstCallWithState( |
| 366 | CallState.ACTIVE, |
| 367 | CallState.DIALING, |
| 368 | CallState.RINGING, |
| 369 | CallState.ON_HOLD); |
| 370 | } |
| 371 | |
| 372 | if (call != null) { |
| 373 | if (call.getState() == CallState.RINGING) { |
| 374 | call.reject(false /* rejectWithMessage */, null); |
| 375 | } else { |
| 376 | call.disconnect(); |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | return false; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Make sure the caller has the MODIFY_PHONE_STATE permission. |
| 386 | * |
| 387 | * @throws SecurityException if the caller does not have the required permission |
| 388 | */ |
| 389 | private void enforceModifyPermission() { |
| 390 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 391 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
| 392 | } |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 393 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 394 | private void enforceModifyPermissionOrDefaultDialer() { |
| 395 | if (!isDefaultDialerCalling()) { |
| 396 | enforceModifyPermission(); |
| 397 | } |
| 398 | } |
| 399 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 400 | private void enforceModifyPermissionOrCallingPackage(String packageName) { |
| 401 | // TODO(santoscordon): Use a new telecomm permission for this instead of reusing modify. |
| 402 | try { |
| 403 | enforceModifyPermission(); |
| 404 | } catch (SecurityException e) { |
| 405 | enforceCallingPackage(packageName); |
| 406 | } |
| 407 | } |
| 408 | |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 409 | private void enforceReadPermission() { |
| 410 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 411 | android.Manifest.permission.READ_PHONE_STATE, null); |
Santos Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 412 | } |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 413 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 414 | private void enforceReadPermissionOrDefaultDialer() { |
| 415 | if (!isDefaultDialerCalling()) { |
| 416 | enforceReadPermission(); |
| 417 | } |
| 418 | } |
| 419 | |
Ihab Awad | 502e5fe | 2014-07-09 12:34:48 -0700 | [diff] [blame] | 420 | private void enforceCallingPackage(String packageName) { |
| 421 | mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); |
| 422 | } |
| 423 | |
Ihab Awad | 98a5560 | 2014-06-30 21:27:28 -0700 | [diff] [blame] | 424 | private void showCallScreenInternal(boolean showDialpad) { |
| 425 | CallsManager.getInstance().getInCallController().bringToForeground(showDialpad); |
| 426 | } |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 427 | |
Santos Cordon | 46592f0 | 2014-07-07 15:11:35 -0700 | [diff] [blame] | 428 | private boolean isDefaultDialerCalling() { |
| 429 | ComponentName defaultDialerComponent = getDefaultPhoneApp(); |
| 430 | if (defaultDialerComponent != null) { |
| 431 | try { |
| 432 | mAppOpsManager.checkPackage( |
| 433 | Binder.getCallingUid(), defaultDialerComponent.getPackageName()); |
| 434 | return true; |
| 435 | } catch (SecurityException e) { |
| 436 | Log.e(TAG, e, "Could not get default dialer."); |
| 437 | } |
| 438 | } |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | private TelephonyManager getTelephonyManager() { |
| 443 | return (TelephonyManager) |
| 444 | TelecommApp.getInstance().getSystemService(Context.TELEPHONY_SERVICE); |
| 445 | } |
| 446 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 447 | private void publish() { |
| 448 | Log.d(this, "publish: %s", this); |
| 449 | ServiceManager.addService(SERVICE_NAME, this); |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 450 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 451 | |
| 452 | private MainThreadRequest sendRequestAsync(int command, int arg1) { |
| 453 | MainThreadRequest request = new MainThreadRequest(); |
| 454 | mMainThreadHandler.obtainMessage(command, arg1, 0, request).sendToTarget(); |
| 455 | return request; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Posts the specified command to be executed on the main thread, waits for the request to |
| 460 | * complete, and returns the result. |
| 461 | */ |
| 462 | private Object sendRequest(int command) { |
| 463 | if (Looper.myLooper() == mMainThreadHandler.getLooper()) { |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 464 | MainThreadRequest request = new MainThreadRequest(); |
| 465 | mMainThreadHandler.handleMessage(mMainThreadHandler.obtainMessage(command, request)); |
| 466 | return request.result; |
| 467 | } else { |
| 468 | MainThreadRequest request = sendRequestAsync(command, 0); |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 469 | |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 470 | // Wait for the request to complete |
| 471 | synchronized (request) { |
| 472 | while (request.result == null) { |
| 473 | try { |
| 474 | request.wait(); |
| 475 | } catch (InterruptedException e) { |
| 476 | // Do nothing, go back and wait until the request is complete |
| 477 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 478 | } |
| 479 | } |
Sailesh Nepal | bca199f | 2014-07-02 15:57:44 -0700 | [diff] [blame] | 480 | return request.result; |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 481 | } |
Santos Cordon | 23baed3 | 2014-06-27 14:45:39 -0700 | [diff] [blame] | 482 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 483 | } |