Search in sources :

Example 1 with PrimarySwitchPreference

use of com.android.settings.widget.PrimarySwitchPreference in project android_packages_apps_Settings by omnirom.

the class ChannelListPreferenceControllerTest method testUpdateFullList_channelUpdates.

@Test
@UiThreadTest
public void testUpdateFullList_channelUpdates() {
    List<NotificationChannelGroup> inGroups = new ArrayList<>();
    NotificationChannelGroup inGroup = new NotificationChannelGroup("group", "Group");
    NotificationChannel channelA = new NotificationChannel("channelA", "Channel A", IMPORTANCE_HIGH);
    NotificationChannel channelB = new NotificationChannel("channelB", "Channel B", IMPORTANCE_NONE);
    inGroup.addChannel(channelA);
    inGroup.addChannel(channelB);
    inGroups.add(inGroup);
    NotificationsSentState sentA = new NotificationsSentState();
    sentA.avgSentDaily = 2;
    sentA.avgSentWeekly = 10;
    NotificationsSentState sentB = new NotificationsSentState();
    sentB.avgSentDaily = 0;
    sentB.avgSentWeekly = 2;
    mAppRow.sentByChannel.put("channelA", sentA);
    // Test that the channels' properties are reflected in the preference
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group", group.getKey());
        assertEquals(3, group.getPreferenceCount());
        assertNull(group.getPreference(0).getKey());
        assertEquals("All \"Group\" notifications", group.getPreference(0).getTitle());
        PrimarySwitchPreference channelAPref = (PrimarySwitchPreference) group.getPreference(1);
        assertEquals("channelA", channelAPref.getKey());
        assertEquals("Channel A", channelAPref.getTitle());
        assertEquals(Boolean.TRUE, channelAPref.getCheckedState());
        assertEquals("~2 notifications per day", channelAPref.getSummary());
        assertNotNull(channelAPref.getIcon());
        PrimarySwitchPreference channelBPref = (PrimarySwitchPreference) group.getPreference(2);
        assertEquals("channelB", channelBPref.getKey());
        assertEquals("Channel B", channelBPref.getTitle());
        assertEquals(Boolean.FALSE, channelBPref.getCheckedState());
        assertNull(channelBPref.getSummary());
        assertNull(channelBPref.getIcon());
    }
    channelA.setImportance(IMPORTANCE_NONE);
    channelB.setImportance(IMPORTANCE_DEFAULT);
    mAppRow.sentByChannel.remove("channelA");
    mAppRow.sentByChannel.put("channelB", sentB);
    // Test that changing the channels' properties correctly updates the preference
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group", group.getKey());
        assertEquals(3, group.getPreferenceCount());
        assertNull(group.getPreference(0).getKey());
        assertEquals("All \"Group\" notifications", group.getPreference(0).getTitle());
        PrimarySwitchPreference channelAPref = (PrimarySwitchPreference) group.getPreference(1);
        assertEquals("channelA", channelAPref.getKey());
        assertEquals("Channel A", channelAPref.getTitle());
        assertEquals(Boolean.FALSE, channelAPref.getCheckedState());
        assertNull(channelAPref.getSummary());
        assertNull(channelAPref.getIcon());
        PrimarySwitchPreference channelBPref = (PrimarySwitchPreference) group.getPreference(2);
        assertEquals("channelB", channelBPref.getKey());
        assertEquals("Channel B", channelBPref.getTitle());
        assertEquals(Boolean.TRUE, channelBPref.getCheckedState());
        assertEquals("~2 notifications per week", channelBPref.getSummary());
        assertNotNull(channelBPref.getIcon());
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) NotificationsSentState(com.android.settings.notification.NotificationBackend.NotificationsSentState) ArrayList(java.util.ArrayList) PreferenceGroup(androidx.preference.PreferenceGroup) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 2 with PrimarySwitchPreference

use of com.android.settings.widget.PrimarySwitchPreference in project android_packages_apps_Settings by omnirom.

the class ChannelListPreferenceControllerTest method testUpdateFullList_groupBlockedChange.

