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 android.telephony.mbms; 18 19 import android.os.Binder; 20 import android.os.RemoteException; 21 22 import java.util.concurrent.Executor; 23 24 /** @hide */ 25 public class InternalStreamingServiceCallback extends IStreamingServiceCallback.Stub { 26 private final StreamingServiceCallback mAppCallback; 27 private final Executor mExecutor; 28 private volatile boolean mIsStopped = false; 29 InternalStreamingServiceCallback(StreamingServiceCallback appCallback, Executor executor)30 public InternalStreamingServiceCallback(StreamingServiceCallback appCallback, 31 Executor executor) { 32 mAppCallback = appCallback; 33 mExecutor = executor; 34 } 35 36 @Override onError(final int errorCode, final String message)37 public void onError(final int errorCode, final String message) throws RemoteException { 38 if (mIsStopped) { 39 return; 40 } 41 42 final long token = Binder.clearCallingIdentity(); 43 try { 44 mExecutor.execute(new Runnable() { 45 @Override 46 public void run() { 47 mAppCallback.onError(errorCode, message); 48 } 49 }); 50 } finally { 51 Binder.restoreCallingIdentity(token); 52 } 53 } 54 55 @Override onStreamStateUpdated(final int state, final int reason)56 public void onStreamStateUpdated(final int state, final int reason) throws RemoteException { 57 if (mIsStopped) { 58 return; 59 } 60 61 final long token = Binder.clearCallingIdentity(); 62 try { 63 mExecutor.execute(new Runnable() { 64 @Override 65 public void run() { 66 mAppCallback.onStreamStateUpdated(state, reason); 67 } 68 }); 69 } finally { 70 Binder.restoreCallingIdentity(token); 71 } 72 } 73 74 @Override onMediaDescriptionUpdated()75 public void onMediaDescriptionUpdated() throws RemoteException { 76 if (mIsStopped) { 77 return; 78 } 79 80 final long token = Binder.clearCallingIdentity(); 81 try { 82 mExecutor.execute(new Runnable() { 83 @Override 84 public void run() { 85 mAppCallback.onMediaDescriptionUpdated(); 86 } 87 }); 88 } finally { 89 Binder.restoreCallingIdentity(token); 90 } 91 } 92 93 @Override onBroadcastSignalStrengthUpdated(final int signalStrength)94 public void onBroadcastSignalStrengthUpdated(final int signalStrength) throws RemoteException { 95 if (mIsStopped) { 96 return; 97 } 98 99 final long token = Binder.clearCallingIdentity(); 100 try { 101 mExecutor.execute(new Runnable() { 102 @Override 103 public void run() { 104 mAppCallback.onBroadcastSignalStrengthUpdated(signalStrength); 105 } 106 }); 107 } finally { 108 Binder.restoreCallingIdentity(token); 109 } 110 } 111 112 @Override onStreamMethodUpdated(final int methodType)113 public void onStreamMethodUpdated(final int methodType) throws RemoteException { 114 if (mIsStopped) { 115 return; 116 } 117 118 final long token = Binder.clearCallingIdentity(); 119 try { 120 mExecutor.execute(new Runnable() { 121 @Override 122 public void run() { 123 mAppCallback.onStreamMethodUpdated(methodType); 124 } 125 }); 126 } finally { 127 Binder.restoreCallingIdentity(token); 128 } 129 } 130 stop()131 public void stop() { 132 mIsStopped = true; 133 } 134 } 135