Browse Source

Show individual range progress

master v0.1.4
Trevor Slocum 3 years ago
parent
commit
2f845a967b
  1. 4
      CHANGELOG
  2. 6
      app/build.gradle
  3. 2
      app/src/main/java/space/rocketnine/gophast/Download.java
  4. 21
      app/src/main/java/space/rocketnine/gophast/DownloadAdapter.java
  5. 32
      app/src/main/java/space/rocketnine/gophast/DownloadRunnable.java
  6. 12
      app/src/main/java/space/rocketnine/gophast/GoPhast.java
  7. 70
      app/src/main/java/space/rocketnine/gophast/ProgressBar.java
  8. 2
      app/src/main/res/layout/list_downloads.xml

4
CHANGELOG

@ -1,3 +1,7 @@
0.1.4:
- Show individual range progress
- Support servers which do not allow partial downloads
0.1.3:
- Improve pause and resume support

6
app/build.gradle

@ -1,7 +1,7 @@
apply plugin: 'com.android.application'
project.ext {
libraryVersion = "0.1.5" // https://git.sr.ht/~tslocum/gophast/refs
libraryVersion = "0.1.6" // https://git.sr.ht/~tslocum/gophast/refs
}
android {
@ -13,8 +13,8 @@ android {
minSdkVersion 16
targetSdkVersion 28
versionCode 1013
versionName "0.1.3"
versionCode 1014
versionName "0.1.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

2
app/src/main/java/space/rocketnine/gophast/Download.java

@ -11,7 +11,7 @@ public class Download {
String Name;
long Status;
long Size;
long Downloaded;
byte[] Downloaded;
Uri Path;
boolean Paused;
boolean Complete;

21
app/src/main/java/space/rocketnine/gophast/DownloadAdapter.java

@ -2,7 +2,6 @@ package space.rocketnine.gophast;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.format.Formatter;
@ -10,7 +9,6 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.ArrayList;
@ -31,12 +29,10 @@ public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.Downlo
}
public static class DeltaProgress {
int progress;
boolean animate;
byte[] progress;
public DeltaProgress(int p, boolean a) {
public DeltaProgress(byte[] p) {
progress = p;
animate = a;
}
}
@ -70,7 +66,6 @@ public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.Downlo
txtSize = v.findViewById(R.id.downloadSize);
progressBar = v.findViewById(R.id.downloadProgress);
progressBar.setMax(10000);
}
}
@ -102,11 +97,7 @@ public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.Downlo
holder.txtURL.setText(d.URL);
setDownloadSize(holder, new DeltaSize(d.Size));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.progressBar.setProgress((int) (d.Downloaded * 10000.0 / d.Size), false);
} else {
holder.progressBar.setProgress((int) (d.Downloaded * 10000.0 / d.Size));
}
holder.progressBar.setProgress(d.Downloaded);
holder.v.setOnClickListener(v -> {
Download download = downloads.get(holder.getAdapterPosition());
@ -217,11 +208,7 @@ public class DownloadAdapter extends RecyclerView.Adapter<DownloadAdapter.Downlo
}
private void setDownloadProgress(final DownloadViewHolder holder, DeltaProgress d) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
holder.progressBar.setProgress(d.progress, d.animate);
} else {
holder.progressBar.setProgress(d.progress);
}
holder.progressBar.setProgress(d.progress);
}
private void setDownloadSize(final DownloadViewHolder holder, DeltaSize d) {

32
app/src/main/java/space/rocketnine/gophast/DownloadRunnable.java

@ -6,6 +6,8 @@ import android.os.Process;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.Arrays;
import manager.Manager;
import manager.NewDownloadInfo;
@ -13,6 +15,7 @@ class DownloadRunnable implements Runnable {
Download d;
GoPhast gophast;
RecyclerView recyclerView;
byte[] downloaded;
boolean wasPaused;
boolean animate;
@ -25,11 +28,7 @@ class DownloadRunnable implements Runnable {
private Runnable notifyProgressRunnable = new Runnable() {
@Override
public void run() {
gophast.adapter.notifyItemChanged(d.Index, new DownloadAdapter.DeltaProgress((int) (d.Downloaded * 10000.0 / d.Size), animate));
if (!animate && d.Downloaded > 0) {
animate = true;
}
gophast.adapter.notifyItemChanged(d.Index, new DownloadAdapter.DeltaProgress(downloaded));
}
};
private Runnable notifyStatusRunnable = new Runnable() {
@ -63,21 +62,24 @@ class DownloadRunnable implements Runnable {
recyclerView.post(notifySizeRunnable);
byte[] finished = "10000".getBytes();
long size = d.Size;
long id = d.ID;
long lastDownloaded = -99;
long newDownloaded;
byte[] lastDownloaded = "0,10000".getBytes();
int delay = 25;
boolean active;
for (; ; ) {
newDownloaded = Manager.getDownloaded(id);
active = lastDownloaded != newDownloaded;
downloaded = Manager.getVerboseDownloaded(id);
active = !Arrays.equals(lastDownloaded, downloaded);
if (active) {
d.Downloaded = newDownloaded;
lastDownloaded = newDownloaded;
recyclerView.post(notifyProgressRunnable);
if (newDownloaded == size) {
d.Downloaded = downloaded;
lastDownloaded = downloaded;
delay = 25;
if (Arrays.equals(downloaded, finished)) {
d.Paused = false;
d.Complete = true;
recyclerView.post(notifyStatusRunnable);
@ -106,8 +108,6 @@ class DownloadRunnable implements Runnable {
wasPaused = false;
recyclerView.post(notifyStatusRunnable);
delay = 25;
} else {
delay = 75;
}
try {
@ -126,10 +126,10 @@ class DownloadRunnable implements Runnable {
d.DownloadManagerID = getApp().DownloadManager.addCompletedDownload(d.Name, d.URL, true, d.getMIME(), d.Path.toString(), d.Size, true);
}
d.Downloaded = d.Size;
d.Downloaded = finished;
} else {
Log.d("gophast", "Status <= 0 " + d.Status);
d.Downloaded = 0;
d.Downloaded = new byte[0];
}
} catch (Exception e) {
Log.d("gophast", "Failed to download " + d.URL);

12
app/src/main/java/space/rocketnine/gophast/GoPhast.java

@ -107,7 +107,7 @@ public class GoPhast extends Application {
Metadata md;
try {
md = Manager.fetchMetadata(info);
md = Manager.fetchMetadata(info);
} catch (Exception e) {
longToast(e.getMessage());
return;
@ -128,15 +128,13 @@ public class GoPhast extends Application {
// TODO: Update add dialog with "fetching metadata" and overwrite warning for smoother transition
// Allow passing known metadata later to reduce transfer time
promtExistingFile(download, activity);
promptExistingFile(download, activity);
} else {
finishAddingDownload(download);
}
}
void promtExistingFile(Download download, Activity activity) {
final View view = activity.getLayoutInflater().inflate(R.layout.dialog_file_exists, null);
void promptExistingFile(Download download, Activity activity) {
AlertDialog.Builder alert = new AlertDialog.Builder(activity, R.style.AppTheme_Dialog)
.setTitle(String.format(getString(R.string.overwriteFile), download.Name))
.setIcon(R.drawable.ic_warning_white)
@ -226,7 +224,7 @@ public class GoPhast extends Application {
acquireWakeLock(d.ID);
}
void togglePauseDownload(Download d){
void togglePauseDownload(Download d) {
if (!d.Paused) {
exec.execute(new Runnable() {
@Override
@ -267,7 +265,7 @@ public class GoPhast extends Application {
}
}
void toggleOpenDownloadWhenComplete(Download d){
void toggleOpenDownloadWhenComplete(Download d) {
if (d.Complete) {
return;
}

70
app/src/main/java/space/rocketnine/gophast/ProgressBar.java

@ -0,0 +1,70 @@
package space.rocketnine.gophast;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class ProgressBar extends View {
Paint paintYellow;
int[] progress;
public ProgressBar(Context context, AttributeSet attribs) {
super(context, attribs);
paintYellow = new Paint();
paintYellow.setColor(context.getResources().getColor(R.color.accent));
}
public void setProgress(byte[] p) {
if (p != null && p.length > 0) {
String[] ps = new String(p).split(",");
int[] newProgress = new int[ps.length];
int i = 0;
for (String prog : ps) {
newProgress[i] = Integer.valueOf(prog);
i++;
}
progress = newProgress;
} else {
progress = new int[0];
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
if (progress.length == 0) {
return;
}
int w = getWidth();
int h = getHeight();
float scale = (float) w / 10000;
int left = 0;
int size = 0;
boolean fill = false;
for (int p : progress) {
fill = !fill;
size = (int) (p * scale);
if (size == 0) {
continue;
} else if (size > w - left) {
size = w - left;
}
if (fill) {
canvas.drawRect(left, 0, left + size, h, paintYellow);
}
left += size;
}
}
}

2
app/src/main/res/layout/list_downloads.xml

@ -50,7 +50,7 @@
</LinearLayout>
<ProgressBar
<space.rocketnine.gophast.ProgressBar
android:id="@+id/downloadProgress"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"

Loading…
Cancel
Save