Implement a separate controller for ring volume

When ring volume is separated from notification, a new xml preferece and
controller is needed for it, so that the settings search can show/hide
the slice correctly.

1. Use a separate preference and controller for ring volume (vs ring &
notification combined)
2. Notification slice in settings no longer grays out when ringer mode
is set to mute or vibrate.
3. Introduce an abstract RingerModeAffected preference controller class
to factor out duplicate code among ring, notification, and separate-ring
controller classes.

Bug: b/259084354

Test:   make ROBOTEST_FILTER=RingVolumePreferenceControllerTest
RunSettingsRoboTests -j40
        make ROBOTEST_FILTER=SeparateRingVolumePreferenceControllerTest
RunSettingsRoboTests -j40
        make ROBOTEST_FILTER=NotificationVolumePreferenceControllerTest
RunSettingsRoboTests -j40
        make ROBOTEST_FILTER=VolumePanelTest RunSettingsRoboTests -j40
	make
ROBOTEST_FILTER=RingerModeAffectedVolumePreferenceControllerTest -j40

Known Issue:
1. When streams are separate and ring volume set to mute/vibrate,
notification is set to zero, but not disabled. So it can be turned on
by user (and in settings the icon will stay mute/vibrate instead of
changing to the normal notification icon).

2. In the above scenario after notification is unmuted in settings,
the notification icon continues to stay vibrate/mute -- should change
to the normal notification icon.

Note: This feature is controlled using a boolean DeviceConfig flag:
systemui/"volume_separate_ring". The default value is 'false', which is
meant to keep the experience the same as before. It will be set to
'true' for teamfood and dogfood. Eventually the flag will be removed and
the code in the 'true' branch will prevail.


Change-Id: Ibec871eafeef4081e96c5e0dd04535565d50a077
This commit is contained in:
Behnam Heydarshahi
2022-12-15 21:51:13 +00:00
parent e722587923
commit 4f87dd4b56
13 changed files with 615 additions and 288 deletions

View File

