| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.carrier; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.content.ComponentName; |
| 24 |
import android.content.Context; |
| 25 |
import android.content.Intent; |
| 26 |
import android.content.ServiceConnection; |
| 27 |
import android.net.Uri; |
| 28 |
import android.os.Binder; |
| 29 |
import android.os.IBinder; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.telephony.SmsMessage; |
| 32 |
|
| 33 |
import com.android.internal.util.Preconditions; |
| 34 |
|
| 35 |
import java.util.List; |
| 36 |
import java.util.Objects; |
| 37 |
import java.util.concurrent.Executor; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@SystemApi |
| 56 |
public final class CarrierMessagingServiceWrapper implements AutoCloseable { |
| 57 |
|
| 58 |
|
| 59 |
private volatile CarrierMessagingServiceConnection mCarrierMessagingServiceConnection; |
| 60 |
|
| 61 |
private volatile ICarrierMessagingService mICarrierMessagingService; |
| 62 |
private Runnable mOnServiceReadyCallback; |
| 63 |
private Executor mServiceReadyCallbackExecutor; |
| 64 |
private Context mContext; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
@SystemApi |
| 78 |
public boolean bindToCarrierMessagingService(@NonNull Context context, |
| 79 |
@NonNull String carrierPackageName, |
| 80 |
@NonNull @CallbackExecutor Executor executor, |
| 81 |
@NonNull Runnable onServiceReadyCallback) { |
| 82 |
Preconditions.checkState(mCarrierMessagingServiceConnection == null); |
| 83 |
Objects.requireNonNull(context); |
| 84 |
Objects.requireNonNull(carrierPackageName); |
| 85 |
Objects.requireNonNull(executor); |
| 86 |
Objects.requireNonNull(onServiceReadyCallback); |
| 87 |
|
| 88 |
Intent intent = new Intent(CarrierMessagingService.SERVICE_INTERFACE); |
| 89 |
intent.setPackage(carrierPackageName); |
| 90 |
mCarrierMessagingServiceConnection = new CarrierMessagingServiceConnection(); |
| 91 |
mOnServiceReadyCallback = onServiceReadyCallback; |
| 92 |
mServiceReadyCallbackExecutor = executor; |
| 93 |
mContext = context; |
| 94 |
return context.bindService(intent, mCarrierMessagingServiceConnection, |
| 95 |
Context.BIND_AUTO_CREATE); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@SystemApi |
| 104 |
public void disconnect() { |
| 105 |
Preconditions.checkNotNull(mCarrierMessagingServiceConnection); |
| 106 |
mContext.unbindService(mCarrierMessagingServiceConnection); |
| 107 |
mCarrierMessagingServiceConnection = null; |
| 108 |
mOnServiceReadyCallback = null; |
| 109 |
mServiceReadyCallbackExecutor = null; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
private void onServiceReady(ICarrierMessagingService carrierMessagingService) { |
| 118 |
mICarrierMessagingService = carrierMessagingService; |
| 119 |
if (mOnServiceReadyCallback != null && mServiceReadyCallbackExecutor != null) { |
| 120 |
final long identity = Binder.clearCallingIdentity(); |
| 121 |
try { |
| 122 |
mServiceReadyCallbackExecutor.execute(mOnServiceReadyCallback); |
| 123 |
} finally { |
| 124 |
Binder.restoreCallingIdentity(identity); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
@SystemApi |
| 144 |
public void receiveSms(@NonNull MessagePdu pdu, @NonNull @SmsMessage.Format String format, |
| 145 |
int destPort, int subId, @NonNull @CallbackExecutor final Executor executor, |
| 146 |
@NonNull final CarrierMessagingCallback callback) { |
| 147 |
if (mICarrierMessagingService != null) { |
| 148 |
try { |
| 149 |
mICarrierMessagingService.filterSms(pdu, format, destPort, subId, |
| 150 |
new CarrierMessagingCallbackInternal(callback, executor)); |
| 151 |
} catch (RemoteException e) { |
| 152 |
throw new RuntimeException(e); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
@SystemApi |
| 172 |
public void sendTextSms(@NonNull String text, int subId, @NonNull String destAddress, |
| 173 |
@CarrierMessagingService.SendRequest int sendSmsFlag, |
| 174 |
@NonNull @CallbackExecutor final Executor executor, |
| 175 |
@NonNull final CarrierMessagingCallback callback) { |
| 176 |
Objects.requireNonNull(mICarrierMessagingService); |
| 177 |
try { |
| 178 |
mICarrierMessagingService.sendTextSms(text, subId, destAddress, sendSmsFlag, |
| 179 |
new CarrierMessagingCallbackInternal(callback, executor)); |
| 180 |
} catch (RemoteException e) { |
| 181 |
throw new RuntimeException(e); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@SystemApi |
| 201 |
public void sendDataSms(@NonNull byte[] data, int subId, @NonNull String destAddress, |
| 202 |
int destPort, @CarrierMessagingService.SendRequest int sendSmsFlag, |
| 203 |
@NonNull @CallbackExecutor final Executor executor, |
| 204 |
@NonNull final CarrierMessagingCallback callback) { |
| 205 |
Objects.requireNonNull(mICarrierMessagingService); |
| 206 |
try { |
| 207 |
mICarrierMessagingService.sendDataSms(data, subId, destAddress, destPort, |
| 208 |
sendSmsFlag, new CarrierMessagingCallbackInternal( |
| 209 |
callback, executor)); |
| 210 |
} catch (RemoteException e) { |
| 211 |
throw new RuntimeException(e); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@SystemApi |
| 230 |
public void sendMultipartTextSms(@NonNull List<String> parts, int subId, |
| 231 |
@NonNull String destAddress, |
| 232 |
@CarrierMessagingService.SendRequest int sendSmsFlag, |
| 233 |
@NonNull @CallbackExecutor final Executor executor, |
| 234 |
@NonNull final CarrierMessagingCallback callback) { |
| 235 |
Objects.requireNonNull(mICarrierMessagingService); |
| 236 |
try { |
| 237 |
mICarrierMessagingService.sendMultipartTextSms(parts, subId, destAddress, |
| 238 |
sendSmsFlag, new CarrierMessagingCallbackInternal(callback, executor)); |
| 239 |
} catch (RemoteException e) { |
| 240 |
throw new RuntimeException(e); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
@SystemApi |
| 258 |
public void sendMms(@NonNull Uri pduUri, int subId, @NonNull Uri location, |
| 259 |
@NonNull @CallbackExecutor final Executor executor, |
| 260 |
@NonNull final CarrierMessagingCallback callback) { |
| 261 |
Objects.requireNonNull(mICarrierMessagingService); |
| 262 |
try { |
| 263 |
mICarrierMessagingService.sendMms(pduUri, subId, location, |
| 264 |
new CarrierMessagingCallbackInternal(callback, executor)); |
| 265 |
} catch (RemoteException e) { |
| 266 |
throw new RuntimeException(e); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
@SystemApi |
| 283 |
public void downloadMms(@NonNull Uri pduUri, int subId, @NonNull Uri location, |
| 284 |
@NonNull @CallbackExecutor final Executor executor, |
| 285 |
@NonNull final CarrierMessagingCallback callback) { |
| 286 |
Objects.requireNonNull(mICarrierMessagingService); |
| 287 |
try { |
| 288 |
mICarrierMessagingService.downloadMms(pduUri, subId, location, |
| 289 |
new CarrierMessagingCallbackInternal(callback, executor)); |
| 290 |
} catch (RemoteException e) { |
| 291 |
throw new RuntimeException(e); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
@Override |
| 297 |
public void close() { |
| 298 |
disconnect(); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
private final class CarrierMessagingServiceConnection implements ServiceConnection { |
| 305 |
@Override |
| 306 |
public void onServiceConnected(ComponentName name, IBinder service) { |
| 307 |
onServiceReady(ICarrierMessagingService.Stub.asInterface(service)); |
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
public void onServiceDisconnected(ComponentName name) { |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
@SystemApi |
| 321 |
public interface CarrierMessagingCallback { |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
default void onReceiveSmsComplete( |
| 332 |
@CarrierMessagingService.FilterCompleteResult int result) { |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
default void onSendSmsComplete(@CarrierMessagingService.SendResult |
| 346 |
int result, int messageRef) { |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
default void onSendMultipartSmsComplete(@CarrierMessagingService.SendResult |
| 359 |
int result, @Nullable int[] messageRefs) { |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
default void onSendMmsComplete(@CarrierMessagingService.SendResult |
| 373 |
int result, @Nullable byte[] sendConfPdu) { |
| 374 |
|
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
default void onDownloadMmsComplete(@CarrierMessagingService.DownloadResult |
| 384 |
int result) { |
| 385 |
|
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
private final class CarrierMessagingCallbackInternal |
| 390 |
extends ICarrierMessagingCallback.Stub { |
| 391 |
final CarrierMessagingCallback mCarrierMessagingCallback; |
| 392 |
final Executor mExecutor; |
| 393 |
|
| 394 |
CarrierMessagingCallbackInternal(CarrierMessagingCallback callback, |
| 395 |
final Executor executor) { |
| 396 |
mCarrierMessagingCallback = callback; |
| 397 |
mExecutor = executor; |
| 398 |
} |
| 399 |
|
| 400 |
@Override |
| 401 |
public void onFilterComplete(int result) throws RemoteException { |
| 402 |
mExecutor.execute(() -> mCarrierMessagingCallback.onReceiveSmsComplete(result)); |
| 403 |
} |
| 404 |
|
| 405 |
@Override |
| 406 |
public void onSendSmsComplete(int result, int messageRef) throws RemoteException { |
| 407 |
mExecutor.execute(() -> mCarrierMessagingCallback.onSendSmsComplete( |
| 408 |
result, messageRef)); |
| 409 |
} |
| 410 |
|
| 411 |
@Override |
| 412 |
public void onSendMultipartSmsComplete(int result, int[] messageRefs) |
| 413 |
throws RemoteException { |
| 414 |
mExecutor.execute(() -> mCarrierMessagingCallback.onSendMultipartSmsComplete( |
| 415 |
result, messageRefs)); |
| 416 |
} |
| 417 |
|
| 418 |
@Override |
| 419 |
public void onSendMmsComplete(int result, byte[] sendConfPdu) throws RemoteException { |
| 420 |
mExecutor.execute(() -> mCarrierMessagingCallback.onSendMmsComplete( |
| 421 |
result, sendConfPdu)); |
| 422 |
} |
| 423 |
|
| 424 |
@Override |
| 425 |
public void onDownloadMmsComplete(int result) throws RemoteException { |
| 426 |
mExecutor.execute(() -> mCarrierMessagingCallback.onDownloadMmsComplete(result)); |
| 427 |
} |
| 428 |
} |
| 429 |
} |