• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.connecteddevice.usb;
18 
19 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
20 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
21 import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
22 
23 import android.content.Context;
24 
25 import androidx.preference.PreferenceCategory;
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settings.Utils;
30 import com.android.settingslib.widget.SelectorWithWidgetPreference;
31 
32 /**
33  * This class controls the radio buttons for switching between
34  * USB device and host mode.
35  */
36 public class UsbDetailsDataRoleController extends UsbDetailsController
37         implements SelectorWithWidgetPreference.OnClickListener {
38 
39     private PreferenceCategory mPreferenceCategory;
40     private SelectorWithWidgetPreference mDevicePref;
41     private SelectorWithWidgetPreference mHostPref;
42 
43     private SelectorWithWidgetPreference mNextRolePref;
44 
45     private final Runnable mFailureCallback = () -> {
46         if (mNextRolePref != null) {
47             mNextRolePref.setSummary(R.string.usb_switching_failed);
48             mNextRolePref = null;
49         }
50     };
51 
UsbDetailsDataRoleController(Context context, UsbDetailsFragment fragment, UsbBackend backend)52     public UsbDetailsDataRoleController(Context context, UsbDetailsFragment fragment,
53             UsbBackend backend) {
54         super(context, fragment, backend);
55     }
56 
57     @Override
displayPreference(PreferenceScreen screen)58     public void displayPreference(PreferenceScreen screen) {
59         super.displayPreference(screen);
60         mPreferenceCategory = screen.findPreference(getPreferenceKey());
61         mHostPref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_HOST),
62                 R.string.usb_control_host);
63         mDevicePref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_DEVICE),
64                 R.string.usb_control_device);
65     }
66 
67     @Override
refresh(boolean connected, long functions, int powerRole, int dataRole)68     protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
69         if (dataRole == DATA_ROLE_DEVICE) {
70             mDevicePref.setChecked(true);
71             mHostPref.setChecked(false);
72             mPreferenceCategory.setEnabled(true);
73         } else if (dataRole == DATA_ROLE_HOST) {
74             mDevicePref.setChecked(false);
75             mHostPref.setChecked(true);
76             mPreferenceCategory.setEnabled(true);
77         } else if (!connected || dataRole == DATA_ROLE_NONE) {
78             mPreferenceCategory.setEnabled(false);
79             if (mNextRolePref == null) {
80                 // Disconnected with no operation pending, so clear subtexts
81                 mHostPref.setSummary("");
82                 mDevicePref.setSummary("");
83             }
84         }
85 
86         if (mNextRolePref != null && dataRole != DATA_ROLE_NONE) {
87             if (UsbBackend.dataRoleFromString(mNextRolePref.getKey()) == dataRole) {
88                 // Clear switching text if switch succeeded
89                 mNextRolePref.setSummary("");
90             } else {
91                 // Set failure text if switch failed
92                 mNextRolePref.setSummary(R.string.usb_switching_failed);
93             }
94             mNextRolePref = null;
95             mHandler.removeCallbacks(mFailureCallback);
96         }
97     }
98 
99     @Override
onRadioButtonClicked(SelectorWithWidgetPreference preference)100     public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
101         int role = UsbBackend.dataRoleFromString(preference.getKey());
102         if (role != mUsbBackend.getDataRole() && mNextRolePref == null
103                 && !Utils.isMonkeyRunning()) {
104             mUsbBackend.setDataRole(role);
105             mNextRolePref = preference;
106             preference.setSummary(R.string.usb_switching);
107 
108             mHandler.postDelayed(mFailureCallback,
109                     mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
110                             : UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
111         }
112     }
113 
114     @Override
isAvailable()115     public boolean isAvailable() {
116         return !Utils.isMonkeyRunning();
117     }
118 
119     @Override
getPreferenceKey()120     public String getPreferenceKey() {
121         return "usb_details_data_role";
122     }
123 
makeRadioPreference(String key, int titleId)124     private SelectorWithWidgetPreference makeRadioPreference(String key, int titleId) {
125         SelectorWithWidgetPreference pref = new SelectorWithWidgetPreference(
126                 mPreferenceCategory.getContext());
127         pref.setKey(key);
128         pref.setTitle(titleId);
129         pref.setOnClickListener(this);
130         mPreferenceCategory.addPreference(pref);
131         return pref;
132     }
133 }
134