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.settings.wifi.dpp;
18 
19 import static android.content.Intent.ACTION_CLOSE_SYSTEM_DIALOGS;
20 import static android.content.Intent.FLAG_RECEIVER_FOREGROUND;
21 
22 import android.app.Activity;
23 import android.app.KeyguardManager;
24 import android.app.settings.SettingsEnums;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.view.WindowManager;
28 
29 import androidx.activity.result.ActivityResult;
30 import androidx.activity.result.contract.ActivityResultContracts;
31 
32 import com.android.internal.annotations.VisibleForTesting;
33 import com.android.settings.R;
34 import com.android.settings.core.InstrumentedActivity;
35 
36 /**
37  * Sharing a Wi-Fi network by QR code after unlocking. Used by {@code InternetDialog} in QS.
38  */
39 public class WifiDppConfiguratorAuthActivity extends InstrumentedActivity {
40     private static final String WIFI_SHARING_KEY_ALIAS = "wifi_sharing_auth_key";
41     private static final int MAX_UNLOCK_SECONDS = 60;
42 
43     @Override
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         // This is a transparent activity, disable the dim.
47         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
48         Intent authIntent = getSystemService(KeyguardManager.class)
49                 .createConfirmDeviceCredentialIntent(
50                         getText(R.string.wifi_dpp_lockscreen_title), null, getUserId());
51         if (authIntent == null
52                 || WifiDppUtils.isUnlockedWithinSeconds(
53                         WIFI_SHARING_KEY_ALIAS, MAX_UNLOCK_SECONDS)) {
54             startQrCodeActivity();
55             finish();
56         } else {
57             registerForActivityResult(
58                     new ActivityResultContracts.StartActivityForResult(),
59                     this::onAuthResult).launch(authIntent);
60         }
61     }
62 
63     @VisibleForTesting
onAuthResult(ActivityResult result)64     void onAuthResult(ActivityResult result) {
65         if (result.getResultCode() == Activity.RESULT_OK) {
66             startQrCodeActivity();
67         }
68         finish();
69     }
70 
startQrCodeActivity()71     private void startQrCodeActivity() {
72         // Close quick settings shade
73         sendBroadcast(
74                 new Intent(ACTION_CLOSE_SYSTEM_DIALOGS).setFlags(FLAG_RECEIVER_FOREGROUND));
75         Intent qrCodeIntent = new Intent();
76         qrCodeIntent.setAction(
77                 WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
78         qrCodeIntent.setPackage(getPackageName());
79         qrCodeIntent.putExtras(getIntent());
80         startActivity(qrCodeIntent);
81     }
82 
83     @Override
getMetricsCategory()84     public int getMetricsCategory() {
85         return SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR;
86     }
87 }
88