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 import android.os.UserManager;
20 
21 import androidx.annotation.VisibleForTesting;
22 import androidx.fragment.app.Fragment;
23 import androidx.preference.PreferenceViewHolder;
24 
25 import com.android.settingslib.RestrictedLockUtils;
26 import com.android.wifitrackerlib.WifiEntry;
27 
28 /**
29  * WifiEntryPreference that can be long pressed.
30  */
31 public class LongPressWifiEntryPreference extends WifiEntryPreference {
32 
33     private final Fragment mFragment;
34 
LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment)35     public LongPressWifiEntryPreference(Context context, WifiEntry wifiEntry, Fragment fragment) {
36         super(context, wifiEntry);
37         mFragment = fragment;
38         checkRestrictionAndSetDisabled();
39     }
40 
41     @Override
onBindViewHolder(final PreferenceViewHolder view)42     public void onBindViewHolder(final PreferenceViewHolder view) {
43         super.onBindViewHolder(view);
44         if (mFragment != null) {
45             view.itemView.setOnCreateContextMenuListener(mFragment);
46             view.itemView.setTag(this);
47             view.itemView.setLongClickable(true);
48         }
49     }
50 
51     @Override
refresh()52     public void refresh() {
53         super.refresh();
54         setEnabled(shouldEnabled());
55     }
56 
57     @VisibleForTesting
shouldEnabled()58     boolean shouldEnabled() {
59         WifiEntry wifiEntry = getWifiEntry();
60         if (wifiEntry == null) return false;
61 
62         boolean enabled = wifiEntry.canConnect();
63         // If Wi-Fi is connected or saved network, leave it enabled to disconnect or configure.
64         if (!enabled && (wifiEntry.canDisconnect() || wifiEntry.isSaved())) {
65             enabled = true;
66         }
67         return enabled;
68     }
69 
70     @VisibleForTesting
checkRestrictionAndSetDisabled()71     void checkRestrictionAndSetDisabled() {
72         if (!getWifiEntry().hasAdminRestrictions()) {
73             return;
74         }
75         RestrictedLockUtils.EnforcedAdmin admin = null;
76         Context context = getContext();
77         if (context != null) {
78             admin = RestrictedLockUtils.getProfileOrDeviceOwner(context, context.getUser());
79         }
80         if (admin == null) {
81             // Use UserManager.DISALLOW_ADD_WIFI_CONFIG as default Wi-Fi network restriction.
82             admin = RestrictedLockUtils.EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(
83                     UserManager.DISALLOW_ADD_WIFI_CONFIG);
84         }
85         setDisabledByAdmin(admin);
86     }
87 }
88