Merge "Implement all remaining Units Preferences" into pi-car-dev
diff --git a/res/values/preference_keys.xml b/res/values/preference_keys.xml
index fc9c2d7..73f4405 100644
--- a/res/values/preference_keys.xml
+++ b/res/values/preference_keys.xml
@@ -267,10 +267,12 @@
 
     <!-- Units Settings -->
     <string name="pk_units_settings_entry" translatable="false">units_settings_entry</string>
-    <string name="pk_units_distance_and_volume" translatable="false">units_distance_and_volume</string>
+    <string name="pk_units_distance" translatable="false">units_distance</string>
     <string name="pk_units_speed">units_speed</string>
     <string name="pk_units_fuel_consumption">units_fuel_consumption</string>
+    <string name="pk_units_energy_consumption">units_energy_consumption</string>
     <string name="pk_units_temperature">units_temperature</string>
+    <string name="pk_units_volume" translatable="false">units_volume</string>
     <string name="pk_units_pressure">units_pressure</string>
 
     <!-- Location Settings -->
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 1805e8d..cda2da0 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -384,13 +384,22 @@
     <!-- Units Settings -->
     <!-- Units Settings title [CHAR LIMIT=10] -->
     <string name="units_settings">Units</string>
-    <string name="units_distance_and_volume_title">Distance and volume</string>
     <string name="units_speed_title">Speed</string>
+    <string name="units_distance_title">Distance</string>
     <string name="units_fuel_consumption_title">Fuel consumption</string>
-    <string name="units_battery_consumption_title">Energy consumption</string>
+    <string name="units_energy_consumption_title">Energy consumption</string>
     <string name="units_temperature_title">Temperature</string>
+    <string name="units_volume_title">Volume</string>
     <string name="units_pressure_title">Pressure</string>
 
+    <!--Units list entry, it shows the unit abbreviation (not translated) and then the same unit as it is pronounced, in that order. (e.g. mph - Miles per hour) [CHAR LIMIT = 120] -->
+    <string name="units_list_entry"><xliff:g example="mph" id="unit_abbreviation">%1$s</xliff:g> - <xliff:g example="Miles per hour" id="unit_pronunciation">%2$s</xliff:g></string>
+    <!--Units template for ratio units with numerators and denominators [CHAR LIMIT = 50]-->
+    <string name="units_ratio"><xliff:g example="km" id="unit_numerator">%1$s</xliff:g>/<xliff:g example="h" id="unit_denominator">%2$s</xliff:g></string>
+    <!--Units numerator part that can be used in ratio units string template-->
+    <string name="units_ratio_numerator"><xliff:g example="10" id="unit_numerator_quantity">%1$d</xliff:g><xliff:g example="kWh" id="unit_numerator_unit">%2$s</xliff:g></string>
+    <!--Units denominator part that can be used in ratio units string template-->
+    <string name="units_ratio_denominator"><xliff:g example="100" id="unit_denominator_quantity">%1$d</xliff:g><xliff:g example="mi" id="unit_denominator_unit">%2$s</xliff:g></string>
     <!-- Meter per Second Unit as it is pronounced [CHAR LIMIT=50]-->
     <string name="units_unit_name_meter_per_sec">Meter per Second</string>
     <!-- Revolutions per Minute Unit as it is pronounced [CHAR LIMIT=50]-->
diff --git a/res/xml/units_fragment.xml b/res/xml/units_fragment.xml
index ee17049..ab1d687 100644
--- a/res/xml/units_fragment.xml
+++ b/res/xml/units_fragment.xml
@@ -18,8 +18,45 @@
                   xmlns:settings="http://schemas.android.com/apk/res-auto"
                   android:title="@string/units_settings" >
     <ListPreference
+        android:key="@string/pk_units_speed"
+        android:title="@string/units_speed_title"
+        android:dialogTitle="@string/units_speed_title"
+        settings:controller="com.android.car.settings.units.UnitsSpeedPreferenceController"/>
+
+    <ListPreference
+        android:key="@string/pk_units_distance"
+        android:title="@string/units_distance_title"
+        android:dialogTitle="@string/units_distance_title"
+        settings:controller="com.android.car.settings.units.UnitsDistancePreferenceController"/>
+
+    <ListPreference
+        android:key="@string/pk_units_fuel_consumption"
+        android:title="@string/units_fuel_consumption_title"
+        android:dialogTitle="@string/units_fuel_consumption_title"
+        settings:controller="com.android.car.settings.units.UnitsFuelConsumptionPreferenceController"/>
+
+    <ListPreference
+        android:key="@string/pk_units_energy_consumption"
+        android:title="@string/units_energy_consumption_title"
+        android:dialogTitle="@string/units_energy_consumption_title"
+        settings:controller="com.android.car.settings.units.UnitsEnergyConsumptionPreferenceController"/>
+
+    <ListPreference
         android:key="@string/pk_units_temperature"
         android:title="@string/units_temperature_title"
         android:dialogTitle="@string/units_temperature_title"
         settings:controller="com.android.car.settings.units.UnitsTemperaturePreferenceController"/>
