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.Handler; 21 22 import androidx.annotation.UiThread; 23 import androidx.annotation.VisibleForTesting; 24 25 import com.android.settings.core.PreferenceControllerMixin; 26 import com.android.settings.flags.Flags; 27 import com.android.settings.wifi.dpp.WifiDppUtils; 28 import com.android.settingslib.core.AbstractPreferenceController; 29 30 /** 31 * This class provides common members and refresh functionality for usb controllers. 32 */ 33 public abstract class UsbDetailsController extends AbstractPreferenceController 34 implements PreferenceControllerMixin { 35 36 protected final Context mContext; 37 protected final UsbDetailsFragment mFragment; 38 protected final UsbBackend mUsbBackend; 39 40 @VisibleForTesting 41 Handler mHandler; 42 UsbDetailsController(Context context, UsbDetailsFragment fragment, UsbBackend backend)43 public UsbDetailsController(Context context, UsbDetailsFragment fragment, UsbBackend backend) { 44 super(context); 45 mContext = context; 46 mFragment = fragment; 47 mUsbBackend = backend; 48 mHandler = new Handler(context.getMainLooper()); 49 } 50 51 @Override isAvailable()52 public boolean isAvailable() { 53 return true; 54 } 55 56 /** 57 * Called when the USB state has changed, so that this component can be refreshed. 58 * 59 * @param connected Whether USB is connected 60 * @param functions A mask of the currently enabled functions 61 * @param powerRole The current power role 62 * @param dataRole The current data role 63 */ 64 @UiThread refresh(boolean connected, long functions, int powerRole, int dataRole)65 protected abstract void refresh(boolean connected, long functions, int powerRole, int dataRole); 66 67 /** Protects given action with an auth challenge. */ requireAuthAndExecute(Runnable action)68 protected final void requireAuthAndExecute(Runnable action) { 69 if (Flags.enableAuthChallengeForUsbPreferences() && !mFragment.isUserAuthenticated()) { 70 WifiDppUtils.showLockScreen(mContext, () -> { 71 mFragment.setUserAuthenticated(true); 72 action.run(); 73 }); 74 } else { 75 action.run(); 76 } 77 } 78 } 79