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