1 /*
2  * Copyright (C) 2018 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.settings.wifi.dpp;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Intent;
21 import android.util.EventLog;
22 import android.util.Log;
23 
24 import androidx.annotation.VisibleForTesting;
25 import androidx.fragment.app.FragmentTransaction;
26 
27 import com.android.settings.R;
28 import com.android.settingslib.wifi.WifiRestrictionsCache;
29 
30 /**
31  * To provision "this" device with specified Wi-Fi network.
32  *
33  * To use intent action {@code ACTION_ENROLLEE_QR_CODE_SCANNER}, specify the SSID string of the
34  * Wi-Fi network to be provisioned in {@code WifiDppUtils.EXTRA_WIFI_SSID}.
35  */
36 public class WifiDppEnrolleeActivity extends WifiDppBaseActivity implements
37         WifiDppQrCodeScannerFragment.OnScanWifiDppSuccessListener {
38     private static final String TAG = "WifiDppEnrolleeActivity";
39 
40     static final String ACTION_ENROLLEE_QR_CODE_SCANNER =
41             "android.settings.WIFI_DPP_ENROLLEE_QR_CODE_SCANNER";
42 
43     @VisibleForTesting
44     protected WifiRestrictionsCache mWifiRestrictionsCache;
45 
46     @Override
getMetricsCategory()47     public int getMetricsCategory() {
48         return SettingsEnums.SETTINGS_WIFI_DPP_ENROLLEE;
49     }
50 
51     @Override
handleIntent(Intent intent)52     protected void handleIntent(Intent intent) {
53         String action = intent != null ? intent.getAction() : null;
54         if (action == null) {
55             finish();
56             return;
57         }
58 
59         if (!isWifiConfigAllowed()) {
60             Log.e(TAG, "The user is not allowed to configure Wi-Fi.");
61             finish();
62             EventLog.writeEvent(0x534e4554, "202017876", getApplicationContext().getUserId(),
63                     "The user is not allowed to configure Wi-Fi.");
64             return;
65         }
66 
67         switch (action) {
68             case ACTION_ENROLLEE_QR_CODE_SCANNER:
69                 String ssid = intent.getStringExtra(WifiDppUtils.EXTRA_WIFI_SSID);
70                 showQrCodeScannerFragment(ssid);
71                 break;
72             default:
73                 Log.e(TAG, "Launch with an invalid action");
74                 finish();
75         }
76     }
77 
isWifiConfigAllowed()78     private boolean isWifiConfigAllowed() {
79         if (mWifiRestrictionsCache == null) {
80             mWifiRestrictionsCache = WifiRestrictionsCache.getInstance(getApplicationContext());
81         }
82         return mWifiRestrictionsCache.isConfigWifiAllowed();
83     }
84 
85     @VisibleForTesting
showQrCodeScannerFragment(String ssid)86     protected void showQrCodeScannerFragment(String ssid) {
87         WifiDppQrCodeScannerFragment fragment =
88                 (WifiDppQrCodeScannerFragment) mFragmentManager.findFragmentByTag(
89                         WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
90 
91         if (fragment == null) {
92             fragment = new WifiDppQrCodeScannerFragment(ssid);
93         } else {
94             if (fragment.isVisible()) {
95                 return;
96             }
97 
98             // When the fragment in back stack but not on top of the stack, we can simply pop
99             // stack because current fragment transactions are arranged in an order
100             mFragmentManager.popBackStackImmediate();
101             return;
102         }
103         final FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
104 
105         fragmentTransaction.replace(R.id.fragment_container, fragment,
106                 WifiDppUtils.TAG_FRAGMENT_QR_CODE_SCANNER);
107         fragmentTransaction.commit();
108     }
109 
110     @Override
onScanWifiDppSuccess(WifiQrCode wifiQrCode)111     public void onScanWifiDppSuccess(WifiQrCode wifiQrCode) {
112         // Do nothing
113     }
114 }
115