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;
}
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);
}
}
}
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());
}
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);
}
Aggregations