Network traffic with automatic units [1/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: I01582a3eaa73fa6845776c5b131ddc1e0deabe39
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index 9664012..265ca28 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -392,6 +392,7 @@
         <item>@string/network_traffic_units_megabits</item>
         <item>@string/network_traffic_units_kilobytes</item>
         <item>@string/network_traffic_units_megabytes</item>
+        <item>@string/network_traffic_units_auto</item>
     </string-array>
 
     <string-array name="network_traffic_units_values" translatable="false">
@@ -399,6 +400,39 @@
         <item>1</item>
         <item>2</item>
         <item>3</item>
+        <item>4</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_entries" translatable="false">
+        <item>@string/network_traffic_show_units_off</item>
+        <item>@string/network_traffic_show_units_on</item>
+        <item>@string/network_traffic_show_units_compact</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_values" translatable="false">
+        <item>0</item>
+        <item>1</item>
+        <item>2</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_entries_bits" translatable="false">
+        <item>@string/network_traffic_show_units_off</item>
+        <item>@string/network_traffic_show_units_on</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_values_bits" translatable="false">
+        <item>0</item>
+        <item>1</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_entries_auto" translatable="false">
+        <item>@string/network_traffic_show_units_on</item>
+        <item>@string/network_traffic_show_units_compact</item>
+    </string-array>
+
+    <string-array name="network_traffic_show_units_values_auto" translatable="false">
+        <item>1</item>
+        <item>2</item>
     </string-array>
 
     <!-- Sms limit -->
diff --git a/res/values/strings.xml b/res/values/strings.xml
index f63a42c..0358a7e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -516,7 +516,11 @@
     <string name="network_traffic_units_megabits">Megabits per second (Mb/s)</string>
     <string name="network_traffic_units_kilobytes">Kilobytes per second (kB/s)</string>
     <string name="network_traffic_units_megabytes">Megabytes per second (MB/s)</string>
+    <string name="network_traffic_units_auto">Automatic (kB/s or MB/s)</string>
     <string name="network_traffic_show_units">Show units</string>
+    <string name="network_traffic_show_units_off">Off</string>
+    <string name="network_traffic_show_units_on">On</string>
+    <string name="network_traffic_show_units_compact">Compact</string>
     <string name="network_traffic_show_units_summary">Whether to show traffic measurement units in the statusbar</string>
     <string name="network_traffic_disabled_clock">Network traffic disabled due to clock position</string>
 
diff --git a/res/xml/network_traffic_settings.xml b/res/xml/network_traffic_settings.xml
index efa7aa4..ea6be97 100644
--- a/res/xml/network_traffic_settings.xml
+++ b/res/xml/network_traffic_settings.xml
@@ -35,10 +35,12 @@
         android:summary="%s"
         android:title="@string/network_traffic_units_title" />
 
-    <lineageos.preference.LineageSecureSettingSwitchPreference
-        android:defaultValue="true"
+    <DropDownPreference
+        android:entries="@array/network_traffic_show_units_entries"
+        android:entryValues="@array/network_traffic_show_units_values"
         android:key="network_traffic_show_units"
-        android:summary="@string/network_traffic_show_units_summary"
+        android:persistent="false"
+        android:summary="%s"
         android:title="@string/network_traffic_show_units" />
 
 </PreferenceScreen>
diff --git a/src/org/lineageos/lineageparts/statusbar/NetworkTrafficSettings.java b/src/org/lineageos/lineageparts/statusbar/NetworkTrafficSettings.java
index 316f615..e2827a6 100644
--- a/src/org/lineageos/lineageparts/statusbar/NetworkTrafficSettings.java
+++ b/src/org/lineageos/lineageparts/statusbar/NetworkTrafficSettings.java
@@ -29,11 +29,21 @@
     private static final int POSITION_CENTER = 1;
     private static final int POSITION_END = 2;
 
+    private static final int UNITS_KILOBITS = 0;
+    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;
+
     private DropDownPreference mNetTrafficMode;
     private DropDownPreference mNetTrafficPosition;
     private LineageSecureSettingSwitchPreference mNetTrafficAutohide;
     private DropDownPreference mNetTrafficUnits;
-    private LineageSecureSettingSwitchPreference mNetTrafficShowUnits;
+    private DropDownPreference mNetTrafficShowUnits;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
@@ -90,11 +100,12 @@
         mNetTrafficUnits = findPreference(LineageSettings.Secure.NETWORK_TRAFFIC_UNITS);
         mNetTrafficUnits.setOnPreferenceChangeListener(this);
         int units = LineageSettings.Secure.getInt(resolver,
-                LineageSettings.Secure.NETWORK_TRAFFIC_UNITS, /* Mbps */ 1);
+                LineageSettings.Secure.NETWORK_TRAFFIC_UNITS, UNITS_KILOBYTES);
         mNetTrafficUnits.setValue(String.valueOf(units));
 
         mNetTrafficShowUnits = findPreference(LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS);
         mNetTrafficShowUnits.setOnPreferenceChangeListener(this);
+        adjustShowUnitsState(units, resolver);
 
         updateEnabledStates(mode);
     }
@@ -114,10 +125,48 @@
             int units = Integer.parseInt((String) newValue);
             LineageSettings.Secure.putInt(getActivity().getContentResolver(),
                     LineageSettings.Secure.NETWORK_TRAFFIC_UNITS, units);
+            adjustShowUnitsState(units, getActivity().getContentResolver());
+        } else if (preference == mNetTrafficShowUnits) {
+            int showUnits = Integer.valueOf((String) newValue);
+            LineageSettings.Secure.putInt(getActivity().getContentResolver(),
+                    LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS, showUnits);
         }
         return true;
     }
 
+    private void adjustShowUnitsState(int units, ContentResolver resolver) {
+        int showUnits = LineageSettings.Secure.getInt(resolver,
+                LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS, SHOW_UNITS_ON);
+        if (units == UNITS_KILOBYTES || units == UNITS_MEGABYTES) {
+            // off, on, compact
+            mNetTrafficShowUnits.setEntries(R.array.network_traffic_show_units_entries);
+            mNetTrafficShowUnits.setEntryValues(R.array.network_traffic_show_units_values);
+        } else {
+            boolean putShowUnits = false;
+            if (units == UNITS_AUTOBYTES) {
+                if (showUnits == SHOW_UNITS_OFF) {
+                    showUnits = SHOW_UNITS_COMPACT;
+                    putShowUnits = true;
+                }
+                // on, compact
+                mNetTrafficShowUnits.setEntries(R.array.network_traffic_show_units_entries_auto);
+                mNetTrafficShowUnits.setEntryValues(R.array.network_traffic_show_units_values_auto);
+            } else {
+                if (showUnits == SHOW_UNITS_COMPACT) {
+                    showUnits = SHOW_UNITS_ON;
+                    putShowUnits = true;
+                }
+                // off, on
+                mNetTrafficShowUnits.setEntries(R.array.network_traffic_show_units_entries_bits);
+                mNetTrafficShowUnits.setEntryValues(R.array.network_traffic_show_units_values_bits);
+            }
+            if (putShowUnits)
+                LineageSettings.Secure.putInt(resolver,
+                        LineageSettings.Secure.NETWORK_TRAFFIC_SHOW_UNITS, showUnits);
+        }
+        mNetTrafficShowUnits.setValue(String.valueOf(showUnits));
+    }
+
     private void updateEnabledStates(int mode) {
         final boolean enabled = mode != 0;
         mNetTrafficPosition.setEnabled(enabled);