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