Skip to content

Commit a9e03e4

Browse files
committed
New model manager implementation pt. 1
1 parent e0f59a0 commit a9e03e4

16 files changed

Lines changed: 1352 additions & 90 deletions

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ dependencies {
124124
implementation "androidx.preference:preference:1.1.0-alpha02" //prima era 1.1.0-alpha02
125125
implementation "androidx.core:core-splashscreen:1.0.1"
126126
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
127+
implementation "com.github.worker8:RadioGroupPlus:1.0.1"
127128
//Download library
128129
implementation 'com.github.amitshekhariitbhu:PRDownloader:1.0.2'
129130
// DJL HuggingFace tokenizers wrapper

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.icu.text.DecimalFormat;
2222
import android.os.Bundle;
2323
import android.os.Looper;
24-
import android.telecom.Call;
2524
import android.view.LayoutInflater;
2625
import android.view.View;
2726
import android.view.ViewGroup;
@@ -43,7 +42,6 @@
4342
import nie.translator.rtranslator.downloader2.DownloadInfo;
4443
import nie.translator.rtranslator.downloader2.DownloadInfoExtended;
4544
import nie.translator.rtranslator.downloader2.DownloadManager;
46-
import nie.translator.rtranslator.downloader2.Downloader2;
4745

4846
public class DownloadFragment2 extends Fragment {
4947
@Nullable
@@ -229,7 +227,6 @@ public void onClick(View v) {
229227
public void onStart() {
230228
super.onStart();
231229
if(global != null && DOWNLOAD_INFOS != null) {
232-
downloader.startService(); // we eventually start the service (if it is already started nothing will happen)
233230
//if the internal or external free memory are low, we show a warning
234231
double requiredSize = 0;
235232
for (DownloadInfo downloadInfo : DOWNLOAD_INFOS) {
@@ -263,6 +260,7 @@ public void onServiceConnected(){
263260
this.onProgress(downloadStatus.get(index), runningDownload, downloadStatus.get(index).getCurrentProgress(), runningDownload.getCurrentProgress(), runningDownload.isUnzipping(), runningDownload.isTestingIntegrity());
264261
}
265262
}
263+
//todo: manage the case where a download is paused
266264
}
267265
}
268266
}
@@ -307,7 +305,7 @@ public void onError(DownloadGroupInfo downloadGroup, DownloadInfo download, int
307305
}
308306
};
309307

310-
downloader.subscribe(callback);
308+
downloader.subscribeAndResumeDownload(callback);
311309
}
312310
}
313311

app/src/main/java/nie/translator/rtranslator/downloader2/DownloadManager.java

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@
66
import android.content.Context;
77
import android.content.Intent;
88
import android.content.ServiceConnection;
9+
import android.content.SharedPreferences;
910
import android.os.IBinder;
1011
import android.util.Log;
1112

1213
import androidx.annotation.Nullable;
1314

15+
import com.google.gson.Gson;
16+
import com.google.gson.reflect.TypeToken;
17+
1418
import java.util.ArrayList;
1519

