Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Hall Liu | 17449f9 | 2018-03-28 15:54:07 -0700 | [diff] [blame] | 19 | import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 20 | |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
Hall Liu | 2398921 | 2017-07-18 11:30:27 -0700 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
| 24 | import android.annotation.SystemApi; |
Hall Liu | 64e22af | 2017-10-20 11:30:16 -0700 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 27 | import android.content.Context; |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 29 | import android.os.IBinder; |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 31 | import android.telephony.mbms.InternalStreamingServiceCallback; |
Hall Liu | 17449f9 | 2018-03-28 15:54:07 -0700 | [diff] [blame] | 32 | import android.telephony.mbms.InternalStreamingSessionCallback; |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 33 | import android.telephony.mbms.MbmsErrors; |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 34 | import android.telephony.mbms.MbmsStreamingSessionCallback; |
Hall Liu | 0842831 | 2017-05-19 15:28:09 -0700 | [diff] [blame] | 35 | import android.telephony.mbms.MbmsUtils; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 36 | import android.telephony.mbms.StreamingService; |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 37 | import android.telephony.mbms.StreamingServiceCallback; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 38 | import android.telephony.mbms.StreamingServiceInfo; |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 39 | import android.telephony.mbms.vendor.IMbmsStreamingService; |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 40 | import android.util.ArraySet; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | |
| 43 | import java.util.List; |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 45 | import java.util.concurrent.Executor; |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 46 | import java.util.concurrent.atomic.AtomicBoolean; |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 47 | import java.util.concurrent.atomic.AtomicReference; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 48 | |
Hall Liu | 46cbcd1 | 2017-07-07 13:33:16 -0700 | [diff] [blame] | 49 | /** |
| 50 | * This class provides functionality for streaming media over MBMS. |
Hall Liu | 46cbcd1 | 2017-07-07 13:33:16 -0700 | [diff] [blame] | 51 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 52 | public class MbmsStreamingSession implements AutoCloseable { |
| 53 | private static final String LOG_TAG = "MbmsStreamingSession"; |
Hall Liu | 2398921 | 2017-07-18 11:30:27 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Service action which must be handled by the middleware implementing the MBMS streaming |
| 57 | * interface. |
| 58 | * @hide |
| 59 | */ |
| 60 | @SystemApi |
| 61 | @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 62 | public static final String MBMS_STREAMING_SERVICE_ACTION = |
| 63 | "android.telephony.action.EmbmsStreaming"; |
| 64 | |
Hall Liu | 64e22af | 2017-10-20 11:30:16 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Metadata key that specifies the component name of the service to bind to for file-download. |
| 67 | * @hide |
| 68 | */ |
| 69 | @TestApi |
| 70 | public static final String MBMS_STREAMING_SERVICE_OVERRIDE_METADATA = |
| 71 | "mbms-streaming-service-override"; |
| 72 | |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 73 | private static AtomicBoolean sIsInitialized = new AtomicBoolean(false); |
| 74 | |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 75 | private AtomicReference<IMbmsStreamingService> mService = new AtomicReference<>(null); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 76 | private IBinder.DeathRecipient mDeathRecipient = new IBinder.DeathRecipient() { |
| 77 | @Override |
| 78 | public void binderDied() { |
| 79 | sIsInitialized.set(false); |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 80 | sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, "Received death notification"); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 81 | } |
| 82 | }; |
| 83 | |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 84 | private InternalStreamingSessionCallback mInternalCallback; |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 85 | private ServiceConnection mServiceConnection; |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 86 | private Set<StreamingService> mKnownActiveStreamingServices = new ArraySet<>(); |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 87 | |
| 88 | private final Context mContext; |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 89 | private int mSubscriptionId = INVALID_SUBSCRIPTION_ID; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 90 | |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 91 | /** @hide */ |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 92 | private MbmsStreamingSession(Context context, Executor executor, int subscriptionId, |
| 93 | MbmsStreamingSessionCallback callback) { |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 94 | mContext = context; |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 95 | mSubscriptionId = subscriptionId; |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 96 | mInternalCallback = new InternalStreamingSessionCallback(callback, executor); |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 100 | * Create a new {@link MbmsStreamingSession} using the given subscription ID. |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 101 | * |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 102 | * Note that this call will bind a remote service. You may not call this method on your app's |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 103 | * main thread. |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 104 | * |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 105 | * You may only have one instance of {@link MbmsStreamingSession} per UID. If you call this |
| 106 | * method while there is an active instance of {@link MbmsStreamingSession} in your process |
| 107 | * (in other words, one that has not had {@link #close()} called on it), this method will |
| 108 | * throw an {@link IllegalStateException}. If you call this method in a different process |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 109 | * running under the same UID, an error will be indicated via |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 110 | * {@link MbmsStreamingSessionCallback#onError(int, String)}. |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 111 | * |
| 112 | * Note that initialization may fail asynchronously. If you wish to try again after you |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 113 | * receive such an asynchronous error, you must call {@link #close()} on the instance of |
| 114 | * {@link MbmsStreamingSession} that you received before calling this method again. |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 115 | * |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 116 | * @param context The {@link Context} to use. |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 117 | * @param executor The executor on which you wish to execute callbacks. |
| 118 | * @param subscriptionId The subscription ID to use. |
Hall Liu | 64a98f9 | 2017-07-14 13:39:54 -0700 | [diff] [blame] | 119 | * @param callback A callback object on which you wish to receive results of asynchronous |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 120 | * operations. |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 121 | * @return An instance of {@link MbmsStreamingSession}, or null if an error occurred. |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 122 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 123 | public static @Nullable MbmsStreamingSession create(@NonNull Context context, |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 124 | @NonNull Executor executor, int subscriptionId, |
| 125 | final @NonNull MbmsStreamingSessionCallback callback) { |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 126 | if (!sIsInitialized.compareAndSet(false, true)) { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 127 | throw new IllegalStateException("Cannot create two instances of MbmsStreamingSession"); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 128 | } |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 129 | MbmsStreamingSession session = new MbmsStreamingSession(context, executor, |
| 130 | subscriptionId, callback); |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 131 | |
Hall Liu | 23d80af | 2017-09-01 17:59:15 -0700 | [diff] [blame] | 132 | final int result = session.bindAndInitialize(); |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 133 | if (result != MbmsErrors.SUCCESS) { |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 134 | sIsInitialized.set(false); |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 135 | executor.execute(new Runnable() { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void run() { |
| 138 | callback.onError(result, null); |
| 139 | } |
| 140 | }); |
| 141 | return null; |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 142 | } |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 143 | return session; |
Hall Liu | 563e150c | 2017-04-25 15:04:26 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 147 | * Create a new {@link MbmsStreamingSession} using the system default data subscription ID. |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 148 | * See {@link #create(Context, Executor, int, MbmsStreamingSessionCallback)}. |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 149 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 150 | public static MbmsStreamingSession create(@NonNull Context context, |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 151 | @NonNull Executor executor, @NonNull MbmsStreamingSessionCallback callback) { |
| 152 | return create(context, executor, SubscriptionManager.getDefaultSubscriptionId(), callback); |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 156 | * Terminates this instance. Also terminates |
| 157 | * any streaming services spawned from this instance as if |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 158 | * {@link StreamingService#close()} had been called on them. After this method returns, |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 159 | * no further callbacks originating from the middleware will be enqueued on the provided |
| 160 | * instance of {@link MbmsStreamingSessionCallback}, but callbacks that have already been |
| 161 | * enqueued will still be delivered. |
| 162 | * |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 163 | * It is safe to call {@link #create(Context, Executor, int, MbmsStreamingSessionCallback)} to |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 164 | * obtain another instance of {@link MbmsStreamingSession} immediately after this method |
| 165 | * returns. |
Hall Liu | 46cbcd1 | 2017-07-07 13:33:16 -0700 | [diff] [blame] | 166 | * |
| 167 | * May throw an {@link IllegalStateException} |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 168 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 169 | public void close() { |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 170 | try { |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 171 | IMbmsStreamingService streamingService = mService.get(); |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 172 | if (streamingService == null || mServiceConnection == null) { |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 173 | // Ignore and return, assume already disposed. |
| 174 | return; |
| 175 | } |
Hall Liu | 9f116ef | 2017-06-23 16:32:30 -0700 | [diff] [blame] | 176 | streamingService.dispose(mSubscriptionId); |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 177 | for (StreamingService s : mKnownActiveStreamingServices) { |
| 178 | s.getCallback().stop(); |
| 179 | } |
| 180 | mKnownActiveStreamingServices.clear(); |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 181 | mContext.unbindService(mServiceConnection); |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 182 | } catch (RemoteException e) { |
| 183 | // Ignore for now |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 184 | } finally { |
| 185 | mService.set(null); |
| 186 | sIsInitialized.set(false); |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 187 | mServiceConnection = null; |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 188 | mInternalCallback.stop(); |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 189 | } |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /** |
| 193 | * An inspection API to retrieve the list of streaming media currently be advertised. |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 194 | * The results are returned asynchronously via |
| 195 | * {@link MbmsStreamingSessionCallback#onStreamingServicesUpdated(List)} on the callback |
| 196 | * provided upon creation. |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 197 | * |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 198 | * Multiple calls replace the list of service classes of interest. |
Robert Greenwalt | d447cea | 2017-04-21 20:30:14 -0700 | [diff] [blame] | 199 | * |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 200 | * May throw an {@link IllegalArgumentException} or an {@link IllegalStateException}. |
Robert Greenwalt | d447cea | 2017-04-21 20:30:14 -0700 | [diff] [blame] | 201 | * |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 202 | * @param serviceClassList A list of streaming service classes that the app would like updates |
| 203 | * on. The exact names of these classes should be negotiated with the |
| 204 | * wireless carrier separately. |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 205 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 206 | public void requestUpdateStreamingServices(List<String> serviceClassList) { |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 207 | IMbmsStreamingService streamingService = mService.get(); |
| 208 | if (streamingService == null) { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 209 | throw new IllegalStateException("Middleware not yet bound"); |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 210 | } |
| 211 | try { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 212 | int returnCode = streamingService.requestUpdateStreamingServices( |
| 213 | mSubscriptionId, serviceClassList); |
Hall Liu | 17449f9 | 2018-03-28 15:54:07 -0700 | [diff] [blame] | 214 | if (returnCode == MbmsErrors.UNKNOWN) { |
| 215 | // Unbind and throw an obvious error |
| 216 | close(); |
| 217 | throw new IllegalStateException("Middleware must not return an unknown error code"); |
| 218 | } |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 219 | if (returnCode != MbmsErrors.SUCCESS) { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 220 | sendErrorToApp(returnCode, null); |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 221 | } |
Hall Liu | 80a8687 | 2017-05-26 15:49:32 -0700 | [diff] [blame] | 222 | } catch (RemoteException e) { |
Hall Liu | edbd456 | 2017-05-17 18:15:27 -0700 | [diff] [blame] | 223 | Log.w(LOG_TAG, "Remote process died"); |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 224 | mService.set(null); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 225 | sIsInitialized.set(false); |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 226 | sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, null); |
Hall Liu | 8023332 | 2017-05-02 15:06:29 -0700 | [diff] [blame] | 227 | } |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 231 | * Starts streaming a requested service, reporting status to the indicated callback. |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 232 | * Returns an object used to control that stream. The stream may not be ready for consumption |
| 233 | * immediately upon return from this method -- wait until the streaming state has been |
Hall Liu | 6e1d2fee | 2017-06-30 14:37:30 -0700 | [diff] [blame] | 234 | * reported via |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 235 | * {@link android.telephony.mbms.StreamingServiceCallback#onStreamStateUpdated(int, int)} |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 236 | * |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 237 | * May throw an {@link IllegalArgumentException} or an {@link IllegalStateException} |
Robert Greenwalt | d447cea | 2017-04-21 20:30:14 -0700 | [diff] [blame] | 238 | * |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 239 | * Asynchronous errors through the callback include any of the errors in |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 240 | * {@link MbmsErrors.GeneralErrors} or |
| 241 | * {@link MbmsErrors.StreamingErrors}. |
Hall Liu | 46cbcd1 | 2017-07-07 13:33:16 -0700 | [diff] [blame] | 242 | * |
| 243 | * @param serviceInfo The information about the service to stream. |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 244 | * @param executor The executor on which you wish to execute callbacks for this stream. |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 245 | * @param callback A callback that'll be called when something about the stream changes. |
Hall Liu | 46cbcd1 | 2017-07-07 13:33:16 -0700 | [diff] [blame] | 246 | * @return An instance of {@link StreamingService} through which the stream can be controlled. |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 247 | * May be {@code null} if an error occurred. |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 248 | */ |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 249 | public @Nullable StreamingService startStreaming(StreamingServiceInfo serviceInfo, |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 250 | @NonNull Executor executor, StreamingServiceCallback callback) { |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 251 | IMbmsStreamingService streamingService = mService.get(); |
| 252 | if (streamingService == null) { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 253 | throw new IllegalStateException("Middleware not yet bound"); |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 254 | } |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 255 | |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 256 | InternalStreamingServiceCallback serviceCallback = new InternalStreamingServiceCallback( |
Hall Liu | 5b84487 | 2018-02-06 11:36:17 -0800 | [diff] [blame] | 257 | callback, executor); |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 258 | |
| 259 | StreamingService serviceForApp = new StreamingService( |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 260 | mSubscriptionId, streamingService, this, serviceInfo, serviceCallback); |
| 261 | mKnownActiveStreamingServices.add(serviceForApp); |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 262 | |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 263 | try { |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 264 | int returnCode = streamingService.startStreaming( |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 265 | mSubscriptionId, serviceInfo.getServiceId(), serviceCallback); |
Hall Liu | 17449f9 | 2018-03-28 15:54:07 -0700 | [diff] [blame] | 266 | if (returnCode == MbmsErrors.UNKNOWN) { |
| 267 | // Unbind and throw an obvious error |
| 268 | close(); |
| 269 | throw new IllegalStateException("Middleware must not return an unknown error code"); |
| 270 | } |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 271 | if (returnCode != MbmsErrors.SUCCESS) { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 272 | sendErrorToApp(returnCode, null); |
| 273 | return null; |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 274 | } |
Hall Liu | 80a8687 | 2017-05-26 15:49:32 -0700 | [diff] [blame] | 275 | } catch (RemoteException e) { |
Hall Liu | edbd456 | 2017-05-17 18:15:27 -0700 | [diff] [blame] | 276 | Log.w(LOG_TAG, "Remote process died"); |
Hall Liu | 727a05b | 2017-06-16 14:59:57 -0700 | [diff] [blame] | 277 | mService.set(null); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 278 | sIsInitialized.set(false); |
Hall Liu | e373ee5 | 2017-09-08 18:02:38 -0700 | [diff] [blame] | 279 | sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, null); |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 280 | return null; |
Hall Liu | fb1ee0e | 2017-05-11 13:32:32 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Hall Liu | ee19cfc | 2017-08-03 18:26:39 -0700 | [diff] [blame] | 283 | return serviceForApp; |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 286 | /** @hide */ |
| 287 | public void onStreamingServiceStopped(StreamingService service) { |
| 288 | mKnownActiveStreamingServices.remove(service); |
| 289 | } |
| 290 | |
| 291 | private int bindAndInitialize() { |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 292 | mServiceConnection = new ServiceConnection() { |
| 293 | @Override |
| 294 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 295 | IMbmsStreamingService streamingService = |
| 296 | IMbmsStreamingService.Stub.asInterface(service); |
| 297 | int result; |
| 298 | try { |
| 299 | result = streamingService.initialize(mInternalCallback, |
| 300 | mSubscriptionId); |
| 301 | } catch (RemoteException e) { |
| 302 | Log.e(LOG_TAG, "Service died before initialization"); |
| 303 | sendErrorToApp( |
| 304 | MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, |
| 305 | e.toString()); |
| 306 | sIsInitialized.set(false); |
| 307 | return; |
| 308 | } catch (RuntimeException e) { |
| 309 | Log.e(LOG_TAG, "Runtime exception during initialization"); |
| 310 | sendErrorToApp( |
| 311 | MbmsErrors.InitializationErrors.ERROR_UNABLE_TO_INITIALIZE, |
| 312 | e.toString()); |
| 313 | sIsInitialized.set(false); |
| 314 | return; |
| 315 | } |
| 316 | if (result == MbmsErrors.UNKNOWN) { |
| 317 | // Unbind and throw an obvious error |
| 318 | close(); |
| 319 | throw new IllegalStateException("Middleware must not return" |
| 320 | + " an unknown error code"); |
| 321 | } |
| 322 | if (result != MbmsErrors.SUCCESS) { |
| 323 | sendErrorToApp(result, "Error returned during initialization"); |
| 324 | sIsInitialized.set(false); |
| 325 | return; |
| 326 | } |
| 327 | try { |
| 328 | streamingService.asBinder().linkToDeath(mDeathRecipient, 0); |
| 329 | } catch (RemoteException e) { |
| 330 | sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, |
| 331 | "Middleware lost during initialization"); |
| 332 | sIsInitialized.set(false); |
| 333 | return; |
| 334 | } |
| 335 | mService.set(streamingService); |
| 336 | } |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 337 | |
Hall Liu | 718986d | 2019-08-29 15:42:47 -0700 | [diff] [blame] | 338 | @Override |
| 339 | public void onServiceDisconnected(ComponentName name) { |
| 340 | sIsInitialized.set(false); |
| 341 | mService.set(null); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void onNullBinding(ComponentName name) { |
| 346 | Log.w(LOG_TAG, "bindAndInitialize: Remote service returned null"); |
| 347 | sendErrorToApp(MbmsErrors.ERROR_MIDDLEWARE_LOST, |
| 348 | "Middleware service binding returned null"); |
| 349 | sIsInitialized.set(false); |
| 350 | mService.set(null); |
| 351 | mContext.unbindService(this); |
| 352 | } |
| 353 | }; |
| 354 | return MbmsUtils.startBinding(mContext, MBMS_STREAMING_SERVICE_ACTION, mServiceConnection); |
Hall Liu | fa2115e | 2017-04-27 13:20:09 -0700 | [diff] [blame] | 355 | } |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 356 | |
| 357 | private void sendErrorToApp(int errorCode, String message) { |
| 358 | try { |
Hall Liu | d666a89 | 2017-08-28 14:10:46 -0700 | [diff] [blame] | 359 | mInternalCallback.onError(errorCode, message); |
Hall Liu | d5c955f | 2017-07-20 15:32:51 -0700 | [diff] [blame] | 360 | } catch (RemoteException e) { |
| 361 | // Ignore, should not happen locally. |
| 362 | } |
| 363 | } |
Robert Greenwalt | 4dded7a | 2016-12-05 16:33:32 -0800 | [diff] [blame] | 364 | } |