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.settings.qc.QCUtils.getActionDisabledDialogIntent;
21 import static com.android.car.settings.qc.QCUtils.getAvailabilityStatusForZoneFromXml;
22 import static com.android.car.settings.qc.SettingsQCRegistry.WIFI_TILE_URI;
23 
24 import android.app.PendingIntent;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.graphics.drawable.Icon;
28 import android.net.Uri;
29 import android.net.wifi.WifiManager;
30 import android.os.UserManager;
31 
32 import com.android.car.qc.QCItem;
33 import com.android.car.qc.QCTile;
34 import com.android.car.settings.R;
35 import com.android.car.settings.enterprise.EnterpriseUtils;
36 
37 /**
38  * QCItem for showing a wifi toggle.
39  */
40 public class WifiTile extends SettingsQCItem {
41     private final WifiManager mWifiManager;
42 
WifiTile(Context context)43     public WifiTile(Context context) {
44         super(context);
45         setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context,
46                 R.xml.network_and_internet_fragment, R.string.pk_wifi_entry_state_switch));
47         mWifiManager = context.getSystemService(WifiManager.class);
48     }
49 
50     @Override
getQCItem()51     QCItem getQCItem() {
52         if (isHiddenForZone()) {
53             return null;
54         }
55         int wifiState = mWifiManager.getWifiState();
56         boolean wifiEnabled = wifiState == WifiManager.WIFI_STATE_ENABLED
57                 || wifiState == WifiManager.WIFI_STATE_ENABLING;
58         Icon icon = Icon.createWithResource(getContext(), WifiQCUtils.getIcon(mWifiManager));
59 
60         String userRestriction = UserManager.DISALLOW_CONFIG_WIFI;
61         boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(),
62                 userRestriction);
63         boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(),
64                 userRestriction);
65 
66         boolean isReadOnlyForZone = isReadOnlyForZone();
67         PendingIntent disabledPendingIntent = isReadOnlyForZone
68                 ? QCUtils.getDisabledToastBroadcastIntent(getContext())
69                 : getActionDisabledDialogIntent(getContext(), userRestriction);
70 
71         return new QCTile.Builder()
72                 .setIcon(icon)
73                 .setChecked(wifiEnabled)
74                 .setAction(getBroadcastIntent())
75                 .setEnabled(!WifiQCUtils.isWifiBusy(mWifiManager) && !hasUmRestrictions
76                         && !hasDpmRestrictions && isWritableForZone())
77                 .setClickableWhileDisabled(hasDpmRestrictions | isReadOnlyForZone)
78                 .setDisabledClickAction(disabledPendingIntent)
79                 .setSubtitle(WifiQCUtils.getSubtitle(getContext(), mWifiManager))
80                 .build();
81     }
82 
83     @Override
getUri()84     Uri getUri() {
85         return WIFI_TILE_URI;
86     }
87 
88     @Override
onNotifyChange(Intent intent)89     void onNotifyChange(Intent intent) {
90         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
91                 !mWifiManager.isWifiEnabled());
92         mWifiManager.setWifiEnabled(newState);
93     }
94 
95     @Override
getBackgroundWorkerClass()96     Class getBackgroundWorkerClass() {
97         return WifiTileWorker.class;
98     }
99 }
100