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.embmsfrontend; 18 19 import android.net.Uri; 20 import android.telephony.MbmsStreamingSession; 21 import android.telephony.mbms.StreamingService; 22 import android.telephony.mbms.StreamingServiceCallback; 23 import android.telephony.mbms.StreamingServiceInfo; 24 import android.widget.Toast; 25 26 public class StreamingServiceTracker { 27 private class Callback extends StreamingServiceCallback { 28 @Override 29 public void onError(int errorCode, String message) { 30 String toastMessage = "Error: " + errorCode + ": " + message; 31 mActivity.runOnUiThread(() -> 32 Toast.makeText(mActivity, toastMessage, Toast.LENGTH_SHORT).show()); 33 } 34 35 @Override 36 public void onStreamStateUpdated(int state, int reason) { 37 StreamingServiceTracker.this.onStreamStateUpdated(state, reason); 38 } 39 40 @Override 41 public void onStreamMethodUpdated(int method) { 42 StreamingServiceTracker.this.onStreamMethodUpdated(method); 43 } 44 } 45 46 private final EmbmsTestStreamingApp mActivity; 47 private final StreamingServiceInfo mStreamingServiceInfo; 48 private StreamingService mStreamingService; 49 50 private int mState = StreamingService.STATE_STOPPED; 51 private Uri mStreamingUri = Uri.EMPTY; 52 private int mMethod = StreamingService.UNICAST_METHOD; 53 54 public StreamingServiceTracker(EmbmsTestStreamingApp appActivity, StreamingServiceInfo info) { 55 mActivity = appActivity; 56 mStreamingServiceInfo = info; 57 } 58 59 /** 60 * Start streaming using the provided streaming session 61 */ 62 public boolean startStreaming(MbmsStreamingSession streamingManager) { 63 mStreamingService = streamingManager.startStreaming(mStreamingServiceInfo, 64 mActivity.getMainThreadHandler()::post, new Callback()); 65 return true; 66 } 67 68 public void stopStreaming() { 69 mStreamingService.close(); 70 } 71 72 public String getServiceId() { 73 return mStreamingServiceInfo.getServiceId(); 74 } 75 76 public int getState() { 77 return mState; 78 } 79 80 public Uri getUri() { 81 return mStreamingUri; 82 } 83 84 public int getMethod() { 85 return mMethod; 86 } 87 88 private void onStreamStateUpdated(int state, int reason) { 89 if (state == StreamingService.STATE_STARTED && mState != StreamingService.STATE_STARTED) { 90 mStreamingUri = mStreamingService.getPlaybackUri(); 91 mActivity.updateUri(); 92 } 93 mState = state; 94 mActivity.updateStreamingState(); 95 mActivity.runOnUiThread(() -> 96 Toast.makeText(mActivity, "State change reason: " + reason, Toast.LENGTH_SHORT) 97 .show()); 98 } 99 100 private void onStreamMethodUpdated(int method) { 101 if (mMethod != method) { 102 mMethod = method; 103 mActivity.updateMethod(); 104 } 105 } 106 107 @Override 108 public String toString() { 109 return "Tracked service with ID " + getServiceId(); 110 } 111 } 112