1620
public class DownloadManager implements ServiceConnection {
@@ -20,8 +24,8 @@ public class DownloadManager implements ServiceConnection {
2024
@Nullable
2125
private DownloaderService downloaderService;
2226
private final Downloader2.ClientCallback serviceCallback;
23-
private boolean serviceStarted = false;
24-
27+
private ArrayList<DownloadGroupInfo> downloadsToStart = new ArrayList<>();
28+
private boolean shouldStartAllDownloads = false;
2529

2630

2731
public DownloadManager(Context context) {
@@ -58,25 +62,14 @@ public void onError(DownloadGroupInfo downloadGroup, DownloadInfo download, int
5862
}
5963

6064
/**
61-
* This method will start the download service and resume all the unfinished downloads
65+
* This method will start the download service (if there are downloads to resume) and resume all the unfinished downloads
6266
*/
63-
public void startService(){
64-
final Intent intent = new Intent(context, DownloaderService.class);
65-
//intent.putExtra("notification", notification);
66-
context.startService(intent);
67-
this.serviceStarted = true;
68-
if (callback != null) { //if we have previously called subscribe before starting the service we will bind here
69-
boolean result = context.bindService(new Intent(context, DownloaderService.class), this, BIND_ABOVE_CLIENT);
70-
Log.d("bind download", result ? "success" : "failed");
71-
}
72-
}
73-
74-
public void subscribe(@Nullable Callback callback) {
67+
public void subscribeAndResumeDownload(@Nullable Callback callback) {
7568
if(this.callback == null) {
7669
this.callback = callback;
77-
if(serviceStarted) { //if we have not started yet the service, we will bind after starting it, not now (this way the service will not stop when we unbind)
78-
boolean result = context.bindService(new Intent(context, DownloaderService.class), this, BIND_ABOVE_CLIENT);
79-
Log.d("bind download", result ? "success" : "failed");
70+
boolean shouldStartService = areDownloadsRunning(false);
71+
if(shouldStartService) {
72+
startAndBindService();
8073
}
8174
}
8275
}
@@ -91,12 +84,15 @@ public void unsubscribe() {
9184
}
9285
}
9386

94-
public boolean startDownload(DownloadGroupInfo downloadGroup){
95-
if(downloaderService != null) {
87+
public void startDownload(DownloadGroupInfo downloadGroup){
88+
if(downloaderService == null && !downloadsToStart.contains(downloadGroup)){
89+
downloadsToStart.add(downloadGroup);
90+
if(!DownloaderService.running){
91+
startAndBindService();
92+
}
93+
}else if(downloaderService != null) {
9694
downloaderService.startDownload(downloadGroup);
97-
return true;
9895
}
99-
return false;
10096
}
10197

10298
public boolean stopDownload(DownloadGroupInfo downloadGroup){
@@ -107,8 +103,21 @@ public boolean stopDownload(DownloadGroupInfo downloadGroup){
107103
return false;
108104
}
109105

110-
public boolean startAllDownloads() {
106+
public boolean cancelDownload(DownloadGroupInfo downloadGroup){
111107
if(downloaderService != null) {
108+
downloaderService.cancelDownload(downloadGroup);
109+
return true;
110+
}
111+
return false;
112+
}
113+
114+
public boolean startAllDownloads() {
115+
if(downloaderService == null && !shouldStartAllDownloads){
116+
shouldStartAllDownloads = true;
117+
if(!DownloaderService.running){
118+
startAndBindService();
119+
}
120+
} else if(downloaderService != null) {
112121
downloaderService.startAllDownloads();
113122
return true;
114123
}
@@ -123,18 +132,61 @@ public boolean stopAllDownloads() {
123132
return false;
124133
}
125134

135+
public boolean cancelAllDownloads() {
136+
if(downloaderService != null) {
137+
downloaderService.cancelAllDownloads();
138+
return true;
139+
}
140+
return false;
141+
}
142+
126143
public ArrayList<DownloadGroupInfo> getDownloadsStatus() {
127144
if (downloaderService != null) {
128145
return downloaderService.getDownloadsStatus();
129146
}
130147
return null;
131148
}
132149

150+
private void startAndBindService(){
151+
// start the service
152+
final Intent intent = new Intent(context, DownloaderService.class);
153+
context.startService(intent);
154+
//we bind to the service
155+
boolean result = context.bindService(new Intent(context, DownloaderService.class), this, BIND_ABOVE_CLIENT);
156+
Log.d("bind download", result ? "success" : "failed");
157+
}
158+
159+
private boolean areDownloadsRunning(boolean includePaused){
160+
SharedPreferences sharedPreferences = context.getSharedPreferences("default", Context.MODE_PRIVATE);
161+
String downloadsStatusString = sharedPreferences.getString("downloadsStatus", "");
162+
if (!downloadsStatusString.isEmpty()) {
163+
//we check if there are unfinished downloads that are not paused (or also paused ones if includePaused is true)
164+
Gson gson = new Gson();
165+
ArrayList<DownloadGroupInfo> downloadGroupInfos = gson.fromJson(downloadsStatusString, new TypeToken<ArrayList<DownloadGroupInfo>>() {}.getType());
166+
if (downloadGroupInfos != null) {
167+
for (DownloadGroupInfo groupInfo : downloadGroupInfos) {
168+
if (!groupInfo.isAllDownloadCompleted() && (includePaused || groupInfo.getRunningDownloadIndex() != -1)) {
169+
return true;
170+
}
171+
}
172+
}
173+
}
174+
return false;
175+
}
176+
133177
@Override
134178
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
135179
this.downloaderService = ((DownloaderService.LocalBinder) iBinder).getService();
136180
downloaderService.registerClient(serviceCallback);
137181
if(callback != null) callback.onServiceConnected();
182+
for(DownloadGroupInfo downloadGroup: downloadsToStart) {
183+
downloaderService.startDownload(downloadGroup);
184+
}
185+
downloadsToStart.clear();
186+
if(shouldStartAllDownloads){
187+
downloaderService.startAllDownloads();
188+
shouldStartAllDownloads = false;
189+
}
138190
}
139191

140192
@Override

app/src/main/java/nie/translator/rtranslator/downloader2/Downloader2.java

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import com.downloader.OnStartOrResumeListener;
1414
import com.downloader.PRDownloader;
1515
import com.downloader.Progress;
16-
import com.google.common.collect.Lists;
1716

1817
import java.io.BufferedInputStream;
1918
import java.io.File;
20-
import java.io.FileInputStream;
2119
import java.io.FileOutputStream;
2220
import java.io.IOException;
2321
import java.io.InputStream;
@@ -41,7 +39,7 @@ public class Downloader2 {
4139

4240
public Downloader2(DownloadGroupInfo downloadGroupInfo, Context context, ClientCallback callback) {
4341
this.downloadGroupInfo = downloadGroupInfo;
44-
this.downloadGroupInfo.setRunningDownloadIndex(-1);
42+
//this.downloadGroupInfo.setRunningDownloadIndex(-1);
4543
this.context = context;
4644
this.callback = callback;
4745
}
@@ -51,12 +49,7 @@ public void startDownloads(){
5149
if (downloadGroupInfo.getRunningDownloadIndex() == -1) {
5250
// eventual resume of the download based on the previous ones that have been completed
5351
// (or start from 0 if none of the previous ones are completed)
54-
int startIndex = 0;
55-
for(int i=0; i<downloadGroupInfo.downloadsInfo.length; i++){
56-
if(downloadGroupInfo.downloadsInfo[i].isAllCompleted()){
57-
startIndex++;
58-
}
59-
}
52+
int startIndex = findFirstIncompletedDownload();
6053
DownloadInfoExtended startDownload = downloadGroupInfo.downloadsInfo[startIndex];
6154
if(startIndex > 0) lastDownloadSuccessIndex = startIndex-1;
6255
downloadGroupInfo.setRunningDownloadIndex(startIndex);
@@ -81,21 +74,25 @@ public void startDownloads(){
8174
}
8275

8376
public void pauseDownloads(){
84-
// we cancel the current download (for now this is the best option, it is difficult, if not impossible, to pause without having access to the server)
85-
PRDownloader.cancel(downloadGroupInfo.downloadsInfo[downloadGroupInfo.getRunningDownloadIndex()].getDownloadId());
77+
int currentDownloadIndex = downloadGroupInfo.getRunningDownloadIndex();
78+
if(currentDownloadIndex != -1) {
79+
// we cancel the current download (for now this is the best option, it is difficult, if not impossible, to pause without having access to the server)
80+
PRDownloader.cancel(downloadGroupInfo.downloadsInfo[currentDownloadIndex].getDownloadId());
81+
downloadGroupInfo.setRunningDownloadIndex(-1);
82+
}
8683
}
8784

8885
public void cancelDownloads(){
8986
int currentDownloadIndex = downloadGroupInfo.getRunningDownloadIndex();
9087
if(currentDownloadIndex != -1) {
9188
// we cancel the current download
9289
PRDownloader.cancel(downloadGroupInfo.downloadsInfo[currentDownloadIndex].getDownloadId());
93-
// we delete the already downloaded files of this group of download
94-
for (int i = 0; i <= currentDownloadIndex; i++){
95-
File file = new File(downloadGroupInfo.downloadsInfo[currentDownloadIndex].getDestinationCompletePath());
96-
if (file.exists()) {
97-
file.delete();
98-
}
90+
}
91+
// we delete the already downloaded files of this group of download
92+
for (int i = 0; i <= downloadGroupInfo.downloadsInfo.length; i++){
93+
File file = new File(downloadGroupInfo.downloadsInfo[i].getDestinationCompletePath());
94+
if (file.exists()) {
95+
file.delete();
9996
}
10097
}
10198
}
@@ -270,6 +267,16 @@ public boolean equals(@Nullable Object obj) {
270267
return super.equals(obj);
271268
}
272269

270+
private int findFirstIncompletedDownload(){
271+
int index = 0;
272+
for(int i=0; i<downloadGroupInfo.downloadsInfo.length; i++){
273+
if(downloadGroupInfo.downloadsInfo[i].isAllCompleted()){
274+
index++;
275+
}
276+
}
277+
return index;
278+
}
279+
273280
private void unpackZip(String path, String zipname, Listener listener) {
274281
new Thread(new Runnable() {
275282
@Override

app/src/main/java/nie/translator/rtranslator/downloader2/DownloaderService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class DownloaderService extends Service {
3333
private long lastSummaryUpdateTime = 0;
3434
private final java.util.Map<Integer, Integer> lastChildProgresses = new java.util.HashMap<>();
3535
private final java.util.Map<Integer, Long> lastChildUpdateTimes = new java.util.HashMap<>();
36-
private boolean started = false;
36+
public static boolean running = false;
3737

3838
public class LocalBinder extends Binder {
3939
DownloaderService getService() {
@@ -52,7 +52,7 @@ public void onCreate() {
5252
@Override
5353
public int onStartCommand(Intent intent, int flags, int startId) {
5454
Log.i("download", "download service started");
55-
if(started) {
55+
if(!running) {
5656
// Initialize the notification system
5757
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
5858
summaryBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
@@ -85,7 +85,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8585
}
8686
}
8787
}
88-
started = true;
88+
running = true;
8989

9090
return super.onStartCommand(intent, flags, startId);
9191
}
@@ -397,6 +397,7 @@ public void notifyError(DownloadGroupInfo downloadGroup, DownloadInfo download,
397397
@Override
398398
public void onDestroy() {
399399
super.onDestroy();
400+
running = false;
400401
synchronized (downloaders) {
401402
downloaders.clear();
402403
}

0 commit comments

Comments
 (0)