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.HOTSPOT_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.TetheringManager;
29 import android.net.Uri;
30 import android.net.wifi.WifiManager;
31 import android.os.UserManager;
32 
33 import com.android.car.qc.QCItem;
34 import com.android.car.qc.QCTile;
35 import com.android.car.settings.R;
36 import com.android.car.settings.enterprise.EnterpriseUtils;
37 import com.android.car.settings.wifi.WifiTetherUtil;
38 
39 /**
40  * QCItem for showing a hotspot toggle.
41  */
42 public class HotspotTile extends SettingsQCItem {
43     private final TetheringManager mTetheringManager;
44     private final WifiManager mWifiManager;
45     // Assume hotspot is available until notified otherwise.
46     private boolean mIsSupported = true;
47 
HotspotTile(Context context)48     public HotspotTile(Context context) {
49         super(context);
50         setAvailabilityStatusForZone(getAvailabilityStatusForZoneFromXml(context,
51                 R.xml.network_and_internet_fragment, R.string.pk_wifi_tether_settings_entry));
52         mTetheringManager = context.getSystemService(TetheringManager.class);
53         mWifiManager = context.getSystemService(WifiManager.class);
54     }
55 
56     @Override
getQCItem()57     QCItem getQCItem() {
58         if (isHiddenForZone()) {
59             return null;
60         }
61         Icon actionIcon = Icon.createWithResource(getContext(), R.drawable.ic_qc_hotspot);
62 
63         String userRestriction = UserManager.DISALLOW_CONFIG_TETHERING;
64         boolean hasDpmRestrictions = EnterpriseUtils.hasUserRestrictionByDpm(getContext(),
65                 userRestriction);
66         boolean hasUmRestrictions = EnterpriseUtils.hasUserRestrictionByUm(getContext(),
67                 userRestriction);
68 
69         boolean isReadOnlyForZone = isReadOnlyForZone();
70         PendingIntent disabledPendingIntent = isReadOnlyForZone
71                 ? QCUtils.getDisabledToastBroadcastIntent(getContext())
72                 : getActionDisabledDialogIntent(getContext(), userRestriction);
73 
74         QCTile.Builder tileBuilder = new QCTile.Builder()
75                 .setSubtitle(getContext().getString(R.string.hotspot_settings_title))
76                 .setIcon(actionIcon)
77                 .setChecked(HotspotQCUtils.isHotspotEnabled(mWifiManager))
78                 .setEnabled(!HotspotQCUtils.isHotspotBusy(mWifiManager) && !hasUmRestrictions
79                         && !hasDpmRestrictions && isWritableForZone())
80                 .setAvailable(mIsSupported)
81                 .setAction(getBroadcastIntent())
82                 .setClickableWhileDisabled(hasDpmRestrictions || isReadOnlyForZone)
83                 .setDisabledClickAction(disabledPendingIntent);
84         return tileBuilder.build();
85     }
86 
87     @Override
getUri()88     Uri getUri() {
89         return HOTSPOT_TILE_URI;
90     }
91 
getHotspotSupported()92     boolean getHotspotSupported() {
93         return mIsSupported;
94     }
95 
setHotspotSupported(boolean supported)96     void setHotspotSupported(boolean supported) {
97         mIsSupported = supported;
98     }
99 
100     @Override
onNotifyChange(Intent intent)101     void onNotifyChange(Intent intent) {
102         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
103                 !mWifiManager.isWifiApEnabled());
104         if (newState) {
105             WifiTetherUtil.startTethering(mTetheringManager,
106                     HotspotQCUtils.getDefaultStartTetheringCallback(getContext(), getUri()));
107         } else {
108             WifiTetherUtil.stopTethering(mTetheringManager);
109         }
110     }
111 
112     @Override
getBackgroundWorkerClass()113     Class getBackgroundWorkerClass() {
114         return HotspotTileWorker.class;
115     }
116 }
117