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