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

Commit 264a86ba authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

SystemUI: improve lockscreen tile behavior



* Stay disabled when disabled
* No need to use Keyguard locks, use internal update monitor

Change-Id: I9e66458d5789aa45b933ca60643e049d932ce58b
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 7f59eb0e
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@
    <string name="quick_settings_compass_value" translatable="false"><xliff:g id="degrees">%1$.0f</xliff:g>\u00b0 <xliff:g id="direction">%2$s</xliff:g></string>
    <string name="quick_settings_compass_init">Initializing\u2026</string>
    <string name="quick_settings_lockscreen_label">Lock screen</string>
    <string name="quick_settings_lockscreen_label_enforced">Lock screen enforced</string>
    <string name="quick_settings_network_adb_label">ADB over network</string>
    <string name="quick_settings_lte_label">LTE</string>
    <string name="quick_settings_visualizer_label" translatable="false">AudioFX</string>
+47 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.app.Profile;
import android.app.ProfileManager;
import android.app.SearchManager;
import android.app.StatusBarManager;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
@@ -252,6 +253,11 @@ public class KeyguardViewMediator extends SystemUI {
     * */
    private boolean mHiding;

    /**
     * Whether we are disabling the lock screen internally
     */
    private boolean mInternallyDisabled = false;

    /**
     * we send this intent when the keyguard is dismissed.
     */
@@ -743,6 +749,23 @@ public class KeyguardViewMediator extends SystemUI {
        }
    }

    /**
     * Set the internal keyguard enabled state. This allows SystemUI to disable the lockscreen,
     * overriding any apps.
     * @param enabled
     */
    public void setKeyguardEnabledInternal(boolean enabled) {
        mInternallyDisabled = !enabled;
        setKeyguardEnabled(enabled);
        if (mInternallyDisabled) {
            mNeedToReshowWhenReenabled = false;
        }
    }

    public boolean getKeyguardEnabledInternal() {
        return !mInternallyDisabled;
    }

    /**
     * Same semantics as {@link android.view.WindowManagerPolicy#enableKeyguard}; provide
     * a way for external stuff to override normal keyguard behavior.  For instance
@@ -752,6 +775,12 @@ public class KeyguardViewMediator extends SystemUI {
        synchronized (this) {
            if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");

            if (mInternallyDisabled && enabled && !lockscreenEnforcedByDevicePolicy()) {
                // if keyguard is forcefully disabled internally (by lock screen tile), don't allow
                // it to be enabled externally, unless the device policy manager says so.
                return;
            }

            mExternallyEnabled = enabled;

            if (!enabled && mShowing) {
@@ -974,6 +1003,24 @@ public class KeyguardViewMediator extends SystemUI {
        return simLockedOrMissing;
    }

    public boolean lockscreenEnforcedByDevicePolicy() {
        DevicePolicyManager dpm = (DevicePolicyManager)
                mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
        if (dpm != null) {
            int passwordQuality = dpm.getPasswordQuality(null);
            switch (passwordQuality) {
                case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
                case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
                case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
                case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
                case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
                case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
                    return true;
            }
        }
        return false;
    }

    /**
     * Dismiss the keyguard through the security layers.
     */
+37 −45
Original line number Diff line number Diff line
@@ -16,14 +16,17 @@

package com.android.systemui.qs.tiles;

import android.app.KeyguardManager;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;

import android.widget.Toast;
import com.android.systemui.R;
import com.android.systemui.SystemUIApplication;
import com.android.systemui.keyguard.KeyguardViewMediator;
import com.android.systemui.qs.QSTile;
import com.android.systemui.statusbar.policy.KeyguardMonitor;

@@ -34,18 +37,20 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
            "com.android.internal.action.KEYGUARD_SERVICE_STATE_CHANGED";
    private static final String KEYGUARD_SERVICE_EXTRA_ACTIVE = "active";

    private static final String KEY_DISABLED = "lockscreen_disabled";
    private static final String KEY_ENABLED = "lockscreen_enabled";

    private KeyguardViewMediator mKeyguardViewMediator;
    private KeyguardMonitor mKeyguard;
    private KeyguardManager.KeyguardLock mLock;
    private boolean mLockscreenDisabled;
    private boolean mPersistedState;
    private boolean mKeyguardBound;
    private SharedPreferences mPrefs;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateBasedOnIntent(intent);
            mKeyguardBound = intent.getBooleanExtra(KEYGUARD_SERVICE_EXTRA_ACTIVE, false);
            applyLockscreenState();
            refreshState();
        }
    };

