157 lines
4.7 KiB
Java
157 lines
4.7 KiB
Java
package com.afunx.ble.blelitelib.operation;
|
|
|
|
import android.bluetooth.BluetoothGattCallback;
|
|
import android.os.Looper;
|
|
import android.util.LongSparseArray;
|
|
import android.util.SparseArray;
|
|
import com.afunx.ble.blelitelib.connector.BleConnector;
|
|
import com.afunx.ble.blelitelib.log.BleLiteLog;
|
|
import com.afunx.ble.blelitelib.proxy.BleGattClientProxy;
|
|
import com.afunx.ble.blelitelib.threadpool.BleThreadpool;
|
|
import java.util.concurrent.Future;
|
|
|
|
/* loaded from: classes.dex */
|
|
public abstract class BleOperationAbs<T> implements BleOperation {
|
|
private static final String TAG = "BleOperationAbs";
|
|
private BluetoothGattCallback mBluetoothGattCallback;
|
|
private BleConnector mConnector;
|
|
private BleGattClientProxy mOwner;
|
|
private T mResult;
|
|
private static final BleThreadpool sThreadpool = BleThreadpool.getInstance();
|
|
private static final SparseArray<Future<?>> mInterruptableTasks = new SparseArray<>();
|
|
private final LongSparseArray<Runnable> mRunnables = new LongSparseArray<>();
|
|
private final BleOperationLock mLock = new BleOperationLock();
|
|
|
|
private void clearBleOperationAbs() {
|
|
this.mRunnables.clear();
|
|
this.mResult = null;
|
|
this.mOwner = null;
|
|
this.mConnector = null;
|
|
this.mBluetoothGattCallback = null;
|
|
}
|
|
|
|
private Runnable getRunnable(long j, boolean z) {
|
|
Runnable runnable;
|
|
synchronized (this.mRunnables) {
|
|
runnable = this.mRunnables.get(j);
|
|
if (z) {
|
|
this.mRunnables.remove(j);
|
|
}
|
|
}
|
|
return runnable;
|
|
}
|
|
|
|
public final void clear() {
|
|
BleLiteLog.v(TAG, "clear() called by " + this);
|
|
clearConcurrentOperation();
|
|
clearBleOperationAbs();
|
|
}
|
|
|
|
protected abstract void clearConcurrentOperation();
|
|
|
|
public final void doRunnableAsync(long j, boolean z) {
|
|
Runnable runnable = getRunnable(j, z);
|
|
if (runnable != null) {
|
|
sThreadpool.submit(runnable);
|
|
}
|
|
}
|
|
|
|
public final void doRunnableSelfAsync(boolean z) {
|
|
if (!z) {
|
|
sThreadpool.submit(this);
|
|
} else {
|
|
if (Looper.getMainLooper() == Looper.myLooper()) {
|
|
throw new IllegalStateException("It can't be called in UI Main Thread.");
|
|
}
|
|
sThreadpool.submitInMain(this);
|
|
}
|
|
}
|
|
|
|
public final void doRunnableSelfAsyncInterruptable(int i) {
|
|
if (Looper.getMainLooper() == Looper.myLooper()) {
|
|
throw new IllegalStateException("It can't be called in UI Main Thread.");
|
|
}
|
|
if (!(this instanceof BleInterruptable)) {
|
|
throw new IllegalArgumentException("Only BleInterruptable could call the method.");
|
|
}
|
|
synchronized (mInterruptableTasks) {
|
|
Future<?> future = mInterruptableTasks.get(i);
|
|
if (future != null) {
|
|
future.cancel(true);
|
|
}
|
|
mInterruptableTasks.put(i, sThreadpool.submit(this));
|
|
}
|
|
}
|
|
|
|
public final void doRunnableUIAsync(long j, boolean z) {
|
|
Runnable runnable = getRunnable(j, z);
|
|
if (runnable != null) {
|
|
sThreadpool.submitInMain(runnable);
|
|
}
|
|
}
|
|
|
|
public final BluetoothGattCallback getBluetoothGattCallback() {
|
|
return this.mBluetoothGattCallback;
|
|
}
|
|
|
|
public final BleConnector getConnector() {
|
|
return this.mConnector;
|
|
}
|
|
|
|
public final BleGattClientProxy getOwner() {
|
|
return this.mOwner;
|
|
}
|
|
|
|
public final T getResult() {
|
|
return this.mResult;
|
|
}
|
|
|
|
public final boolean isNotified() {
|
|
return this.mLock.isNotified();
|
|
}
|
|
|
|
public final void notifyLock() {
|
|
this.mLock.notifyLock();
|
|
}
|
|
|
|
public final void putRunnable(long j, Runnable runnable) {
|
|
synchronized (this.mRunnables) {
|
|
this.mRunnables.put(j, runnable);
|
|
}
|
|
}
|
|
|
|
public final void removeRunnable(long j) {
|
|
synchronized (this.mRunnables) {
|
|
this.mRunnables.remove(j);
|
|
}
|
|
}
|
|
|
|
public final void setBluetoothGattCallback(BluetoothGattCallback bluetoothGattCallback) {
|
|
this.mBluetoothGattCallback = bluetoothGattCallback;
|
|
}
|
|
|
|
public final void setConnector(BleConnector bleConnector) {
|
|
this.mConnector = bleConnector;
|
|
}
|
|
|
|
public final void setOwner(BleGattClientProxy bleGattClientProxy) {
|
|
this.mOwner = bleGattClientProxy;
|
|
}
|
|
|
|
public final void setResult(T t) {
|
|
this.mResult = t;
|
|
}
|
|
|
|
public String toString() {
|
|
return String.format("[%s-%d-%04x]", getClass().getSimpleName(), Integer.valueOf(hashCode()), Long.valueOf(getOperatcionCode()));
|
|
}
|
|
|
|
public final void waitLock(long j) {
|
|
this.mLock.waitLock(j);
|
|
}
|
|
|
|
public final Runnable getRunnable(long j) {
|
|
return getRunnable(j, false);
|
|
}
|
|
}
|