@@ -17,10 +17,8 @@
package com.android.settings.notification;
import android.app.ActivityThread;
import android.app.INotificationManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@@ -28,57 +26,42 @@ import android.media.AudioManager;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ServiceManager;
import android.os.Vibrator;
import android.provider.DeviceConfig;
import android.service.notification.NotificationListenerService;
import android.text.TextUtils;
import android.util.Log;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.preference.PreferenceScreen;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.config.sysui.SystemUiDeviceConfigFlags;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.Objects;
import java.util.Set;
/**
* Update notification volume icon in Settings in response to user adjusting volume.
*/
public class NotificationVolumePreferenceController extends VolumeSeekBarPreferenceController {
public class NotificationVolumePreferenceController extends
RingerModeAffectedVolumePreferenceController {
private static final String TAG = "NotificationVolumePreferenceController";
private static final String KEY_NOTIFICATION_VOLUME = "notification_volume";
private static final boolean CONFIG_DEFAULT_VAL = false;
private boolean mSeparateNotification;
private static final String TAG = "NotificationVolumePreferenceController";
private Vibrator mVibrator;
private int mRingerMode = AudioManager.RINGER_MODE_NORMAL;
private ComponentName mSuppressor;
private final RingReceiver mReceiver = new RingReceiver();
private final H mHandler = new H();
private INotificationManager mNoMan;
private int mMuteIcon;
private final int mNormalIconId = R.drawable.ic_notifications;
private final int mVibrateIconId = R.drawable.ic_volume_ringer_vibrate;
private final int mSilentIconId = R.drawable.ic_notifications_off_24dp;
public NotificationVolumePreferenceController(Context context) {
this(context, KEY_NOTIFICATION_VOLUME);
}
public NotificationVolumePreferenceController(Context context, String key) {
super(context, key);
super(context, key, TAG);
mVibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
if (mVibrator != null && !mVibrator.hasVibrator()) {
mVibrator = null;
}
mNormalIconId = R.drawable.ic_notifications;
mVibrateIconId = R.drawable.ic_volume_ringer_vibrate;
mSilentIconId = R.drawable.ic_notifications_off_24dp;
updateRingerMode();
}
@@ -94,13 +77,12 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
if (mPreference == null) {
setupVolPreference(screen);
}
mSeparateNotification = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, CONFIG_DEFAULT_VAL);
mSeparateNotification = isSeparateNotificationConfigEnabled();
if (mPreference != null) {
mPreference.setVisible(getAvailabilityStatus() == AVAILABLE);
}
updateEffectsSuppressor();
updatePreferenceIconAndSliderState();
selectPreferenceIconState();
}
/**
@@ -110,8 +92,7 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
Set<String> changeSet = properties.getKeyset();
if (changeSet.contains(SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION)) {
boolean newVal = properties.getBoolean(
SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, CONFIG_DEFAULT_VAL);
boolean newVal = isSeparateNotificationConfigEnabled();
if (newVal != mSeparateNotification) {
mSeparateNotification = newVal;
// manually hiding the preference because being unavailable does not do the job
@@ -143,8 +124,7 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
@Override
public int getAvailabilityStatus() {
boolean separateNotification = DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_SYSTEMUI,
SystemUiDeviceConfigFlags.VOLUME_SEPARATE_NOTIFICATION, false);
boolean separateNotification = isSeparateNotificationConfigEnabled();
return mContext.getResources().getBoolean(R.bool.config_show_notification_volume)
&& !mHelper.isSingleVolume()
@@ -152,72 +132,18 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public boolean isSliceable() {
return TextUtils.equals(getPreferenceKey(), KEY_NOTIFICATION_VOLUME);
}
@Override
public boolean isPublicSlice() {
return true;
}
@Override
public String getPreferenceKey() {
return KEY_NOTIFICATION_VOLUME;
}
@Override
public boolean useDynamicSliceSummary() {
return true;
}
@Override
public int getAudioStream() {
return AudioManager.STREAM_NOTIFICATION;
}
@Override
public int getMuteIcon() {
return mMuteIcon;
}
private void updateRingerMode() {
final int ringerMode = mHelper.getRingerModeInternal();
if (mRingerMode == ringerMode) return;
mRingerMode = ringerMode;
updatePreferenceIconAndSliderState();
}
private void updateEffectsSuppressor() {
final ComponentName suppressor = NotificationManager.from(mContext).getEffectsSuppressor();
if (Objects.equals(suppressor, mSuppressor)) return;
if (mNoMan == null) {
mNoMan = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE));
}
final int hints;
try {
hints = mNoMan.getHintsFromListenerNoToken();
} catch (android.os.RemoteException exception) {
Log.w(TAG, "updateEffectsSuppressor: " + exception.getLocalizedMessage());
return;
}
if (hintsMatch(hints)) {
mSuppressor = suppressor;
if (mPreference != null) {
final String text = SuppressorHelper.getSuppressionText(mContext, suppressor);
mPreference.setSuppressionText(text);
}
}
}
@VisibleForTesting
boolean hintsMatch(int hints) {
protected boolean hintsMatch(int hints) {
boolean allEffectsDisabled =
(hints & NotificationListenerService.HINT_HOST_DISABLE_EFFECTS) != 0;
boolean notificationEffectsDisabled =
@@ -226,20 +152,18 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
return allEffectsDisabled || notificationEffectsDisabled;
}
private void updatePreferenceIconAndSliderState() {
@Override
protected void selectPreferenceIconState() {
if (mPreference != null) {
if (mVibrator != null && mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
mMuteIcon = mVibrateIconId;
mPreference.showIcon(mVibrateIconId);
mPreference.setEnabled(false);
} else if (mRingerMode == AudioManager.RINGER_MODE_SILENT
|| mVibrator == null && mRingerMode == AudioManager.RINGER_MODE_VIBRATE) {
mMuteIcon = mSilentIconId;
mPreference.showIcon(mSilentIconId);
mPreference.setEnabled(false);
} else { // ringmode normal: could be that we are still silent
mPreference.setEnabled(true);
if (mHelper.getStreamVolume(AudioManager.STREAM_NOTIFICATION) == 0) {
// ring is in normal, but notification is in silent
mMuteIcon = mSilentIconId;
@@ -270,7 +194,7 @@ public class NotificationVolumePreferenceController extends VolumeSeekBarPrefere
updateRingerMode();
break;
case NOTIFICATION_VOLUME_CHANGED:
updatePreferenceIconAndSliderState();
selectPreferenceIconState();
break;
}
}