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 19 import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.provider.Settings; 24 25 import androidx.annotation.VisibleForTesting; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceScreen; 28 import androidx.preference.TwoStatePreference; 29 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settingslib.RestrictedSwitchPreference; 32 import com.android.settingslib.development.DeveloperOptionsPreferenceController; 33 34 public class UsbAudioRoutingPreferenceController extends DeveloperOptionsPreferenceController 35 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin { 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 44 private RestrictedSwitchPreference mPreference; 45 UsbAudioRoutingPreferenceController(Context context)46 public UsbAudioRoutingPreferenceController(Context context) { 47 super(context); 48 } 49 50 @Override getPreferenceKey()51 public String getPreferenceKey() { 52 return USB_AUDIO_KEY; 53 } 54 55 @Override displayPreference(PreferenceScreen screen)56 public void displayPreference(PreferenceScreen screen) { 57 super.displayPreference(screen); 58 mPreference = screen.findPreference(getPreferenceKey()); 59 } 60 61 @Override onPreferenceChange(Preference preference, Object newValue)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 updateState(Preference preference)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); 74 mPreference.setChecked(usbAudioRoutingMode != SETTING_VALUE_OFF); 75 mPreference.setDisabledByAdmin( 76 checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId())); 77 } 78 79 @Override onDeveloperOptionsSwitchDisabled()80 protected void onDeveloperOptionsSwitchDisabled() { 81 super.onDeveloperOptionsSwitchDisabled(); 82 Settings.Secure.putInt(mContext.getContentResolver(), 83 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, SETTING_VALUE_OFF); 84 ((TwoStatePreference) mPreference).setChecked(false); 85 } 86 87 @Override onDeveloperOptionsSwitchEnabled()88 protected void onDeveloperOptionsSwitchEnabled() { 89 super.onDeveloperOptionsSwitchEnabled(); 90 mPreference.setDisabledByAdmin( 91 checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId())); 92 } 93 } 94