Merge "Launch SecurityHub fragment on SECURITY_SETTINGS intent when available." into sc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
b6cea338a0
@@ -19,11 +19,14 @@ package com.android.settings;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.util.FeatureFlagUtils;
|
import android.util.FeatureFlagUtils;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
import com.android.settings.core.FeatureFlags;
|
import com.android.settings.core.FeatureFlags;
|
||||||
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
import com.android.settings.enterprise.EnterprisePrivacySettings;
|
||||||
import com.android.settings.overlay.FeatureFactory;
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
import com.android.settings.security.SecuritySettingsFeatureProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Top-level Settings activity
|
* Top-level Settings activity
|
||||||
@@ -118,7 +121,39 @@ public class Settings extends SettingsActivity {
|
|||||||
* Activity for Reduce Bright Colors.
|
* Activity for Reduce Bright Colors.
|
||||||
*/
|
*/
|
||||||
public static class ReduceBrightColorsSettingsActivity extends SettingsActivity { /* empty */ }
|
public static class ReduceBrightColorsSettingsActivity extends SettingsActivity { /* empty */ }
|
||||||
public static class SecurityDashboardActivity extends SettingsActivity { /* empty */ }
|
/** Activity for the security dashboard. */
|
||||||
|
public static class SecurityDashboardActivity extends SettingsActivity {
|
||||||
|
|
||||||
|
/** Whether the given fragment is allowed. */
|
||||||
|
@VisibleForTesting
|
||||||
|
@Override
|
||||||
|
public boolean isValidFragment(String fragmentName) {
|
||||||
|
return super.isValidFragment(fragmentName)
|
||||||
|
|| (fragmentName != null
|
||||||
|
&& TextUtils.equals(fragmentName, getAlternativeFragmentName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInitialFragmentName(Intent intent) {
|
||||||
|
final String alternativeFragmentName = getAlternativeFragmentName();
|
||||||
|
if (alternativeFragmentName != null) {
|
||||||
|
return alternativeFragmentName;
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.getInitialFragmentName(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getAlternativeFragmentName() {
|
||||||
|
String alternativeFragmentClassname = null;
|
||||||
|
final SecuritySettingsFeatureProvider securitySettingsFeatureProvider =
|
||||||
|
FeatureFactory.getFactory(this).getSecuritySettingsFeatureProvider();
|
||||||
|
if (securitySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment()) {
|
||||||
|
alternativeFragmentClassname = securitySettingsFeatureProvider
|
||||||
|
.getAlternativeSecuritySettingsFragmentClassname();
|
||||||
|
}
|
||||||
|
return alternativeFragmentClassname;
|
||||||
|
}
|
||||||
|
}
|
||||||
public static class UsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
|
public static class UsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
|
||||||
public static class AppUsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
|
public static class AppUsageAccessSettingsActivity extends SettingsActivity { /* empty */ }
|
||||||
public static class LocationSettingsActivity extends SettingsActivity { /* empty */ }
|
public static class LocationSettingsActivity extends SettingsActivity { /* empty */ }
|
||||||
|
@@ -219,7 +219,7 @@ public class SettingsActivity extends SettingsBaseActivity
|
|||||||
private String getMetricsTag() {
|
private String getMetricsTag() {
|
||||||
String tag = null;
|
String tag = null;
|
||||||
if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
|
if (getIntent() != null && getIntent().hasExtra(EXTRA_SHOW_FRAGMENT)) {
|
||||||
tag = getIntent().getStringExtra(EXTRA_SHOW_FRAGMENT);
|
tag = getInitialFragmentName(getIntent());
|
||||||
}
|
}
|
||||||
if (TextUtils.isEmpty(tag)) {
|
if (TextUtils.isEmpty(tag)) {
|
||||||
Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
|
Log.w(LOG_TAG, "MetricsTag is invalid " + tag);
|
||||||
@@ -262,7 +262,7 @@ public class SettingsActivity extends SettingsBaseActivity
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Getting Intent properties can only be done after the super.onCreate(...)
|
// Getting Intent properties can only be done after the super.onCreate(...)
|
||||||
final String initialFragmentName = intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
|
final String initialFragmentName = getInitialFragmentName(intent);
|
||||||
|
|
||||||
// This is a "Sub Settings" when:
|
// This is a "Sub Settings" when:
|
||||||
// - this is a real SubSettings
|
// - this is a real SubSettings
|
||||||
@@ -363,6 +363,12 @@ public class SettingsActivity extends SettingsBaseActivity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns the initial fragment name that the activity will launch. */
|
||||||
|
@VisibleForTesting
|
||||||
|
public String getInitialFragmentName(Intent intent) {
|
||||||
|
return intent.getStringExtra(EXTRA_SHOW_FRAGMENT);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
|
protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
|
||||||
theme.applyStyle(R.style.SetupWizardPartnerResource, true);
|
theme.applyStyle(R.style.SetupWizardPartnerResource, true);
|
||||||
|
@@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 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.security;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
|
||||||
|
import com.android.settings.Settings;
|
||||||
|
import com.android.settings.SettingsActivity;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class)
|
||||||
|
public class SecurityDashboardActivityTest {
|
||||||
|
private static final String ALTERNATIVE_FRAGMENT_CLASSNAME = "AlternativeFragmentClassname";
|
||||||
|
private static final String DEFAULT_FRAGMENT_CLASSNAME = "DefaultFragmentClassname";
|
||||||
|
|
||||||
|
private SecuritySettingsFeatureProvider mSecuritySettingsFeatureProvider;
|
||||||
|
private Settings.SecurityDashboardActivity mActivity;
|
||||||
|
private Intent mDefaultIntent;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
FakeFeatureFactory mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||||
|
mSecuritySettingsFeatureProvider = mFeatureFactory.getSecuritySettingsFeatureProvider();
|
||||||
|
mDefaultIntent = new Intent();
|
||||||
|
mDefaultIntent.setAction(android.provider.Settings.ACTION_SECURITY_SETTINGS);
|
||||||
|
mDefaultIntent.setClass(InstrumentationRegistry.getInstrumentation().getTargetContext(),
|
||||||
|
Settings.SecurityDashboardActivity.class);
|
||||||
|
mDefaultIntent.putExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT, DEFAULT_FRAGMENT_CLASSNAME);
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
|
||||||
|
try {
|
||||||
|
mActivity =
|
||||||
|
(Settings.SecurityDashboardActivity) InstrumentationRegistry
|
||||||
|
.getInstrumentation().newActivity(
|
||||||
|
getClass().getClassLoader(),
|
||||||
|
Settings.SecurityDashboardActivity.class.getName(),
|
||||||
|
mDefaultIntent);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e); // nothing to do
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noAvailableAlternativeFragmentAvailable_defaultFragmentSet() {
|
||||||
|
when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
|
||||||
|
.thenReturn(false);
|
||||||
|
|
||||||
|
assertThat(mActivity.getInitialFragmentName(mDefaultIntent))
|
||||||
|
.isEqualTo(DEFAULT_FRAGMENT_CLASSNAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void alternativeFragmentAvailable_alternativeFragmentSet() {
|
||||||
|
when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
|
||||||
|
.thenReturn(true);
|
||||||
|
when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
|
||||||
|
.thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
|
||||||
|
|
||||||
|
assertThat(mActivity.getInitialFragmentName(mDefaultIntent))
|
||||||
|
.isEqualTo(ALTERNATIVE_FRAGMENT_CLASSNAME);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void noAvailableAlternativeFragmentAvailable_alternativeFragmentNotValid() {
|
||||||
|
when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
|
||||||
|
.thenReturn(false);
|
||||||
|
|
||||||
|
assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void alternativeFragmentAvailable_alternativeFragmentIsValid() {
|
||||||
|
when(mSecuritySettingsFeatureProvider.hasAlternativeSecuritySettingsFragment())
|
||||||
|
.thenReturn(true);
|
||||||
|
when(mSecuritySettingsFeatureProvider.getAlternativeSecuritySettingsFragmentClassname())
|
||||||
|
.thenReturn(ALTERNATIVE_FRAGMENT_CLASSNAME);
|
||||||
|
|
||||||
|
assertThat(mActivity.isValidFragment(ALTERNATIVE_FRAGMENT_CLASSNAME)).isTrue();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user