1 /* 2 * Copyright (C) 2021 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.datausage; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.icu.text.MeasureFormat; 22 import android.icu.util.MeasureUnit; 23 24 import androidx.annotation.NonNull; 25 import androidx.preference.PreferenceGroup; 26 import androidx.preference.TwoStatePreference; 27 28 import com.android.car.settings.R; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.GroupSelectionPreferenceController; 31 import com.android.car.ui.preference.CarUiRadioButtonPreference; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** Class that manages whether the data warning gigabyte or megabyte radio buttons are selected */ 37 public class DataUsageUnitPreferenceController extends GroupSelectionPreferenceController { 38 39 private final String mGbPreferenceKey; 40 private final String mMbPreferenceKey; 41 42 private String mSelectedKey; 43 DataUsageUnitPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)44 public DataUsageUnitPreferenceController(Context context, String preferenceKey, 45 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 46 super(context, preferenceKey, fragmentController, uxRestrictions); 47 mGbPreferenceKey = context.getString(R.string.pk_data_usage_radio_gb); 48 mMbPreferenceKey = context.getString(R.string.pk_data_usage_radio_mb); 49 } 50 51 @Override getPreferenceType()52 protected Class<PreferenceGroup> getPreferenceType() { 53 return PreferenceGroup.class; 54 } 55 56 /** Sets whether GB or MB radio button should be selected by default */ setDefaultSelection(boolean isGb)57 public void setDefaultSelection(boolean isGb) { 58 mSelectedKey = isGb ? mGbPreferenceKey : mMbPreferenceKey; 59 } 60 61 @Override checkInitialized()62 protected void checkInitialized() { 63 if (mSelectedKey == null) { 64 throw new IllegalStateException( 65 "Default selection should be set before calling this function"); 66 } 67 } 68 69 @Override getCurrentCheckedKey()70 protected String getCurrentCheckedKey() { 71 return mSelectedKey; 72 } 73 74 @NonNull 75 @Override getGroupPreferences()76 protected List<TwoStatePreference> getGroupPreferences() { 77 List<TwoStatePreference> entries = new ArrayList<>(); 78 79 MeasureFormat formatter = MeasureFormat.getInstance( 80 getContext().getResources().getConfiguration().locale, 81 MeasureFormat.FormatWidth.SHORT); 82 83 // Create GB preference 84 CarUiRadioButtonPreference gbPreference = new CarUiRadioButtonPreference(getContext()); 85 gbPreference.setKey(mGbPreferenceKey); 86 gbPreference.setTitle(formatter.getUnitDisplayName(MeasureUnit.GIGABYTE)); 87 88 // Create MB preference 89 CarUiRadioButtonPreference mbPreference = new CarUiRadioButtonPreference(getContext()); 90 mbPreference.setKey(mMbPreferenceKey); 91 mbPreference.setTitle(formatter.getUnitDisplayName(MeasureUnit.MEGABYTE)); 92 93 entries.add(gbPreference); 94 entries.add(mbPreference); 95 return entries; 96 } 97 98 @Override handleGroupItemSelected(TwoStatePreference preference)99 protected boolean handleGroupItemSelected(TwoStatePreference preference) { 100 mSelectedKey = preference.getKey().equals(mGbPreferenceKey) 101 ? mGbPreferenceKey : mMbPreferenceKey; 102 return true; 103 } 104 105 /** Returns whether the gigabyte radio button is selected */ isGbSelected()106 public boolean isGbSelected() { 107 return mSelectedKey.equals(mGbPreferenceKey); 108 } 109 } 110