diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 79a7171abb8..2c61ec62b28 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -4680,6 +4680,16 @@ + + + + + + + diff --git a/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivity.java b/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivity.java new file mode 100644 index 00000000000..aa77dc178e3 --- /dev/null +++ b/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivity.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.wifi.dpp; + +import static android.content.Intent.ACTION_CLOSE_SYSTEM_DIALOGS; +import static android.content.Intent.FLAG_RECEIVER_FOREGROUND; + +import android.app.Activity; +import android.app.KeyguardManager; +import android.app.settings.SettingsEnums; +import android.content.Intent; +import android.os.Bundle; +import android.view.WindowManager; + +import androidx.activity.result.ActivityResult; +import androidx.activity.result.contract.ActivityResultContracts; + +import com.android.internal.annotations.VisibleForTesting; +import com.android.settings.R; +import com.android.settings.core.InstrumentedActivity; + +/** + * Sharing a Wi-Fi network by QR code after unlocking. Used by {@code InternetDialog} in QS. + */ +public class WifiDppConfiguratorAuthActivity extends InstrumentedActivity { + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // This is a transparent activity, disable the dim. + getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); + Intent authIntent = getSystemService(KeyguardManager.class) + .createConfirmDeviceCredentialIntent( + getText(R.string.wifi_dpp_lockscreen_title), null, getUserId()); + if (authIntent == null) { + startQrCodeActivity(); + finish(); + } else { + registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + this::onAuthResult).launch(authIntent); + } + } + + @VisibleForTesting + void onAuthResult(ActivityResult result) { + if (result.getResultCode() == Activity.RESULT_OK) { + startQrCodeActivity(); + } + finish(); + } + + private void startQrCodeActivity() { + // Close quick settings shade + sendBroadcast( + new Intent(ACTION_CLOSE_SYSTEM_DIALOGS).setFlags(FLAG_RECEIVER_FOREGROUND)); + Intent qrCodeIntent = new Intent(); + qrCodeIntent.setAction( + WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR); + qrCodeIntent.putExtras(getIntent()); + startActivity(qrCodeIntent); + } + + @Override + public int getMetricsCategory() { + return SettingsEnums.SETTINGS_WIFI_DPP_CONFIGURATOR; + } +} diff --git a/tests/componenttests/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivityTest.java b/tests/componenttests/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivityTest.java new file mode 100644 index 00000000000..94b93699309 --- /dev/null +++ b/tests/componenttests/src/com/android/settings/wifi/dpp/WifiDppConfiguratorAuthActivityTest.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.wifi.dpp; + +import static android.app.Activity.RESULT_OK; + +import static androidx.test.espresso.intent.Intents.intended; +import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction; + +import static com.google.common.truth.Truth.assertThat; + +import static org.hamcrest.Matchers.equalTo; + +import android.app.KeyguardManager; + +import androidx.activity.result.ActivityResult; +import androidx.test.core.app.ActivityScenario; +import androidx.test.espresso.intent.Intents; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.SmallTest; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class WifiDppConfiguratorAuthActivityTest { + + @Before + public void setup() { + Intents.init(); + } + + @After + public void teardown() throws Exception { + Intents.release(); + } + + @Test + public void launchActivity_sendAuthIntent() { + ActivityScenario activityScenario = + ActivityScenario.launch(WifiDppConfiguratorAuthActivity.class); + assertThat(activityScenario).isNotNull(); + intended(hasAction(equalTo(KeyguardManager.ACTION_CONFIRM_DEVICE_CREDENTIAL_WITH_USER))); + } + + @Test + public void launchActivity_sendQrCodeIntent() { + ActivityScenario.launch(WifiDppConfiguratorAuthActivity.class).onActivity(activity -> + activity.onAuthResult(new ActivityResult(RESULT_OK, /* data= */ null)) + ); + intended(hasAction( + equalTo(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR))); + } + + @Test + public void launchActivity_shouldFinish() { + ActivityScenario.launch(WifiDppConfiguratorAuthActivity.class).onActivity(activity -> { + activity.onAuthResult(new ActivityResult(RESULT_OK, /* data= */ null)); + assertThat(activity.isFinishing()).isTrue(); + }); + } +}