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.SettingsQCRegistry.HOTSPOT_TILE_URI;
21 
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.drawable.Icon;
25 import android.net.TetheringManager;
26 import android.net.Uri;
27 import android.net.wifi.WifiManager;
28 
29 import com.android.car.qc.QCItem;
30 import com.android.car.qc.QCTile;
31 import com.android.car.settings.R;
32 
33 /**
34  * QCItem for showing a hotspot toggle.
35  */
36 public class HotspotTile extends SettingsQCItem {
37     private final TetheringManager mTetheringManager;
38     private final WifiManager mWifiManager;
39     // Assume hotspot is available until notified otherwise.
40     private boolean mIsSupported = true;
41 
HotspotTile(Context context)42     public HotspotTile(Context context) {
43         super(context);
44         mTetheringManager = context.getSystemService(TetheringManager.class);
45         mWifiManager = context.getSystemService(WifiManager.class);
46     }
47 
48     @Override
getQCItem()49     QCItem getQCItem() {
50         Icon actionIcon = Icon.createWithResource(getContext(), R.drawable.ic_qc_hotspot);
51 
52         QCTile.Builder tileBuilder = new QCTile.Builder()
53                 .setSubtitle(getContext().getString(R.string.hotspot_settings_title))
54                 .setIcon(actionIcon)
55                 .setChecked(HotspotQCUtils.isHotspotEnabled(mWifiManager))
56                 .setEnabled(!HotspotQCUtils.isHotspotBusy(mWifiManager))
57                 .setAvailable(mIsSupported)
58                 .setAction(getBroadcastIntent());
59         return tileBuilder.build();
60     }
61 
62     @Override
getUri()63     Uri getUri() {
64         return HOTSPOT_TILE_URI;
65     }
66 
getHotspotSupported()67     boolean getHotspotSupported() {
68         return mIsSupported;
69     }
70 
setHotspotSupported(boolean supported)71     void setHotspotSupported(boolean supported) {
72         mIsSupported = supported;
73     }
74 
75     @Override
onNotifyChange(Intent intent)76     void onNotifyChange(Intent intent) {
77         boolean newState = intent.getBooleanExtra(QC_ACTION_TOGGLE_STATE,
78                 !mWifiManager.isWifiApEnabled());
79         if (newState) {
80             HotspotQCUtils.enableHotspot(mTetheringManager,
81                     HotspotQCUtils.getDefaultStartTetheringCallback(getContext(), getUri()));
82         } else {
83             HotspotQCUtils.disableHotspot(mTetheringManager);
84         }
85     }
86 
87     @Override
getBackgroundWorkerClass()88     Class getBackgroundWorkerClass() {
89         return HotspotTileWorker.class;
90     }
91 }
92