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 17 package com.android.car.settings.qc; 18 19 import static com.android.car.qc.QCItem.QC_ACTION_TOGGLE_STATE; 20 import static com.android.car.qc.QCItem.QC_TYPE_ACTION_SWITCH; 21 import static com.android.car.settings.qc.QCUtils.getActionDisabledDialogIntent; 22 import static com.android.car.settings.qc.QCUtils.getAvailabilityStatusForZoneFromXml; 23 import static com.android.car.settings.qc.SettingsQCRegistry.WIFI_ROW_URI; 24 25 import android.app.PendingIntent; 26 import android.content.Context; 27 import android.content.Intent; 28 import android.graphics.drawable.Icon; 29 import android.net.Uri; 30 import android.net.wifi.WifiManager; 31 import android.os.UserManager; 32 33 import com.android.car.qc.QCActionItem; 34 import com.android.car.qc.QCItem; 35 import com.android.car.qc.QCList; 36 import com.android.car.qc.QCRow; 37 import com.android.car.settings.R; 38 import com.android.car.settings.enterprise.EnterpriseUtils; 39 40 /** 41 * QCItem for showing a wifi row element. 42 * The row contains a wifi icon, the status or the currently connected wifi SSID, and a switch 43 * to enable/disable wifi. 44 */ 45 public class WifiRow extends SettingsQCItem { 46 private final WifiManager mWifiManager; 47 WifiRow(Context context)48 public WifiRow(Context context) { 49 super(context); 50 setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context, 51 R.xml.network_and_internet_fragment, R.string.pk_wifi_entry_state_switch)); 52 mWifiManager = context.getSystemService(WifiManager.class); 53 } 54 55 @Override getQCItem()56 QCItem getQCItem() { 57 if (isHiddenForZone()) { 58 return null; 59 } 60 int wifiState = mWifiManager.getWifiState(); 61 boolean wifiEnabled = wifiState == WifiManager.WIFI_STATE_ENABLED 62 || wifiState == WifiManager.WIFI_STATE_ENABLING; 63 64 // TODO(b/304694358): Use api that detects connected secondary networks 65 Icon icon = Icon.createWithResource(getContext(), WifiQCUtils.getIcon(mWifiManager)); 66 67 String userRestriction = UserManager.DISALLOW_CONFIG_WIFI; 68 boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(), 69 userRestriction); 70 boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(), 71 userRestriction); 72 73 boolean isReadOnlyForZone = isReadOnlyForZone(); 74 PendingIntent disabledPendingIntent = isReadOnlyForZone 75 ? QCUtils.getDisabledToastBroadcastIntent(getContext()) 76 : getActionDisabledDialogIntent(getContext(), userRestriction); 77 78 QCActionItem wifiToggle = new QCActionItem.Builder(QC_TYPE_ACTION_SWITCH) 79 .setChecked(wifiEnabled) 80 .setAction(getBroadcastIntent()) 81 .setEnabled(!WifiQCUtils.isWifiBusy(mWifiManager) && !hasUmRestrictions 82 && !hasDpmRestrictions && isWritableForZone()) 83 .setClickableWhileDisabled(hasDpmRestrictions | isReadOnlyForZone) 84 .setDisabledClickAction(disabledPendingIntent) 85 .setContentDescription(getContext(), R.string.wifi_settings) 86 .build(); 87 88 QCRow wifiRow = new QCRow.Builder() 89 .setIcon(icon) 90 .setTitle(getContext().getString(R.string.wifi_settings)) 91 .setSubtitle(WifiQCUtils.getSubtitle(getContext(), mWifiManager)) 92 .addEndItem(wifiToggle) 93 .build(); 94 95 return new QCList.Builder() 96 .addRow(wifiRow) 97 .build(); 98 } 99 100 @Override getUri()101 Uri getUri() { 102 return WIFI_ROW_URI; 103 } 104 105 @Override onNotifyChange(Intent intent)106 void onNotifyChange(Intent intent) { 107 boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE, 108 !mWifiManager.isWifiEnabled()); 109 mWifiManager.setWifiEnabled(newState); 110 } 111 112 @Override getBackgroundWorkerClass()113 Class getBackgroundWorkerClass() { 114 return WifiRowWorker.class; 115 } 116 } 117