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     private boolean mUserAuthenticated = false;
49 
50     @VisibleForTesting
51     UsbConnectionBroadcastReceiver mUsbReceiver;
52 
53     private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
54             (connected, functions, powerRole, dataRole, isUsbFigured) -> {
55                 for (UsbDetailsController controller : mControllers) {
56                     controller.refresh(connected, functions, powerRole, dataRole);
57                 }
58             };
59 
isUserAuthenticated()60     boolean isUserAuthenticated() {
61         return mUserAuthenticated;
62     }
63 
setUserAuthenticated(boolean userAuthenticated)64     void setUserAuthenticated(boolean userAuthenticated) {
65         mUserAuthenticated = userAuthenticated;
66     }
67 
68     @Override
onStart()69     public void onStart() {
70         super.onStart();
71         mUserAuthenticated = false;
72     }
73 
74     @Override
onViewCreated(View view, Bundle savedInstanceState)75     public void onViewCreated(View view, Bundle savedInstanceState) {
76         super.onViewCreated(view, savedInstanceState);
77         Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), getListView());
78     }
79 
80     @Override
getMetricsCategory()81     public int getMetricsCategory() {
82         return SettingsEnums.USB_DEVICE_DETAILS;
83     }
84 
85     @Override
getLogTag()86     protected String getLogTag() {
87         return TAG;
88     }
89 
90     @Override
getPreferenceScreenResId()91     protected int getPreferenceScreenResId() {
92         return R.xml.usb_details_fragment;
93     }
94 
95     @Override
createPreferenceControllers(Context context)96     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
97         mUsbBackend = new UsbBackend(context);
98         mControllers = createControllerList(context, mUsbBackend, this);
99         mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener,
100                 mUsbBackend);
101         this.getSettingsLifecycle().addObserver(mUsbReceiver);
102 
103         return new ArrayList<>(mControllers);
104     }
105 
createControllerList(Context context, UsbBackend usbBackend, UsbDetailsFragment fragment)106     private static List<UsbDetailsController> createControllerList(Context context,
107             UsbBackend usbBackend, UsbDetailsFragment fragment) {
108         List<UsbDetailsController> ret = new ArrayList<>();
109         ret.add(new UsbDetailsHeaderController(context, fragment, usbBackend));
110         ret.add(new UsbDetailsDataRoleController(context, fragment, usbBackend));
111         ret.add(new UsbDetailsFunctionsController(context, fragment, usbBackend));
112         ret.add(new UsbDetailsPowerRoleController(context, fragment, usbBackend));
113         ret.add(new UsbDetailsTranscodeMtpController(context, fragment, usbBackend));
114         return ret;
115     }
116 
117     /**
118      * For Search.
119      */
120     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
121             new BaseSearchIndexProvider(R.xml.usb_details_fragment) {
122                 @Override
123                 protected boolean isPageSearchEnabled(Context context) {
124                     return checkIfUsbDataSignalingIsDisabled(
125                             context, UserHandle.myUserId()) == null;
126                 }
127 
128                 @Override
129                 public List<AbstractPreferenceController> createPreferenceControllers(
130                         Context context) {
131                     return new ArrayList<>(
132                             createControllerList(context, new UsbBackend(context), null));
133                 }
134             };
135 }
136