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

Commit 5a96c12a authored by Danny Baumann's avatar Danny Baumann
Browse files

Add option to hide music controls in lockscreen.

JIRA:CYAN-1566
Change-Id: Iae3dd9914b1685c6bf1e39e421372ce0bdfb93cc
parent 9e3fa9cb
Loading
Loading
Loading
Loading
+7 −0
Original line number Original line Diff line number Diff line
@@ -2413,6 +2413,13 @@ public final class Settings {
         */
         */
        public static final String LOCKSCREEN_TARGETS = "lockscreen_targets";
        public static final String LOCKSCREEN_TARGETS = "lockscreen_targets";


        /**
         * Whether music controls should be shown on the lockscreen if a supporting
         * music player is active.
         * @hide
         */
        public static final String LOCKSCREEN_MUSIC_CONTROLS = "lockscreen_music_controls";

        /**
        /**
         * @deprecated Use {@link android.provider.Settings.Global#LOW_BATTERY_SOUND}
         * @deprecated Use {@link android.provider.Settings.Global#LOW_BATTERY_SOUND}
         * instead
         * instead
+65 −9
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.app.PendingIntent.CanceledException;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.database.ContentObserver;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.media.AudioManager;
import android.media.IRemoteControlDisplay;
import android.media.IRemoteControlDisplay;
@@ -32,6 +33,8 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.Spannable;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.ForegroundColorSpan;
@@ -75,6 +78,8 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
    private AudioManager mAudioManager;
    private AudioManager mAudioManager;
    private IRemoteControlDisplayWeak mIRCD;
    private IRemoteControlDisplayWeak mIRCD;
    private boolean mMusicClientPresent = true;
    private boolean mMusicClientPresent = true;
    private boolean mShouldBeShown = true;
    private boolean mAttachNotified = false;


    /**
    /**
     * The metadata which should be populated into the view once we've been attached
     * The metadata which should be populated into the view once we've been attached
@@ -126,6 +131,20 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
    };
    };
    private KeyguardHostView.TransportCallback mTransportCallback;
    private KeyguardHostView.TransportCallback mTransportCallback;


    private ContentObserver mSettingsObserver = new ContentObserver(mHandler) {
        @Override
        public void onChange(boolean selfChange) {
            updateSettings();
        }
    };

    KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {
        @Override
        public void onUserSwitched(int userId) {
            updateSettings();
        }
    };

    /**
    /**
     * This class is required to have weak linkage to the current TransportControlView
     * This class is required to have weak linkage to the current TransportControlView
     * because the remote process can hold a strong reference to this binder object and
     * because the remote process can hold a strong reference to this binder object and
@@ -198,20 +217,48 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
    protected void onListenerDetached() {
    protected void onListenerDetached() {
        mMusicClientPresent = false;
        mMusicClientPresent = false;
        if (DEBUG) Log.v(TAG, "onListenerDetached()");
        if (DEBUG) Log.v(TAG, "onListenerDetached()");
        if (mTransportCallback != null) {
        callCallbackIfNeeded();
            mTransportCallback.onListenerDetached();
        } else {
            Log.w(TAG, "onListenerDetached: no callback");
        }
    }
    }


    private void onListenerAttached() {
    private void onListenerAttached() {
        mMusicClientPresent = true;
        mMusicClientPresent = true;
        if (DEBUG) Log.v(TAG, "onListenerAttached()");
        if (DEBUG) Log.v(TAG, "onListenerAttached()");
        if (mTransportCallback != null) {
        callCallbackIfNeeded();
    }

    private void updateSettings() {
        boolean oldShown = mShouldBeShown;
        mShouldBeShown = Settings.System.getIntForUser(mContext.getContentResolver(),
                Settings.System.LOCKSCREEN_MUSIC_CONTROLS, 1, UserHandle.USER_CURRENT) != 0;
        if (DEBUG) Log.v(TAG, "updateSettings(): mShouldBeShown=" + mShouldBeShown);
        if (oldShown != mShouldBeShown) {
            callCallbackIfNeeded();
            if (mShouldBeShown && mMusicClientPresent
                    && mCurrentPlayState != RemoteControlClient.PLAYSTATE_NONE) {
                // send out the play state change event that we suppressed earlier
                mTransportCallback.onPlayStateChanged();
            }
        }
    }

    private void callCallbackIfNeeded() {
        if (mTransportCallback == null) {
            Log.w(TAG, "callCallbackIfNeeded: no callback");
            return;
        }

        boolean shouldBeAttached = mMusicClientPresent && mShouldBeShown;
        if (DEBUG) {
            Log.v(TAG, "callCallbackIfNeeded(): shouldBeAttached=" + shouldBeAttached +
                    ", mAttachNotified=" + mAttachNotified);
        }

        if (shouldBeAttached && !mAttachNotified) {
            mTransportCallback.onListenerAttached();
            mTransportCallback.onListenerAttached();
        } else {
            mAttachNotified = true;
            Log.w(TAG, "onListenerAttached(): no callback");
        } else if (!shouldBeAttached && mAttachNotified) {
            mTransportCallback.onListenerDetached();
            mAttachNotified = false;
        }
        }
    }
    }


@@ -245,6 +292,11 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
        if (!mAttached) {
        if (!mAttached) {
            if (DEBUG) Log.v(TAG, "Registering TCV " + this);
            if (DEBUG) Log.v(TAG, "Registering TCV " + this);
            mAudioManager.registerRemoteControlDisplay(mIRCD);
            mAudioManager.registerRemoteControlDisplay(mIRCD);
            KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mUpdateCallback);
            mContext.getContentResolver().registerContentObserver(
                    Settings.System.getUriFor(Settings.System.LOCKSCREEN_MUSIC_CONTROLS),
                    false, mSettingsObserver);
            updateSettings();
        }
        }
        mAttached = true;
        mAttached = true;
    }
    }
@@ -256,6 +308,8 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
        if (mAttached) {
        if (mAttached) {
            if (DEBUG) Log.v(TAG, "Unregistering TCV " + this);
            if (DEBUG) Log.v(TAG, "Unregistering TCV " + this);
            mAudioManager.unregisterRemoteControlDisplay(mIRCD);
            mAudioManager.unregisterRemoteControlDisplay(mIRCD);
            KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mUpdateCallback);
            mContext.getContentResolver().unregisterContentObserver(mSettingsObserver);
        }
        }
        mAttached = false;
        mAttached = false;
    }
    }
@@ -381,8 +435,10 @@ public class KeyguardTransportControlView extends FrameLayout implements OnClick
        mBtnPlay.setImageResource(imageResId);
        mBtnPlay.setImageResource(imageResId);
        mBtnPlay.setContentDescription(getResources().getString(imageDescId));
        mBtnPlay.setContentDescription(getResources().getString(imageDescId));
        mCurrentPlayState = state;
        mCurrentPlayState = state;
        if (mShouldBeShown) {
            mTransportCallback.onPlayStateChanged();
            mTransportCallback.onPlayStateChanged();
        }
        }
    }


    static class SavedState extends BaseSavedState {
    static class SavedState extends BaseSavedState {
        boolean clientPresent;
        boolean clientPresent;