blob: 27a08a81f2eabc839b9238c334ff73ad18f7a072 [file] [log] [blame]
jeffreyhuang7e81c842017-09-28 17:59:55 -07001/*
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
17package com.android.settings.development;
18
Alex Johnston7d4db752021-01-15 20:46:05 +000019import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled;
20
jeffreyhuang7e81c842017-09-28 17:59:55 -070021import android.content.Context;
Alex Johnston7d4db752021-01-15 20:46:05 +000022import android.os.UserHandle;
jeffreyhuang7e81c842017-09-28 17:59:55 -070023import android.provider.Settings;
jeffreyhuang7e81c842017-09-28 17:59:55 -070024
Fan Zhangc7162cd2018-06-18 15:21:41 -070025import androidx.annotation.VisibleForTesting;
26import androidx.preference.Preference;
Alex Johnston7d4db752021-01-15 20:46:05 +000027import androidx.preference.PreferenceScreen;
Chaohui Wang15ca95a2023-10-23 12:38:18 +080028import androidx.preference.TwoStatePreference;
Fan Zhangc7162cd2018-06-18 15:21:41 -070029
Fan Zhang23f8d592018-08-28 15:11:40 -070030import com.android.settings.core.PreferenceControllerMixin;
Alex Johnston7d4db752021-01-15 20:46:05 +000031import com.android.settingslib.RestrictedSwitchPreference;
Fan Zhang23f8d592018-08-28 15:11:40 -070032import com.android.settingslib.development.DeveloperOptionsPreferenceController;
33
jeffreyhuang7e81c842017-09-28 17:59:55 -070034public class UsbAudioRoutingPreferenceController extends DeveloperOptionsPreferenceController
jeffreyhuang37df3d62017-10-06 11:37:56 -070035 implements Preference.OnPreferenceChangeListener, PreferenceControllerMixin {
jeffreyhuang7e81c842017-09-28 17:59:55 -070036
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 Johnston7d4db752021-01-15 20:46:05 +000044 private RestrictedSwitchPreference mPreference;
45
jeffreyhuang7e81c842017-09-28 17:59:55 -070046 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 Johnston7d4db752021-01-15 20:46:05 +000056 public void displayPreference(PreferenceScreen screen) {
57 super.displayPreference(screen);
58 mPreference = screen.findPreference(getPreferenceKey());
59 }
60
61 @Override
jeffreyhuang7e81c842017-09-28 17:59:55 -070062 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 Johnston7d4db752021-01-15 20:46:05 +000074 mPreference.setChecked(usbAudioRoutingMode != SETTING_VALUE_OFF);
75 mPreference.setDisabledByAdmin(
76 checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId()));
jeffreyhuang7e81c842017-09-28 17:59:55 -070077 }
78
79 @Override
80 protected void onDeveloperOptionsSwitchDisabled() {
Doris Ling4fbf04c2018-03-01 10:33:14 -080081 super.onDeveloperOptionsSwitchDisabled();
jeffreyhuang7e81c842017-09-28 17:59:55 -070082 Settings.Secure.putInt(mContext.getContentResolver(),
83 Settings.Secure.USB_AUDIO_AUTOMATIC_ROUTING_DISABLED, SETTING_VALUE_OFF);
Chaohui Wang15ca95a2023-10-23 12:38:18 +080084 ((TwoStatePreference) mPreference).setChecked(false);
jeffreyhuang7e81c842017-09-28 17:59:55 -070085 }
Alex Johnston46c72f32021-03-17 15:18:00 +000086
87 @Override
88 protected void onDeveloperOptionsSwitchEnabled() {
89 super.onDeveloperOptionsSwitchEnabled();
90 mPreference.setDisabledByAdmin(
91 checkIfUsbDataSignalingIsDisabled(mContext, UserHandle.myUserId()));
92 }
jeffreyhuang7e81c842017-09-28 17:59:55 -070093}