Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0fcc4796 authored by Chelsea Hao's avatar Chelsea Hao Committed by Android (Google) Code Review
Browse files

Merge "Created a new activity in settings App." into main

parents 4f32231c 3ecc87b3
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -4680,6 +4680,16 @@
            </intent-filter>
        </activity>

        <activity
            android:name=".wifi.dpp.WifiDppConfiguratorAuthActivity"
            android:theme="@style/Transparent"
            android:exported="true">
            <intent-filter>
                <action android:name="android.settings.WIFI_DPP_CONFIGURATOR_AUTH_QR_CODE_GENERATOR"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

        <activity
            android:name=".wifi.dpp.WifiDppEnrolleeActivity"
            android:exported="true">
+82 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+79 −0
Original line number Diff line number Diff line
/*
 * 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<WifiDppConfiguratorAuthActivity> 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();
        });
    }
}