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.Handler;
20import android.os.RemoteException;
21
22/**
23 * @hide
24 */
25public class InternalDownloadStateCallback extends IDownloadStateCallback.Stub {
26    private final Handler mHandler;
27    private final DownloadStateCallback mAppCallback;
28    private volatile boolean mIsStopped = false;
29
30    public InternalDownloadStateCallback(DownloadStateCallback appCallback, Handler handler) {
31        mAppCallback = appCallback;
32        mHandler = handler;
33    }
34
35    @Override
36    public void onProgressUpdated(final DownloadRequest request, final FileInfo fileInfo,
37            final int currentDownloadSize, final int fullDownloadSize, final int currentDecodedSize,
38            final int fullDecodedSize) throws RemoteException {
39        if (mIsStopped) {
40            return;
41        }
42
43        mHandler.post(new Runnable() {
44            @Override
45            public void run() {
46                mAppCallback.onProgressUpdated(request, fileInfo, currentDownloadSize,
47                        fullDownloadSize, currentDecodedSize, fullDecodedSize);
48            }
49        });
50    }
51
52    @Override
53    public void onStateUpdated(final DownloadRequest request, final FileInfo fileInfo,
54            final int state) throws RemoteException {
55        if (mIsStopped) {
56            return;
57        }
58
59        mHandler.post(new Runnable() {
60            @Override
61            public void run() {
62                mAppCallback.onStateUpdated(request, fileInfo, state);
63            }
64        });
65    }
66
67    public void stop() {
68        mIsStopped = true;
69    }
70}
71