Skip to content

Commit

Permalink
Updated et ajout du ETA pour le dl
Browse files Browse the repository at this point in the history
  • Loading branch information
Celestial04 committed Mar 30, 2024
1 parent 8050c70 commit 8b50c46
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 27 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ android {
applicationId "com.hugdev.webapper"
minSdk 24
targetSdk 34
versionCode 6
versionName "3.0.1"
versionCode 7
versionName "3.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Material3.Light.NoActionBar"
android:usesCleartextTraffic="true"
tools:targetApi="31">
tools:targetApi="34">
<activity
android:name=".tuto_3"
android:exported="false" />
Expand Down
64 changes: 41 additions & 23 deletions app/src/main/java/com/hugdev/webapper/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package com.hugdev.webapper;

import static android.app.DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR;
import static android.app.DownloadManager.COLUMN_STATUS;
import static android.app.DownloadManager.COLUMN_TOTAL_SIZE_BYTES;
import static android.app.DownloadManager.Query;
import static android.app.DownloadManager.Request;
import static android.app.DownloadManager.STATUS_FAILED;
import static android.app.DownloadManager.STATUS_SUCCESSFUL;
import static android.app.usage.UsageEvents.Event.NONE;

import static com.hugdev.webapper.R.string.vitesse_du_tl_chargement;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.content.DialogInterface;
Expand Down Expand Up @@ -52,14 +45,26 @@
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private AlertDialog alert2;
private int selectedTheme;
// Méthode pour formater le temps en heures, minutes et secondes
// Méthode pour formater le temps en heures, minutes et secondes
// Méthode pour formater le temps en heures, minutes et secondes
private String formatDuration(long seconds) {
long hours = seconds / 3600;
long minutes = (seconds % 3600) / 60;
long secs = seconds % 60;
return String.format(Locale.getDefault(), "%02d:%02d:%02d", hours, minutes, secs);
}




@SuppressLint("SetJavaScriptEnabled")
Expand All @@ -70,6 +75,8 @@ protected void onCreate(Bundle savedInstanceState) {
int savedTheme = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("selected_theme", NONE);
if (savedTheme != NONE) {
selectedTheme = savedTheme;
}else{
selectedTheme = R.style.Theme_WebApper_dark;
}
setTheme(selectedTheme);
DynamicColors.applyToActivitiesIfAvailable(this.getApplication());
Expand Down Expand Up @@ -136,6 +143,7 @@ public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request
}
});


