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 com.android.settingslib.RestrictedLockUtilsInternal.checkIfUsbDataSignalingIsDisabled; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.os.UserHandle; 25 import android.view.View; 26 27 import androidx.annotation.VisibleForTesting; 28 29 import com.android.settings.R; 30 import com.android.settings.Utils; 31 import com.android.settings.dashboard.DashboardFragment; 32 import com.android.settings.search.BaseSearchIndexProvider; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 import com.android.settingslib.search.SearchIndexable; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 /** 40 * Controls the USB device details and provides updates to individual controllers. 41 */ 42 @SearchIndexable(forTarget = SearchIndexable.ALL & ~SearchIndexable.ARC) 43 public class UsbDetailsFragment extends DashboardFragment { 44 private static final String TAG = UsbDetailsFragment.class.getSimpleName(); 45 46 private List<UsbDetailsController> mControllers; 47 private UsbBackend mUsbBackend; 48 49 @VisibleForTesting 50 UsbConnectionBroadcastReceiver mUsbReceiver; 51 52 private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener = 53 (connected, functions, powerRole, dataRole) -> { 54 for (UsbDetailsController controller : mControllers) { 55 controller.refresh(connected, functions, powerRole, dataRole); 56 } 57 }; 58 59 @Override onViewCreated(View view, Bundle savedInstanceState)60 public void onViewCreated(View view, Bundle savedInstanceState) { 61 super.onViewCreated(view, savedInstanceState); 62 Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), getListView()); 63 } 64 65 @Override getMetricsCategory()66 public int getMetricsCategory() { 67 return SettingsEnums.USB_DEVICE_DETAILS; 68 } 69 70 @Override getLogTag()71 protected String getLogTag() { 72 return TAG; 73 } 74 75 @Override getPreferenceScreenResId()76 protected int getPreferenceScreenResId() { 77 return R.xml.usb_details_fragment; 78 } 79 80 @Override createPreferenceControllers(Context context)81 protected List<AbstractPreferenceController> createPreferenceControllers(Context context) { 82 mUsbBackend = new UsbBackend(context); 83 mControllers = createControllerList(context, mUsbBackend, this); 84 mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener, 85 mUsbBackend); 86 this.getSettingsLifecycle().addObserver(mUsbReceiver); 87 88 return new ArrayList<>(mControllers); 89 } 90 createControllerList(Context context, UsbBackend usbBackend, UsbDetailsFragment fragment)91 private static List<UsbDetailsController> createControllerList(Context context, 92 UsbBackend usbBackend, UsbDetailsFragment fragment) { 93 List<UsbDetailsController> ret = new ArrayList<>(); 94 ret.add(new UsbDetailsHeaderController(context, fragment, usbBackend)); 95 ret.add(new UsbDetailsDataRoleController(context, fragment, usbBackend)); 96 ret.add(new UsbDetailsFunctionsController(context, fragment, usbBackend)); 97 ret.add(new UsbDetailsPowerRoleController(context, fragment, usbBackend)); 98 ret.add(new UsbDetailsTranscodeMtpController(context, fragment, usbBackend)); 99 return ret; 100 } 101 102 /** 103 * For Search. 104 */ 105 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 106 new BaseSearchIndexProvider(R.xml.usb_details_fragment) { 107 @Override 108 protected boolean isPageSearchEnabled(Context context) { 109 return checkIfUsbDataSignalingIsDisabled( 110 context, UserHandle.myUserId()) == null; 111 } 112 113 @Override 114 public List<AbstractPreferenceController> createPreferenceControllers( 115 Context context) { 116 return new ArrayList<>( 117 createControllerList(context, new UsbBackend(context), null)); 118 } 119 }; 120 } 121