Network traffic with automatic units [2/2]

A new network traffic display unit option "automatic" offers
a compact display of the network traffic by using at maximum
three digits and an abbreviated unit string.
Comes in handy for situations with reduced space in the
status bar.

Change-Id: Ib4d969924ad5a345b03540070e49a0473f343ad3
This commit is contained in:
Wolfram Liebchen
2023-09-12 19:58:52 +02:00
committed by Michael Bestas
parent dba5da3057
commit 9bc61e26c8
4 changed files with 53 additions and 17 deletions

View File

@@ -109,6 +109,8 @@
<string name="megabitspersecond_short">Mb/s</string>
<string name="kilobytespersecond_short">kB/s</string>
<string name="megabytespersecond_short">MB/s</string>
<string name="kilobytespersecond_compact">k</string>
<string name="megabytespersecond_compact">M</string>
<!-- Trust interface -->
<!-- This string will be referenced from other apps when they're referring to the Trust interface.

View File

@@ -137,6 +137,8 @@
<java-symbol type="string" name="megabitspersecond_short" />
<java-symbol type="string" name="kilobytespersecond_short" />
<java-symbol type="string" name="megabytespersecond_short" />
<java-symbol type="string" name="kilobytespersecond_compact" />
<java-symbol type="string" name="megabytespersecond_compact" />
<java-symbol type="array" name="config_vendorPlatformSignatures" />

View File

@@ -2904,22 +2904,31 @@ public final class LineageSettings {
/**
* Measurement unit preference for network traffic
* 0 = kBit/s
* 1 = MBit/s
* 2 = kByte/s
* 3 = MByte/s
* 4 = automatic kByte/s or MByte/s
* @hide
*/
public static final String NETWORK_TRAFFIC_UNITS = "network_traffic_units";
/** @hide */
public static final Validator NETWORK_TRAFFIC_UNITS_VALIDATOR =
new InclusiveIntegerRangeValidator(0, 3);
new InclusiveIntegerRangeValidator(0, 4);
/**
* Whether or not to show measurement units in the network traffic indiciator
* How to show measurement units in the network traffic indiciator
* 0 = off
* 1 = on
* 2 = compact
* @hide
*/
public static final String NETWORK_TRAFFIC_SHOW_UNITS = "network_traffic_show_units";
/** @hide */
public static final Validator NETWORK_TRAFFIC_SHOW_UNITS_VALIDATOR = sBooleanValidator;
public static final Validator NETWORK_TRAFFIC_SHOW_UNITS_VALIDATOR =
new InclusiveIntegerRangeValidator(0, 2);
/**
* Trust warnings status

View File

@@ -73,6 +73,11 @@ public class NetworkTraffic extends TextView {
private static final int UNITS_MEGABITS = 1;
private static final int UNITS_KILOBYTES = 2;
private static final int UNITS_MEGABYTES = 3;
private static final int UNITS_AUTOBYTES = 4;
private static final int SHOW_UNITS_OFF = 0;
private static final int SHOW_UNITS_ON = 1;
private static final int SHOW_UNITS_COMPACT = 2;
// Thresholds themselves are always defined in kbps
private static final long AUTOHIDE_THRESHOLD_KILOBITS = 10;
@@ -97,7 +102,7 @@ public class NetworkTraffic extends TextView {
private boolean mAutoHide;
private long mAutoHideThreshold;
private int mUnits;
private boolean mShowUnits;
private int mShowUnits;
private int mIconTint = Color.WHITE;
private Drawable mDrawable;
@@ -244,30 +249,48 @@ public class NetworkTraffic extends TextView {
private String formatOutput(long kbps) {
final String value;
final String unit;
int unitid = 0;
switch (mUnits) {
case UNITS_KILOBITS:
value = String.format("%d", kbps);
unit = mContext.getString(R.string.kilobitspersecond_short);
unitid = R.string.kilobitspersecond_short;
break;
case UNITS_MEGABITS:
value = String.format("%.1f", (float) kbps / 1000);
unit = mContext.getString(R.string.megabitspersecond_short);
unitid = R.string.megabitspersecond_short;
break;
case UNITS_KILOBYTES:
value = String.format("%d", kbps / 8);
unit = mContext.getString(R.string.kilobytespersecond_short);
break;
case UNITS_AUTOBYTES:
if (kbps < 8000 || mUnits == UNITS_KILOBYTES) {
value = String.format("%.0f", (float) kbps / 8 );
unitid = mShowUnits == SHOW_UNITS_COMPACT
? R.string.kilobytespersecond_compact
: R.string.kilobytespersecond_short;
break;
}
case UNITS_MEGABYTES:
value = String.format("%.2f", (float) kbps / 8000);
unit = mContext.getString(R.string.megabytespersecond_short);
{
final String format;
if (kbps < 80000) {
format = "%.2f";
} else if (kbps < 800000) {
format = "%.1f";
} else {
format = "%.0f";
}
value = String.format(format, (float) kbps / 8000 );
}
unitid = mShowUnits == SHOW_UNITS_COMPACT
? R.string.megabytespersecond_compact
: R.string.megabytespersecond_short;
break;
default:
value = "unknown";
unit = "unknown";
break;
}
if (mShowUnits) {
if (mShowUnits > SHOW_UNITS_OFF && unitid != 0) {
unit = mContext.getString(unitid);
return value + " " + unit;
} else {
return value;
@@ -411,7 +434,9 @@ public class NetworkTraffic extends TextView {
mAutoHide = LineageSettings.Secure.getInt(resolver,
LineageSettings.Secure.NETWORK_TRAFFIC_AUTOHIDE, 0) == 1;
mUnits = LineageSettings.Secure.getInt(resolver,
LineageSettings.Secure.NETWORK_TRAFFIC_UNITS, /* Mbps */ 1);
LineageSettings.Secure.NETWORK_TRAFFIC_UNITS, UNITS_KILOBYTES);
mShowUnits = LineageSettings.Secure.getInt(resolver,
LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS, SHOW_UNITS_ON);
switch (mUnits) {
case UNITS_KILOBITS:
@@ -421,6 +446,7 @@ public class NetworkTraffic extends TextView {
mAutoHideThreshold = AUTOHIDE_THRESHOLD_MEGABITS;
break;
case UNITS_KILOBYTES:
case UNITS_AUTOBYTES:
mAutoHideThreshold = AUTOHIDE_THRESHOLD_KILOBYTES;
break;
case UNITS_MEGABYTES:
@@ -431,9 +457,6 @@ public class NetworkTraffic extends TextView {
break;
}
mShowUnits = LineageSettings.Secure.getInt(resolver,
LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS, 1) == 1;
if (mMode != MODE_DISABLED) {
updateTrafficDrawable();
}