+
+    <ListPreference
+        android:key="@string/pk_units_volume"
+        android:title="@string/units_volume_title"
+        android:dialogTitle="@string/units_volume_title"
+        settings:controller="com.android.car.settings.units.UnitsVolumePreferenceController"/>
+
+    <ListPreference
+        android:key="@string/pk_units_pressure"
+        android:title="@string/units_pressure_title"
+        android:dialogTitle="@string/units_pressure_title"
+        settings:controller="com.android.car.settings.units.UnitsPressurePreferenceController"/>
+
 </PreferenceScreen>
diff --git a/src/com/android/car/settings/units/CarUnitsManager.java b/src/com/android/car/settings/units/CarUnitsManager.java
index e8d8b69..112faf3 100644
--- a/src/com/android/car/settings/units/CarUnitsManager.java
+++ b/src/com/android/car/settings/units/CarUnitsManager.java
@@ -109,7 +109,7 @@
         List<Integer> availableUnitsId = new ArrayList<Integer>();
         List<Unit> units = new ArrayList<Unit>();
 
-        if (configs.get(0) == null) {
+        if (configs == null || configs.size() < 1 || configs.get(0) == null) {
             return null;
         }
 
diff --git a/src/com/android/car/settings/units/UnitsBasePreferenceController.java b/src/com/android/car/settings/units/UnitsBasePreferenceController.java
index 83567fd..8d9066e 100644
--- a/src/com/android/car/settings/units/UnitsBasePreferenceController.java
+++ b/src/com/android/car/settings/units/UnitsBasePreferenceController.java
@@ -26,6 +26,7 @@
 import androidx.annotation.CallSuper;
 import androidx.preference.ListPreference;
 
+import com.android.car.settings.R;
 import com.android.car.settings.common.FragmentController;
 import com.android.car.settings.common.PreferenceController;
 import com.android.internal.annotations.VisibleForTesting;
@@ -151,8 +152,9 @@
     }
 
     protected String generateEntryStringFromUnit(Unit unit) {
-        return getContext().getString(unit.getAbbreviationResId()) + " - "
-                + getContext().getString(unit.getNameResId());
+        return getContext().getString(R.string.units_list_entry,
+                getContext().getString(unit.getAbbreviationResId()),
+                getContext().getString(unit.getNameResId()));
     }
 
     protected String[] getIdsOfSupportedUnits() {
diff --git a/src/com/android/car/settings/units/UnitsDistancePreferenceController.java b/src/com/android/car/settings/units/UnitsDistancePreferenceController.java
new file mode 100644
index 0000000..cc69797
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsDistancePreferenceController.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Distance Display. */
+public class UnitsDistancePreferenceController extends UnitsBasePreferenceController {
+
+    public UnitsDistancePreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.DISTANCE_DISPLAY_UNITS;
+    }
+
+}
diff --git a/src/com/android/car/settings/units/UnitsEnergyConsumptionPreferenceController.java b/src/com/android/car/settings/units/UnitsEnergyConsumptionPreferenceController.java
new file mode 100644
index 0000000..00b763b
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsEnergyConsumptionPreferenceController.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.VehicleUnit;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.R;
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Energy Consumption Display. */
+public class UnitsEnergyConsumptionPreferenceController extends UnitsBasePreferenceController {
+
+    private final String mKWh;
+
+    public UnitsEnergyConsumptionPreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+        mKWh = getContext().getString(UnitsMap.KILOWATT_HOUR.getAbbreviationResId());
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.EV_BATTERY_DISPLAY_UNITS;
+    }
+
+    @Override
+    protected String generateSummaryFromUnit(Unit unit) {
+        return getAbbreviationByUnit(unit);
+    }
+
+    @Override
+    protected String generateEntryStringFromUnit(Unit unit) {
+        int unitNameResId;
+        switch (unit.getId()) {
+            case VehicleUnit.MILE:
+                unitNameResId = R.string.units_unit_name_kilowatt_per_hundred_miles;
+                break;
+            default:
+                unitNameResId = R.string.units_unit_name_kilowatt_per_hundred_kilometers;
+        }
+
+        return getContext().getString(R.string.units_list_entry, getAbbreviationByUnit(unit),
+                getContext().getString(unitNameResId));
+
+    }
+
+    private String getAbbreviationByUnit(Unit unit) {
+        int quantity = 100;
+        String denominator = getContext().getString(R.string.units_ratio_denominator, quantity,
+                getContext().getString(unit.getAbbreviationResId()));
+
+        return getContext().getString(R.string.units_ratio, mKWh, denominator);
+    }
+}
diff --git a/src/com/android/car/settings/units/UnitsFuelConsumptionPreferenceController.java b/src/com/android/car/settings/units/UnitsFuelConsumptionPreferenceController.java
new file mode 100644
index 0000000..95e7f70
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsFuelConsumptionPreferenceController.java
@@ -0,0 +1,101 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.VehicleUnit;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.R;
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Fuel Consumption Display. */
+public class UnitsFuelConsumptionPreferenceController extends UnitsBasePreferenceController {
+
+    public UnitsFuelConsumptionPreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.FUEL_VOLUME_DISPLAY_UNITS;
+    }
+
+    @Override
+    protected String generateSummaryFromUnit(Unit unit) {
+        return getAbbreviationByVolumeUnit(unit);
+    }
+
+    @Override
+    protected String generateEntryStringFromUnit(Unit unit) {
+        return getContext().getString(R.string.units_list_entry, getAbbreviationByVolumeUnit(unit),
+                getPronouncedNameByVolumeUnit(unit));
+    }
+
+    private String getAbbreviationByVolumeUnit(Unit unit) {
+        String abbreviation;
+        switch (unit.getId()) {
+            case VehicleUnit.US_GALLON:
+                abbreviation = getContext().getString(
+                        R.string.units_unit_abbreviation_miles_per_gallon_us);
+                break;
+            case VehicleUnit.IMPERIAL_GALLON:
+                abbreviation = getContext().getString(
+                        R.string.units_unit_abbreviation_miles_per_gallon_uk);
+                break;
+            default:
+                if (getCarUnitsManager().isDistanceOverVolume()) {
+                    abbreviation = getContext().getString(
+                            R.string.units_unit_abbreviation_kilometers_per_liter);
+                } else {
+                    abbreviation = getContext().getString(
+                            R.string.units_unit_abbreviation_liters_per_hundred_kilometers);
+                }
+        }
+        return abbreviation;
+    }
+
+    private String getPronouncedNameByVolumeUnit(Unit unit) {
+        String pronounced;
+        switch (unit.getId()) {
+            case VehicleUnit.US_GALLON:
+                pronounced = getContext().getString(R.string.units_unit_name_miles_per_gallon_us);
+                break;
+            case VehicleUnit.IMPERIAL_GALLON:
+                pronounced = getContext().getString(R.string.units_unit_name_miles_per_gallon_uk);
+                break;
+            default:
+                if (getCarUnitsManager().isDistanceOverVolume()) {
+                    pronounced = getContext().getString(
+                            R.string.units_unit_name_kilometers_per_liter);
+                } else {
+                    pronounced = getContext().getString(
+                            R.string.units_unit_name_liter_per_hundred_kilometers);
+                }
+        }
+        return pronounced;
+    }
+}
diff --git a/src/com/android/car/settings/units/UnitsPressurePreferenceController.java b/src/com/android/car/settings/units/UnitsPressurePreferenceController.java
new file mode 100644
index 0000000..51da7d6
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsPressurePreferenceController.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Pressure Display. */
+public class UnitsPressurePreferenceController extends UnitsBasePreferenceController {
+
+    public UnitsPressurePreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.TIRE_PRESSURE_DISPLAY_UNITS;
+    }
+}
diff --git a/src/com/android/car/settings/units/UnitsSpeedPreferenceController.java b/src/com/android/car/settings/units/UnitsSpeedPreferenceController.java
new file mode 100644
index 0000000..120a3d0
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsSpeedPreferenceController.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Speed Display. */
+public class UnitsSpeedPreferenceController extends UnitsBasePreferenceController {
+
+    public UnitsSpeedPreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.VEHICLE_SPEED_DISPLAY_UNITS;
+    }
+}
diff --git a/src/com/android/car/settings/units/UnitsTemperaturePreferenceController.java b/src/com/android/car/settings/units/UnitsTemperaturePreferenceController.java
index fa1968a..72dbdff 100644
--- a/src/com/android/car/settings/units/UnitsTemperaturePreferenceController.java
+++ b/src/com/android/car/settings/units/UnitsTemperaturePreferenceController.java
@@ -28,8 +28,7 @@
 public class UnitsTemperaturePreferenceController extends UnitsBasePreferenceController {
 
     public UnitsTemperaturePreferenceController(Context context, String preferenceKey,
-            FragmentController fragmentController,
-            CarUxRestrictions uxRestrictions) {
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
         super(context, preferenceKey, fragmentController, uxRestrictions);
     }
 
diff --git a/src/com/android/car/settings/units/UnitsVolumePreferenceController.java b/src/com/android/car/settings/units/UnitsVolumePreferenceController.java
new file mode 100644
index 0000000..5c7ab2b
--- /dev/null
+++ b/src/com/android/car/settings/units/UnitsVolumePreferenceController.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.car.settings.units;
+
+import android.car.VehiclePropertyIds;
+import android.car.drivingstate.CarUxRestrictions;
+import android.content.Context;
+
+import androidx.preference.ListPreference;
+
+import com.android.car.settings.common.FragmentController;
+
+/** Controls {@link Unit} used for Volume Display. */
+public class UnitsVolumePreferenceController extends UnitsBasePreferenceController {
+
+    public UnitsVolumePreferenceController(Context context, String preferenceKey,
+            FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
+        super(context, preferenceKey, fragmentController, uxRestrictions);
+    }
+
+    @Override
+    protected Class<ListPreference> getPreferenceType() {
+        return ListPreference.class;
+    }
+
+    @Override
+    protected int getPropertyId() {
+        return VehiclePropertyIds.FUEL_VOLUME_DISPLAY_UNITS;
+    }
+
+}