Skip to content

Commit bc743d1

Browse files
committed
show overall transfer speed of filetransfers
1 parent 32132a3 commit bc743d1

5 files changed

Lines changed: 71 additions & 3 deletions

File tree

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperFiletransfer.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import java.nio.ByteBuffer;
4343
import java.util.List;
4444
import java.util.Random;
45+
import java.util.concurrent.ExecutionException;
4546

4647
import androidx.core.content.FileProvider;
4748

@@ -341,14 +342,32 @@ public static void set_filetransfer_accepted_from_id(long filetransfer_id)
341342
{
342343
try
343344
{
344-
orma.updateFiletransfer().idEq(filetransfer_id).ft_accepted(true).execute();
345+
orma.updateFiletransfer().idEq(filetransfer_id).ft_accepted(true).
346+
transfer_start_ts(System.currentTimeMillis()).
347+
execute();
345348
}
346349
catch (Exception e)
347350
{
348351
e.printStackTrace();
349352
}
350353
}
351354

355+
public static String format_speed(float bytes_per_second) {
356+
try {
357+
if (bytes_per_second > 1024) {
358+
if (bytes_per_second > (1024 * 1024)) {
359+
return String.format("%.2f MiB/s", bytes_per_second / (1024 * 1024));
360+
} else {
361+
return String.format("%.2f kiB/s", bytes_per_second / 1024);
362+
}
363+
} else {
364+
return String.format("%.2f Bytes/s", bytes_per_second);
365+
}
366+
} catch (Exception e) {
367+
}
368+
return "? Bytes/s";
369+
}
370+
352371
public static long get_filetransfer_filenum_from_id(long filetransfer_id)
353372
{
354373
try

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/HelperMessage.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import static com.zoffcc.applications.trifa.MainActivity.main_handler_s;
5050
import static com.zoffcc.applications.trifa.TRIFAGlobals.PUSH_URL_TRIGGER_GET_MESSAGE_FOR_delta_ms_after;
5151
import static com.zoffcc.applications.trifa.TRIFAGlobals.PUSH_URL_TRIGGER_GET_MESSAGE_FOR_delta_ms_prev;
52+
import static com.zoffcc.applications.trifa.ToxVars.TOX_FILE_CONTROL.TOX_FILE_CONTROL_RESUME;
5253
import static com.zoffcc.applications.trifa.ToxVars.TOX_HASH_LENGTH;
5354
import static com.zoffcc.applications.trifa.ToxVars.TOX_MESSAGE_TYPE.TOX_MESSAGE_TYPE_HIGH_LEVEL_ACK;
5455
import static com.zoffcc.applications.trifa.TrifaToxService.orma;

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/MainActivity.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6924,6 +6924,20 @@ else if (ft.kind == TOX_FILE_KIND_FTV2.value)
69246924
}
69256925
else
69266926
{
6927+
if (position == 0)
6928+
{
6929+
// this is the first chunk of an outgoing FT
6930+
// remember this timestamp as FT start time
6931+
try
6932+
{
6933+
orma.updateFiletransfer().idEq(ft.id).transfer_start_ts(
6934+
System.currentTimeMillis()).execute();
6935+
}
6936+
catch(Exception e)
6937+
{
6938+
}
6939+
}
6940+
69276941
if (ft.storage_frame_work)
69286942
{
69296943
long file_chunk_length = length;
@@ -7073,6 +7087,20 @@ else if (ft.kind == TOX_FILE_KIND_FTV2.value)
70737087
}
70747088
else
70757089
{
7090+
if (position == 0)
7091+
{
7092+
// this is the first chunk of an outgoing FT
7093+
// remember this timestamp as FT start time
7094+
try
7095+
{
7096+
orma.updateFiletransfer().idEq(ft.id).transfer_start_ts(
7097+
System.currentTimeMillis()).execute();
7098+
}
7099+
catch(Exception e)
7100+
{
7101+
}
7102+
}
7103+
70767104
if (ft.storage_frame_work)
70777105
{
70787106
long file_chunk_length = length;

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/MessageListHolder_file_incoming_state_resume.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import androidx.core.content.ContextCompat;
4141
import androidx.recyclerview.widget.RecyclerView;
4242

43+
import static com.zoffcc.applications.trifa.HelperFiletransfer.format_speed;
4344
import static com.zoffcc.applications.trifa.HelperFiletransfer.get_filetransfer_filenum_from_id;
4445
import static com.zoffcc.applications.trifa.HelperFiletransfer.remove_vfs_ft_from_cache;
4546
import static com.zoffcc.applications.trifa.HelperFiletransfer.set_filetransfer_state_from_id;
@@ -209,8 +210,17 @@ public void bindMessageList(Message m)
209210
final int percent = (int) (100f * (float) ft_.current_position / (float) ft_.filesize);
210211
// Log.i(TAG, "getView:033:STATE:RESUME:percent=" + percent + " cur=" + ft_.current_position + " size=" + ft_.filesize);
211212
ft_progressbar.setProgress(percent);
213+
long start_ts = ft_.transfer_start_ts;
214+
long now = System.currentTimeMillis();
215+
long delta_ts_ms = now - start_ts;
216+
if (delta_ts_ms < 1)
217+
{
218+
delta_ts_ms = 1;
219+
}
220+
float speed_bytes_per_second = (((float)ft_.current_position) / (float)delta_ts_ms) * 1000.0f;
212221
// TODO: make text better
213-
textView.setText("" + message.text + "\n" + ft_.current_position + "/" + ft_.filesize + "\n receiving ...");
222+
textView.setText("" + message.text + "\n" + ft_.current_position + "/" + ft_.filesize + "\n receiving ..."+
223+
"\n" + format_speed(speed_bytes_per_second));
214224
if (MESSAGE_TEXT_SIZE[PREF__global_font_size] > MESSAGE_TEXT_SIZE_FT_SMALL)
215225
{
216226
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, MESSAGE_TEXT_SIZE_FT_SMALL);

android-refimpl-app/app/src/main/java/com/zoffcc/applications/trifa/MessageListHolder_file_outgoing_state_resume.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import androidx.documentfile.provider.DocumentFile;
5353
import androidx.recyclerview.widget.RecyclerView;
5454

55+
import static com.zoffcc.applications.trifa.HelperFiletransfer.format_speed;
5556
import static com.zoffcc.applications.trifa.HelperFiletransfer.get_filetransfer_filenum_from_id;
5657
import static com.zoffcc.applications.trifa.HelperFiletransfer.remove_ft_from_cache;
5758
import static com.zoffcc.applications.trifa.HelperFiletransfer.set_filetransfer_state_from_id;
@@ -204,9 +205,18 @@ public void bindMessageList(Message m)
204205
final int percent = (int) (100f * (float) ft_.current_position / (float) ft_.filesize);
205206
// Log.i(TAG, "getView:033:STATE:RESUME:percent=" + percent + " cur=" + ft_.current_position + " size=" + ft_.filesize);
206207
ft_progressbar.setProgress(percent);
208+
long start_ts = ft_.transfer_start_ts;
209+
long now = System.currentTimeMillis();
210+
long delta_ts_ms = now - start_ts;
211+
if (delta_ts_ms < 1)
212+
{
213+
delta_ts_ms = 1;
214+
}
215+
float speed_bytes_per_second = (((float)ft_.current_position) / (float)delta_ts_ms) * 1000.0f;
207216
// TODO: make text better
208217
textView.setAutoLinkText(
209-
"" + message.text + "\n" + ft_.current_position + "/" + ft_.filesize + "\n sending ...");
218+
"" + message.text + "\n" + ft_.current_position + "/" + ft_.filesize + "\n sending ..." +
219+
"\n" + format_speed(speed_bytes_per_second));
210220
}
211221
else
212222
{

0 commit comments

Comments
 (0)