webView.setDownloadListener(new DownloadListener() {
final List<AlertDialog.Builder> dialogsList = new ArrayList<>();

Expand All @@ -162,7 +170,7 @@ public void onClick(DialogInterface dialog, int id) {
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
dialogsList.add(builder2);
builder2.setTitle("Téléchargement en cours...");
builder2.setMessage("");
builder2.setMessage("Téléchargement de votre fichier en cours...");
builder2.setCancelable(false);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
Expand All @@ -171,13 +179,16 @@ public void onClick(DialogInterface dialog, int id) {
// Ajouter un TextView pour afficher les statistiques de téléchargement
TextView textView = new TextView(MainActivity.this);
TextView speedl = new TextView(MainActivity.this);
TextView textViewETA = new TextView(MainActivity.this); // Ajouter un TextView pour afficher l'ETA
textView.setText(R.string.t_l_chargement_en_cours);
speedl.setText(vitesse_du_tl_chargement);
speedl.setText("Vitesse : 0.0 MB/s"); // Mettez la valeur de la vitesse du téléchargement correcte ici
textViewETA.setText("ETA: Calculating..."); // Initialiser l'ETA avec un message par défaut
speedl.setTextSize(14);
textView.setTextSize(20);

layout.addView(textView);
layout.addView(speedl);
layout.addView(textViewETA); // Ajouter le TextView ETA
// Ajouter un bouton "Annuler"
Button cancelButton = new Button(MainActivity.this);
cancelButton.setText("Annuler");
Expand Down Expand Up @@ -211,7 +222,7 @@ public void onClick(DialogInterface dialog, int id) {
}
});

AlertDialog alert2 = builder2.create();
alert2 = builder2.create();
dialog.dismiss();
alert2.show();

Expand All @@ -228,33 +239,38 @@ public void run() {
query.setFilterById(downloadId);
Cursor cursor = downloadManager.query(query);
if (cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(COLUMN_STATUS);
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
if (status == STATUS_SUCCESSFUL) {
if (status == DownloadManager.STATUS_SUCCESSFUL) {
alert2.dismiss(); // Fermer le AlertDialog affichant les statistiques
Toast.makeText(getApplicationContext(), getString(R.string.le_tl_chargement_de) + fileName + " est terminé.", Toast.LENGTH_SHORT).show();
} else if (status == STATUS_FAILED) {
} else if (status == DownloadManager.STATUS_FAILED) {
alert2.dismiss(); // Fermer le AlertDialog affichant les statistiques
Toast.makeText(getApplicationContext(), getString(R.string.le_tl_chargement_de) + fileName + " a échoué.", Toast.LENGTH_SHORT).show();
} else {
@SuppressLint("Range") int downloadedBytes = cursor.getInt(cursor.getColumnIndex(COLUMN_BYTES_DOWNLOADED_SO_FAR));
@SuppressLint("Range") int bytesTotal = cursor.getInt(cursor.getColumnIndex(COLUMN_TOTAL_SIZE_BYTES));
int progress = (int) ((bytesDownloaded[0] * 100L) / bytesTotal);
@SuppressLint("Range") int downloadedBytes = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
@SuppressLint("Range") int bytesTotal = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
int progress = (int) ((downloadedBytes * 100L) / (double) bytesTotal);

long elapsedTime = System.currentTimeMillis() - startTime;
double bytesPerSecond = (downloadedBytes - bytesDownloaded[0]) * 1000.0 / elapsedTime / 1000000.0;

// Calculer l'ETA
int bytesRemaining = bytesTotal - bytesDownloaded[0];
int bytesRemaining = bytesTotal - downloadedBytes;
long etaSeconds = (long) (bytesRemaining / bytesPerSecond);
String etaFormatted = formatDuration(etaSeconds);
textViewETA.setText("ETA : " + etaFormatted);



// Afficher la vitesse de transfert dans la TextView
speedl.setText(MessageFormat.format("{0}{1} MB/S", vitesse_du_tl_chargement, String.format("%.1f", bytesPerSecond)));
speedl.setText("Vitesse : " + String.format("%.1f", bytesPerSecond) + " MB/s");
// Mettre à jour le nombre d'octets téléchargés jusqu'à présent
bytesDownloaded[0] = downloadedBytes;
textView.setText(R.string.t_l_chargement_en_cours + progress + "%");
progressdl.setProgress(progress);
handler.postDelayed(this, 1000); // Vérifier à nouveau dans 50 milliseconde
textView.setText(getString(R.string.t_l_chargement_en_cours) + Math.abs(progress) + "%");

progressdl.setProgress(Math.abs(progress));
handler.postDelayed(this, 500); // Vérifier à nouveau dans 50 millisecondes
}
}
cursor.close();
Expand All @@ -279,7 +295,9 @@ public void run() {
}

private void showNextDialog() {
alert2.dismiss();
if (alert2 != null && alert2.isShowing()) {
alert2.dismiss();
}
}
});

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/hugdev/webapper/tuto_2.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
int savedTheme = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("selected_theme", NONE);
if (savedTheme != NONE) {
selectedTheme = savedTheme;
}else{
selectedTheme = R.style.Theme_WebApper_dark;
}
setTheme(selectedTheme);
DynamicColors.applyToActivitiesIfAvailable(this.getApplication());
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/hugdev/webapper/tuto_3.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ protected void onCreate(Bundle savedInstanceState) {
int savedTheme = getSharedPreferences("PREFERENCE", MODE_PRIVATE).getInt("selected_theme", NONE);
if (savedTheme != NONE) {
selectedTheme = savedTheme;
}else{
selectedTheme = R.style.Theme_WebApper_dark;
}
setTheme(selectedTheme);
DynamicColors.applyToActivitiesIfAvailable(this.getApplication());
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android.useAndroidX=true
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.enableJetifier=true
android.enableJetifier=false
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.unsafe.configuration-cache=true
Expand Down

0 comments on commit 8b50c46

Please sign in to comment.