1/*
2 * Copyright (C) 2018 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.concurrent.Executor;
23
24/**
25 * @hide
26 */
27public class InternalDownloadProgressListener extends IDownloadProgressListener.Stub {
28    private final Executor mExecutor;
29    private final DownloadProgressListener mAppListener;
30    private volatile boolean mIsStopped = false;
31
32    public InternalDownloadProgressListener(DownloadProgressListener appListener,
33            Executor executor) {
34        mAppListener = appListener;
35        mExecutor = executor;
36    }
37
38    @Override
39    public void onProgressUpdated(final DownloadRequest request, final FileInfo fileInfo,
40            final int currentDownloadSize, final int fullDownloadSize, final int currentDecodedSize,
41            final int fullDecodedSize) throws RemoteException {
42        if (mIsStopped) {
43            return;
44        }
45
46        mExecutor.execute(new Runnable() {
47            @Override
48            public void run() {
49                long token = Binder.clearCallingIdentity();
50                try {
51                    mAppListener.onProgressUpdated(request, fileInfo, currentDownloadSize,
52                            fullDownloadSize, currentDecodedSize, fullDecodedSize);
53                } finally {
54                    Binder.restoreCallingIdentity(token);
55                }
56            }
57        });
58    }
59
60    public void stop() {
61        mIsStopped = true;
62    }
63}
64