• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Energy Consumption Display. */
30 public class UnitsEnergyConsumptionPreferenceController extends UnitsBasePreferenceController {
31 
32     private final String mKWh;
33 
UnitsEnergyConsumptionPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)34     public UnitsEnergyConsumptionPreferenceController(Context context, String preferenceKey,
35             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
36         super(context, preferenceKey, fragmentController, uxRestrictions);
37         mKWh = getContext().getString(UnitsMap.KILOWATT_HOUR.getAbbreviationResId());
38     }
39 
40     @Override
getPreferenceType()41     protected Class<ListPreference> getPreferenceType() {
42         return ListPreference.class;
43     }
44 
45     @Override
getPropertyId()46     protected int getPropertyId() {
47         return VehiclePropertyIds.EV_BATTERY_DISPLAY_UNITS;
48     }
49 
50     @Override
generateSummaryFromUnit(Unit unit)51     protected String generateSummaryFromUnit(Unit unit) {
52         return getAbbreviationByUnit(unit);
53     }
54 
55     @Override
generateEntryStringFromUnit(Unit unit)56     protected String generateEntryStringFromUnit(Unit unit) {
57         int unitNameResId;
58         switch (unit.getId()) {
59             case VehicleUnit.MILE:
60                 unitNameResId = R.string.units_unit_name_kilowatt_per_hundred_miles;
61                 break;
62             default:
63                 unitNameResId = R.string.units_unit_name_kilowatt_per_hundred_kilometers;
64         }
65 
66         return getContext().getString(R.string.units_list_entry, getAbbreviationByUnit(unit),
67                 getContext().getString(unitNameResId));
68 
69     }
70 
getAbbreviationByUnit(Unit unit)71     private String getAbbreviationByUnit(Unit unit) {
72         int quantity = 100;
73         String denominator = getContext().getString(R.string.units_ratio_denominator, quantity,
74                 getContext().getString(unit.getAbbreviationResId()));
75 
76         return getContext().getString(R.string.units_ratio, mKWh, denominator);
77     }
78 }
79