• 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.Car;
20 import android.car.VehiclePropertyIds;
21 import android.car.VehicleUnit;
22 import android.car.hardware.property.CarPropertyManager;
23 import android.content.Context;
24 import android.provider.SearchIndexableResource;
25 
26 import androidx.annotation.LayoutRes;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.CarSettingActivities;
30 import com.android.car.settings.common.SettingsFragment;
31 import com.android.car.settings.search.CarBaseSearchIndexProvider;
32 import com.android.settingslib.search.SearchIndexable;
33 import com.android.settingslib.search.SearchIndexableRaw;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 /** Fragment to host Units-related preferences. */
39 @SearchIndexable
40 public class UnitsSettingsFragment extends SettingsFragment {
41 
42     @Override
43     @LayoutRes
getPreferenceScreenResId()44     protected int getPreferenceScreenResId() {
45         return R.xml.units_fragment;
46     }
47 
48     public static final CarBaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
49             new CarBaseSearchIndexProvider(R.xml.units_fragment,
50                     CarSettingActivities.UnitsSettingsActivity.class) {
51 
52                 private CarPropertyManager mCarPropertyManager;
53 
54                 @Override
55                 public List<SearchIndexableResource> getXmlResourcesToIndex(Context context,
56                         boolean enabled) {
57                     return null;
58                 }
59 
60                 @Override
61                 public List<SearchIndexableRaw> getRawDataToIndex(Context context,
62                         boolean enabled) {
63                     List<SearchIndexableRaw> rawData = new ArrayList<>();
64                     Car car = Car.createCar(context);
65                     mCarPropertyManager = (CarPropertyManager) car.getCarManager(
66                             Car.PROPERTY_SERVICE);
67                     if (mCarPropertyManager != null) {
68                         boolean hasUnits = false;
69                         if (isValidVehicleProperty(
70                                 VehiclePropertyIds.VEHICLE_SPEED_DISPLAY_UNITS)) {
71                             hasUnits = true;
72                             rawData.add(createRawDataEntry(context,
73                                     context.getString(R.string.pk_units_speed),
74                                     context.getString(R.string.units_speed_title),
75                                     context.getString(R.string.units_settings)));
76                         }
77                         if (isValidVehicleProperty(VehiclePropertyIds.DISTANCE_DISPLAY_UNITS)) {
78                             hasUnits = true;
79                             rawData.add(createRawDataEntry(context,
80                                     context.getString(R.string.pk_units_distance),
81                                     context.getString(R.string.units_distance_title),
82                                     context.getString(R.string.units_settings)));
83                         }
84                         if (isValidVehicleProperty(VehiclePropertyIds.FUEL_VOLUME_DISPLAY_UNITS)) {
85                             hasUnits = true;
86                             rawData.add(createRawDataEntry(context,
87                                     context.getString(R.string.pk_units_fuel_consumption),
88                                     context.getString(R.string.units_fuel_consumption_title),
89                                     context.getString(R.string.units_settings)));
90                             rawData.add(createRawDataEntry(context,
91                                     context.getString(R.string.pk_units_volume),
92                                     context.getString(R.string.units_volume_title),
93                                     context.getString(R.string.units_settings)));
94                         }
95                         if (isValidVehicleProperty(VehiclePropertyIds.EV_BATTERY_DISPLAY_UNITS)) {
96                             hasUnits = true;
97                             rawData.add(createRawDataEntry(context,
98                                     context.getString(R.string.pk_units_energy_consumption),
99                                     context.getString(R.string.units_energy_consumption_title),
100                                     context.getString(R.string.units_settings)));
101                         }
102                         if (isValidVehicleProperty(
103                                 VehiclePropertyIds.HVAC_TEMPERATURE_DISPLAY_UNITS)) {
104                             hasUnits = true;
105                             rawData.add(createRawDataEntry(context,
106                                     context.getString(R.string.pk_units_temperature),
107                                     context.getString(R.string.units_temperature_title),
108                                     context.getString(R.string.units_settings)));
109                         }
110                         if (isValidVehicleProperty(
111                                 VehiclePropertyIds.TIRE_PRESSURE_DISPLAY_UNITS)) {
112                             hasUnits = true;
113                             rawData.add(createRawDataEntry(context,
114                                     context.getString(R.string.pk_units_pressure),
115                                     context.getString(R.string.units_pressure_title),
116                                     context.getString(R.string.units_settings)));
117                         }
118                         if (hasUnits) {
119                             rawData.add(createRawDataEntry(context,
120                                     context.getString(R.string.psk_units),
121                                     context.getString(R.string.units_settings),
122                                     context.getString(R.string.units_settings)));
123                         }
124                     }
125                     car.disconnect();
126                     return rawData;
127                 }
128 
129                 private boolean isValidVehicleProperty(int propertyId) {
130                     return mCarPropertyManager.getIntProperty(propertyId, /* area= */ 0)
131                             != VehicleUnit.SHOULD_NOT_USE;
132                 }
133             };
134 }
135