Search in sources :

Example 6 with PrimarySwitchPreference

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

the class ChannelListPreferenceController method findOrCreateChannelPrefForKey.

/**
 * Looks for the channel preference for the given channel's key at the expected index, if that
 * doesn't match, it checks all rows, and if it can't find that channel anywhere, it creates
 * the preference.
 */
@NonNull
private PrimarySwitchPreference findOrCreateChannelPrefForKey(@NonNull PreferenceGroup groupPrefGroup, @NonNull String key, int expectedIndex) {
    int preferenceCount = groupPrefGroup.getPreferenceCount();
    if (expectedIndex < preferenceCount) {
        Preference preference = groupPrefGroup.getPreference(expectedIndex);
        if (key.equals(preference.getKey())) {
            return (PrimarySwitchPreference) preference;
        }
    }
    for (int i = 0; i < preferenceCount; i++) {
        Preference preference = groupPrefGroup.getPreference(i);
        if (key.equals(preference.getKey())) {
            preference.setOrder(expectedIndex);
            return (PrimarySwitchPreference) preference;
        }
    }
    PrimarySwitchPreference channelPref = new PrimarySwitchPreference(mContext);
    channelPref.setOrder(expectedIndex);
    channelPref.setKey(key);
    groupPrefGroup.addPreference(channelPref);
    return channelPref;
}
Also used : SwitchPreference(androidx.preference.SwitchPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) Preference(androidx.preference.Preference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) NonNull(androidx.annotation.NonNull)

Example 7 with PrimarySwitchPreference

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

the class ChannelListPreferenceController method updateGroupPreferences.

private void updateGroupPreferences(@NonNull NotificationChannelGroup group, @NonNull PreferenceGroup groupPrefGroup) {
    int initialPrefCount = groupPrefGroup.getPreferenceCount();
    List<Preference> finalOrderedPrefs = new ArrayList<>();
    if (group.getId() == null) {
        // For the 'null' group, set the "Other" title.
        groupPrefGroup.setTitle(R.string.notification_channels_other);
    } else {
        // For an app-defined group, set their name and create a row to toggle 'isBlocked'.
        groupPrefGroup.setTitle(group.getName());
        finalOrderedPrefs.add(addOrUpdateGroupToggle(groupPrefGroup, group));
    }
    // Here "empty" means having no channel rows; the group toggle is ignored for this purpose.
    boolean initiallyEmpty = groupPrefGroup.getPreferenceCount() == finalOrderedPrefs.size();
    // For each channel, add or update the preference object.
    final List<NotificationChannel> channels = group.isBlocked() ? Collections.emptyList() : group.getChannels();
    Collections.sort(channels, CHANNEL_COMPARATOR);
    for (NotificationChannel channel : channels) {
        if (!TextUtils.isEmpty(channel.getConversationId()) && !channel.isDemoted()) {
            // conversations get their own section
            continue;
        }
        // Get or create the row, and populate its current state.
        PrimarySwitchPreference channelPref = findOrCreateChannelPrefForKey(groupPrefGroup, channel.getId(), /* expectedIndex */
        finalOrderedPrefs.size());
        updateSingleChannelPrefs(channelPref, channel, group.isBlocked());
        finalOrderedPrefs.add(channelPref);
    }
    int postAddPrefCount = groupPrefGroup.getPreferenceCount();
    // If any channels were inserted (into a non-empty list) or need to be removed, we need to
    // remove all preferences and re-add them all.
    // This is required to ensure proper ordering of inserted channels, and it simplifies logic
    // at the cost of computation in the rare case that the list is changing.
    int numFinalGroups = finalOrderedPrefs.size();
    boolean hasInsertions = !initiallyEmpty && initialPrefCount != numFinalGroups;
    boolean requiresRemoval = postAddPrefCount != numFinalGroups;
    if (hasInsertions || requiresRemoval) {
        groupPrefGroup.removeAll();
        for (Preference preference : finalOrderedPrefs) {
            groupPrefGroup.addPreference(preference);
        }
    }
}
Also used : NotificationChannel(android.app.NotificationChannel) SwitchPreference(androidx.preference.SwitchPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) Preference(androidx.preference.Preference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) ArrayList(java.util.ArrayList) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference)

Example 8 with PrimarySwitchPreference

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

the class AppChannelsBypassingDndPreferenceController method populateList.

private void populateList() {
    if (mPreferenceCategory == null) {
        return;
    }
    mPreferenceCategory.removeAll();
    mPreferenceCategory.addPreference(mAllNotificationsToggle);
    for (NotificationChannel channel : mChannels) {
        PrimarySwitchPreference channelPreference = new PrimarySwitchPreference(mContext);
        channelPreference.setDisabledByAdmin(mAdmin);
        channelPreference.setSwitchEnabled((mAdmin == null || !channelPreference.isDisabledByAdmin()) && isChannelConfigurable(channel) && showNotification(channel));
        channelPreference.setTitle(BidiFormatter.getInstance().unicodeWrap(channel.getName()));
        channelPreference.setChecked(showNotificationInDnd(channel));
        channelPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {

            @Override
            public boolean onPreferenceChange(Preference pref, Object val) {
                boolean switchOn = (Boolean) val;
                channel.setBypassDnd(switchOn);
                channel.lockFields(NotificationChannel.USER_LOCKED_PRIORITY);
                mBackend.updateChannel(mAppRow.pkg, mAppRow.uid, channel);
                mAllNotificationsToggle.setChecked(areAllChannelsBypassing());
                return true;
            }
        });
        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);
        channelPreference.setOnPreferenceClickListener(preference -> {
            new SubSettingLauncher(mContext).setDestination(ChannelNotificationSettings.class.getName()).setArguments(channelArgs).setUserHandle(UserHandle.of(mAppRow.userId)).setTitleRes(com.android.settings.R.string.notification_channel_title).setSourceMetricsCategory(SettingsEnums.DND_APPS_BYPASSING).launch();
            return true;
        });
        mPreferenceCategory.addPreference(channelPreference);
    }
    mAllNotificationsToggle.setChecked(areAllChannelsBypassing());
}
Also used : NotificationChannel(android.app.NotificationChannel) SwitchPreference(androidx.preference.SwitchPreference) RestrictedSwitchPreference(com.android.settingslib.RestrictedSwitchPreference) Preference(androidx.preference.Preference) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) SubSettingLauncher(com.android.settings.core.SubSettingLauncher) Bundle(android.os.Bundle) PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference)

Example 9 with PrimarySwitchPreference

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

the class PreventRingingParentPreferenceControllerTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mContext = spy(RuntimeEnvironment.application.getApplicationContext());
    when(mContext.getResources()).thenReturn(mResources);
    when(mResources.getInteger(com.android.internal.R.integer.config_keyChordPowerVolumeUp)).thenReturn(PreventRingingParentPreferenceController.KEY_CHORD_POWER_VOLUME_UP_MUTE_TOGGLE);
    mController = new PreventRingingParentPreferenceController(mContext, "test_key");
    mPreference = new PrimarySwitchPreference(mContext);
    when(mScreen.findPreference("test_key")).thenReturn(mPreference);
    mController.displayPreference(mScreen);
}
Also used : PrimarySwitchPreference(com.android.settings.widget.PrimarySwitchPreference) Before(org.junit.Before)

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