1 /* 2 * Copyright (C) 2021 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 package com.android.settings.wifi; 17 18 import android.content.Context; 19 20 import androidx.annotation.VisibleForTesting; 21 import androidx.fragment.app.Fragment; 22 import androidx.preference.PreferenceViewHolder; 23 24 import com.android.wifitrackerlib.WifiEntry; 25 26 /** 27 * WifiEntryPreference that can be long pressed. 28 */ 29 public class LongPressWifiEntryPreference extends WifiEntryPreference { 30 31 private final Fragment mFragment; 32 LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment)33 public LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment) { 34 super(context, wifiEntry); 35 mFragment = fragment; 36 } 37 38 @Override onBindViewHolder(final PreferenceViewHolder view)39 public void onBindViewHolder(final PreferenceViewHolder view) { 40 super.onBindViewHolder(view); 41 if (mFragment != null) { 42 view.itemView.setOnCreateContextMenuListener(mFragment); 43 view.itemView.setTag(this); 44 view.itemView.setLongClickable(true); 45 } 46 } 47 48 @Override refresh()49 public void refresh() { 50 super.refresh(); 51 setEnabled(shouldEnabled()); 52 } 53 54 @VisibleForTesting shouldEnabled()55 boolean shouldEnabled() { 56 WifiEntry wifiEntry = getWifiEntry(); 57 if (wifiEntry == null) return false; 58 59 boolean enabled = wifiEntry.canConnect(); 60 // If Wi-Fi is connected or saved network, leave it enabled to disconnect or configure. 61 if (!enabled && (wifiEntry.canDisconnect() || wifiEntry.isSaved())) { 62 enabled = true; 63 } 64 return enabled; 65 } 66 } 67