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.POWER_ROLE_NONE;
20 import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
21 import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
22 
23 import android.content.Context;
24 
25 import androidx.preference.Preference;
26 import androidx.preference.Preference.OnPreferenceClickListener;
27 import androidx.preference.PreferenceCategory;
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.SwitchPreferenceCompat;
30 import androidx.preference.TwoStatePreference;
31 
32 import com.android.settings.R;
33 import com.android.settings.Utils;
34 
35 /**
36  * This class controls the switch for changing USB power direction.
37  */
38 public class UsbDetailsPowerRoleController extends UsbDetailsController
39         implements OnPreferenceClickListener {
40     private static final String KEY_USB_USE_POWER_ONLY = "usb_use_power_only";
41 
42     private PreferenceCategory mPreferenceCategory;
43     private TwoStatePreference mSwitchPreference;
44 
45     private int mNextPowerRole;
46 
47     private final Runnable mFailureCallback = () -> {
48         if (mNextPowerRole != POWER_ROLE_NONE) {
49             mSwitchPreference.setSummary(R.string.usb_switching_failed);
50             mNextPowerRole = POWER_ROLE_NONE;
51         }
52     };
53 
UsbDetailsPowerRoleController(Context context, UsbDetailsFragment fragment, UsbBackend backend)54     public UsbDetailsPowerRoleController(Context context, UsbDetailsFragment fragment,
55             UsbBackend backend) {
56         super(context, fragment, backend);
57         mNextPowerRole = POWER_ROLE_NONE;
58     }
59 
60     @Override
displayPreference(PreferenceScreen screen)61     public void displayPreference(PreferenceScreen screen) {
62         super.displayPreference(screen);
63         mPreferenceCategory = screen.findPreference(getPreferenceKey());
64         mSwitchPreference = new SwitchPreferenceCompat(mPreferenceCategory.getContext());
65         mSwitchPreference.setTitle(R.string.usb_use_power_only);
66         mSwitchPreference.setKey(KEY_USB_USE_POWER_ONLY);
67         mSwitchPreference.setOnPreferenceClickListener(this);
68         mPreferenceCategory.addPreference(mSwitchPreference);
69     }
70 
71     @Override
refresh(boolean connected, long functions, int powerRole, int dataRole)72     protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
73         // Hide this option if this is not a PD compatible connection
74         if (connected && !mUsbBackend.areAllRolesSupported()) {
75             mFragment.getPreferenceScreen().removePreference(mPreferenceCategory);
76         } else if (connected && mUsbBackend.areAllRolesSupported()) {
77             mFragment.getPreferenceScreen().addPreference(mPreferenceCategory);
78         }
79         if (powerRole == POWER_ROLE_SOURCE) {
80             mSwitchPreference.setChecked(true);
81             mPreferenceCategory.setEnabled(true);
82         } else if (powerRole == POWER_ROLE_SINK) {
83             mSwitchPreference.setChecked(false);
84             mPreferenceCategory.setEnabled(true);
85         } else if (!connected || powerRole == POWER_ROLE_NONE) {
86             mPreferenceCategory.setEnabled(false);
87             if (mNextPowerRole == POWER_ROLE_NONE) {
88                 mSwitchPreference.setSummary("");
89             }
90         }
91 
92         if (mNextPowerRole != POWER_ROLE_NONE
93                 && powerRole != POWER_ROLE_NONE) {
94             if (mNextPowerRole == powerRole) {
95                 // Clear switching text if switch succeeded
96                 mSwitchPreference.setSummary("");
97             } else {
98                 // Set failure text if switch failed
99                 mSwitchPreference.setSummary(R.string.usb_switching_failed);
100             }
101             mNextPowerRole = POWER_ROLE_NONE;
102             mHandler.removeCallbacks(mFailureCallback);
103         }
104     }
105 
106     @Override
onPreferenceClick(Preference preference)107     public boolean onPreferenceClick(Preference preference) {
108         int newRole = mSwitchPreference.isChecked() ? POWER_ROLE_SOURCE
109                 : POWER_ROLE_SINK;
110         if (mUsbBackend.getPowerRole() != newRole && mNextPowerRole == POWER_ROLE_NONE
111                 && !Utils.isMonkeyRunning()) {
112             mUsbBackend.setPowerRole(newRole);
113 
114             mNextPowerRole = newRole;
115             mSwitchPreference.setSummary(R.string.usb_switching);
116 
117             mHandler.postDelayed(mFailureCallback,
118                     mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
119                             : UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
120         }
121 
122         // We don't know that the action succeeded until called back in refresh()
123         mSwitchPreference.setChecked(!mSwitchPreference.isChecked());
124         return true;
125     }
126 
127     @Override
isAvailable()128     public boolean isAvailable() {
129         return !Utils.isMonkeyRunning();
130     }
131 
132     @Override
getPreferenceKey()133     public String getPreferenceKey() {
134         return "usb_details_power_role";
135     }
136 }
137