jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.settings.development; |
| 18 | |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 19 | import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled; |
| 20 | |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 22 | import android.os.UserHandle; |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 23 | import android.provider.Settings; |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 24 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 25 | import androidx.annotation.VisibleForTesting; |
| 26 | import androidx.preference.Preference; |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 27 | import androidx.preference.PreferenceScreen; |
Chaohui Wang | 15ca95a | 2023-10-23 12:38:18 +0800 | [diff] [blame] | 28 | import androidx.preference.TwoStatePreference; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 29 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 30 | import com.android.settings.core.PreferenceControllerMixin; |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 31 | import com.android.settingslib.RestrictedSwitchPreference; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 32 | import com.android.settingslib.development.DeveloperOptionsPreferenceController; |
| 33 | |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 34 | public class UsbAudioRoutingPreferenceController extends DeveloperOptionsPreferenceController |
jeffreyhuang | 37df3d6 | 2017-10-06 11:37:56 -0700 | [diff] [blame] | 35 | implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 36 | |
| 37 | private static final String USB_AUDIO_KEY = "usb_audio"; |
| 38 | |
| 39 | @VisibleForTesting |
| 40 | static final int SETTING_VALUE_ON = 1; |
| 41 | @VisibleForTesting |
| 42 | static final int SETTING_VALUE_OFF = 0; |
| 43 | |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 44 | private RestrictedSwitchPreference mPreference; |
| 45 | |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 46 | public UsbAudioRoutingPreferenceController(Context context) { |
| 47 | super(context); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public String getPreferenceKey() { |
| 52 | return USB_AUDIO_KEY; |
| 53 | } |
| 54 | |
| 55 | @Override |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 56 | public void displayPreference(PreferenceScreen screen) { |
| 57 | super.displayPreference(screen); |
| 58 | mPreference = screen.findPreference(getPreferenceKey()); |
| 59 | } |
| 60 | |
| 61 | @Override |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 62 | public boolean onPreferenceChange(Preference preference, Object newValue) { |
| 63 | final boolean isEnabled = (Boolean) newValue; |
| 64 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 65 | Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, |
| 66 | isEnabled ? SETTING_VALUE_ON : SETTING_VALUE_OFF); |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void updateState(Preference preference) { |
| 72 | final int usbAudioRoutingMode = Settings.Secure.getInt(mContext.getContentResolver(), |
| 73 | Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, SETTING_VALUE_OFF); |
Alex Johnston | 7d4db75 | 2021-01-15 20:46:05 +0000 | [diff] [blame] | 74 | mPreference.setChecked(usbAudioRoutingMode != SETTING_VALUE_OFF); |
| 75 | mPreference.setDisabledByAdmin( |
| 76 | checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId())); |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | protected void onDeveloperOptionsSwitchDisabled() { |
Doris Ling | 4fbf04c | 2018-03-01 10:33:14 -0800 | [diff] [blame] | 81 | super.onDeveloperOptionsSwitchDisabled(); |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 82 | Settings.Secure.putInt(mContext.getContentResolver(), |
| 83 | Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, SETTING_VALUE_OFF); |
Chaohui Wang | 15ca95a | 2023-10-23 12:38:18 +0800 | [diff] [blame] | 84 | ((TwoStatePreference) mPreference).setChecked(false); |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 85 | } |
Alex Johnston | 46c72f3 | 2021-03-17 15:18:00 +0000 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | protected void onDeveloperOptionsSwitchEnabled() { |
| 89 | super.onDeveloperOptionsSwitchEnabled(); |
| 90 | mPreference.setDisabledByAdmin( |
| 91 | checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId())); |
| 92 | } |
jeffreyhuang | 7e81c84 | 2017-09-28 17:59:55 -0700 | [diff] [blame] | 93 | } |