248 lines
7.5 KiB
Java
248 lines
7.5 KiB
Java
package com.liulishuo.filedownloader.database;
|
|
|
|
import android.os.Handler;
|
|
import android.os.HandlerThread;
|
|
import android.os.Message;
|
|
import com.liulishuo.filedownloader.database.FileDownloadDatabase;
|
|
import com.liulishuo.filedownloader.model.ConnectionModel;
|
|
import com.liulishuo.filedownloader.model.FileDownloadModel;
|
|
import com.liulishuo.filedownloader.util.FileDownloadLog;
|
|
import com.liulishuo.filedownloader.util.FileDownloadProperties;
|
|
import com.liulishuo.filedownloader.util.FileDownloadUtils;
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.concurrent.locks.LockSupport;
|
|
|
|
/* loaded from: classes.dex */
|
|
public class RemitDatabase implements FileDownloadDatabase {
|
|
private Handler c;
|
|
private volatile Thread g;
|
|
private final List<Integer> e = new ArrayList();
|
|
private AtomicInteger f = new AtomicInteger();
|
|
private final NoDatabaseImpl a = new NoDatabaseImpl();
|
|
private final SqliteDatabaseImpl b = new SqliteDatabaseImpl();
|
|
private final long d = FileDownloadProperties.a().b;
|
|
|
|
public RemitDatabase() {
|
|
HandlerThread handlerThread = new HandlerThread(FileDownloadUtils.k("RemitHandoverToDB"));
|
|
handlerThread.start();
|
|
this.c = new Handler(handlerThread.getLooper(), new Handler.Callback() { // from class: com.liulishuo.filedownloader.database.RemitDatabase.1
|
|
@Override // android.os.Handler.Callback
|
|
public boolean handleMessage(Message message) {
|
|
int i = message.what;
|
|
if (i == 0) {
|
|
if (RemitDatabase.this.g != null) {
|
|
LockSupport.unpark(RemitDatabase.this.g);
|
|
RemitDatabase.this.g = null;
|
|
}
|
|
return false;
|
|
}
|
|
try {
|
|
RemitDatabase.this.f.set(i);
|
|
RemitDatabase.this.h(i);
|
|
RemitDatabase.this.e.add(Integer.valueOf(i));
|
|
return false;
|
|
} finally {
|
|
RemitDatabase.this.f.set(0);
|
|
if (RemitDatabase.this.g != null) {
|
|
LockSupport.unpark(RemitDatabase.this.g);
|
|
RemitDatabase.this.g = null;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private void f(int i) {
|
|
this.c.removeMessages(i);
|
|
if (this.f.get() != i) {
|
|
h(i);
|
|
return;
|
|
}
|
|
this.g = Thread.currentThread();
|
|
this.c.sendEmptyMessage(0);
|
|
LockSupport.park();
|
|
}
|
|
|
|
private boolean g(int i) {
|
|
return !this.e.contains(Integer.valueOf(i));
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public void h(int i) {
|
|
if (FileDownloadLog.a) {
|
|
FileDownloadLog.a(this, "sync cache to db %d", Integer.valueOf(i));
|
|
}
|
|
this.b.a(this.a.e(i));
|
|
List<ConnectionModel> d = this.a.d(i);
|
|
this.b.b(i);
|
|
Iterator<ConnectionModel> it = d.iterator();
|
|
while (it.hasNext()) {
|
|
this.b.a(it.next());
|
|
}
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void clear() {
|
|
this.a.clear();
|
|
this.b.clear();
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public List<ConnectionModel> d(int i) {
|
|
return this.a.d(i);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public FileDownloadModel e(int i) {
|
|
return this.a.e(i);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public boolean remove(int i) {
|
|
this.b.remove(i);
|
|
return this.a.remove(i);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void b(int i) {
|
|
this.a.b(i);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.b(i);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void c(int i) {
|
|
this.c.sendEmptyMessageDelayed(i, this.d);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void c(int i, long j) {
|
|
this.a.c(i, j);
|
|
if (g(i)) {
|
|
f(i);
|
|
}
|
|
this.b.c(i, j);
|
|
this.e.remove(Integer.valueOf(i));
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(ConnectionModel connectionModel) {
|
|
this.a.a(connectionModel);
|
|
if (g(connectionModel.c())) {
|
|
return;
|
|
}
|
|
this.b.a(connectionModel);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void b(int i, long j) {
|
|
this.a.b(i, j);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.b(i, j);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, int i2, long j) {
|
|
this.a.a(i, i2, j);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i, i2, j);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, int i2) {
|
|
this.a.a(i, i2);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i, i2);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(FileDownloadModel fileDownloadModel) {
|
|
this.a.a(fileDownloadModel);
|
|
if (g(fileDownloadModel.getId())) {
|
|
return;
|
|
}
|
|
this.b.a(fileDownloadModel);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, String str, long j, long j2, int i2) {
|
|
this.a.a(i, str, j, j2, i2);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i, str, j, j2, i2);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, long j, String str, String str2) {
|
|
this.a.a(i, j, str, str2);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i, j, str, str2);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i) {
|
|
this.a.a(i);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, Throwable th) {
|
|
this.a.a(i, th);
|
|
if (g(i)) {
|
|
return;
|
|
}
|
|
this.b.a(i, th);
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, Throwable th, long j) {
|
|
this.a.a(i, th, j);
|
|
if (g(i)) {
|
|
f(i);
|
|
}
|
|
this.b.a(i, th, j);
|
|
this.e.remove(Integer.valueOf(i));
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public void a(int i, long j) {
|
|
this.a.a(i, j);
|
|
if (g(i)) {
|
|
this.c.removeMessages(i);
|
|
if (this.f.get() == i) {
|
|
this.g = Thread.currentThread();
|
|
this.c.sendEmptyMessage(0);
|
|
LockSupport.park();
|
|
this.b.a(i, j);
|
|
}
|
|
} else {
|
|
this.b.a(i, j);
|
|
}
|
|
this.e.remove(Integer.valueOf(i));
|
|
}
|
|
|
|
@Override // com.liulishuo.filedownloader.database.FileDownloadDatabase
|
|
public FileDownloadDatabase.Maintainer a() {
|
|
SqliteDatabaseImpl sqliteDatabaseImpl = this.b;
|
|
NoDatabaseImpl noDatabaseImpl = this.a;
|
|
return sqliteDatabaseImpl.a(noDatabaseImpl.a, noDatabaseImpl.b);
|
|
}
|
|
}
|