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

Commit 1a5ba284 authored by Danesh M's avatar Danesh M Committed by Abhisek Devkota
Browse files

Add ability to ignore interruptions while active media playback

Change-Id: I162066593edbcafecfbdc54d042938edcffc17ab
parent fbbf7e1a
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -7674,6 +7674,12 @@ public final class Settings {
         */
        public static final String POLICY_CONTROL_STYLE = "policy_control_style";

        /**
         * @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.
+48 −6
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;
@@ -34,7 +35,6 @@ import android.app.KeyguardManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Profile;
import android.app.ProfileGroup;
import android.app.ProfileManager;
import android.app.StatusBarManager;
@@ -57,6 +57,9 @@ import android.media.AudioAttributes;
import android.media.AudioManager;
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.Bundle;
@@ -126,6 +129,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
@@ -269,6 +273,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();
@@ -783,13 +789,13 @@ public class NotificationManagerService extends SystemService {
        }
    };

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

@@ -814,6 +820,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);
        }

@@ -854,10 +863,26 @@ public class NotificationManagerService extends SystemService {
            }

            updateNotificationPulse();

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

    private LEDSettingsObserver mSettingsObserver;
    private void updateDisableDucking() {
        if (!mSystemReady) {
            return;
        }
        final MediaSessionManager mediaSessionManager = (MediaSessionManager) mContext
                .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() {
@@ -999,7 +1024,7 @@ public class NotificationManagerService extends SystemService {
        IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
        getContext().registerReceiver(mIntentReceiver, sdFilter);

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

        mArchive = new Archive(resources.getInteger(
@@ -1037,6 +1062,8 @@ public class NotificationManagerService extends SystemService {
            // Grab our optional AudioService
            mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
            mZenModeHelper.setAudioManager(mAudioManager);

            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.
@@ -1949,6 +1976,21 @@ public class NotificationManagerService extends SystemService {
        idOut[0] = id;
    }

    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 buzzBeepBlinked = false;
        final Notification notification = record.sbn.getNotification();
@@ -2021,7 +2063,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;