• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.wifi;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.graphics.Bitmap;
22 import android.net.wifi.SoftApConfiguration;
23 import android.util.Log;
24 import android.widget.ImageView;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.FragmentController;
28 import com.android.car.settings.wifi.preferences.WifiTetherQrCodePreference;
29 import com.android.settingslib.qrcode.QrCodeGenerator;
30 
31 /**
32  * Controls WiFi Hotspot QR code.
33  */
34 public class WifiTetherQrCodePreferenceController extends
35         WifiTetherBasePreferenceController<WifiTetherQrCodePreference> implements
36         WifiTetheringHandler.WifiTetheringAvailabilityListener {
37     private static final String TAG = WifiTetherPasswordPreferenceController.class.getSimpleName();
38     private static final String QR_CODE_FORMAT = "WIFI:T:WPA;S:%s;P:%s;;";
39     private static final String QR_CODE_FORMAT_NOPASS = "WIFI:T:nopass;S:%s;;";
40 
41     private WifiTetheringHandler mWifiTetheringHandler;
42 
WifiTetherQrCodePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public WifiTetherQrCodePreferenceController(Context context,
44             String preferenceKey,
45             FragmentController fragmentController,
46             CarUxRestrictions uxRestrictions) {
47         super(context, preferenceKey, fragmentController, uxRestrictions);
48         mWifiTetheringHandler = new WifiTetheringHandler(context,
49                 fragmentController.getSettingsLifecycle(), this);
50     }
51 
52     @Override
getPreferenceType()53     protected Class<WifiTetherQrCodePreference> getPreferenceType() {
54         return WifiTetherQrCodePreference.class;
55     }
56 
57     @Override
getSummary()58     protected String getSummary() {
59         return null;
60     }
61 
62     @Override
getDefaultSummary()63     protected String getDefaultSummary() {
64         return null;
65     }
66 
67     @Override
onStartInternal()68     protected void onStartInternal() {
69         mWifiTetheringHandler.onStartInternal();
70     }
71 
72     @Override
onStopInternal()73     protected void onStopInternal() {
74         mWifiTetheringHandler.onStopInternal();
75     }
76 
77     @Override
onWifiTetheringAvailable()78     public void onWifiTetheringAvailable() {
79         updateQrCode();
80     }
81 
82     @Override
onWifiTetheringUnavailable()83     public void onWifiTetheringUnavailable() {
84         updateQrCode();
85     }
86 
87     @Override
enablePreference()88     public void enablePreference() {
89         // No op.
90     }
91 
92     @Override
disablePreference()93     public void disablePreference() {
94         // No op.
95     }
96 
97     @Override
getDefaultAvailabilityStatus()98     protected int getDefaultAvailabilityStatus() {
99         if (mWifiTetheringHandler.isWifiTetheringEnabled()) {
100             return AVAILABLE_FOR_VIEWING;
101         }
102         return CONDITIONALLY_UNAVAILABLE;
103     }
104 
updateQrCode()105     private void updateQrCode() {
106         if (mWifiTetheringHandler.isWifiTetheringEnabled()) {
107             String name = getCarSoftApConfig().getSsid();
108             String password = getCarSoftApConfig().getPassphrase();
109             int securityType = getCarSoftApConfig().getSecurityType();
110             String content;
111             if (securityType == SoftApConfiguration.SECURITY_TYPE_OPEN) {
112                 content = String.format(QR_CODE_FORMAT_NOPASS, name);
113             } else {
114                 content = String.format(QR_CODE_FORMAT, name, password);
115             }
116             try {
117                 int size = Math.round(getContext().getResources().getDimension(
118                         R.dimen.hotspot_qr_code_size));
119                 Bitmap bmp = QrCodeGenerator.encodeQrCode(content, size);
120 
121                 ImageView qrCode = getPreference().getQRCodeView();
122                 if (qrCode != null) {
123                     qrCode.setImageBitmap(bmp);
124                 }
125             } catch (Exception e) {
126                 Log.w(TAG, "Exception found: " + e);
127             }
128         }
129 
130         refreshUi();
131     }
132 
133 }
134