James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 19 | import android.net.Uri; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 20 | import android.os.Binder; |
| 21 | import android.os.RemoteException; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 22 | import android.os.ServiceSpecificException; |
Brad Ebinger | b7a866a | 2020-01-22 17:51:55 -0800 | [diff] [blame^] | 23 | import android.telephony.SubscriptionManager; |
Peter Wang | c035ce4 | 2020-01-08 21:00:22 -0800 | [diff] [blame] | 24 | import android.telephony.TelephonyFrameworkInitializer; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 25 | import android.telephony.ims.ImsException; |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 26 | import android.telephony.ims.RegistrationManager; |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 27 | import android.telephony.ims.aidl.IImsCapabilityCallback; |
| 28 | import android.telephony.ims.aidl.IImsRcsController; |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 29 | import android.telephony.ims.aidl.IImsRegistrationCallback; |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 30 | import android.telephony.ims.aidl.IRcsUceControllerCallback; |
| 31 | import android.telephony.ims.feature.RcsFeature; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 32 | import android.telephony.ims.stub.ImsRegistrationImplBase; |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 33 | import android.util.Log; |
| 34 | |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 35 | import com.android.ims.ImsManager; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 36 | import com.android.ims.RcsFeatureManager; |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 37 | import com.android.internal.telephony.IIntegerConsumer; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 38 | import com.android.internal.telephony.Phone; |
| 39 | import com.android.internal.telephony.imsphone.ImsPhone; |
James.cf Lin | c9f35a4 | 2020-01-15 02:35:22 +0800 | [diff] [blame] | 40 | import com.android.services.telephony.rcs.TelephonyRcsService; |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 41 | |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 42 | import java.util.List; |
| 43 | |
| 44 | /** |
| 45 | * Implementation of the IImsRcsController interface. |
| 46 | */ |
| 47 | public class ImsRcsController extends IImsRcsController.Stub { |
| 48 | private static final String TAG = "ImsRcsController"; |
| 49 | |
| 50 | /** The singleton instance. */ |
| 51 | private static ImsRcsController sInstance; |
| 52 | |
| 53 | private PhoneGlobals mApp; |
James.cf Lin | c9f35a4 | 2020-01-15 02:35:22 +0800 | [diff] [blame] | 54 | private TelephonyRcsService mRcsService; |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Initialize the singleton ImsRcsController instance. |
| 58 | * This is only done once, at startup, from PhoneApp.onCreate(). |
| 59 | */ |
| 60 | static ImsRcsController init(PhoneGlobals app) { |
| 61 | synchronized (ImsRcsController.class) { |
| 62 | if (sInstance == null) { |
| 63 | sInstance = new ImsRcsController(app); |
| 64 | } else { |
| 65 | Log.wtf(TAG, "init() called multiple times! sInstance = " + sInstance); |
| 66 | } |
| 67 | return sInstance; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** Private constructor; @see init() */ |
| 72 | private ImsRcsController(PhoneGlobals app) { |
| 73 | Log.i(TAG, "ImsRcsController"); |
| 74 | mApp = app; |
Peter Wang | c035ce4 | 2020-01-08 21:00:22 -0800 | [diff] [blame] | 75 | TelephonyFrameworkInitializer |
| 76 | .getTelephonyServiceManager().getTelephonyImsServiceRegisterer().register(this); |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 77 | } |
| 78 | |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 79 | /** |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 80 | * Register a IImsRegistrationCallback to receive IMS network registration state. |
| 81 | */ |
| 82 | @Override |
| 83 | public void registerImsRegistrationCallback(int subId, IImsRegistrationCallback callback) |
| 84 | throws RemoteException { |
| 85 | enforceReadPrivilegedPermission("registerImsRegistrationCallback"); |
| 86 | final long token = Binder.clearCallingIdentity(); |
| 87 | try { |
| 88 | getRcsFeatureManager(subId).registerImsRegistrationCallback(callback); |
| 89 | } catch (com.android.ims.ImsException e) { |
| 90 | Log.e(TAG, "registerImsRegistrationCallback: sudId=" + subId + ", " + e.getMessage()); |
| 91 | throw new ServiceSpecificException(e.getCode()); |
| 92 | } finally { |
| 93 | Binder.restoreCallingIdentity(token); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Removes an existing {@link RegistrationCallback}. |
| 99 | */ |
| 100 | @Override |
| 101 | public void unregisterImsRegistrationCallback(int subId, IImsRegistrationCallback callback) { |
| 102 | enforceReadPrivilegedPermission("unregisterImsRegistrationCallback"); |
| 103 | final long token = Binder.clearCallingIdentity(); |
| 104 | try { |
| 105 | getRcsFeatureManager(subId).unregisterImsRegistrationCallback(callback); |
| 106 | } catch (ServiceSpecificException e) { |
| 107 | Log.e(TAG, "unregisterImsRegistrationCallback: error=" + e.errorCode); |
| 108 | } finally { |
| 109 | Binder.restoreCallingIdentity(token); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the IMS service registration state for the RcsFeature associated with this sub id. |
| 115 | */ |
| 116 | @Override |
| 117 | public void getImsRcsRegistrationState(int subId, IIntegerConsumer consumer) { |
| 118 | enforceReadPrivilegedPermission("getImsRcsRegistrationState"); |
| 119 | final long token = Binder.clearCallingIdentity(); |
| 120 | try { |
| 121 | getImsPhone(subId).getImsRcsRegistrationState(regState -> { |
| 122 | try { |
| 123 | consumer.accept((regState == null) |
| 124 | ? RegistrationManager.REGISTRATION_STATE_NOT_REGISTERED : regState); |
| 125 | } catch (RemoteException e) { |
| 126 | Log.w(TAG, "getImsRcsRegistrationState: callback is not available."); |
| 127 | } |
| 128 | }); |
| 129 | } finally { |
| 130 | Binder.restoreCallingIdentity(token); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Gets the Transport Type associated with the current IMS RCS registration. |
| 136 | */ |
| 137 | @Override |
| 138 | public void getImsRcsRegistrationTransportType(int subId, IIntegerConsumer consumer) { |
| 139 | enforceReadPrivilegedPermission("getImsRcsRegistrationTransportType"); |
| 140 | final long token = Binder.clearCallingIdentity(); |
| 141 | try { |
| 142 | getImsPhone(subId).getImsRcsRegistrationTech(regTech -> { |
| 143 | // Convert registration tech from ImsRegistrationImplBase -> RegistrationManager |
| 144 | int regTechConverted = (regTech == null) |
| 145 | ? ImsRegistrationImplBase.REGISTRATION_TECH_NONE : regTech; |
| 146 | regTechConverted = RegistrationManager.IMS_REG_TO_ACCESS_TYPE_MAP.get( |
| 147 | regTechConverted); |
| 148 | try { |
| 149 | consumer.accept(regTechConverted); |
| 150 | } catch (RemoteException e) { |
| 151 | Log.w(TAG, "getImsRcsRegistrationTransportType: callback is not available."); |
| 152 | } |
| 153 | }); |
| 154 | } finally { |
| 155 | Binder.restoreCallingIdentity(token); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 160 | * Register a capability callback which will provide RCS availability updates for the |
| 161 | * subscription specified. |
| 162 | * |
| 163 | * @param subId the subscription ID |
| 164 | * @param callback The ImsCapabilityCallback to be registered. |
| 165 | */ |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 166 | @Override |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 167 | public void registerRcsAvailabilityCallback(int subId, IImsCapabilityCallback callback) |
| 168 | throws RemoteException { |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 169 | enforceReadPrivilegedPermission("registerRcsAvailabilityCallback"); |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 170 | final long token = Binder.clearCallingIdentity(); |
| 171 | try { |
| 172 | getRcsFeatureManager(subId).registerRcsAvailabilityCallback(callback); |
| 173 | } catch (com.android.ims.ImsException e) { |
| 174 | Log.e(TAG, "registerRcsAvailabilityCallback: sudId=" + subId + ", " + e.getMessage()); |
| 175 | throw new ServiceSpecificException(e.getCode()); |
| 176 | } finally { |
| 177 | Binder.restoreCallingIdentity(token); |
| 178 | } |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 179 | } |
| 180 | |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 181 | /** |
| 182 | * Remove the registered capability callback. |
| 183 | * |
| 184 | * @param subId the subscription ID |
| 185 | * @param callback The ImsCapabilityCallback to be removed. |
| 186 | */ |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 187 | @Override |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 188 | public void unregisterRcsAvailabilityCallback(int subId, IImsCapabilityCallback callback) { |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 189 | enforceReadPrivilegedPermission("unregisterRcsAvailabilityCallback"); |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 190 | final long token = Binder.clearCallingIdentity(); |
| 191 | try { |
| 192 | getRcsFeatureManager(subId).unregisterRcsAvailabilityCallback(callback); |
| 193 | } catch (com.android.ims.ImsException e) { |
| 194 | Log.e(TAG, "unregisterRcsAvailabilityCallback: sudId=" + subId + "," + e.getMessage()); |
| 195 | } finally { |
| 196 | Binder.restoreCallingIdentity(token); |
| 197 | } |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 198 | } |
| 199 | |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 200 | /** |
| 201 | * Query for the capability of an IMS RCS service |
| 202 | * |
| 203 | * @param subId the subscription ID |
| 204 | * @param capability the RCS capability to query. |
| 205 | * @param radioTech the radio tech that this capability failed for |
| 206 | * @return true if the RCS capability is capable for this subscription, false otherwise. |
| 207 | */ |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 208 | @Override |
| 209 | public boolean isCapable(int subId, |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 210 | @RcsFeature.RcsImsCapabilities.RcsImsCapabilityFlag int capability, |
| 211 | @ImsRegistrationImplBase.ImsRegistrationTech int radioTech) { |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 212 | enforceReadPrivilegedPermission("isCapable"); |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 213 | final long token = Binder.clearCallingIdentity(); |
| 214 | try { |
| 215 | return getRcsFeatureManager(subId).isCapable(capability, radioTech); |
| 216 | } catch (com.android.ims.ImsException e) { |
| 217 | Log.e(TAG, "isCapable: sudId=" + subId |
| 218 | + ", capability=" + capability + ", " + e.getMessage()); |
| 219 | return false; |
| 220 | } finally { |
| 221 | Binder.restoreCallingIdentity(token); |
| 222 | } |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 223 | } |
| 224 | |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 225 | /** |
| 226 | * Query the availability of an IMS RCS capability. |
| 227 | * |
| 228 | * @param subId the subscription ID |
| 229 | * @param capability the RCS capability to query. |
| 230 | * @return true if the RCS capability is currently available for the associated subscription, |
| 231 | * false otherwise. |
| 232 | */ |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 233 | @Override |
| 234 | public boolean isAvailable(int subId, |
| 235 | @RcsFeature.RcsImsCapabilities.RcsImsCapabilityFlag int capability) { |
| 236 | enforceReadPrivilegedPermission("isAvailable"); |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 237 | final long token = Binder.clearCallingIdentity(); |
| 238 | try { |
| 239 | return getRcsFeatureManager(subId).isAvailable(capability); |
| 240 | } catch (com.android.ims.ImsException e) { |
| 241 | Log.e(TAG, "isAvailable: sudId=" + subId |
| 242 | + ", capability=" + capability + ", " + e.getMessage()); |
| 243 | return false; |
| 244 | } finally { |
| 245 | Binder.restoreCallingIdentity(token); |
| 246 | } |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public void requestCapabilities(int subId, List<Uri> contactNumbers, |
| 251 | IRcsUceControllerCallback c) { |
| 252 | enforceReadPrivilegedPermission("requestCapabilities"); |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public int getUcePublishState(int subId) { |
| 257 | enforceReadPrivilegedPermission("getUcePublishState"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public boolean isUceSettingEnabled(int subId) { |
| 263 | enforceReadPrivilegedPermission("isUceSettingEnabled"); |
Brad Ebinger | b7a866a | 2020-01-22 17:51:55 -0800 | [diff] [blame^] | 264 | return SubscriptionManager.getBooleanSubscriptionProperty(subId, |
| 265 | SubscriptionManager.IMS_RCS_UCE_ENABLED, false /*defaultValue*/, mApp); |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void setUceSettingEnabled(int subId, boolean isEnabled) { |
| 270 | enforceModifyPermission(); |
Brad Ebinger | b7a866a | 2020-01-22 17:51:55 -0800 | [diff] [blame^] | 271 | SubscriptionManager.setSubscriptionProperty(subId, SubscriptionManager.IMS_RCS_UCE_ENABLED, |
| 272 | (isEnabled ? "1" : "0")); |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Make sure either called from same process as self (phone) or IPC caller has read privilege. |
| 277 | * |
| 278 | * @throws SecurityException if the caller does not have the required permission |
| 279 | */ |
| 280 | private void enforceReadPrivilegedPermission(String message) { |
| 281 | mApp.enforceCallingOrSelfPermission( |
| 282 | android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Make sure the caller has the MODIFY_PHONE_STATE permission. |
| 287 | * |
| 288 | * @throws SecurityException if the caller does not have the required permission |
| 289 | */ |
| 290 | private void enforceModifyPermission() { |
| 291 | mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null); |
| 292 | } |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 293 | |
| 294 | /** |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 295 | * Retrieve ImsPhone instance. |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 296 | * |
| 297 | * @param subId the subscription ID |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 298 | * @return The ImsPhone instance |
| 299 | * @throws ServiceSpecificException if getting ImsPhone instance failed. |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 300 | */ |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 301 | private ImsPhone getImsPhone(int subId) { |
| 302 | if (!ImsManager.isImsSupportedOnDevice(mApp)) { |
| 303 | throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION, |
| 304 | "IMS is not available on device."); |
| 305 | } |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 306 | Phone phone = PhoneGlobals.getPhone(subId); |
| 307 | if (phone == null) { |
| 308 | throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION, |
| 309 | "Invalid subscription Id: " + subId); |
| 310 | } |
| 311 | ImsPhone imsPhone = (ImsPhone) phone.getImsPhone(); |
| 312 | if (imsPhone == null) { |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 313 | throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE, |
| 314 | "Cannot find ImsPhone instance: " + subId); |
| 315 | } |
| 316 | return imsPhone; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Retrieve RcsFeatureManager instance. |
| 321 | * |
| 322 | * @param subId the subscription ID |
| 323 | * @return The RcsFeatureManager instance |
| 324 | * @throws ServiceSpecificException if getting RcsFeatureManager instance failed. |
| 325 | */ |
| 326 | private RcsFeatureManager getRcsFeatureManager(int subId) { |
| 327 | if (!ImsManager.isImsSupportedOnDevice(mApp)) { |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 328 | throw new ServiceSpecificException(ImsException.CODE_ERROR_UNSUPPORTED_OPERATION, |
James.cf Lin | dc2d542 | 2019-12-31 14:40:25 +0800 | [diff] [blame] | 329 | "IMS is not available on device."); |
| 330 | } |
| 331 | Phone phone = PhoneGlobals.getPhone(subId); |
| 332 | if (phone == null) { |
| 333 | throw new ServiceSpecificException(ImsException.CODE_ERROR_INVALID_SUBSCRIPTION, |
| 334 | "Invalid subscription Id: " + subId); |
| 335 | } |
| 336 | ImsPhone imsPhone = (ImsPhone) phone.getImsPhone(); |
| 337 | if (imsPhone == null) { |
| 338 | throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE, |
James.cf Lin | cad981c | 2019-12-10 20:37:56 +0800 | [diff] [blame] | 339 | "Cannot find ImsPhone instance: " + subId); |
| 340 | } |
| 341 | RcsFeatureManager rcsFeatureManager = imsPhone.getRcsManager(); |
| 342 | if (rcsFeatureManager == null) { |
| 343 | throw new ServiceSpecificException(ImsException.CODE_ERROR_SERVICE_UNAVAILABLE, |
| 344 | "Cannot find RcsFeatureManager instance: " + subId); |
| 345 | } |
| 346 | return rcsFeatureManager; |
| 347 | } |
James.cf Lin | c9f35a4 | 2020-01-15 02:35:22 +0800 | [diff] [blame] | 348 | |
| 349 | void setRcsService(TelephonyRcsService rcsService) { |
| 350 | mRcsService = rcsService; |
| 351 | } |
James.cf Lin | af3183c | 2019-10-24 00:59:00 +0800 | [diff] [blame] | 352 | } |