NekoX/TMessagesProj/src/main/java/org/telegram/messenger/AnimatedFileDrawableStream....

139 lines
4.1 KiB
Java
Raw Normal View History

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();
2022-06-21 04:51:00 +02:00
private long lastOffset;
2019-05-14 14:08:05 +02:00
private boolean waitingForLoad;
2019-08-22 01:53:26 +02:00
private boolean preview;
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
}
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 {
2022-06-21 04:51:00 +02:00
long availableLength = 0;
2019-03-03 21:40:48 +01:00
try {
while (availableLength == 0) {
2022-06-21 04:51:00 +02:00
long[] 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) {
2022-02-12 06:22:45 +01:00
FileLog.e(e, false);
2019-03-03 21:40:48 +01:00
}
2022-06-21 04:51:00 +02:00
return (int) availableLength;
2019-03-03 21:40:48 +01:00
}
}
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();
}
}
}