337 lines
14 KiB
Java
337 lines
14 KiB
Java
package com.kofax.kmc.kut.utilities.async;
|
|
|
|
import android.os.Handler;
|
|
import android.os.Looper;
|
|
import com.kofax.kmc.kut.utilities.error.ErrorInfo;
|
|
import com.kofax.mobile.sdk._internal.k;
|
|
import java.util.EventObject;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import java.util.concurrent.Future;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
/* loaded from: classes3.dex */
|
|
public class TaskRunner {
|
|
private static final String TAG = "TaskRunner";
|
|
private static final int ss = 50;
|
|
private int st;
|
|
private ExecutorService su;
|
|
private Map<Callable<?>, CopyOnWriteArrayList<TaskCompletedListener>> sv = new ConcurrentHashMap();
|
|
private CopyOnWriteArrayList<TaskCompletedListener> sw = new CopyOnWriteArrayList<>();
|
|
private Map<Long, Callable<?>> sx = new ConcurrentHashMap();
|
|
private Map<Callable<?>, Long> sy = new ConcurrentHashMap();
|
|
private long sz = UUID.randomUUID().getLeastSignificantBits() >>> 1;
|
|
private long sA = UUID.randomUUID().getLeastSignificantBits() >>> 1;
|
|
|
|
/* loaded from: classes3.dex */
|
|
public interface TaskCompletedListener {
|
|
void onTaskCompleted(TaskCompletedEvent taskCompletedEvent);
|
|
}
|
|
|
|
/* loaded from: classes3.dex */
|
|
public class TaskCompletedEvent extends EventObject {
|
|
private static final long serialVersionUID = 1;
|
|
private long bb;
|
|
private Object sB;
|
|
private ErrorInfo sC;
|
|
final TaskRunner sD;
|
|
|
|
/* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
|
|
public TaskCompletedEvent(TaskRunner taskRunner, Object obj, long j, Object obj2, ErrorInfo errorInfo) {
|
|
super(obj);
|
|
this.sD = taskRunner;
|
|
this.bb = j;
|
|
this.sC = errorInfo;
|
|
this.sB = obj2;
|
|
}
|
|
|
|
public final Object getTaskReturnValue() {
|
|
return this.sB;
|
|
}
|
|
|
|
public final long getTaskID() {
|
|
return this.bb;
|
|
}
|
|
|
|
public final ErrorInfo getTaskError() {
|
|
return this.sC;
|
|
}
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes3.dex */
|
|
public class a {
|
|
public long sA;
|
|
final TaskRunner sD;
|
|
public Future<?> sE;
|
|
|
|
a(TaskRunner taskRunner, Future<?> future, long j) {
|
|
this.sD = taskRunner;
|
|
this.sE = future;
|
|
this.sA = j;
|
|
}
|
|
}
|
|
|
|
public TaskRunner(int i) {
|
|
this.st = 1;
|
|
this.su = null;
|
|
if (i <= 0) {
|
|
throw new IllegalArgumentException("threads parameter on constructor must be > 0");
|
|
}
|
|
if (i > 50) {
|
|
throw new IllegalArgumentException("threads parameter on constructor is > max thread pool size: 50");
|
|
}
|
|
int i2 = i << 1;
|
|
this.st = i2;
|
|
this.su = Executors.newFixedThreadPool(i2);
|
|
}
|
|
|
|
public final void addOnTaskCompletedListener(TaskCompletedListener taskCompletedListener, Callable<?> callable, boolean z) {
|
|
synchronized (this) {
|
|
CopyOnWriteArrayList<TaskCompletedListener> copyOnWriteArrayList = this.sv.get(callable);
|
|
if (copyOnWriteArrayList == null) {
|
|
copyOnWriteArrayList = new CopyOnWriteArrayList<>();
|
|
this.sv.put(callable, copyOnWriteArrayList);
|
|
}
|
|
copyOnWriteArrayList.addIfAbsent(taskCompletedListener);
|
|
if (z) {
|
|
this.sw.addIfAbsent(taskCompletedListener);
|
|
}
|
|
}
|
|
}
|
|
|
|
public final void removeOnTaskCompletedListener(TaskCompletedListener taskCompletedListener, Callable<?> callable) {
|
|
synchronized (this) {
|
|
CopyOnWriteArrayList<TaskCompletedListener> copyOnWriteArrayList = this.sv.get(callable);
|
|
if (copyOnWriteArrayList != null) {
|
|
copyOnWriteArrayList.remove(taskCompletedListener);
|
|
if (copyOnWriteArrayList.isEmpty()) {
|
|
this.sv.remove(callable);
|
|
}
|
|
}
|
|
this.sw.remove(taskCompletedListener);
|
|
}
|
|
}
|
|
|
|
public long submit(Callable<?> callable) {
|
|
long j;
|
|
synchronized (this) {
|
|
if (callable == null) {
|
|
throw new NullPointerException("'Callable<?> task' parameter passed to submit is null");
|
|
}
|
|
if (this.su == null) {
|
|
throw new IllegalThreadStateException("submit failure: threadpool has not been created");
|
|
}
|
|
if (!this.sv.containsKey(callable)) {
|
|
throw new UnsupportedOperationException("submit failure: no Image Captured Listeners have been registered -> addOnImageCapturedListener()");
|
|
}
|
|
if (this.su.isShutdown()) {
|
|
this.su = Executors.newFixedThreadPool(this.st);
|
|
this.sv.clear();
|
|
this.sx.clear();
|
|
this.sy.clear();
|
|
this.sw.clear();
|
|
}
|
|
Future submit = this.su.submit(callable);
|
|
long j2 = this.sA;
|
|
this.sA = j2 + 1;
|
|
a aVar = new a(this, submit, j2);
|
|
this.sy.put(callable, Long.valueOf(this.sz));
|
|
this.sx.put(Long.valueOf(aVar.sA), callable);
|
|
this.su.execute(new b(this, aVar));
|
|
j = this.sz;
|
|
this.sz = 1 + j;
|
|
}
|
|
return j;
|
|
}
|
|
|
|
public void shutdown() {
|
|
synchronized (this) {
|
|
ExecutorService executorService = this.su;
|
|
if (executorService != null) {
|
|
executorService.shutdown();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<Runnable> shutdownNow() {
|
|
List<Runnable> shutdownNow;
|
|
synchronized (this) {
|
|
ExecutorService executorService = this.su;
|
|
shutdownNow = executorService != null ? executorService.shutdownNow() : null;
|
|
}
|
|
return shutdownNow;
|
|
}
|
|
|
|
public boolean awaitTermination(long j, TimeUnit timeUnit) throws InterruptedException {
|
|
boolean awaitTermination;
|
|
synchronized (this) {
|
|
ExecutorService executorService = this.su;
|
|
awaitTermination = executorService != null ? executorService.awaitTermination(j, timeUnit) : true;
|
|
}
|
|
return awaitTermination;
|
|
}
|
|
|
|
public boolean isTerminated() {
|
|
boolean z;
|
|
synchronized (this) {
|
|
ExecutorService executorService = this.su;
|
|
if (executorService != null) {
|
|
z = executorService.isTerminated();
|
|
}
|
|
}
|
|
return z;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes3.dex */
|
|
public class b implements Runnable {
|
|
final TaskRunner sD;
|
|
private a sF;
|
|
private Handler handler = new Handler(Looper.getMainLooper());
|
|
private ErrorInfo sC = ErrorInfo.KMC_SUCCESS;
|
|
private Object sB = null;
|
|
private TaskCompletedListener sG = null;
|
|
|
|
b(TaskRunner taskRunner, a aVar) {
|
|
this.sD = taskRunner;
|
|
this.sF = null;
|
|
this.sF = aVar;
|
|
}
|
|
|
|
private void a(CopyOnWriteArrayList<TaskCompletedListener> copyOnWriteArrayList, Long l) {
|
|
String str = TaskRunner.TAG;
|
|
StringBuilder sb = new StringBuilder("TaskWaiter(): Call the TaskCompleted handler on thread id: ");
|
|
sb.append(Thread.currentThread().getId());
|
|
k.c(str, sb.toString());
|
|
Iterator<TaskCompletedListener> it = copyOnWriteArrayList.iterator();
|
|
while (it.hasNext()) {
|
|
this.sG = it.next();
|
|
if (!this.sD.sw.contains(this.sG)) {
|
|
this.sG.onTaskCompleted(new TaskCompletedEvent(this.sD, this, l.longValue(), this.sB, this.sC));
|
|
} else {
|
|
this.handler.post(new Runnable(this, l) { // from class: com.kofax.kmc.kut.utilities.async.TaskRunner.b.1
|
|
final Long sH;
|
|
final b sI;
|
|
|
|
{
|
|
this.sI = this;
|
|
this.sH = l;
|
|
}
|
|
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
this.sI.sG.onTaskCompleted(new TaskCompletedEvent(this.sI.sD, this, this.sH.longValue(), this.sI.sB, this.sI.sC));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
/* JADX WARN: Multi-variable type inference failed */
|
|
/* JADX WARN: Type inference failed for: r0v0, types: [java.lang.String] */
|
|
/* JADX WARN: Type inference failed for: r0v15, types: [java.util.Map] */
|
|
/* JADX WARN: Type inference failed for: r1v0, types: [java.lang.String] */
|
|
/* JADX WARN: Type inference failed for: r1v17, types: [java.lang.Long] */
|
|
/* JADX WARN: Type inference failed for: r1v23 */
|
|
/* JADX WARN: Type inference failed for: r1v24 */
|
|
/* JADX WARN: Type inference failed for: r1v25 */
|
|
/* JADX WARN: Type inference failed for: r2v0, types: [java.lang.String] */
|
|
/* JADX WARN: Type inference failed for: r2v14, types: [java.util.concurrent.CopyOnWriteArrayList] */
|
|
/* JADX WARN: Type inference failed for: r2v19 */
|
|
/* JADX WARN: Type inference failed for: r2v20 */
|
|
/* JADX WARN: Type inference failed for: r2v21 */
|
|
@Override // java.lang.Runnable
|
|
public void run() {
|
|
Callable callable;
|
|
String str = "TaskWaiter unable to retrieve taskID from taskSubmitted Map";
|
|
String str2 = "TaskWaiter unable to retrieve listeners List from taskCompletedListeners Map";
|
|
String str3 = "TaskWaiter unable to retrieve task from taskCompletedFutures Map";
|
|
try {
|
|
try {
|
|
this.sB = this.sF.sE.get();
|
|
callable = (Callable) this.sD.sx.get(Long.valueOf(this.sF.sA));
|
|
} catch (InterruptedException e) {
|
|
ErrorInfo errorInfo = ErrorInfo.KMC_GN_UNKNOWN_ERROR;
|
|
this.sC = errorInfo;
|
|
errorInfo.setErrCause(e.getCause().getMessage());
|
|
callable = (Callable) this.sD.sx.get(Long.valueOf(this.sF.sA));
|
|
if (callable == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve task from taskCompletedFutures Map");
|
|
}
|
|
CopyOnWriteArrayList copyOnWriteArrayList = (CopyOnWriteArrayList) this.sD.sv.get(callable);
|
|
if (copyOnWriteArrayList == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve listeners List from taskCompletedListeners Map");
|
|
}
|
|
Long l = (Long) this.sD.sy.get(callable);
|
|
str2 = l;
|
|
str3 = copyOnWriteArrayList;
|
|
if (l == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve taskID from taskSubmitted Map");
|
|
}
|
|
} catch (ExecutionException e2) {
|
|
ErrorInfo errorInfo2 = ErrorInfo.KMC_GN_UNKNOWN_ERROR;
|
|
this.sC = errorInfo2;
|
|
errorInfo2.setErrCause(e2.getCause().getMessage());
|
|
callable = (Callable) this.sD.sx.get(Long.valueOf(this.sF.sA));
|
|
if (callable == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve task from taskCompletedFutures Map");
|
|
}
|
|
CopyOnWriteArrayList copyOnWriteArrayList2 = (CopyOnWriteArrayList) this.sD.sv.get(callable);
|
|
if (copyOnWriteArrayList2 == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve listeners List from taskCompletedListeners Map");
|
|
}
|
|
Long l2 = (Long) this.sD.sy.get(callable);
|
|
str2 = l2;
|
|
str3 = copyOnWriteArrayList2;
|
|
if (l2 == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve taskID from taskSubmitted Map");
|
|
}
|
|
}
|
|
if (callable == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve task from taskCompletedFutures Map");
|
|
}
|
|
CopyOnWriteArrayList copyOnWriteArrayList3 = (CopyOnWriteArrayList) this.sD.sv.get(callable);
|
|
if (copyOnWriteArrayList3 == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve listeners List from taskCompletedListeners Map");
|
|
}
|
|
Long l3 = (Long) this.sD.sy.get(callable);
|
|
str2 = l3;
|
|
str3 = copyOnWriteArrayList3;
|
|
if (l3 == null) {
|
|
throw new IllegalThreadStateException("TaskWaiter unable to retrieve taskID from taskSubmitted Map");
|
|
}
|
|
this.sD.sx.remove(Long.valueOf(this.sF.sA));
|
|
str = this.sD.sy;
|
|
str.remove(callable);
|
|
a(str3, str2);
|
|
} catch (Throwable th) {
|
|
Callable callable2 = (Callable) this.sD.sx.get(Long.valueOf(this.sF.sA));
|
|
if (callable2 == null) {
|
|
throw new IllegalThreadStateException(str3);
|
|
}
|
|
CopyOnWriteArrayList copyOnWriteArrayList4 = (CopyOnWriteArrayList) this.sD.sv.get(callable2);
|
|
if (copyOnWriteArrayList4 == null) {
|
|
throw new IllegalThreadStateException(str2);
|
|
}
|
|
Long l4 = (Long) this.sD.sy.get(callable2);
|
|
if (l4 == null) {
|
|
throw new IllegalThreadStateException(str);
|
|
}
|
|
this.sD.sx.remove(Long.valueOf(this.sF.sA));
|
|
this.sD.sy.remove(callable2);
|
|
a(copyOnWriteArrayList4, l4);
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
}
|