@@ -54,13 +59,12 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
        mPrefs = mContext.getSharedPreferences("quicksettings", Context.MODE_PRIVATE);

        mKeyguard = host.getKeyguardMonitor();
        mLockscreenDisabled = getPersistedState();
        mKeyguardViewMediator =
                ((SystemUIApplication) mContext.getApplicationContext()).getComponent(KeyguardViewMediator.class);
        mPersistedState = getPersistedState();

        IntentFilter filter = new IntentFilter(KEYGUARD_SERVICE_ACTION_STATE_CHANGE);
        Intent i = mContext.registerReceiver(mReceiver, filter);
        if (i != null) {
            updateBasedOnIntent(i);
        }
        mContext.registerReceiver(mReceiver, filter);
    }

    @Override
@@ -79,31 +83,32 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>

    @Override
    protected void handleClick() {
        setLockscreenEnabled(!mLockscreenDisabled);
        setPersistedState(!mPersistedState);
        applyLockscreenState();
        refreshState();
    }

    @Override
    protected void handleUpdateState(BooleanState state, Object arg) {
        boolean hideTile = !mLockscreenDisabled
                && mKeyguard.isShowing() && mKeyguard.isSecure();

        state.visible = mKeyguardBound && !hideTile;
        state.label = mContext.getString(R.string.quick_settings_lockscreen_label);
        state.iconId = mKeyguardBound && mLockscreenDisabled
                ? R.drawable.ic_qs_lock_screen_off
                : R.drawable.ic_qs_lock_screen_on;
        final boolean lockscreenEnforced = mKeyguardViewMediator.lockscreenEnforcedByDevicePolicy();
        final boolean lockscreenEnabled = lockscreenEnforced
                || mPersistedState
                || mKeyguardViewMediator.getKeyguardEnabledInternal();

        state.visible = mKeyguardBound
                && !(mKeyguard.isShowing() && mKeyguard.isSecure());
        state.label = mContext.getString(lockscreenEnforced
                ? R.string.quick_settings_lockscreen_label_enforced
                : R.string.quick_settings_lockscreen_label);
        state.iconId = lockscreenEnabled
                ? R.drawable.ic_qs_lock_screen_on
                : R.drawable.ic_qs_lock_screen_off;
    }

    @Override
    public void destroy() {
        super.destroy();
    protected void handleDestroy() {
        super.handleDestroy();
        mContext.unregisterReceiver(mReceiver);
        if (mLock != null) {
            mLock.reenableKeyguard();
            mLock = null;
        }
    }

    @Override
@@ -111,34 +116,21 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
        refreshState();
    }

    private void updateBasedOnIntent(Intent intent) {
        mKeyguardBound = intent.getBooleanExtra(KEYGUARD_SERVICE_EXTRA_ACTIVE, false);
        applyLockscreenState();
    }

    private void applyLockscreenState() {
        if (!mKeyguardBound) {
            // do nothing yet
            return;
        }
        if (mLock == null) {
            KeyguardManager kgm = (KeyguardManager)
                    mContext.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
            mLock = kgm.newKeyguardLock(LockscreenToggleTile.class.getSimpleName());
        }
        if (mLockscreenDisabled) {
            mLock.disableKeyguard();
        } else {
            mLock.reenableKeyguard();
        }
        refreshState();

        mKeyguardViewMediator.setKeyguardEnabledInternal(mPersistedState);
    }

    private boolean getPersistedState() {
        return mPrefs.getBoolean(KEY_DISABLED, false);
        return mPrefs.getBoolean(KEY_ENABLED, true);
    }

    private void setLockscreenEnabled(boolean disabled) {
        mPrefs.edit().putBoolean(KEY_DISABLED, disabled).apply();
        mLockscreenDisabled = disabled;
    private void setPersistedState(boolean enabled) {
        mPrefs.edit().putBoolean(KEY_ENABLED, enabled).apply();
        mPersistedState = enabled;
    }
}
+4 −2
Original line number Diff line number Diff line
package com.android.internal.policy.impl.keyguard;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -126,7 +127,8 @@ public class KeyguardServiceDelegate {
    private void sendStateChangeBroadcast(boolean bound) {
        Intent i = new Intent(ACTION_STATE_CHANGE);
        i.putExtra(EXTRA_ACTIVE, bound);
        mScrim.getContext().sendStickyBroadcast(i);
        mScrim.getContext().sendBroadcastAsUser(i, UserHandle.ALL,
                Manifest.permission.CONTROL_KEYGUARD);
    }

    private final ServiceConnection mKeyguardConnection = new ServiceConnection() {
@@ -345,9 +347,9 @@ public class KeyguardServiceDelegate {
    public void onBootCompleted() {
        if (mKeyguardService != null) {
            mKeyguardService.onBootCompleted();
            sendStateChangeBroadcast(true);
        }
        mKeyguardState.bootCompleted = true;
        sendStateChangeBroadcast(true);
    }

    public void onActivityDrawn() {