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.ui.paintbooth.preferences; 18 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.os.Bundle; 21 import android.widget.Toast; 22 23 import androidx.annotation.NonNull; 24 import androidx.preference.Preference; 25 import androidx.preference.PreferenceGroup; 26 27 import com.android.car.ui.paintbooth.R; 28 import com.android.car.ui.preference.CarUiTwoActionBasePreference; 29 import com.android.car.ui.preference.CarUiTwoActionIconPreference; 30 import com.android.car.ui.preference.CarUiTwoActionSwitchPreference; 31 import com.android.car.ui.preference.CarUiTwoActionTextPreference; 32 import com.android.car.ui.preference.PreferenceFragment; 33 import com.android.car.ui.preference.UxRestrictablePreference; 34 import com.android.car.ui.utils.CarUxRestrictionsUtil; 35 36 import java.util.Objects; 37 38 /** 39 * Fragment to load preferences 40 */ 41 public class PreferenceDemoFragment extends PreferenceFragment { 42 43 CarUxRestrictionsUtil.OnUxRestrictionsChangedListener mOnUxRestrictionsChangedListener = 44 restrictions -> { 45 boolean isRestricted = 46 (restrictions.getActiveRestrictions() 47 & CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP) 48 == CarUxRestrictions.UX_RESTRICTIONS_NO_SETUP; 49 50 restrictPreference(getPreferenceScreen(), isRestricted); 51 }; 52 restrictPreference(Preference preference, boolean restrict)53 private void restrictPreference(Preference preference, boolean restrict) { 54 if (preference == null) { 55 return; 56 } 57 58 if (preference instanceof UxRestrictablePreference) { 59 ((UxRestrictablePreference) preference).setUxRestricted(restrict); 60 ((UxRestrictablePreference) preference).setOnClickWhileRestrictedListener(p -> 61 Toast.makeText(getContext(), R.string.car_ui_restricted_while_driving, 62 Toast.LENGTH_LONG).show()); 63 } 64 65 if (preference instanceof PreferenceGroup) { 66 PreferenceGroup preferenceGroup = (PreferenceGroup) preference; 67 for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) { 68 restrictPreference(preferenceGroup.getPreference(i), restrict); 69 } 70 } 71 } 72 73 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)74 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 75 // Load the preferences from an XML resource 76 setPreferencesFromResource(R.xml.preference_samples, rootKey); 77 78 setupTwoActionPreferenceClickListeners(requirePreference("twoactiontext")); 79 setupTwoActionPreferenceClickListeners(requirePreference("twoactiontextborderless")); 80 setupTwoActionPreferenceClickListeners(requirePreference("twoactionicon")); 81 setupTwoActionPreferenceClickListeners(requirePreference("twoactionswitch")); 82 } 83 84 @Override onStart()85 public void onStart() { 86 super.onStart(); 87 CarUxRestrictionsUtil.getInstance(requireContext()) 88 .register(mOnUxRestrictionsChangedListener); 89 } 90 91 @Override onStop()92 public void onStop() { 93 CarUxRestrictionsUtil.getInstance(requireContext()) 94 .unregister(mOnUxRestrictionsChangedListener); 95 super.onStop(); 96 } 97 setupTwoActionPreferenceClickListeners(CarUiTwoActionBasePreference preference)98 private void setupTwoActionPreferenceClickListeners(CarUiTwoActionBasePreference preference) { 99 if (preference instanceof CarUiTwoActionSwitchPreference) { 100 ((CarUiTwoActionSwitchPreference) preference).setOnSecondaryActionClickListener( 101 (selected) -> preference.setSecondaryActionEnabled(false)); 102 } else if (preference instanceof CarUiTwoActionTextPreference) { 103 ((CarUiTwoActionTextPreference) preference).setOnSecondaryActionClickListener( 104 () -> preference.setSecondaryActionEnabled(false)); 105 } else { 106 ((CarUiTwoActionIconPreference) preference).setOnSecondaryActionClickListener( 107 () -> preference.setSecondaryActionEnabled(false)); 108 } 109 110 preference.setOnPreferenceClickListener((pref) -> { 111 if (!preference.isSecondaryActionEnabled()) { 112 preference.setSecondaryActionEnabled(true); 113 } else { 114 preference.setSecondaryActionVisible( 115 !preference.isSecondaryActionVisible()); 116 } 117 return true; 118 }); 119 } 120 121 @NonNull requirePreference(CharSequence key)122 private <T extends Preference> T requirePreference(CharSequence key) { 123 T pref = findPreference(key); 124 return Objects.requireNonNull(pref); 125 } 126 } 127