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
17package android.telephony.mbms;
18
19import android.os.Binder;
20import android.os.RemoteException;
21
22import java.util.List;
23import java.util.concurrent.Executor;
24
25/** @hide */
26public class InternalStreamingSessionCallback extends IMbmsStreamingSessionCallback.Stub {
27    private final Executor mExecutor;
28    private final MbmsStreamingSessionCallback mAppCallback;
29    private volatile boolean mIsStopped = false;
30
31    public InternalStreamingSessionCallback(MbmsStreamingSessionCallback appCallback,
32            Executor executor) {
33        mAppCallback = appCallback;
34        mExecutor = executor;
35    }
36
37    @Override
38    public void onError(final int errorCode, final String message) throws RemoteException {
39        if (mIsStopped) {
40            return;
41        }
42
43        mExecutor.execute(new Runnable() {
44            @Override
45            public void run() {
46                long token = Binder.clearCallingIdentity();
47                try {
48                    mAppCallback.onError(errorCode, message);
49                } finally {
50                    Binder.restoreCallingIdentity(token);
51                }
52            }
53        });
54    }
55
56    @Override
57    public void onStreamingServicesUpdated(final List<StreamingServiceInfo> services)
58            throws RemoteException {
59        if (mIsStopped) {
60            return;
61        }
62
63        mExecutor.execute(new Runnable() {
64            @Override
65            public void run() {
66                long token = Binder.clearCallingIdentity();
67                try {
68                    mAppCallback.onStreamingServicesUpdated(services);
69                } finally {
70                    Binder.restoreCallingIdentity(token);
71                }
72            }
73        });
74    }
75
76    @Override
77    public void onMiddlewareReady() throws RemoteException {
78        if (mIsStopped) {
79            return;
80        }
81
82        mExecutor.execute(new Runnable() {
83            @Override
84            public void run() {
85                long token = Binder.clearCallingIdentity();
86                try {
87                    mAppCallback.onMiddlewareReady();
88                } finally {
89                    Binder.restoreCallingIdentity(token);
90                }
91            }
92        });
93    }
94
95    public void stop() {
96        mIsStopped = true;
97    }
98}
99