Fix broken Safety Center redirect when SafetySourceData is null

Bug: b/373942609
Test: m & atest CellularSecurityPreferenceControllerTest
Flag: EXEMPT bugfix
Change-Id: I2373ccb5cb51bca23815db9f7645efccc3e0cd88
This commit is contained in:
yomna
2024-12-06 04:44:36 +00:00
parent 3a40af147e
commit d4dc1fed9e
3 changed files with 22 additions and 19 deletions

View File

@@ -132,6 +132,7 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="android.permission.READ_APP_SPECIFIC_LOCALES" /> <uses-permission android:name="android.permission.READ_APP_SPECIFIC_LOCALES" />
<uses-permission android:name="android.permission.QUERY_ADMIN_POLICY" /> <uses-permission android:name="android.permission.QUERY_ADMIN_POLICY" />
<uses-permission android:name="android.permission.MANAGE_SAFETY_CENTER" />
<uses-permission android:name="android.permission.READ_SAFETY_CENTER_STATUS" /> <uses-permission android:name="android.permission.READ_SAFETY_CENTER_STATUS" />
<uses-permission android:name="android.permission.SEND_SAFETY_CENTER_UPDATE" /> <uses-permission android:name="android.permission.SEND_SAFETY_CENTER_UPDATE" />
<uses-permission android:name="android.permission.START_VIEW_APP_FEATURES" /> <uses-permission android:name="android.permission.START_VIEW_APP_FEATURES" />

View File

@@ -23,6 +23,7 @@ import android.content.pm.PackageManager;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.safetycenter.SafetyCenterManager; import android.safetycenter.SafetyCenterManager;
import android.safetycenter.SafetySourceData;
import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionInfo;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import android.text.TextUtils; import android.text.TextUtils;
@@ -48,6 +49,7 @@ import java.util.List;
public class CellularSecurityPreferenceController extends BasePreferenceController { public class CellularSecurityPreferenceController extends BasePreferenceController {
private static final String LOG_TAG = "CellularSecurityPreferenceController"; private static final String LOG_TAG = "CellularSecurityPreferenceController";
private static final String SAFETY_SOURCE_ID = "AndroidCellularNetworkSecurity";
private @Nullable TelephonyManager mTelephonyManager; private @Nullable TelephonyManager mTelephonyManager;
@@ -134,13 +136,30 @@ public class CellularSecurityPreferenceController extends BasePreferenceControll
if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) { if (!TextUtils.equals(preference.getKey(), getPreferenceKey())) {
return super.handlePreferenceTreeClick(preference); return super.handlePreferenceTreeClick(preference);
} }
boolean isSafetyCenterSupported = isSafetyCenterSupported(); if (!isSafetyCenterSupported()) {
if (isSafetyCenterSupported && areNotificationsEnabled()) { // Realistically, it's unlikely to end up in handlePreferenceTreeClick with SafetyCenter
// being not supported on the device.
return false;
}
// Need to check that both Safety Center is available on device, and also that the HALs are
// enabled before showing the Safety Center UI. Otherwise, we need to take them to the page
// where the HALs can be enabled.
SafetyCenterManager safetyCenterManager = mContext.getSystemService(
SafetyCenterManager.class);
SafetySourceData data = null;
if (safetyCenterManager.isSafetyCenterEnabled()) {
data = safetyCenterManager.getSafetySourceData(SAFETY_SOURCE_ID);
}
// Can only redirect to SafetyCenter if it has received data via the SafetySource, as
// SafetyCenter doesn't support redirecting to a specific page associated with a source
// if it hasn't received data from that source. See b/373942609 for details.
if (data != null && areNotificationsEnabled()) {
Intent safetyCenterIntent = new Intent(Intent.ACTION_SAFETY_CENTER); Intent safetyCenterIntent = new Intent(Intent.ACTION_SAFETY_CENTER);
safetyCenterIntent.putExtra(SafetyCenterManager.EXTRA_SAFETY_SOURCES_GROUP_ID, safetyCenterIntent.putExtra(SafetyCenterManager.EXTRA_SAFETY_SOURCES_GROUP_ID,
"AndroidCellularNetworkSecuritySources"); "AndroidCellularNetworkSecuritySources");
mContext.startActivity(safetyCenterIntent); mContext.startActivity(safetyCenterIntent);
} else { } else {
Log.v(LOG_TAG, "Hardware APIs not enabled, or data source is null.");
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putString(CellularSecuritySettingsFragment.KEY_CELLULAR_SECURITY_PREFERENCE, ""); bundle.putString(CellularSecuritySettingsFragment.KEY_CELLULAR_SECURITY_PREFERENCE, "");

View File

@@ -26,7 +26,6 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
@@ -50,7 +49,6 @@ import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
@@ -148,21 +146,6 @@ public final class CellularSecurityPreferenceControllerTest {
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
} }
@Test
public void handlePreferenceTreeClick_safetyCenterSupported_shouldRedirectToSafetyCenter() {
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
doReturn(true).when(mTelephonyManager).isNullCipherNotificationsEnabled();
doReturn(true).when(mTelephonyManager)
.isCellularIdentifierDisclosureNotificationsEnabled();
doReturn(true).when(mTelephonyManager).isNullCipherAndIntegrityPreferenceEnabled();
boolean prefHandled = mController.handlePreferenceTreeClick(mPreference);
assertThat(prefHandled).isTrue();
verify(mContext).startActivity(intentCaptor.capture());
assertThat(intentCaptor.getValue().getAction()).isEqualTo(Intent.ACTION_SAFETY_CENTER);
}
private void enableFlags(boolean enabled) { private void enableFlags(boolean enabled) {
if (enabled) { if (enabled) {
mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_MODEM_CIPHER_TRANSPARENCY_UNSOL_EVENTS); mSetFlagsRule.enableFlags(Flags.FLAG_ENABLE_MODEM_CIPHER_TRANSPARENCY_UNSOL_EVENTS);