mirror of
https://github.com/syncthing/syncthing-android.git
synced 2024-11-26 22:31:16 +00:00
Fixed crash if a transfer rate between 1 and 7 b/s was displayed.
This commit is contained in:
parent
74c148b790
commit
1786c6286d
2 changed files with 17 additions and 2 deletions
|
@ -91,6 +91,15 @@ public class RestApiTest extends AndroidTestCase {
|
||||||
assertEquals("1 GiB/s", RestApi.readableTransferRate(getContext(), 8589934592L));
|
assertEquals("1 GiB/s", RestApi.readableTransferRate(getContext(), 8589934592L));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SmallTest
|
||||||
|
public void testConvertNotCrashing() {
|
||||||
|
long[] values = new long[]{-1, 0, 1, 2, 4, 8, 16, 1024, 2^10, 2^15, 2^20, 2^25, 2^30};
|
||||||
|
for (long l : values) {
|
||||||
|
assertNotSame("", RestApi.readableFileSize(getContext(), l));
|
||||||
|
assertNotSame("", RestApi.readableTransferRate(getContext(), l));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@MediumTest
|
@MediumTest
|
||||||
public void testGetConnections() throws InterruptedException {
|
public void testGetConnections() throws InterruptedException {
|
||||||
final CountDownLatch latch = new CountDownLatch(1);
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
|
@ -568,13 +568,19 @@ public class RestApi implements SyncthingService.OnWebGuiAvailableListener,
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a number of bytes to a human readable transfer rate in bits (eg 100 Kb/s).
|
* Converts a number of bytes to a human readable transfer rate in bytes per second
|
||||||
|
* (eg 100 KiB/s).
|
||||||
*/
|
*/
|
||||||
public static String readableTransferRate(Context context, long bits) {
|
public static String readableTransferRate(Context context, long bits) {
|
||||||
final String[] units = context.getResources().getStringArray(R.array.transfer_rate_units);
|
final String[] units = context.getResources().getStringArray(R.array.transfer_rate_units);
|
||||||
if (bits <= 0) return "0 " + units[0];
|
|
||||||
long bytes = bits / 8;
|
long bytes = bits / 8;
|
||||||
|
if (bytes <= 0) return "0 " + units[0];
|
||||||
int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
|
int digitGroups = (int) (Math.log10(bytes) / Math.log10(1024));
|
||||||
|
if (digitGroups < 0 || digitGroups > 4)
|
||||||
|
throw new ArrayIndexOutOfBoundsException("bits=" + Long.toString(bits) +
|
||||||
|
", bytes=" + Long.toString(bytes) +
|
||||||
|
" (int) digitGroups=" + Integer.toString(digitGroups) +
|
||||||
|
" (double) digitGroups=" + Double.toString(Math.log10(bytes) / Math.log10(1024)));
|
||||||
return new DecimalFormat("#,##0.#")
|
return new DecimalFormat("#,##0.#")
|
||||||
.format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
|
.format(bytes / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue