Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [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 com.android.server.media; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.content.ComponentName; |
| 22 | import android.content.Intent; |
| 23 | import android.media.MediaRoute2Info; |
| 24 | import android.media.MediaRoute2ProviderInfo; |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 25 | import android.media.RouteSessionInfo; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 26 | |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | import java.util.Collections; |
| 29 | import java.util.List; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 30 | import java.util.Objects; |
| 31 | |
| 32 | abstract class MediaRoute2Provider { |
| 33 | final ComponentName mComponentName; |
| 34 | final String mUniqueId; |
| 35 | |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 36 | Callback mCallback; |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 37 | private volatile MediaRoute2ProviderInfo mProviderInfo; |
| 38 | private volatile List<RouteSessionInfo> mSessionInfos = Collections.emptyList(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 39 | |
| 40 | MediaRoute2Provider(@NonNull ComponentName componentName) { |
| 41 | mComponentName = Objects.requireNonNull(componentName, "Component name must not be null."); |
| 42 | mUniqueId = componentName.flattenToShortString(); |
| 43 | } |
| 44 | |
| 45 | public void setCallback(MediaRoute2ProviderProxy.Callback callback) { |
| 46 | mCallback = callback; |
| 47 | } |
| 48 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 49 | public abstract void requestCreateSession(String packageName, String routeId, |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 50 | String controlCategory, long requestId); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 51 | public abstract void releaseSession(int sessionId); |
| 52 | |
Hyundo Moon | 9be3548 | 2019-12-30 17:33:13 +0900 | [diff] [blame] | 53 | public abstract void selectRoute(int sessionId, MediaRoute2Info route); |
| 54 | public abstract void deselectRoute(int sessionId, MediaRoute2Info route); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 55 | public abstract void transferToRoute(int sessionId, MediaRoute2Info route); |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 56 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 57 | public abstract void sendControlRequest(MediaRoute2Info route, Intent request); |
| 58 | public abstract void requestSetVolume(MediaRoute2Info route, int volume); |
| 59 | public abstract void requestUpdateVolume(MediaRoute2Info route, int delta); |
| 60 | |
| 61 | @NonNull |
| 62 | public String getUniqueId() { |
| 63 | return mUniqueId; |
| 64 | } |
| 65 | |
| 66 | @Nullable |
| 67 | public MediaRoute2ProviderInfo getProviderInfo() { |
| 68 | return mProviderInfo; |
| 69 | } |
| 70 | |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 71 | @NonNull |
| 72 | public List<RouteSessionInfo> getSessionInfos() { |
| 73 | return mSessionInfos; |
| 74 | } |
| 75 | |
| 76 | void setAndNotifyProviderState(MediaRoute2ProviderInfo providerInfo, |
| 77 | List<RouteSessionInfo> sessionInfos) { |
| 78 | if (providerInfo == null) { |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 79 | mProviderInfo = null; |
| 80 | } else { |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 81 | mProviderInfo = new MediaRoute2ProviderInfo.Builder(providerInfo) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 82 | .setUniqueId(mUniqueId) |
| 83 | .build(); |
| 84 | } |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 85 | List<RouteSessionInfo> sessionInfoWithProviderId = new ArrayList<RouteSessionInfo>(); |
| 86 | for (RouteSessionInfo sessionInfo : sessionInfos) { |
| 87 | sessionInfoWithProviderId.add( |
| 88 | new RouteSessionInfo.Builder(sessionInfo) |
| 89 | .setProviderId(mUniqueId) |
| 90 | .build()); |
| 91 | } |
| 92 | mSessionInfos = sessionInfoWithProviderId; |
| 93 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 94 | if (mCallback != null) { |
| 95 | mCallback.onProviderStateChanged(this); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public boolean hasComponentName(String packageName, String className) { |
| 100 | return mComponentName.getPackageName().equals(packageName) |
| 101 | && mComponentName.getClassName().equals(className); |
| 102 | } |
| 103 | |
| 104 | public interface Callback { |
Kyunglyul Hyun | f7d5e04 | 2019-11-11 13:56:28 +0900 | [diff] [blame] | 105 | void onProviderStateChanged(@Nullable MediaRoute2Provider provider); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 106 | void onSessionCreated(@NonNull MediaRoute2Provider provider, |
Kyunglyul Hyun | c784724 | 2019-12-30 17:46:04 +0900 | [diff] [blame] | 107 | @Nullable RouteSessionInfo sessionInfo, long requestId); |
Hyundo Moon | 0a92657 | 2019-12-28 15:01:21 +0900 | [diff] [blame] | 108 | // TODO: Remove this when MediaRouter2ServiceImpl notifies clients of session changes. |
| 109 | void onSessionInfoChanged(@NonNull MediaRoute2Provider provider, |
| 110 | @NonNull RouteSessionInfo sessionInfo); |
Hyundo Moon | ca9db78 | 2020-01-01 18:31:15 +0900 | [diff] [blame] | 111 | // TODO: Call this when service actually notifies of session release. |
| 112 | void onSessionReleased(@NonNull MediaRoute2Provider provider, |
| 113 | @NonNull RouteSessionInfo sessionInfo); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 114 | } |
| 115 | } |