Merge "NAS settings change" into sc-dev
This commit is contained in:
@@ -37,7 +37,7 @@ public class NotificationAssistantDialogFragment extends InstrumentedDialogFragm
|
|||||||
final NotificationAssistantDialogFragment dialogFragment =
|
final NotificationAssistantDialogFragment dialogFragment =
|
||||||
new NotificationAssistantDialogFragment();
|
new NotificationAssistantDialogFragment();
|
||||||
final Bundle args = new Bundle();
|
final Bundle args = new Bundle();
|
||||||
args.putString(KEY_COMPONENT, cn.flattenToString());
|
args.putString(KEY_COMPONENT, cn == null ? "" : cn.flattenToString());
|
||||||
dialogFragment.setArguments(args);
|
dialogFragment.setArguments(args);
|
||||||
dialogFragment.setTargetFragment(target, 0);
|
dialogFragment.setTargetFragment(target, 0);
|
||||||
|
|
||||||
|
@@ -19,6 +19,7 @@ package com.android.settings.notification;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.UserHandle;
|
import android.os.UserHandle;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.fragment.app.Fragment;
|
import androidx.fragment.app.Fragment;
|
||||||
@@ -30,6 +31,7 @@ import com.google.common.annotations.VisibleForTesting;
|
|||||||
public class NotificationAssistantPreferenceController extends TogglePreferenceController {
|
public class NotificationAssistantPreferenceController extends TogglePreferenceController {
|
||||||
private static final String TAG = "NASPreferenceController";
|
private static final String TAG = "NASPreferenceController";
|
||||||
private static final int AVAILABLE = 1;
|
private static final int AVAILABLE = 1;
|
||||||
|
private final UserManager mUserManager;
|
||||||
private Fragment mFragment;
|
private Fragment mFragment;
|
||||||
private int mUserId = UserHandle.myUserId();
|
private int mUserId = UserHandle.myUserId();
|
||||||
|
|
||||||
@@ -41,6 +43,7 @@ public class NotificationAssistantPreferenceController extends TogglePreferenceC
|
|||||||
super(context, preferenceKey);
|
super(context, preferenceKey);
|
||||||
mNotificationBackend = backend;
|
mNotificationBackend = backend;
|
||||||
mFragment = fragment;
|
mFragment = fragment;
|
||||||
|
mUserManager = UserManager.get(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,8 +77,10 @@ public class NotificationAssistantPreferenceController extends TogglePreferenceC
|
|||||||
protected void setNotificationAssistantGranted(ComponentName cn) {
|
protected void setNotificationAssistantGranted(ComponentName cn) {
|
||||||
if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 0, mUserId) == 0) {
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, mUserId) == 0) {
|
||||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
for (int profileId : mUserManager.getProfileIds(mUserId, false)) {
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 1, mUserId);
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 1, profileId);
|
||||||
|
}
|
||||||
mNotificationBackend.resetDefaultNotificationAssistant(cn != null);
|
mNotificationBackend.resetDefaultNotificationAssistant(cn != null);
|
||||||
}
|
}
|
||||||
mNotificationBackend.setNotificationAssistantGranted(cn);
|
mNotificationBackend.setNotificationAssistantGranted(cn);
|
||||||
|
@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
@@ -30,12 +31,15 @@ import static org.mockito.Mockito.when;
|
|||||||
|
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.os.UserManager;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentManager;
|
import androidx.fragment.app.FragmentManager;
|
||||||
import androidx.fragment.app.FragmentTransaction;
|
import androidx.fragment.app.FragmentTransaction;
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
|
import com.android.settings.testutils.shadow.ShadowSecureSettings;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -43,6 +47,9 @@ import org.mockito.Answers;
|
|||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class NotificationAssistantPreferenceControllerTest {
|
public class NotificationAssistantPreferenceControllerTest {
|
||||||
@@ -58,6 +65,8 @@ public class NotificationAssistantPreferenceControllerTest {
|
|||||||
private FragmentTransaction mFragmentTransaction;
|
private FragmentTransaction mFragmentTransaction;
|
||||||
@Mock
|
@Mock
|
||||||
private NotificationBackend mBackend;
|
private NotificationBackend mBackend;
|
||||||
|
@Mock
|
||||||
|
private UserManager mUserManager;
|
||||||
private NotificationAssistantPreferenceController mPreferenceController;
|
private NotificationAssistantPreferenceController mPreferenceController;
|
||||||
ComponentName mNASComponent = new ComponentName("a", "b");
|
ComponentName mNASComponent = new ComponentName("a", "b");
|
||||||
|
|
||||||
@@ -65,12 +74,15 @@ public class NotificationAssistantPreferenceControllerTest {
|
|||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||||
|
ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUserManager);
|
||||||
doReturn(mContext).when(mFragment).getContext();
|
doReturn(mContext).when(mFragment).getContext();
|
||||||
when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
|
when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
|
||||||
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
|
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
|
||||||
when(mBackend.getDefaultNotificationAssistant()).thenReturn(mNASComponent);
|
when(mBackend.getDefaultNotificationAssistant()).thenReturn(mNASComponent);
|
||||||
mPreferenceController = new NotificationAssistantPreferenceController(mContext,
|
mPreferenceController = new NotificationAssistantPreferenceController(mContext,
|
||||||
mBackend, mFragment, KEY);
|
mBackend, mFragment, KEY);
|
||||||
|
when(mUserManager.getProfileIds(eq(0), anyBoolean())).thenReturn(new int[] {0, 10});
|
||||||
|
when(mUserManager.getProfileIds(eq(20), anyBoolean())).thenReturn(new int[] {20});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -96,14 +108,19 @@ public class NotificationAssistantPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMigrationFromSetting_userEnable() throws Exception {
|
@Config(shadows = ShadowSecureSettings.class)
|
||||||
|
public void testMigrationFromSetting_userEnable_multiProfile() throws Exception {
|
||||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
|
||||||
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10);
|
||||||
|
|
||||||
//Test user enable for the first time
|
//Test user enable for the first time
|
||||||
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
|
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
|
||||||
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
|
||||||
|
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10));
|
||||||
verify(mBackend, times(1))
|
verify(mBackend, times(1))
|
||||||
.resetDefaultNotificationAssistant(eq(true));
|
.resetDefaultNotificationAssistant(eq(true));
|
||||||
|
|
||||||
@@ -115,14 +132,43 @@ public class NotificationAssistantPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMigrationFromSetting_userDisable() throws Exception {
|
@Config(shadows = ShadowSecureSettings.class)
|
||||||
|
public void testMigrationFromSetting_userEnable_multiUser() throws Exception {
|
||||||
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
|
||||||
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 20);
|
||||||
|
|
||||||
|
//Test user 0 enable for the first time
|
||||||
|
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
|
||||||
|
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
|
||||||
|
assertEquals(0, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 20));
|
||||||
|
verify(mBackend, times(1))
|
||||||
|
.resetDefaultNotificationAssistant(eq(true));
|
||||||
|
|
||||||
|
//Test user enable again, migration should not happen
|
||||||
|
mPreferenceController.setNotificationAssistantGranted(mNASComponent);
|
||||||
|
//Number of invocations should not increase
|
||||||
|
verify(mBackend, times(1))
|
||||||
|
.resetDefaultNotificationAssistant(eq(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Config(shadows = ShadowSecureSettings.class)
|
||||||
|
public void testMigrationFromSetting_userDisable_multiProfile() throws Exception {
|
||||||
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0);
|
||||||
|
Settings.Secure.putIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10);
|
||||||
|
|
||||||
//Test user disable for the first time
|
//Test user disable for the first time
|
||||||
mPreferenceController.setChecked(false);
|
mPreferenceController.setChecked(false);
|
||||||
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 0));
|
||||||
|
assertEquals(1, Settings.Secure.getIntForUser(mContext.getContentResolver(),
|
||||||
|
Settings.Secure.NAS_SETTINGS_UPDATED, 0, 10));
|
||||||
verify(mBackend, times(1))
|
verify(mBackend, times(1))
|
||||||
.resetDefaultNotificationAssistant(eq(false));
|
.resetDefaultNotificationAssistant(eq(false));
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user