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