Home | History | Annotate | Download | only in usb
      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 android.content.Context;
     20 import android.hardware.usb.UsbManager;
     21 import android.hardware.usb.UsbPort;
     22 import android.support.v7.preference.PreferenceCategory;
     23 import android.support.v7.preference.PreferenceScreen;
     24 
     25 import com.android.settings.R;
     26 import com.android.settings.Utils;
     27 import com.android.settings.widget.RadioButtonPreference;
     28 
     29 import java.util.LinkedHashMap;
     30 import java.util.Map;
     31 
     32 /**
     33  * This class controls the radio buttons for choosing between different USB functions.
     34  */
     35 public class UsbDetailsFunctionsController extends UsbDetailsController
     36         implements RadioButtonPreference.OnClickListener {
     37 
     38     static final Map<Long, Integer> FUNCTIONS_MAP = new LinkedHashMap<>();
     39 
     40     static {
     41         FUNCTIONS_MAP.put(UsbManager.FUNCTION_MTP, R.string.usb_use_file_transfers);
     42         FUNCTIONS_MAP.put(UsbManager.FUNCTION_RNDIS, R.string.usb_use_tethering);
     43         FUNCTIONS_MAP.put(UsbManager.FUNCTION_MIDI, R.string.usb_use_MIDI);
     44         FUNCTIONS_MAP.put(UsbManager.FUNCTION_PTP, R.string.usb_use_photo_transfers);
     45         FUNCTIONS_MAP.put(UsbManager.FUNCTION_NONE, R.string.usb_use_charging_only);
     46     }
     47 
     48     private PreferenceCategory mProfilesContainer;
     49 
     50     public UsbDetailsFunctionsController(Context context, UsbDetailsFragment fragment,
     51             UsbBackend backend) {
     52         super(context, fragment, backend);
     53     }
     54 
     55     @Override
     56     public void displayPreference(PreferenceScreen screen) {
     57         super.displayPreference(screen);
     58         mProfilesContainer = (PreferenceCategory) screen.findPreference(getPreferenceKey());
     59     }
     60 
     61     /**
     62      * Gets a switch preference for the particular option, creating it if needed.
     63      */
     64     private RadioButtonPreference getProfilePreference(String key, int titleId) {
     65         RadioButtonPreference pref = (RadioButtonPreference) mProfilesContainer.findPreference(key);
     66         if (pref == null) {
     67             pref = new RadioButtonPreference(mProfilesContainer.getContext());
     68             pref.setKey(key);
     69             pref.setTitle(titleId);
     70             pref.setOnClickListener(this);
     71             mProfilesContainer.addPreference(pref);
     72         }
     73         return pref;
     74     }
     75 
     76     @Override
     77     protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
     78         if (!connected || dataRole != UsbPort.DATA_ROLE_DEVICE) {
     79             mProfilesContainer.setEnabled(false);
     80         } else {
     81             // Functions are only available in device mode
     82             mProfilesContainer.setEnabled(true);
     83         }
     84         RadioButtonPreference pref;
     85         for (long option : FUNCTIONS_MAP.keySet()) {
     86             int title = FUNCTIONS_MAP.get(option);
     87             pref = getProfilePreference(UsbBackend.usbFunctionsToString(option), title);
     88             // Only show supported options
     89             if (mUsbBackend.areFunctionsSupported(option)) {
     90                 pref.setChecked(functions == option);
     91             } else {
     92                 mProfilesContainer.removePreference(pref);
     93             }
     94         }
     95     }
     96 
     97     @Override
     98     public void onRadioButtonClicked(RadioButtonPreference preference) {
     99         long function = UsbBackend.usbFunctionsFromString(preference.getKey());
    100         if (function != mUsbBackend.getCurrentFunctions() && !Utils.isMonkeyRunning()) {
    101             mUsbBackend.setCurrentFunctions(function);
    102         }
    103     }
    104 
    105     @Override
    106     public boolean isAvailable() {
    107         return !Utils.isMonkeyRunning();
    108     }
    109 
    110     @Override
    111     public String getPreferenceKey() {
    112         return "usb_details_functions";
    113     }
    114 }
    115