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.telephony.mbms.StreamingService;
20 import android.telephony.mbms.StreamingServiceCallback;
21 import android.util.Log;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 // Singleton that keeps track of streaming states for all apps using the middleware.
27 public class StreamStateTracker {
28     private static final String LOG_TAG = "MbmsStreamStateTracker";
29 
30     private static final Map<FrontendAppIdentifier, AppActiveStreams>
31             sPerAppStreamStates = new HashMap<>();
32 
getStreamingState(FrontendAppIdentifier appIdentifier, String serviceId)33     public static int getStreamingState(FrontendAppIdentifier appIdentifier, String serviceId) {
34         AppActiveStreams appStreams = sPerAppStreamStates.get(appIdentifier);
35         if (appStreams == null) {
36             return StreamingService.STATE_STOPPED;
37         }
38         return appStreams.getStateForService(serviceId);
39     }
40 
startStreaming(FrontendAppIdentifier appIdentifier, String serviceId, StreamingServiceCallback callback, int reason)41     public static void startStreaming(FrontendAppIdentifier appIdentifier, String serviceId,
42             StreamingServiceCallback callback, int reason) {
43         AppActiveStreams appStreams = sPerAppStreamStates.get(appIdentifier);
44         if (appStreams == null) {
45             appStreams = new AppActiveStreams(appIdentifier);
46             sPerAppStreamStates.put(appIdentifier, appStreams);
47         }
48 
49         appStreams.startStreaming(serviceId, callback, reason);
50     }
51 
stopStreaming(FrontendAppIdentifier appIdentifier, String serviceId, int reason)52     public static void stopStreaming(FrontendAppIdentifier appIdentifier, String serviceId,
53             int reason) {
54         Log.i(LOG_TAG, "Stopping stream " + serviceId);
55         AppActiveStreams appStreams = sPerAppStreamStates.get(appIdentifier);
56         if (appStreams == null) {
57             // It was never started, so don't bother stopping.
58             return;
59         }
60         appStreams.stopStreaming(serviceId, reason);
61     }
62 
dispose(FrontendAppIdentifier appIdentifier, String serviceId)63     public static void dispose(FrontendAppIdentifier appIdentifier, String serviceId) {
64         AppActiveStreams appStreams = sPerAppStreamStates.get(appIdentifier);
65         if (appStreams == null) {
66             // We have no record of this app, so we can just move on.
67             return;
68         }
69         appStreams.dispose(serviceId);
70     }
71 
disposeAll(FrontendAppIdentifier appIdentifier)72     public static void disposeAll(FrontendAppIdentifier appIdentifier) {
73         sPerAppStreamStates.remove(appIdentifier);
74     }
75 
76     // Do not instantiate
StreamStateTracker()77     private StreamStateTracker() {}
78 }
79