1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.settings.units;
18 
19 import android.car.VehiclePropertyIds;
20 import android.car.VehicleUnit;
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 
24 import androidx.preference.ListPreference;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.FragmentController;
28 
29 /** Controls {@link Unit} used for Fuel Consumption Display. */
30 public class UnitsFuelConsumptionPreferenceController extends UnitsBasePreferenceController {
31 
UnitsFuelConsumptionPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)32     public UnitsFuelConsumptionPreferenceController(Context context, String preferenceKey,
33             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
34         super(context, preferenceKey, fragmentController, uxRestrictions);
35     }
36 
37     @Override
getPreferenceType()38     protected Class<ListPreference> getPreferenceType() {
39         return ListPreference.class;
40     }
41 
42     @Override
getPropertyId()43     protected int getPropertyId() {
44         return VehiclePropertyIds.FUEL_VOLUME_DISPLAY_UNITS;
45     }
46 
47     @Override
generateSummaryFromUnit(Unit unit)48     protected String generateSummaryFromUnit(Unit unit) {
49         return getAbbreviationByVolumeUnit(unit);
50     }
51 
52     @Override
generateEntryStringFromUnit(Unit unit)53     protected String generateEntryStringFromUnit(Unit unit) {
54         return getContext().getString(R.string.units_list_entry, getAbbreviationByVolumeUnit(unit),
55                 getPronouncedNameByVolumeUnit(unit));
56     }
57 
getAbbreviationByVolumeUnit(Unit unit)58     private String getAbbreviationByVolumeUnit(Unit unit) {
59         String abbreviation;
60         switch (unit.getId()) {
61             case VehicleUnit.US_GALLON:
62                 abbreviation = getContext().getString(
63                         R.string.units_unit_abbreviation_miles_per_gallon_us);
64                 break;
65             case VehicleUnit.IMPERIAL_GALLON:
66                 abbreviation = getContext().getString(
67                         R.string.units_unit_abbreviation_miles_per_gallon_uk);
68                 break;
69             default:
70                 if (getCarUnitsManager().isDistanceOverVolume()) {
71                     abbreviation = getContext().getString(
72                             R.string.units_unit_abbreviation_kilometers_per_liter);
73                 } else {
74                     abbreviation = getContext().getString(
75                             R.string.units_unit_abbreviation_liters_per_hundred_kilometers);
76                 }
77         }
78         return abbreviation;
79     }
80 
getPronouncedNameByVolumeUnit(Unit unit)81     private String getPronouncedNameByVolumeUnit(Unit unit) {
82         String pronounced;
83         switch (unit.getId()) {
84             case VehicleUnit.US_GALLON:
85                 pronounced = getContext().getString(R.string.units_unit_name_miles_per_gallon_us);
86                 break;
87             case VehicleUnit.IMPERIAL_GALLON:
88                 pronounced = getContext().getString(R.string.units_unit_name_miles_per_gallon_uk);
89                 break;
90             default:
91                 if (getCarUnitsManager().isDistanceOverVolume()) {
92                     pronounced = getContext().getString(
93                             R.string.units_unit_name_kilometers_per_liter);
94                 } else {
95                     pronounced = getContext().getString(
96                             R.string.units_unit_name_liter_per_hundred_kilometers);
97                 }
98         }
99         return pronounced;
100     }
101 }
102