Only call super.updateState when work profile exists
Since the updateState() method is being called regardless of the availability of a work profile, we were trying to get settings for USER_NULL, which was leading to crashes. Flag: com.android.server.notification.polite_notifications Fixes: 402317471 Test: manual with existing work profile & without existing work profile Change-Id: Icddabd9a7575713f42a0f5bd955b65a7e46c7f45
This commit is contained in:
@@ -102,12 +102,12 @@ public class PoliteNotifWorkProfileToggleController extends TogglePreferenceCont
|
|||||||
return CONDITIONALLY_UNAVAILABLE;
|
return CONDITIONALLY_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (mManagedProfileId != UserHandle.USER_NULL) ? AVAILABLE : DISABLED_FOR_USER;
|
return hasManagedProfileUser() ? AVAILABLE : DISABLED_FOR_USER;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isChecked() {
|
public boolean isChecked() {
|
||||||
if (!isCoolDownEnabledForPrimary()) {
|
if (!isCoolDownEnabledForPrimary() || !hasManagedProfileUser()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Settings.System.getIntForUser(mContext.getContentResolver(),
|
return Settings.System.getIntForUser(mContext.getContentResolver(),
|
||||||
@@ -131,7 +131,13 @@ public class PoliteNotifWorkProfileToggleController extends TogglePreferenceCont
|
|||||||
public void updateState(@Nullable Preference preference) {
|
public void updateState(@Nullable Preference preference) {
|
||||||
if (preference == null) return;
|
if (preference == null) return;
|
||||||
preference.setVisible(isAvailable());
|
preference.setVisible(isAvailable());
|
||||||
super.updateState(preference);
|
if (isAvailable()) {
|
||||||
|
super.updateState(preference);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean hasManagedProfileUser() {
|
||||||
|
return mManagedProfileId != UserHandle.USER_NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isCoolDownEnabledForPrimary() {
|
private boolean isCoolDownEnabledForPrimary() {
|
||||||
|
@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.platform.test.annotations.EnableFlags;
|
||||||
import android.platform.test.flag.junit.SetFlagsRule;
|
import android.platform.test.flag.junit.SetFlagsRule;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
@@ -141,6 +142,17 @@ public class PoliteNotifWorkProfileToggleControllerTest {
|
|||||||
assertThat(mController.isChecked()).isFalse();
|
assertThat(mController.isChecked()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(shadows = ShadowSystemSettings.class)
|
||||||
|
@EnableFlags(Flags.FLAG_POLITE_NOTIFICATIONS)
|
||||||
|
public void isChecked_coolDownEnabled_noWorkProfile_shouldReturnFalse() {
|
||||||
|
when(mAudioHelper.getManagedProfileId(any())).thenReturn(UserHandle.USER_NULL);
|
||||||
|
mController = new PoliteNotifWorkProfileToggleController(mContext, PREFERENCE_KEY,
|
||||||
|
mAudioHelper);
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Config(shadows = ShadowSystemSettings.class)
|
@Config(shadows = ShadowSystemSettings.class)
|
||||||
public void setChecked_coolDownEnabled_setTrue_shouldEnablePoliteNotifForWorkProfile() {
|
public void setChecked_coolDownEnabled_setTrue_shouldEnablePoliteNotifForWorkProfile() {
|
||||||
@@ -186,7 +198,6 @@ public class PoliteNotifWorkProfileToggleControllerTest {
|
|||||||
assertThat(mPreference.isVisible()).isTrue();
|
assertThat(mPreference.isVisible()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void setCoolDownEnabled(boolean enabled) {
|
private void setCoolDownEnabled(boolean enabled) {
|
||||||
Settings.System.putInt(mContext.getContentResolver(),
|
Settings.System.putInt(mContext.getContentResolver(),
|
||||||
Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF));
|
Settings.System.NOTIFICATION_COOLDOWN_ENABLED, (enabled ? ON : OFF));
|
||||||
|
Reference in New Issue
Block a user