Kyunglyul Hyun | 1c8188f | 2019-02-01 10:50:11 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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.media; |
| 18 | |
| 19 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 20 | |
| 21 | import android.app.Service; |
| 22 | import android.content.Intent; |
| 23 | import android.os.Handler; |
| 24 | import android.os.IBinder; |
| 25 | import android.os.Looper; |
| 26 | import android.os.RemoteException; |
| 27 | import android.util.Log; |
| 28 | |
| 29 | /** |
| 30 | * @hide |
| 31 | */ |
| 32 | public abstract class MediaRoute2ProviderService extends Service { |
| 33 | private static final String TAG = "MediaRouteProviderSrv"; |
| 34 | |
| 35 | public static final String SERVICE_INTERFACE = "android.media.MediaRoute2ProviderService"; |
| 36 | |
| 37 | private final Handler mHandler; |
| 38 | private ProviderStub mStub; |
| 39 | private IMediaRoute2Callback mCallback; |
| 40 | |
| 41 | public MediaRoute2ProviderService() { |
| 42 | mHandler = new Handler(Looper.getMainLooper()); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public IBinder onBind(Intent intent) { |
| 47 | if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 48 | if (mStub == null) { |
| 49 | mStub = new ProviderStub(); |
| 50 | } |
| 51 | return mStub; |
| 52 | } |
| 53 | return null; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Called when selectRoute is called on a route of the provider. |
| 58 | * |
| 59 | * @param uid The target application uid |
| 60 | * @param routeId The id of the target route |
| 61 | */ |
| 62 | public abstract void onSelect(int uid, String routeId); |
| 63 | |
| 64 | /** |
| 65 | * Updates provider info from selected route and appliation. |
| 66 | * |
| 67 | * TODO: When provider descriptor is defined, this should update the descriptor correctly. |
| 68 | * |
| 69 | * @param uid |
| 70 | * @param routeId |
| 71 | */ |
| 72 | public void updateProvider(int uid, String routeId) { |
| 73 | if (mCallback != null) { |
| 74 | try { |
| 75 | //TODO: After publishState() is fully implemented, delete this. |
| 76 | mCallback.onRouteSelected(uid, routeId); |
| 77 | } catch (RemoteException ex) { |
| 78 | Log.d(TAG, "Failed to update provider"); |
| 79 | } |
| 80 | } |
| 81 | publishState(); |
| 82 | } |
| 83 | |
| 84 | void setCallback(IMediaRoute2Callback callback) { |
| 85 | mCallback = callback; |
| 86 | publishState(); |
| 87 | } |
| 88 | |
| 89 | void publishState() { |
| 90 | //TODO: Send provider descriptor to the MediaRouterService |
| 91 | } |
| 92 | |
| 93 | final class ProviderStub extends IMediaRoute2Provider.Stub { |
| 94 | ProviderStub() { } |
| 95 | |
| 96 | @Override |
| 97 | public void setCallback(IMediaRoute2Callback callback) { |
| 98 | mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::setCallback, |
| 99 | MediaRoute2ProviderService.this, callback)); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void selectRoute(int uid, String id) { |
| 104 | mHandler.sendMessage(obtainMessage(MediaRoute2ProviderService::onSelect, |
| 105 | MediaRoute2ProviderService.this, uid, id)); |
| 106 | } |
| 107 | } |
| 108 | } |