Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/async/BackgroundJob.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.async; | ||||
22 | | ||||
23 | import android.support.annotation.NonNull; | ||||
24 | | ||||
25 | import java.util.concurrent.atomic.AtomicLong; | ||||
26 | | ||||
27 | public abstract class BackgroundJob<I, R> implements Runnable { | ||||
28 | private static AtomicLong atomicLong = new AtomicLong(0); | ||||
29 | protected volatile boolean canceled; | ||||
30 | private BackgroundJobHandler backgroundJobHandler; | ||||
31 | private long id; | ||||
32 | | ||||
33 | protected I requestInfo; | ||||
34 | private Callback<R> callback; | ||||
35 | | ||||
36 | public BackgroundJob(I requestInfo, Callback<R> callback) { | ||||
37 | this.id = atomicLong.incrementAndGet(); | ||||
38 | this.requestInfo = requestInfo; | ||||
39 | this.callback = callback; | ||||
40 | } | ||||
41 | | ||||
42 | void setBackgroundJobHandler(BackgroundJobHandler handler) { | ||||
43 | this.backgroundJobHandler = handler; | ||||
44 | } | ||||
45 | | ||||
46 | public long getId() { return id; } | ||||
47 | public I getRequestInfo() { return requestInfo; } | ||||
48 | | ||||
49 | public void cancel() { | ||||
50 | canceled = true; | ||||
51 | backgroundJobHandler.cancelJob(this); | ||||
52 | } | ||||
53 | | ||||
54 | public boolean isCancelled() { | ||||
55 | return canceled; | ||||
56 | } | ||||
57 | | ||||
58 | public interface Callback<R> { | ||||
59 | void onResult(@NonNull BackgroundJob job, R result); | ||||
60 | void onError(@NonNull BackgroundJob job, @NonNull Throwable error); | ||||
61 | } | ||||
62 | | ||||
63 | protected void reportResult(R result) { | ||||
64 | backgroundJobHandler.runOnUiThread(() -> { | ||||
65 | callback.onResult(this, result); | ||||
66 | backgroundJobHandler.onFinished(this); | ||||
67 | }); | ||||
68 | } | ||||
69 | | ||||
70 | protected void reportError(@NonNull Throwable error) { | ||||
71 | backgroundJobHandler.runOnUiThread(() -> { | ||||
72 | callback.onError(this, error); | ||||
73 | backgroundJobHandler.onFinished(this); | ||||
74 | }); | ||||
75 | } | ||||
76 | } |