-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathAnimatedFileDrawableStream.java
189 lines (167 loc) · 6.29 KB
/
AnimatedFileDrawableStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package org.telegram.messenger;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.util.Log;
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;
private ImageLocation location;
private Object parentObject;
private int currentAccount;
private volatile boolean canceled;
private final Object sync = new Object();
private long lastOffset;
private boolean waitingForLoad;
private boolean preview;
private boolean finishedLoadingFile;
private String finishedFilePath;
private int loadingPriority;
private int debugCanceledCount;
private boolean debugReportSend;
public AnimatedFileDrawableStream(TLRPC.Document d, ImageLocation l, Object p, int a, boolean prev, int loadingPriority, int cacheType) {
document = d;
location = l;
parentObject = p;
currentAccount = a;
preview = prev;
this.loadingPriority = loadingPriority;
loadOperation = FileLoader.getInstance(currentAccount).loadStreamFile(this, document, location, parentObject, 0, preview, loadingPriority, cacheType);
}
public boolean isFinishedLoadingFile() {
return finishedLoadingFile;
}
public String getFinishedFilePath() {
return finishedFilePath;
}
public int read(int offset, int readLength) {
synchronized (sync) {
if (canceled) {
debugCanceledCount++;
if (!debugReportSend && debugCanceledCount > 200) {
debugReportSend = true;
FileLog.e(new RuntimeException("infinity stream reading!!!"));
}
return 0;
}
}
if (readLength == 0) {
return 0;
} else {
long availableLength = 0;
try {
while (availableLength == 0) {
long[] result = loadOperation.getDownloadedLengthFromOffset(offset, readLength);
availableLength = result[0];
if (!finishedLoadingFile && result[1] != 0) {
finishedLoadingFile = true;
finishedFilePath = loadOperation.getCacheFileFinal().getAbsolutePath();
}
if (availableLength == 0) {
synchronized (sync) {
if (canceled) {
cancelLoadingInternal();
return 0;
}
}
countDownLatch = new CountDownLatch(1);
if (loadOperation.isPaused() || lastOffset != offset || preview) {
FileLoadOperation loadOperation = FileLoader.getInstance(currentAccount).loadStreamFile(this, document, location, parentObject, offset, preview, loadingPriority);
if (this.loadOperation != loadOperation) {
this.loadOperation.removeStreamListener(this);
this.loadOperation = loadOperation;
}
lastOffset = offset + availableLength;
}
synchronized (sync) {
if (canceled) {
countDownLatch = null;
cancelLoadingInternal();
return 0;
}
}
if (!preview) {
FileLoader.getInstance(currentAccount).setLoadingVideo(document, false, true);
}
if (countDownLatch != null) {
waitingForLoad = true;
countDownLatch.await();
waitingForLoad = false;
}
}
}
lastOffset = offset + availableLength;
} catch (Exception e) {
FileLog.e(e, false);
}
return (int) availableLength;
}
}
public void cancel() {
cancel(true);
}
public void cancel(boolean removeLoading) {
if (canceled) {
return;
}
synchronized (sync) {
if (countDownLatch != null) {
countDownLatch.countDown();
countDownLatch = null;
if (removeLoading && !canceled && !preview) {
FileLoader.getInstance(currentAccount).removeLoadingVideo(document, false, true);
}
}
if (parentObject instanceof MessageObject) {
MessageObject messageObject = (MessageObject) parentObject;
if (DownloadController.getInstance(messageObject.currentAccount).isDownloading(messageObject.getId())) {
removeLoading = false;
}
}
if (removeLoading) {
cancelLoadingInternal();
}
canceled = true;
}
}
private void cancelLoadingInternal() {
FileLoader.getInstance(currentAccount).cancelLoadFile(document);
if (location != null) {
FileLoader.getInstance(currentAccount).cancelLoadFile(location.location, "mp4");
}
}
public void reset() {
synchronized (sync) {
canceled = false;
}
}
public TLRPC.Document getDocument() {
return document;
}
public ImageLocation getLocation() {
return location;
}
public Object getParentObject() {
return document;
}
public boolean isPreview() {
return preview;
}
public int getCurrentAccount() {
return currentAccount;
}
public boolean isWaitingForLoad() {
return waitingForLoad;
}
@Override
public void newDataAvailable() {
if (countDownLatch != null) {
countDownLatch.countDown();
countDownLatch = null;
}
}
public boolean isCanceled() {
return canceled;
}
}