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.os.Bundle; 21 import android.provider.SearchIndexableResource; 22 import android.support.annotation.VisibleForTesting; 23 24 import com.android.internal.logging.nano.MetricsProto; 25 import com.android.settings.R; 26 import com.android.settings.dashboard.DashboardFragment; 27 import com.android.settings.search.BaseSearchIndexProvider; 28 import com.android.settings.search.Indexable; 29 import com.android.settingslib.core.AbstractPreferenceController; 30 31 import com.google.android.collect.Lists; 32 33 import java.util.ArrayList; 34 import java.util.List; 35 36 /** 37 * Controls the USB device details and provides updates to individual controllers. 38 */ 39 public class UsbDetailsFragment extends DashboardFragment { 40 private static final String TAG = UsbDetailsFragment.class.getSimpleName(); 41 42 private List<UsbDetailsController> mControllers; 43 private UsbBackend mUsbBackend; 44 45 @VisibleForTesting 46 UsbConnectionBroadcastReceiver mUsbReceiver; 47 48 private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener = 49 (connected, functions, powerRole, dataRole) -> { 50 for (UsbDetailsController controller : mControllers) { 51 controller.refresh(connected, functions, powerRole, dataRole); 52 } 53 }; 54 55 @Override getMetricsCategory()56 public int getMetricsCategory() { 57 return MetricsProto.MetricsEvent.USB_DEVICE_DETAILS; 58 } 59 60 @Override getLogTag()61 protected String getLogTag() { 62 return TAG; 63 } 64 65 @Override getPreferenceScreenResId()66 protected int getPreferenceScreenResId() { 67 return R.xml.usb_details_fragment; 68 } 69 70 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)71 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 72 super.onCreatePreferences(savedInstanceState, rootKey); 73 } 74 isConnected()75 public boolean isConnected() { 76 return mUsbReceiver.isConnected(); 77 } 78 79 @Override createPreferenceControllers(Context context)80 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 81 mUsbBackend = new UsbBackend(context); 82 mControllers = createControllerList(context, mUsbBackend, this); 83 mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener, 84 mUsbBackend); 85 this.getLifecycle().addObserver(mUsbReceiver); 86 87 return new ArrayList<>(mControllers); 88 } 89 createControllerList(Context context, UsbBackend usbBackend, UsbDetailsFragment fragment)90 private static List<UsbDetailsController> createControllerList(Context context, 91 UsbBackend usbBackend, UsbDetailsFragment fragment) { 92 List<UsbDetailsController> ret = new ArrayList<>(); 93 ret.add(new UsbDetailsHeaderController(context, fragment, usbBackend)); 94 ret.add(new UsbDetailsDataRoleController(context, fragment, usbBackend)); 95 ret.add(new UsbDetailsFunctionsController(context, fragment, usbBackend)); 96 ret.add(new UsbDetailsPowerRoleController(context, fragment, usbBackend)); 97 return ret; 98 } 99 100 /** 101 * For Search. 102 */ 103 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 104 new BaseSearchIndexProvider() { 105 @Override 106 public List<SearchIndexableResource> getXmlResourcesToIndex( 107 Context context, boolean enabled) { 108 SearchIndexableResource res = new SearchIndexableResource(context); 109 res.xmlResId = R.xml.usb_details_fragment; 110 return Lists.newArrayList(res); 111 } 112 113 @Override 114 public List<String> getNonIndexableKeys(Context context) { 115 return super.getNonIndexableKeys(context); 116 } 117 118 @Override 119 public List<AbstractPreferenceController> createPreferenceControllers( 120 Context context) { 121 return new ArrayList<>( 122 createControllerList(context, new UsbBackend(context), null)); 123 } 124 }; 125 } 126