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 android.telephony;
18 
19 import android.annotation.CallSuper;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.annotation.WorkerThread;
24 import android.app.Service;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.os.IBinder;
28 import android.os.RemoteCallback;
29 import android.telephony.cdma.CdmaSmsCbProgramData;
30 
31 import java.util.List;
32 import java.util.function.Consumer;
33 
34 /**
35  * A service which exposes the cell broadcast handling module to the system.
36  * <p>
37  * To extend this class, you must declare the service in your manifest file to require the
38  * {@link android.Manifest.permission#BIND_CELL_BROADCAST_SERVICE} permission and include an intent
39  * filter with the {@link #CELL_BROADCAST_SERVICE_INTERFACE}.
40  * Implementations of this service should run in the phone process and with its UID.
41  * <p>
42  * For example:
43  * <pre>{@code
44  * <manifest xmlns:android="http://schemas.android.com/apk/res/android"
45  *       android:sharedUserId="android.uid.phone">
46  *   <service android:name=".MyCellBroadcastService"
47  *         android:label="@string/service_name"
48  *         android:process="com.android.phone"
49  *         android:exported="true"
50  *         android:permission="android.permission.BIND_CELL_BROADCAST_SERVICE">
51  *     <intent-filter>
52  *           <action android:name="android.telephony.CellBroadcastService" />
53  *     </intent-filter>
54  *   </service>
55  * </manifest>
56  * }</pre>
57  *
58  * @hide
59  */
60 @SystemApi
61 public abstract class CellBroadcastService extends Service {
62 
63     public static final String CELL_BROADCAST_SERVICE_INTERFACE =
64             "android.telephony.CellBroadcastService";
65 
66     private final ICellBroadcastService.Stub mStubWrapper;
67 
CellBroadcastService()68     public CellBroadcastService() {
69         mStubWrapper = new ICellBroadcastServiceWrapper();
70     }
71 
72     /**
73      * Handle a GSM cell broadcast SMS message forwarded from the system.
74      *
75      * @param slotIndex the index of the slot which received the message
76      * @param message   the SMS PDU
77      */
onGsmCellBroadcastSms(int slotIndex, @NonNull byte[] message)78     public abstract void onGsmCellBroadcastSms(int slotIndex, @NonNull byte[] message);
79 
80     /**
81      * Handle a CDMA cell broadcast SMS message forwarded from the system.
82      *
83      * @param slotIndex       the index of the slot which received the message
84      * @param bearerData      the CDMA SMS bearer data
85      * @param serviceCategory the CDMA SCPT service category
86      */
onCdmaCellBroadcastSms(int slotIndex, @NonNull byte[] bearerData, @CdmaSmsCbProgramData.Category int serviceCategory)87     public abstract void onCdmaCellBroadcastSms(int slotIndex, @NonNull byte[] bearerData,
88             @CdmaSmsCbProgramData.Category int serviceCategory);
89 
90     /**
91      * Handle a CDMA cell broadcast SMS message forwarded from the system.
92      *
93      * @param slotIndex          the index of the slot which received the message
94      * @param smsCbProgramData   the SMS CB program data of the message
95      * @param originatingAddress the originating address of the message, as a non-separated dial
96      *                           string
97      * @param callback           a callback to run after each cell broadcast receiver has handled
98      *                           the SCP message. The bundle will contain a non-separated
99      *                           dial string as and an ArrayList of {@link CdmaSmsCbProgramResults}.
100      */
onCdmaScpMessage(int slotIndex, @NonNull List<CdmaSmsCbProgramData> smsCbProgramData, @NonNull String originatingAddress, @NonNull Consumer<Bundle> callback)101     public abstract void onCdmaScpMessage(int slotIndex,
102             @NonNull List<CdmaSmsCbProgramData> smsCbProgramData,
103             @NonNull String originatingAddress, @NonNull Consumer<Bundle> callback);
104 
105     /**
106      * Get broadcasted area information.
107      *
108      * @param slotIndex the index of the slot which received the area information.
109      *
110      * @return The area information string sent from the network. This is usually the human readable
111      * string shown in Setting app's SIM status page.
112      */
113     @WorkerThread
getCellBroadcastAreaInfo(int slotIndex)114     public abstract @NonNull CharSequence getCellBroadcastAreaInfo(int slotIndex);
115 
116     /**
117      * If overriding this method, call through to the super method for any unknown actions.
118      * {@inheritDoc}
119      */
120     @Override
121     @CallSuper
122     @NonNull
onBind(@ullable Intent intent)123     public IBinder onBind(@Nullable Intent intent) {
124         return mStubWrapper;
125     }
126 
127     /**
128      * A wrapper around ICellBroadcastService that forwards calls to implementations of
129      * {@link CellBroadcastService}.
130      *
131      * @hide
132      */
133     public class ICellBroadcastServiceWrapper extends ICellBroadcastService.Stub {
134         /**
135          * Handle a GSM cell broadcast SMS.
136          *
137          * @param slotIndex the index of the slot which received the broadcast
138          * @param message   the SMS message PDU
139          */
140         @Override
handleGsmCellBroadcastSms(int slotIndex, byte[] message)141         public void handleGsmCellBroadcastSms(int slotIndex, byte[] message) {
142             CellBroadcastService.this.onGsmCellBroadcastSms(slotIndex, message);
143         }
144 
145         /**
146          * Handle a CDMA cell broadcast SMS.
147          *
148          * @param slotIndex       the index of the slot which received the broadcast
149          * @param bearerData      the CDMA SMS bearer data
150          * @param serviceCategory the CDMA SCPT service category
151          */
152         @Override
handleCdmaCellBroadcastSms(int slotIndex, byte[] bearerData, int serviceCategory)153         public void handleCdmaCellBroadcastSms(int slotIndex, byte[] bearerData,
154                 int serviceCategory) {
155             CellBroadcastService.this.onCdmaCellBroadcastSms(slotIndex, bearerData,
156                     serviceCategory);
157         }
158 
159         /**
160          * Handle a CDMA Service Category Program message.
161          *
162          * @param slotIndex          the index of the slot which received the message
163          * @param smsCbProgramData   the SMS CB program data of the message
164          * @param originatingAddress the originating address of the message
165          * @param callback           a callback to run after each cell broadcast receiver has
166          *                           handled the SCP message
167          */
168         @Override
handleCdmaScpMessage(int slotIndex, List<CdmaSmsCbProgramData> smsCbProgramData, String originatingAddress, RemoteCallback callback)169         public void handleCdmaScpMessage(int slotIndex,
170                 List<CdmaSmsCbProgramData> smsCbProgramData, String originatingAddress,
171                 RemoteCallback callback) {
172             Consumer<Bundle> consumer = bundle -> {
173                 callback.sendResult(bundle);
174             };
175             CellBroadcastService.this.onCdmaScpMessage(slotIndex, smsCbProgramData,
176                     originatingAddress, consumer);
177         }
178 
179         /**
180          * Get broadcasted area information
181          *
182          * @param slotIndex         the index of the slot which received the message
183          *
184          * @return The area information
185          */
186         @Override
getCellBroadcastAreaInfo(int slotIndex)187         public @NonNull CharSequence getCellBroadcastAreaInfo(int slotIndex) {
188             return CellBroadcastService.this.getCellBroadcastAreaInfo(slotIndex);
189         }
190     }
191 }
192