Skip to content

Commit 93ba159

Browse files
committed
Improvements of the new download logic
1 parent 28d4ae7 commit 93ba159

8 files changed

Lines changed: 668 additions & 349 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ android {
116116
dependencies {
117117
//implementation fileTree(include: ['*.jar'], dir: 'libs')
118118
// Support libraries
119+
implementation 'com.google.code.gson:gson:2.14.0'
119120
implementation "com.google.android.material:material:1.9.0" //1.9.0
120121
implementation "androidx.cardview:cardview:1.0.0"
121122
implementation "androidx.recyclerview:recyclerview:1.0.0"

app/src/main/java/nie/translator/rtranslator/access/DownloadFragment2.java

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
package nie.translator.rtranslator.access;
1818

1919
import android.annotation.SuppressLint;
20-
import android.content.Context;
2120
import android.content.Intent;
22-
import android.content.SharedPreferences;
2321
import android.icu.text.DecimalFormat;
2422
import android.os.Bundle;
2523
import android.os.Looper;
24+
import android.telecom.Call;
2625
import android.view.LayoutInflater;
2726
import android.view.View;
2827
import android.view.ViewGroup;
@@ -35,23 +34,22 @@
3534

3635
import com.google.android.material.progressindicator.LinearProgressIndicator;
3736

38-
import java.io.File;
37+
import java.util.ArrayList;
3938

4039
import nie.translator.rtranslator.Global;
4140
import nie.translator.rtranslator.LoadingActivity;
4241
import nie.translator.rtranslator.R;
42+
import nie.translator.rtranslator.downloader2.DownloadGroupInfo;
4343
import nie.translator.rtranslator.downloader2.DownloadInfo;
4444
import nie.translator.rtranslator.downloader2.DownloadInfoExtended;
4545
import nie.translator.rtranslator.downloader2.DownloadManager;
4646
import nie.translator.rtranslator.downloader2.Downloader2;
47-
import nie.translator.rtranslator.tools.FileTools;
48-
import nie.translator.rtranslator.voice_translation.neural_networks.NeuralNetworkApi;
4947

5048
public class DownloadFragment2 extends Fragment {
5149
@Nullable
5250
public static String downloadFolder;
5351
@Nullable
54-
public static DownloadInfo[] DOWNLOAD_INFOS;
52+
public static DownloadInfoExtended[] DOWNLOAD_INFOS;
5553
private static final long INTERVAL_TIME_FOR_GUI_UPDATES_MS = 100; //500
5654
private AccessActivity activity;
5755
private Global global;
@@ -105,83 +103,95 @@ public void onActivityCreated(@Nullable Bundle savedInstanceState) {
105103
global = (Global) activity.getApplication();
106104
downloadFolder = global.getFilesDir().getAbsolutePath();
107105
String baseUrl = "https://github.com/niedev/RTranslator/releases/download/2.0.0/";
108-
DOWNLOAD_INFOS = new DownloadInfo[]{
109-
new DownloadInfo(
106+
DOWNLOAD_INFOS = new DownloadInfoExtended[]{
107+
new DownloadInfoExtended(
110108
"NLLB_cache_initializer.onnx",
111109
baseUrl + "NLLB_cache_initializer.onnx",
112110
downloadFolder,
113111
24000,
114-
true
112+
true,
113+
false
115114
),
116-
new DownloadInfo(
115+
new DownloadInfoExtended(
117116
"NLLB_decoder.onnx",
118117
baseUrl + "NLLB_decoder.onnx",
119118
downloadFolder,
120119
171000,
121-
true
120+
true,
121+
false
122122
),
123-
new DownloadInfo(
123+
new DownloadInfoExtended(
124124
"NLLB_embed_and_lm_head.onnx",
125125
baseUrl + "NLLB_embed_and_lm_head.onnx",
126126
downloadFolder,
127127
500000,
128-
true
128+
true,
129+
false
129130
),
130-
new DownloadInfo(
131+
new DownloadInfoExtended(
131132
"NLLB_encoder.onnx",
132133
baseUrl + "NLLB_encoder.onnx",
133134
downloadFolder,
134135
254000,
135-
true
136+
true,
137+
false
136138
),
137-
new DownloadInfo(
139+
new DownloadInfoExtended(
138140
"Whisper_cache_initializer.onnx",
139141
baseUrl + "Whisper_cache_initializer.onnx",
140142
downloadFolder,
141143
14000,
142-
true
144+
true,
145+
false
143146
),
144-
new DownloadInfo(
147+
new DownloadInfoExtended(
145148
"Whisper_cache_initializer_batch.onnx",
146149
baseUrl + "Whisper_cache_initializer_batch.onnx",
147150
downloadFolder,
148151
14000,
149-
true
152+
true,
153+
false
150154
),
151-
new DownloadInfo(
155+
new DownloadInfoExtended(
152156
"Whisper_decoder.onnx",
153157
baseUrl + "Whisper_decoder.onnx",
154158
downloadFolder,
155159
173000,
156-
true
160+
true,
161+
false
157162
),
158-
new DownloadInfo(
163+
new DownloadInfoExtended(
159164
"Whisper_detokenizer.onnx",
160165
baseUrl + "Whisper_detokenizer.onnx",
161166
downloadFolder,
162167
461,
163-
true
168+
true,
169+
false
164170
),
165-
new DownloadInfo(
171+
new DownloadInfoExtended(
166172
"Whisper_encoder.onnx",
167173
baseUrl + "Whisper_encoder.onnx",
168174
downloadFolder,
169175
88000,
170-
true
176+
true,
177+
false
171178
),
172-
new DownloadInfo(
179+
new DownloadInfoExtended(
173180
"Whisper_initializer.onnx",
174181
baseUrl + "Whisper_initializer.onnx",
175182
downloadFolder,
176183
69,
177-
true
184+
true,
185+
false
178186
),
179187

180188

181189
};
190+
182191
mainHandler = new android.os.Handler(Looper.getMainLooper());
183-
downloader = new DownloadManager(global, DOWNLOAD_INFOS);
184-
downloader.startDownloads();
192+
downloader = new DownloadManager(global);
193+
downloader.startService();
194+
//downloader.startAllDownloads();
185195
retryButton.setOnClickListener(new View.OnClickListener() {
186196
@Override
187197
public void onClick(View v) {
@@ -198,15 +208,15 @@ public void onClick(View v) {
198208
public void onClick(View v) {
199209
if(pauseButton.getTag().equals("iconCancel")){
200210
//we pause the download
201-
boolean success = downloader.stopDownload();
211+
boolean success = downloader.stopAllDownloads();
202212
if(success) {
203213
//we change the icon and tag
204214
pauseButton.setImageResource(R.drawable.play_icon);
205215
//pauseButton.setImageDrawable(global.getResources().getDrawable(R.drawable.play_icon, null));
206216
pauseButton.setTag("iconPlay");
207217
}
208218
}else{
209-
downloader.startDownloads();
219+
downloader.startAllDownloads();
210220
//we change the icon and tag
211221
pauseButton.setImageResource(R.drawable.cancel_icon);
212222
//pauseButton.setImageDrawable(global.getResources().getDrawable(R.drawable.cancel_icon, null));
@@ -232,60 +242,70 @@ public void onStart() {
232242
storageWarningText.setVisibility(View.VISIBLE);
233243
}
234244

235-
Downloader2.Callback callback = new Downloader2.Callback() {
245+
final DownloadManager.Callback callback = new DownloadManager.Callback() {
246+
public void onServiceConnected(){
247+
ArrayList<DownloadGroupInfo> downloadStatus = downloader.getDownloadsStatus();
248+
// we eventually start the download if it is the first time
249+
if(downloadStatus == null || !downloadStatus.contains(DOWNLOAD_INFOS)) downloader.startDownload(new DownloadGroupInfo(DOWNLOAD_INFOS));
250+
// we change the GUI based on current download status
251+
if(downloadStatus != null){
252+
int index = downloadStatus.indexOf(DOWNLOAD_INFOS);
253+
if(index != -1) {
254+
if (downloadStatus.get(index).isAllDownloadCompleted()) {
255+
this.onAllCompleted(downloadStatus.get(index));
256+
} else {
257+
DownloadInfoExtended runningDownload = downloadStatus.get(index).getRunningDownload();
258+
if (runningDownload != null) {
259+
if (runningDownload.getCurrentError() != -1) {
260+
this.onError(downloadStatus.get(index), runningDownload, runningDownload.getCurrentError());
261+
} else {
262+
this.onProgress(downloadStatus.get(index), runningDownload, downloadStatus.get(index).getCurrentProgress(), runningDownload.getCurrentProgress(), runningDownload.isUnzipping(), runningDownload.isTestingIntegrity());
263+
}
264+
}
265+
}
266+
}
267+
}
268+
}
269+
236270
@Override
237-
public void onAllDownloadComplete() {
271+
public void onAllCompleted(DownloadGroupInfo downloadGroup) {
238272
startRTranslator();
239273
}
240274

241275
@Override
242-
public void onDownloadComplete(DownloadInfo downloadInfo) {
276+
public void onCompleted(DownloadGroupInfo downloadGroup, DownloadInfo download) {
243277
//for now we do nothing here
244278
}
245279

246280
@SuppressLint("SetTextI18n")
247281
@Override
248-
public void onProgress(DownloadInfo downloadInfo, int progress, boolean testingIntegrity) {
282+
public void onProgress(DownloadGroupInfo downloadGroup, DownloadInfo download, int totalProgress, int progress, boolean testingIntegrity, boolean unzipping) {
249283
//update of progress bar
250-
int progressNormalized = progress * progressBar.getMax() / 100;
284+
int progressNormalized = totalProgress * progressBar.getMax() / 100;
251285
progressBar.setProgress(progressNormalized, true);
252286
//we update the progressNumbersText
253287
double totalSize = 0;
254288
for (DownloadInfo info : DOWNLOAD_INFOS) {
255289
totalSize = totalSize + info.getSize();
256290
}
257291
totalSize = totalSize/1000000; //we convert from Kb to Gb
258-
float downloadedGb = (float) (progress*totalSize/100); //progress : 100 = x : totalSize (where x is downloadedGb)
292+
float downloadedGb = (float) (totalProgress*totalSize/100); //progress : 100 = x : totalSize (where x is downloadedGb)
259293
DecimalFormat decimalFormat = new DecimalFormat("#.##");
260294
progressNumbersText.setText(decimalFormat.format(downloadedGb)+" / "+decimalFormat.format(totalSize)+" GB");
261295
//update of the progress description
262296
if(testingIntegrity){
263-
progressDescriptionText.setText(getString(R.string.description_integrity_check, downloadInfo.getName()));
297+
progressDescriptionText.setText(getString(R.string.description_integrity_check, download.getName()));
264298
}else{
265-
progressDescriptionText.setText(getString(R.string.description_download, downloadInfo.getName()));
299+
progressDescriptionText.setText(getString(R.string.description_download, download.getName()));
266300
}
267301
}
268302

269303
@Override
270-
public void onError(DownloadInfo downloadInfo, int reason) {
304+
public void onError(DownloadGroupInfo downloadGroup, DownloadInfo download, int reason) {
271305
showDownloadError();
272306
}
273307
};
274308

275-
//we change the GUI based on current download status
276-
DownloadInfoExtended downloadStatus = downloader.getRunningDownloadStatus();
277-
if(downloadStatus != null){
278-
if(downloadStatus.isAllDownloadCompleted()){
279-
callback.onAllDownloadComplete();
280-
}else{
281-
if(downloadStatus.getCurrentError() != null){
282-
callback.onError(downloadStatus.getCurrentError().downloadInfo, downloadStatus.getCurrentError().reason);
283-
}else{
284-
callback.onProgress(downloadStatus, downloadStatus.getCurrentProgress(), downloadStatus.isTestingIntegrity());
285-
}
286-
}
287-
}
288-
289309
downloader.subscribe(callback);
290310
}
291311
}
@@ -311,7 +331,7 @@ private void showDownloadError(){
311331
}
312332

313333
private void retryCurrentDownload(){
314-
downloader.startDownloads();
334+
downloader.startAllDownloads();
315335
}
316336

317337
private void startRTranslator(){

0 commit comments

Comments
 (0)