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