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