Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | */ |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 16 | package android.service.carrier; |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 17 | |
Makoto Onuki | da79fb6 | 2019-02-28 11:34:31 -0800 | [diff] [blame] | 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 20 | import android.app.Service; |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Intent; |
| 23 | import android.os.IBinder; |
| 24 | |
| 25 | /** |
| 26 | * If the default SMS app has a service that extends this class, the system always tries to bind |
| 27 | * it so that the process is always running, which allows the app to have a persistent connection |
| 28 | * to the server. |
| 29 | * |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 30 | * <p>The service must have an |
| 31 | * {@link android.telephony.TelephonyManager#ACTION_CARRIER_MESSAGING_CLIENT_SERVICE} |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 32 | * action in the intent handler, and be protected with |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 33 | * {@link android.Manifest.permission#BIND_CARRIER_MESSAGING_CLIENT_SERVICE}. |
| 34 | * However the service does not have to be exported. |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 35 | * |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 36 | * <p>The service must be associated with a non-main process, meaning it must have an |
| 37 | * {@code android:process} tag in its manifest entry. |
| 38 | * |
| 39 | * <p>An app can use |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 40 | * {@link android.content.pm.PackageManager#setComponentEnabledSetting(ComponentName, int, int)} |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 41 | * to disable or enable the service. An app should use it to disable the service when it no longer |
| 42 | * needs to be running. |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 43 | * |
| 44 | * <p>When the owner process crashes, the service will be re-bound automatically after a |
| 45 | * back-off. |
| 46 | * |
| 47 | * <p>Note the process may still be killed if the system is under heavy memory pressure, in which |
| 48 | * case the process will be re-started later. |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 49 | * |
| 50 | * <p>Example: First, define a subclass in the application: |
| 51 | * <pre> |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 52 | * public class MyCarrierMessagingClientService extends CarrierMessagingClientService { |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 53 | * } |
| 54 | * </pre> |
| 55 | * Then, declare it in its {@code AndroidManifest.xml}: |
| 56 | * <pre> |
| 57 | * <service |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 58 | * android:name=".MyCarrierMessagingClientService" |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 59 | * android:exported="false" |
| 60 | * android:process=":persistent" |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 61 | * android:permission="android.permission.BIND_CARRIER_MESSAGING_CLIENT_SERVICE"> |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 62 | * <intent-filter> |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 63 | * <action android:name="android.telephony.action.CARRIER_MESSAGING_CLIENT_SERVICE" /> |
Makoto Onuki | 8c7c5cc | 2018-10-02 09:29:58 -0700 | [diff] [blame] | 64 | * </intent-filter> |
| 65 | * </service> |
| 66 | * </pre> |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 67 | */ |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 68 | public class CarrierMessagingClientService extends Service { |
| 69 | private final ICarrierMessagingClientServiceImpl mImpl; |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 70 | |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 71 | public CarrierMessagingClientService() { |
| 72 | mImpl = new ICarrierMessagingClientServiceImpl(); |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Override |
Makoto Onuki | da79fb6 | 2019-02-28 11:34:31 -0800 | [diff] [blame] | 76 | @NonNull |
| 77 | public final IBinder onBind(@Nullable Intent intent) { |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 78 | return mImpl.asBinder(); |
| 79 | } |
| 80 | |
Makoto Onuki | 05d64cb | 2019-02-01 16:04:16 -0800 | [diff] [blame] | 81 | private class ICarrierMessagingClientServiceImpl extends ICarrierMessagingClientService.Stub { |
Makoto Onuki | df7e481 | 2018-09-24 14:31:25 -0700 | [diff] [blame] | 82 | } |
| 83 | } |