2019-03-03 21:40:48 +01:00
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import org.telegram.tgnet.TLRPC;
|
|
|
|
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
|
|
|
|
|
public class AnimatedFileDrawableStream implements FileLoadOperationStream {
|
|
|
|
|
|
|
|
private FileLoadOperation loadOperation;
|
|
|
|
private CountDownLatch countDownLatch;
|
|
|
|
private TLRPC.Document document;
|
2020-07-26 10:03:38 +02:00
|
|
|
private ImageLocation location;
|
2019-03-03 21:40:48 +01:00
|
|
|
private Object parentObject;
|
|
|
|
private int currentAccount;
|
|
|
|
private volatile boolean canceled;
|
|
|
|
private final Object sync = new Object();
|
|
|
|
private int lastOffset;
|
2019-05-14 14:08:05 +02:00
|
|
|
private boolean waitingForLoad;
|
2019-08-22 01:53:26 +02:00
|
|
|
private boolean preview;
|
2020-06-04 18:47:15 +02:00
|
|
|
private boolean finishedLoadingFile;
|
|
|
|
private String finishedFilePath;
|
|
|
|
|
|
|
|
private boolean ignored;
|
2019-03-03 21:40:48 +01:00
|
|
|
|
2020-07-26 10:03:38 +02:00
|
|
|
public AnimatedFileDrawableStream(TLRPC.Document d, ImageLocation l, Object p, int a, boolean prev) {
|
2019-03-03 21:40:48 +01:00
|
|
|
document = d;
|
2020-07-26 10:03:38 +02:00
|
|
|
location = l;
|
2019-03-03 21:40:48 +01:00
|
|
|
parentObject = p;
|
|
|
|
currentAccount = a;
|
2019-08-22 01:53:26 +02:00
|
|
|
preview = prev;
|
2020-07-26 10:03:38 +02:00
|
|
|
loadOperation = FileLoader.getInstance(currentAccount).loadStreamFile(this, document, location, parentObject, 0, preview);
|
2019-03-03 21:40:48 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 18:47:15 +02:00
|
|
|
public boolean isFinishedLoadingFile() {
|
|
|
|
return finishedLoadingFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFinishedFilePath() {
|
|
|
|
return finishedFilePath;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
public int read(int offset, int readLength) {
|
|
|
|
synchronized (sync) {
|
|
|
|
if (canceled) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readLength == 0) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
int availableLength = 0;
|
|
|
|
try {
|
|
|
|
while (availableLength == 0) {
|
2020-06-04 18:47:15 +02:00
|
|
|
int[] result = loadOperation.getDownloadedLengthFromOffset(offset, readLength);
|
|
|
|
availableLength = result[0];
|
|
|
|
if (!finishedLoadingFile && result[1] != 0) {
|
|
|
|
finishedLoadingFile = true;
|
|
|
|
finishedFilePath = loadOperation.getCacheFileFinal().getAbsolutePath();
|
|
|
|
}
|
2019-03-03 21:40:48 +01:00
|
|
|
if (availableLength == 0) {
|
2019-08-22 01:53:26 +02:00
|
|
|
if (loadOperation.isPaused() || lastOffset != offset || preview) {
|
2020-07-26 10:03:38 +02:00
|
|
|
FileLoader.getInstance(currentAccount).loadStreamFile(this, document, location, parentObject, offset, preview);
|
2019-03-03 21:40:48 +01:00
|
|
|
}
|
|
|
|
synchronized (sync) {
|
|
|
|
if (canceled) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
countDownLatch = new CountDownLatch(1);
|
|
|
|
}
|
2019-08-22 01:53:26 +02:00
|
|
|
if (!preview) {
|
|
|
|
FileLoader.getInstance(currentAccount).setLoadingVideo(document, false, true);
|
|
|
|
}
|
2019-05-14 14:08:05 +02:00
|
|
|
waitingForLoad = true;
|
2019-03-03 21:40:48 +01:00
|
|
|
countDownLatch.await();
|
2019-05-14 14:08:05 +02:00
|
|
|
waitingForLoad = false;
|
2019-03-03 21:40:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lastOffset = offset + availableLength;
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e(e);
|
|
|
|
}
|
|
|
|
return availableLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel() {
|
|
|
|
cancel(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancel(boolean removeLoading) {
|
|
|
|
synchronized (sync) {
|
|
|
|
if (countDownLatch != null) {
|
|
|
|
countDownLatch.countDown();
|
2019-08-22 01:53:26 +02:00
|
|
|
if (removeLoading && !canceled && !preview) {
|
2019-03-03 21:40:48 +01:00
|
|
|
FileLoader.getInstance(currentAccount).removeLoadingVideo(document, false, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
canceled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void reset() {
|
|
|
|
synchronized (sync) {
|
|
|
|
canceled = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.Document getDocument() {
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
2020-07-26 10:03:38 +02:00
|
|
|
public ImageLocation getLocation() {
|
|
|
|
return location;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
public Object getParentObject() {
|
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:53:26 +02:00
|
|
|
public boolean isPreview() {
|
|
|
|
return preview;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
public int getCurrentAccount() {
|
|
|
|
return currentAccount;
|
|
|
|
}
|
|
|
|
|
2019-05-14 14:08:05 +02:00
|
|
|
public boolean isWaitingForLoad() {
|
|
|
|
return waitingForLoad;
|
|
|
|
}
|
|
|
|
|
2019-03-03 21:40:48 +01:00
|
|
|
@Override
|
|
|
|
public void newDataAvailable() {
|
|
|
|
if (countDownLatch != null) {
|
|
|
|
countDownLatch.countDown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|