@Test
@UiThreadTest
public void testUpdateFullList_groupBlockedChange() {
    List<NotificationChannelGroup> inGroups = new ArrayList<>();
    NotificationChannelGroup inGroup = new NotificationChannelGroup("group", "My Group");
    inGroup.addChannel(new NotificationChannel("channelA", "Channel A", IMPORTANCE_DEFAULT));
    inGroup.addChannel(new NotificationChannel("channelB", "Channel B", IMPORTANCE_NONE));
    inGroups.add(inGroup);
    // Test that the group is initially showing all preferences
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group", group.getKey());
        assertEquals(3, group.getPreferenceCount());
        SwitchPreference groupBlockPref = (SwitchPreference) group.getPreference(0);
        assertNull(groupBlockPref.getKey());
        assertEquals("All \"My Group\" notifications", groupBlockPref.getTitle());
        assertTrue(groupBlockPref.isChecked());
        PrimarySwitchPreference channelAPref = (PrimarySwitchPreference) group.getPreference(1);
        assertEquals("channelA", channelAPref.getKey());
        assertEquals("Channel A", channelAPref.getTitle());
        assertEquals(Boolean.TRUE, channelAPref.getCheckedState());
        PrimarySwitchPreference channelBPref = (PrimarySwitchPreference) group.getPreference(2);
        assertEquals("channelB", channelBPref.getKey());
        assertEquals("Channel B", channelBPref.getTitle());
        assertEquals(Boolean.FALSE, channelBPref.getCheckedState());
    }
    // Test that when a group is blocked, the list removes its individual channel preferences
    inGroup.setBlocked(true);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group", group.getKey());
        assertEquals(1, group.getPreferenceCount());
        SwitchPreference groupBlockPref = (SwitchPreference) group.getPreference(0);
        assertNull(groupBlockPref.getKey());
        assertEquals("All \"My Group\" notifications", groupBlockPref.getTitle());
        assertFalse(groupBlockPref.isChecked());
    }
    // Test that when a group is unblocked, the list adds its individual channel preferences
    inGroup.setBlocked(false);
    mController.updateFullList(mGroupList, inGroups);
    {
        assertEquals(1, mGroupList.getPreferenceCount());
        PreferenceGroup group = (PreferenceGroup) mGroupList.getPreference(0);
        assertEquals("group", group.getKey());
        assertEquals(3, group.getPreferenceCount());
        SwitchPreference groupBlockPref = (SwitchPreference) group.getPreference(0);
        assertNull(groupBlockPref.getKey());
        assertEquals("All \"My Group\" notifications", groupBlockPref.getTitle());
        assertTrue(groupBlockPref.isChecked());
        PrimarySwitchPreference channelAPref = (PrimarySwitchPreference) group.getPreference(1);
        assertEquals("channelA", channelAPref.getKey());
        assertEquals("Channel A", channelAPref.getTitle());
        assertEquals(Boolean.TRUE, channelAPref.getCheckedState());
        PrimarySwitchPreference channelBPref = (PrimarySwitchPreference) group.getPreference(2);
        assertEquals("channelB", channelBPref.getKey());
        assertEquals("Channel B", channelBPref.getTitle());
        assertEquals(Boolean.FALSE, channelBPref.getCheckedState());
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) NotificationChannelGroup(android.app.NotificationChannelGroup) SwitchPreference(androidx.preference.SwitchPreference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) ArrayList(java.util.ArrayList) PreferenceGroup(androidx.preference.PreferenceGroup) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 3 with PrimarySwitchPreference

use of com.android.settings.widget.PrimarySwitchPreference in project android_packages_apps_Settings by omnirom.

the class ChannelListPreferenceController method updateSingleChannelPrefs.

/**
 * Update the properties of the channel preference with the values from the channel object.
 */
private void updateSingleChannelPrefs(@NonNull final PrimarySwitchPreference channelPref, @NonNull final NotificationChannel channel, final boolean groupBlocked) {
    channelPref.setSwitchEnabled(mAdmin == null && isChannelBlockable(channel) && isChannelConfigurable(channel) && !groupBlocked);
    if (channel.getImportance() > IMPORTANCE_LOW) {
        channelPref.setIcon(getAlertingIcon());
    } else {
        channelPref.setIcon(R.drawable.empty_icon);
    }
    channelPref.setIconSize(PrimarySwitchPreference.ICON_SIZE_SMALL);
    channelPref.setTitle(channel.getName());
    channelPref.setSummary(NotificationBackend.getSentSummary(mContext, mAppRow.sentByChannel.get(channel.getId()), false));
    channelPref.setChecked(channel.getImportance() != IMPORTANCE_NONE);
    Bundle channelArgs = new Bundle();
    channelArgs.putInt(AppInfoBase.ARG_PACKAGE_UID, mAppRow.uid);
    channelArgs.putString(AppInfoBase.ARG_PACKAGE_NAME, mAppRow.pkg);
    channelArgs.putString(Settings.EXTRA_CHANNEL_ID, channel.getId());
    channelArgs.putBoolean(ARG_FROM_SETTINGS, true);
    channelPref.setIntent(new SubSettingLauncher(mContext).setDestination(ChannelNotificationSettings.class.getName()).setArguments(channelArgs).setTitleRes(R.string.notification_channel_title).setSourceMetricsCategory(SettingsEnums.NOTIFICATION_APP_NOTIFICATION).toIntent());
    channelPref.setOnPreferenceChangeListener((preference, o) -> {
        boolean value = (Boolean) o;
        int importance = value ? Math.max(channel.getOriginalImportance(), IMPORTANCE_LOW) : IMPORTANCE_NONE;
        channel.setImportance(importance);
        channel.lockFields(NotificationChannel.USER_LOCKED_IMPORTANCE);
        PrimarySwitchPreference channelPref1 = (PrimarySwitchPreference) preference;
        channelPref1.setIcon(R.drawable.empty_icon);
        if (channel.getImportance() > IMPORTANCE_LOW) {
            channelPref1.setIcon(getAlertingIcon());
        }
        mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, channel);
        return true;
    });
}
Also used : SubSettingLauncher(com.android.settings.core.SubSettingLauncher) Bundle(android.os.Bundle) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference)

