1 /*
2  * Copyright (C) 2017 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.phone.testapps.embmsmw;
18 
19 import android.net.Uri;
20 import android.telephony.mbms.StreamingServiceInfo;
21 
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 
30 public class StreamingServiceRepository {
31     private static final String STREAMING_SCHEME = "stream";
32     private static final String STREAMING_URI_SSP_PREFIX = "identifier/";
33 
34     private static int sServiceIdCounter = 0;
35     private static final Map<String, StreamingServiceInfo> sIdToServiceInfo =
36             new HashMap<>();
37     private static final Map<String, Uri> sIdToStreamingUri = new HashMap<>();
38 
39     static {
fetchStreamingServices()40         fetchStreamingServices();
41     }
42 
getStreamingServicesForClasses( List<String> serviceClasses)43     public static List<StreamingServiceInfo> getStreamingServicesForClasses(
44             List<String> serviceClasses) {
45         return sIdToServiceInfo.values().stream()
46                 .filter((info) -> serviceClasses.contains(info.getServiceClassName()))
47                 .collect(Collectors.toList());
48     }
49 
getUriForService(String serviceId)50     public static Uri getUriForService(String serviceId) {
51         if (sIdToStreamingUri.containsKey(serviceId)) {
52             return sIdToStreamingUri.get(serviceId);
53         }
54         return null;
55     }
56 
getStreamingServiceInfoForId(String serviceId)57     public static StreamingServiceInfo getStreamingServiceInfoForId(String serviceId) {
58         return sIdToServiceInfo.getOrDefault(serviceId, null);
59     }
60 
createStreamingService(String className)61     private static void createStreamingService(String className) {
62         sServiceIdCounter++;
63         String id = "StreamingServiceId[" + sServiceIdCounter + "]";
64         Map<Locale, String> localeDict = new HashMap<Locale, String>() {{
65                 put(Locale.US, "Entertainment Source " + sServiceIdCounter);
66                 put(Locale.CANADA, "Entertainment Source, eh?" + sServiceIdCounter);
67         }};
68         List<Locale> locales = new ArrayList<Locale>() {{
69                 add(Locale.CANADA);
70                 add(Locale.US);
71         }};
72         StreamingServiceInfo info = new StreamingServiceInfo(localeDict, className, locales,
73                 id, new Date(System.currentTimeMillis() - 10000),
74                 new Date(System.currentTimeMillis() + 10000));
75         sIdToServiceInfo.put(id, info);
76         sIdToStreamingUri.put(id, Uri.fromParts(STREAMING_SCHEME,
77                 STREAMING_URI_SSP_PREFIX + sServiceIdCounter,
78                 null));
79     }
80 
fetchStreamingServices()81     private static void fetchStreamingServices() {
82         createStreamingService("Class1");
83         createStreamingService("Class2");
84         createStreamingService("Class3");
85         createStreamingService("Class4");
86         createStreamingService("Class5");
87         createStreamingService("Class6");
88     }
89 
90     // Do not instantiate
StreamingServiceRepository()91     private StreamingServiceRepository() {}
92 }
93