Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b4e37e48 authored by Danesh M's avatar Danesh M Committed by Steve Kondik
Browse files

Add ability to ignore interruptions while active media playback

Change-Id: I162066593edbcafecfbdc54d042938edcffc17ab
parent c078f3b9
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -8354,6 +8354,12 @@ public final class Settings {
         */
        public static final String POLICY_CONTROL = "policy_control";

        /**
         * @hide
         */
        public static final String ZEN_DISABLE_DUCKING_DURING_MEDIA_PLAYBACK =
                "zen_disable_ducking_during_media_playback";

        /**
         * Defines global zen mode.  ZEN_MODE_OFF, ZEN_MODE_IMPORTANT_INTERRUPTIONS,
         * or ZEN_MODE_NO_INTERRUPTIONS.
+47 −5
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
import static org.xmlpull.v1.XmlPullParser.END_TAG;
import static org.xmlpull.v1.XmlPullParser.START_TAG;

import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityManagerNative;
import android.app.AppGlobals;
@@ -61,6 +62,9 @@ import android.media.AudioManager;
import android.media.AudioManagerInternal;
import android.media.AudioSystem;
import android.media.IRingtonePlayer;
import android.media.session.MediaController;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
@@ -306,6 +310,8 @@ public class NotificationManagerService extends SystemService {
    private NotificationListeners mListeners;
    private ConditionProviders mConditionProviders;
    private NotificationUsageStats mUsageStats;
    private boolean mDisableDuckingWhileMedia;
    private boolean mActiveMedia;

    private static final int MY_UID = Process.myUid();
    private static final int MY_PID = Process.myPid();
@@ -834,13 +840,13 @@ public class NotificationManagerService extends SystemService {
        }
    };

    private final class LEDSettingsObserver extends ContentObserver {
    class SettingsObserver extends ContentObserver {
        private final Uri NOTIFICATION_LIGHT_PULSE_URI
                = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
        private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
                = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);

        LEDSettingsObserver(Handler handler) {
        SettingsObserver(Handler handler) {
            super(handler);
        }

@@ -865,6 +871,9 @@ public class NotificationManagerService extends SystemService {
            resolver.registerContentObserver(Settings.System.getUriFor(
                    Settings.System.NOTIFICATION_LIGHT_PULSE_CUSTOM_VALUES),
                    false, this, UserHandle.USER_ALL);
            resolver.registerContentObserver(Settings.Global.getUriFor(
                    Settings.Global.ZEN_DISABLE_DUCKING_DURING_MEDIA_PLAYBACK), false,
                    this, UserHandle.USER_ALL);
            update(null);
        }

@@ -905,10 +914,26 @@ public class NotificationManagerService extends SystemService {
            }

            updateNotificationPulse();

            mDisableDuckingWhileMedia = Settings.Global.getInt(resolver,
                    Settings.Global.ZEN_DISABLE_DUCKING_DURING_MEDIA_PLAYBACK, 0) == 1;
            updateDisableDucking();
        }
    }

    private LEDSettingsObserver mSettingsObserver;
    private void updateDisableDucking() {
        if (!mSystemReady) {
            return;
        }
        final MediaSessionManager mediaSessionManager = (MediaSessionManager) getContext()
                .getSystemService(Context.MEDIA_SESSION_SERVICE);
        mediaSessionManager.removeOnActiveSessionsChangedListener(mSessionListener);
        if (mDisableDuckingWhileMedia) {
            mediaSessionManager.addOnActiveSessionsChangedListener(mSessionListener, null);
        }
    }

    private SettingsObserver mSettingsObserver;
    private ZenModeHelper mZenModeHelper;

    private final Runnable mBuzzBeepBlinked = new Runnable() {
@@ -1063,7 +1088,7 @@ public class NotificationManagerService extends SystemService {
        getContext().registerReceiverAsUser(mPackageIntentReceiver, UserHandle.ALL, sdFilter, null,
                null);

        mSettingsObserver = new LEDSettingsObserver(mHandler);
        mSettingsObserver = new SettingsObserver(mHandler);
        mSettingsObserver.observe();

        mArchive = new Archive(resources.getInteger(
@@ -1107,6 +1132,8 @@ public class NotificationManagerService extends SystemService {
            mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
            mAudioManagerInternal = getLocalService(AudioManagerInternal.class);
            mZenModeHelper.onSystemReady();

            updateDisableDucking();
        } else if (phase == SystemService.PHASE_THIRD_PARTY_APPS_CAN_START) {
            // This observer will force an update when observe is called, causing us to
            // bind to listener services.
@@ -2411,6 +2438,21 @@ public class NotificationManagerService extends SystemService {
        return false;
    }

    private MediaSessionManager.OnActiveSessionsChangedListener mSessionListener =
            new MediaSessionManager.OnActiveSessionsChangedListener() {
        @Override
        public void onActiveSessionsChanged(@Nullable List<MediaController> controllers) {
            for (MediaController activeSession : controllers) {
                PlaybackState playbackState = activeSession.getPlaybackState();
                if (playbackState != null && playbackState.getState() == PlaybackState.STATE_PLAYING) {
                    mActiveMedia = true;
                    return;
                }
            }
            mActiveMedia = false;
        }
    };

    private void buzzBeepBlinkLocked(NotificationRecord record) {
        boolean buzz = false;
        boolean beep = false;
@@ -2480,7 +2522,7 @@ public class NotificationManagerService extends SystemService {
                hasValidSound = (soundUri != null);
            }

            if (hasValidSound) {
            if (hasValidSound && (!mDisableDuckingWhileMedia || !mActiveMedia)) {
                boolean looping =
                        (notification.flags & Notification.FLAG_INSISTENT) != 0;
                AudioAttributes audioAttributes = audioAttributesForNotification(notification);