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.graphics.drawable.Drawable;
     21 import android.os.Bundle;
     22 
     23 import com.android.internal.annotations.VisibleForTesting;
     24 import com.android.internal.logging.nano.MetricsProto;
     25 import com.android.settings.R;
     26 import com.android.settings.Utils;
     27 import com.android.settings.widget.RadioButtonPickerFragment;
     28 import com.android.settingslib.widget.CandidateInfo;
     29 import com.android.settingslib.widget.FooterPreference;
     30 import com.android.settingslib.widget.FooterPreferenceMixin;
     31 
     32 import com.google.android.collect.Lists;
     33 
     34 import java.util.List;
     35 
     36 /**
     37  * Provides options for selecting the default USB mode.
     38  */
     39 public class UsbDefaultFragment extends RadioButtonPickerFragment {
     40     @VisibleForTesting
     41     UsbBackend mUsbBackend;
     42 
     43     @Override
     44     public void onAttach(Context context) {
     45         super.onAttach(context);
     46         mUsbBackend = new UsbBackend(context);
     47     }
     48 
     49     @Override
     50     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
     51         super.onCreatePreferences(savedInstanceState, rootKey);
     52         FooterPreferenceMixin footer = new FooterPreferenceMixin(this, this.getLifecycle());
     53         FooterPreference pref = footer.createFooterPreference();
     54         pref.setTitle(R.string.usb_default_info);
     55     }
     56 
     57     @Override
     58     public int getMetricsCategory() {
     59         return MetricsProto.MetricsEvent.USB_DEFAULT;
     60     }
     61 
     62     @Override
     63     protected int getPreferenceScreenResId() {
     64         return R.xml.usb_default_fragment;
     65     }
     66 
     67     @Override
     68     protected List<? extends CandidateInfo> getCandidates() {
     69         List<CandidateInfo> ret = Lists.newArrayList();
     70         for (final long option : UsbDetailsFunctionsController.FUNCTIONS_MAP.keySet()) {
     71             final String title = getContext().getString(
     72                     UsbDetailsFunctionsController.FUNCTIONS_MAP.get(option));
     73             final String key = UsbBackend.usbFunctionsToString(option);
     74 
     75             // Only show supported functions
     76             if (mUsbBackend.areFunctionsSupported(option)) {
     77                 ret.add(new CandidateInfo(true /* enabled */) {
     78                     @Override
     79                     public CharSequence loadLabel() {
     80                         return title;
     81                     }
     82 
     83                     @Override
     84                     public Drawable loadIcon() {
     85                         return null;
     86                     }
     87 
     88                     @Override
     89                     public String getKey() {
     90                         return key;
     91                     }
     92                 });
     93             }
     94         }
     95         return ret;
     96     }
     97 
     98     @Override
     99     protected String getDefaultKey() {
    100         return UsbBackend.usbFunctionsToString(mUsbBackend.getDefaultUsbFunctions());
    101     }
    102 
    103     @Override
    104     protected boolean setDefaultKey(String key) {
    105         long functions = UsbBackend.usbFunctionsFromString(key);
    106         if (!Utils.isMonkeyRunning()) {
    107             mUsbBackend.setDefaultUsbFunctions(functions);
    108         }
    109         return true;
    110     }
    111 }