• 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 
18 package com.android.car.settings.units;
19 
20 import android.car.CarNotConnectedException;
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.car.hardware.CarPropertyValue;
23 import android.car.hardware.property.CarPropertyManager;
24 import android.content.Context;
25 
26 import androidx.annotation.CallSuper;
27 import androidx.preference.ListPreference;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.FragmentController;
31 import com.android.car.settings.common.PreferenceController;
32 import com.android.internal.annotations.VisibleForTesting;
33 
34 /**
35  * Shared business logic for preference controllers related to Units.
36  */
37 public abstract class UnitsBasePreferenceController extends PreferenceController<ListPreference> {
38 
39     @VisibleForTesting
40     protected final CarUnitsManager.OnCarServiceListener mOnCarServiceListener =
41             new CarUnitsManager.OnCarServiceListener() {
42                 @Override
43                 public void handleServiceConnected(CarPropertyManager carPropertyManager) {
44                     try {
45                         if (carPropertyManager != null) {
46                             carPropertyManager.registerCallback(mCarPropertyEventCallback,
47                                     getPropertyId(), CarPropertyManager.SENSOR_RATE_ONCHANGE);
48                         }
49                         mSupportedUnits = mCarUnitsManager.getUnitsSupportedByProperty(
50                                 getPropertyId());
51                         if (mSupportedUnits != null && mSupportedUnits.length > 0) {
52                             // first element in the config array is the default Unit per VHAL spec.
53                             mDefaultUnit = mSupportedUnits[0];
54                             getPreference().setEntries(getEntriesOfSupportedUnits());
55                             getPreference().setEntryValues(getIdsOfSupportedUnits());
56                             getPreference().setValue(
57                                     Integer.toString(getUnitUsedByThisProperty().getId()));
58                             refreshUi();
59                         }
60 
61                         mIsCarUnitsManagerStarted = true;
62                     } catch (CarNotConnectedException e) {
63                     }
64                 }
65 
66                 @Override
67                 public void handleServiceDisconnected() {
68                     mIsCarUnitsManagerStarted = false;
69                 }
70             };
71 
72     @VisibleForTesting
73     protected final CarPropertyManager.CarPropertyEventCallback mCarPropertyEventCallback =
74             new CarPropertyManager.CarPropertyEventCallback() {
75                 @Override
76                 public void onChangeEvent(CarPropertyValue value) {
77                     if (value != null && value.getStatus() == CarPropertyValue.STATUS_AVAILABLE) {
78                         mUnitBeingUsed = UnitsMap.MAP.get(value.getValue());
79                         refreshUi();
80                     }
81                 }
82 
83                 @Override
84                 public void onErrorEvent(int propId, int zone) {
85                 }
86             };
87 
88     private Unit[] mSupportedUnits;
89     private Unit mUnitBeingUsed;
90     private Unit mDefaultUnit;
91     private boolean mIsCarUnitsManagerStarted = false;
92     private CarUnitsManager mCarUnitsManager;
93 
UnitsBasePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)94     public UnitsBasePreferenceController(Context context, String preferenceKey,
95             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
96         super(context, preferenceKey, fragmentController, uxRestrictions);
97     }
98 
99     @Override
100     @CallSuper
onCreateInternal()101     protected void onCreateInternal() {
102         super.onCreateInternal();
103         mCarUnitsManager = new CarUnitsManager(getContext());
104         mCarUnitsManager.connect();
105         mCarUnitsManager.registerCarServiceListener(mOnCarServiceListener);
106     }
107 
108     @Override
109     @CallSuper
onDestroyInternal()110     protected void onDestroyInternal() {
111         super.onDestroyInternal();
112         mCarUnitsManager.disconnect();
113         mCarUnitsManager.unregisterCarServiceListener();
114     }
115 
116     @Override
117     @CallSuper
updateState(ListPreference preference)118     protected void updateState(ListPreference preference) {
119         if (mIsCarUnitsManagerStarted && mUnitBeingUsed != null) {
120             preference.setSummary(generateSummaryFromUnit(mUnitBeingUsed));
121             preference.setValue(Integer.toString(mUnitBeingUsed.getId()));
122         }
123     }
124 
125     @Override
126     @CallSuper
handlePreferenceChanged(ListPreference preference, Object newValue)127     public boolean handlePreferenceChanged(ListPreference preference, Object newValue) {
128         int unitId = Integer.parseInt((String) newValue);
129         mCarUnitsManager.setUnitUsedByProperty(getPropertyId(), unitId);
130         return true;
131     }
132 
133     @Override
getAvailabilityStatus()134     protected int getAvailabilityStatus() {
135         return mSupportedUnits != null && mSupportedUnits.length > 0
136                 ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
137     }
138 
getPropertyId()139     protected abstract int getPropertyId();
140 
getEntriesOfSupportedUnits()141     protected String[] getEntriesOfSupportedUnits() {
142         String[] names = new String[mSupportedUnits.length];
143         for (int i = 0; i < names.length; i++) {
144             Unit unit = mSupportedUnits[i];
145             names[i] = generateEntryStringFromUnit(unit);
146         }
147         return names;
148     }
149 
generateSummaryFromUnit(Unit unit)150     protected String generateSummaryFromUnit(Unit unit) {
151         return getContext().getString(unit.getAbbreviationResId());
152     }
153 
generateEntryStringFromUnit(Unit unit)154     protected String generateEntryStringFromUnit(Unit unit) {
155         return getContext().getString(R.string.units_list_entry,
156                 getContext().getString(unit.getAbbreviationResId()),
157                 getContext().getString(unit.getNameResId()));
158     }
159 
getIdsOfSupportedUnits()160     protected String[] getIdsOfSupportedUnits() {
161         String[] ids = new String[mSupportedUnits.length];
162         for (int i = 0; i < ids.length; i++) {
163             ids[i] = Integer.toString(mSupportedUnits[i].getId());
164         }
165         return ids;
166     }
167 
getCarUnitsManager()168     protected CarUnitsManager getCarUnitsManager() {
169         return mCarUnitsManager;
170     }
171 
getUnitUsedByThisProperty()172     private Unit getUnitUsedByThisProperty() {
173         Unit savedUnit = mCarUnitsManager.getUnitUsedByProperty(getPropertyId());
174         if (savedUnit == null) {
175             return mDefaultUnit;
176         }
177         return savedUnit;
178     }
179 
180 }
181