blob: 1267aa88a939ece93a7d19d82a34b05769b269b7 [file] [log] [blame]
Kyunglyul Hyund51666d2019-04-11 04:08:40 +00001/*
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
17package com.android.mediarouteprovider.example;
18
19import android.content.Intent;
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090020import android.media.MediaRoute2Info;
21import android.media.MediaRoute2ProviderInfo;
Kyunglyul Hyund51666d2019-04-11 04:08:40 +000022import android.media.MediaRoute2ProviderService;
23import android.os.IBinder;
24
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090025import java.util.HashMap;
26import java.util.Map;
27
Kyunglyul Hyund51666d2019-04-11 04:08:40 +000028public class SampleMediaRoute2ProviderService extends MediaRoute2ProviderService {
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090029 private static final String TAG = "SampleMediaRoute2Serv";
30
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +090031 public static final String ROUTE_ID1 = "route_id1";
32 public static final String ROUTE_NAME1 = "Sample Route 1";
33 public static final String ROUTE_ID2 = "route_id2";
34 public static final String ROUTE_NAME2 = "Sample Route 2";
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090035
36 public static final String ROUTE_ID_SPECIAL_CATEGORY = "route_special_category";
37 public static final String ROUTE_NAME_SPECIAL_CATEGORY = "Special Category Route";
38
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +090039 public static final String ACTION_REMOVE_ROUTE =
40 "com.android.mediarouteprovider.action_remove_route";
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090041
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090042 public static final String CATEGORY_SAMPLE =
43 "com.android.mediarouteprovider.CATEGORY_SAMPLE";
44 public static final String CATEGORY_SPECIAL =
45 "com.android.mediarouteprovider.CATEGORY_SPECIAL";
46
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090047 Map<String, MediaRoute2Info> mRoutes = new HashMap<>();
48
49 private void initializeRoutes() {
50 MediaRoute2Info route1 = new MediaRoute2Info.Builder(ROUTE_ID1, ROUTE_NAME1)
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090051 .addSupportedCategory(CATEGORY_SAMPLE)
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090052 .build();
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +090053 MediaRoute2Info route2 = new MediaRoute2Info.Builder(ROUTE_ID2, ROUTE_NAME2)
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090054 .addSupportedCategory(CATEGORY_SAMPLE)
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +090055 .build();
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090056 MediaRoute2Info routeSpecial =
57 new MediaRoute2Info.Builder(ROUTE_ID_SPECIAL_CATEGORY, ROUTE_NAME_SPECIAL_CATEGORY)
58 .addSupportedCategory(CATEGORY_SAMPLE)
59 .addSupportedCategory(CATEGORY_SPECIAL)
60 .build();
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090061 mRoutes.put(route1.getId(), route1);
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +090062 mRoutes.put(route2.getId(), route2);
Kyunglyul Hyune23e5ed2019-07-15 16:06:26 +090063 mRoutes.put(routeSpecial.getId(), routeSpecial);
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090064 }
65
66 @Override
67 public void onCreate() {
68 initializeRoutes();
69 }
70
Kyunglyul Hyund51666d2019-04-11 04:08:40 +000071 @Override
72 public IBinder onBind(Intent intent) {
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090073 publishRoutes();
Kyunglyul Hyund51666d2019-04-11 04:08:40 +000074 return super.onBind(intent);
75 }
76
77 @Override
Kyunglyul Hyuna0d47412019-07-01 11:14:24 +090078 public void onSelectRoute(String packageName, String routeId) {
79 MediaRoute2Info route = mRoutes.get(routeId);
80 if (route == null) {
81 return;
82 }
83 mRoutes.put(routeId, new MediaRoute2Info.Builder(route)
84 .setClientPackageName(packageName)
85 .build());
86 publishRoutes();
87 }
88
89 @Override
90 public void onUnselectRoute(String packageName, String routeId) {
91 MediaRoute2Info route = mRoutes.get(routeId);
92 if (route == null) {
93 return;
94 }
95 mRoutes.put(routeId, new MediaRoute2Info.Builder(route)
96 .setClientPackageName(null)
97 .build());
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +090098 publishRoutes();
99 }
100
Kyunglyul Hyuncaae8dc2019-04-29 15:45:23 +0900101 @Override
102 public void onControlRequest(String routeId, Intent request) {
103 if (ACTION_REMOVE_ROUTE.equals(request.getAction())) {
104 MediaRoute2Info route = mRoutes.get(routeId);
105 if (route != null) {
106 mRoutes.remove(routeId);
107 publishRoutes();
108 mRoutes.put(routeId, route);
109 }
110 }
111 }
112
Kyunglyul Hyun3aedf022019-04-15 16:38:19 +0900113 void publishRoutes() {
114 MediaRoute2ProviderInfo info = new MediaRoute2ProviderInfo.Builder()
115 .addRoutes(mRoutes.values())
116 .build();
117 setProviderInfo(info);
Kyunglyul Hyund51666d2019-04-11 04:08:40 +0000118 }
119}