Example 4 with PrimarySwitchPreference

use of com.android.settings.widget.PrimarySwitchPreference in project android_packages_apps_Settings by omnirom.

the class WifiTetherPreferenceControllerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mContext = spy(RuntimeEnvironment.application);
    mLifecycleOwner = () -> mLifecycle;
    mLifecycle = new Lifecycle(mLifecycleOwner);
    FakeFeatureFactory.setupForTest();
    mPreference = new PrimarySwitchPreference(RuntimeEnvironment.application);
    when(mContext.getSystemService(Context.TETHERING_SERVICE)).thenReturn(mTetheringManager);
    when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(mWifiManager);
    when(mScreen.findPreference(anyString())).thenReturn(mPreference);
    mSoftApConfiguration = new SoftApConfiguration.Builder().setSsid(SSID).build();
    when(mWifiManager.getSoftApConfiguration()).thenReturn(mSoftApConfiguration);
    when(mTetheringManager.getTetherableWifiRegexs()).thenReturn(new String[] { "1", "2" });
    mController = new WifiTetherPreferenceController(mContext, mLifecycle, false);
    mController.displayPreference(mScreen);
}
Also used : Lifecycle(com.android.settingslib.core.lifecycle.Lifecycle) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) SoftApConfiguration(android.net.wifi.SoftApConfiguration) Before(org.junit.Before)

Example 5 with PrimarySwitchPreference

use of com.android.settings.widget.PrimarySwitchPreference in project android_packages_apps_Settings by omnirom.

the class AppChannelsBypassingDndPreferenceController method displayPreference.

@Override
public void displayPreference(PreferenceScreen screen) {
    mPreferenceCategory = screen.findPreference(KEY);
    mAllNotificationsToggle = new RestrictedSwitchPreference(mPreferenceCategory.getContext());
    mAllNotificationsToggle.setTitle(R.string.zen_mode_bypassing_app_channels_toggle_all);
    mAllNotificationsToggle.setDisabledByAdmin(mAdmin);
    mAllNotificationsToggle.setEnabled((mAdmin == null || !mAllNotificationsToggle.isDisabledByAdmin()));
    mAllNotificationsToggle.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference pref) {
            SwitchPreference preference = (SwitchPreference) pref;
            final boolean bypassDnd = preference.isChecked();
            for (NotificationChannel channel : mChannels) {
                if (showNotification(channel) && isChannelConfigurable(channel)) {
                    channel.setBypassDnd(bypassDnd);
                    channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
                    mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, channel);
                }
            }
            // toggle all notifications from this app to bypass DND
            for (int i = 1; i < mPreferenceCategory.getPreferenceCount(); i++) {
                PrimarySwitchPreference childPreference = (PrimarySwitchPreference) mPreferenceCategory.getPreference(i);
                childPreference.setChecked(showNotificationInDnd(mChannels.get(i - 1)));
            }
            return true;
        }
    });
    loadAppChannels();
    super.displayPreference(screen);
}
Also used : NotificationChannel(android.app.NotificationChannel) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) SwitchPreference(androidx.preference.SwitchPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) Preference(androidx.preference.Preference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) SwitchPreference(androidx.preference.SwitchPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference)

Aggregations

PrimarySwitchPreference (com.android.settings.widget.PrimarySwitchPreference)9 NotificationChannel (android.app.NotificationChannel)5 SwitchPreference (androidx.preference.SwitchPreference)5 Preference (androidx.preference.Preference)4 RestrictedSwitchPreference (com.android.settingslib.RestrictedSwitchPreference)4 ArrayList (java.util.ArrayList)3 NotificationChannelGroup (android.app.NotificationChannelGroup)2 Bundle (android.os.Bundle)2 PreferenceGroup (androidx.preference.PreferenceGroup)2 UiThreadTest (androidx.test.annotation.UiThreadTest)2 SmallTest (androidx.test.filters.SmallTest)2 SubSettingLauncher (com.android.settings.core.SubSettingLauncher)2 Before (org.junit.Before)2 Test (org.junit.Test)2 SoftApConfiguration (android.net.wifi.SoftApConfiguration)1 NonNull (androidx.annotation.NonNull)1 NotificationsSentState (com.android.settings.notification.NotificationBackend.NotificationsSentState)1 Lifecycle (com.android.settingslib.core.lifecycle.Lifecycle)1