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

Commit 9501c4aa authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

SystemUI: write keyguard enabled setting to CMSettings



Instead of saving this to a shared preference in SystemUI, let's
leverage the CM SDK Settings Provider so we can query this state and let
the user know why the keyguard is disabled.

Also move the logic into KeyguardViewMediator to monitor the state
change and apply it locally.

Ref: CYNGNOS-1513

Change-Id: I08a984797921178082e1e7bda881b7b8a7965ddf
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 70524943
Loading
Loading
Loading
Loading
+67 −3
Original line number Diff line number Diff line
@@ -30,7 +30,9 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
@@ -57,6 +59,7 @@ import android.view.WindowManagerPolicy;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.android.systemui.cm.UserContentObserver;
import com.android.systemui.qs.tiles.LockscreenToggleTile;
import cyanogenmod.app.Profile;
import cyanogenmod.app.ProfileManager;
@@ -78,6 +81,7 @@ import com.android.systemui.statusbar.phone.PhoneStatusBar;
import com.android.systemui.statusbar.phone.ScrimController;
import com.android.systemui.statusbar.phone.StatusBarKeyguardViewManager;
import com.android.systemui.statusbar.phone.StatusBarWindowManager;
import cyanogenmod.providers.CMSettings;

import java.io.FileDescriptor;
import java.io.PrintWriter;
@@ -343,6 +347,50 @@ public class KeyguardViewMediator extends SystemUI {
    private boolean mWakeAndUnlocking;
    private IKeyguardDrawnCallback mDrawnCallback;

    private LockscreenEnabledSettingsObserver mSettingsObserver;
    public static class LockscreenEnabledSettingsObserver extends UserContentObserver {

        private static final String KEY_ENABLED = "lockscreen_enabled";

        private boolean mObserving;
        private SharedPreferences mPrefs;
        private Context mContext;

        public LockscreenEnabledSettingsObserver(Context context, Handler handler) {
            super(handler);
            mContext = context;
            mPrefs = mContext.getSharedPreferences("quicksettings", Context.MODE_PRIVATE);
        }

        public boolean getPersistedDefaultOldSetting() {
            return mPrefs.getBoolean(KEY_ENABLED, true);
        }

        @Override
        public void observe() {
            if (mObserving) {
                return;
            }
            mObserving = true;
            mContext.getContentResolver().registerContentObserver(CMSettings.Secure.getUriFor(
                    CMSettings.Secure.LOCKSCREEN_INTERNALLY_ENABLED), false, this,
                    UserHandle.USER_ALL);
            update();
        }

        @Override
        public void unobserve() {
            if (mObserving) {
                mObserving = false;
                mContext.getContentResolver().unregisterContentObserver(this);
            }
        }

        @Override
        public void update() {
        }
    }

    KeyguardUpdateMonitorCallback mUpdateCallback = new KeyguardUpdateMonitorCallback() {

        @Override
@@ -634,6 +682,20 @@ public class KeyguardViewMediator extends SystemUI {

        mHideAnimation = AnimationUtils.loadAnimation(mContext,
                com.android.internal.R.anim.lock_screen_behind_enter);

        mSettingsObserver = new LockscreenEnabledSettingsObserver(mContext, new Handler()) {
            @Override
            public void update() {
                boolean newDisabledState = CMSettings.Secure.getIntForUser(mContext.getContentResolver(),
                        CMSettings.Secure.LOCKSCREEN_INTERNALLY_ENABLED,
                        getPersistedDefaultOldSetting() ? 1 : 0,
                        UserHandle.USER_CURRENT) == 0;
                if (newDisabledState != mInternallyDisabled && mKeyguardBound) {
                    // it was updated,
                    setKeyguardEnabledInternal(!newDisabledState);
                }
            }
        };
    }

    @Override
@@ -1298,9 +1360,11 @@ public class KeyguardViewMediator extends SystemUI {
                }
            } else if (KEYGUARD_SERVICE_ACTION_STATE_CHANGE.equals(intent.getAction())) {
                mKeyguardBound = intent.getBooleanExtra(KEYGUARD_SERVICE_EXTRA_ACTIVE, false);
                context.sendBroadcast(new Intent(
                        LockscreenToggleTile.ACTION_APPLY_LOCKSCREEN_STATE)
                        .setPackage(context.getPackageName()));
                if (mKeyguardBound) {
                    mSettingsObserver.observe();
                } else {
                    mSettingsObserver.unobserve();
                }
            }
        }
    };
+32 −53
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The CyanogenMod Project
 * Copyright (C) 2015-2016 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,69 +16,67 @@

package com.android.systemui.qs.tiles;

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 android.os.UserHandle;
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;
import cyanogenmod.providers.CMSettings;
import org.cyanogenmod.internal.logging.CMMetricsLogger;

public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
        implements KeyguardMonitor.Callback {

    public static final String ACTION_APPLY_LOCKSCREEN_STATE =
            "com.android.systemui.qs.tiles.action.APPLY_LOCKSCREEN_STATE";

    private static final String KEY_ENABLED = "lockscreen_enabled";

    private static final Intent LOCK_SCREEN_SETTINGS =
            new Intent("android.settings.LOCK_SCREEN_SETTINGS");

    private KeyguardViewMediator mKeyguardViewMediator;
    private KeyguardMonitor mKeyguard;
    private boolean mPersistedState;
    private boolean mKeyguardBound;
    private SharedPreferences mPrefs;
    private boolean mListening;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (mKeyguardViewMediator != null) {
                mKeyguardBound = mKeyguardViewMediator.isKeyguardBound();
                applyLockscreenState();
                refreshState();
            }
        }
    };
    private KeyguardViewMediator.LockscreenEnabledSettingsObserver mSettingsObserver;

    public LockscreenToggleTile(Host host) {
        super(host);
        mPrefs = mContext.getSharedPreferences("quicksettings", Context.MODE_PRIVATE);

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

        mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_APPLY_LOCKSCREEN_STATE));
        mSettingsObserver = new KeyguardViewMediator.LockscreenEnabledSettingsObserver(mContext,
                mUiHandler) {

            @Override
            public void update() {
                boolean newEnabledState = CMSettings.Secure.getIntForUser(
                        mContext.getContentResolver(),
                        CMSettings.Secure.LOCKSCREEN_INTERNALLY_ENABLED,
                        getPersistedDefaultOldSetting() ? 1 : 0,
                        UserHandle.USER_CURRENT) != 0;
                if (newEnabledState != mPersistedState) {
                    mPersistedState = newEnabledState;
                    refreshState();
                }
            }
        };

    }

    @Override
    public void setListening(boolean listening) {
        if (mListening == listening) {
            return;
        }
        mListening = listening;
        if (listening) {
            mSettingsObserver.observe();
            mKeyguard.addCallback(this);
        } else {
            mSettingsObserver.unobserve();
            mKeyguard.removeCallback(this);
        }
    }
@@ -91,8 +89,6 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
    @Override
    protected void handleClick() {
        setPersistedState(!mPersistedState);
        applyLockscreenState();
        refreshState();
    }

    @Override
@@ -139,32 +135,15 @@ public class LockscreenToggleTile extends QSTile<QSTile.BooleanState>
        }
    }

    @Override
    protected void handleDestroy() {
        super.handleDestroy();
        mContext.unregisterReceiver(mReceiver);
    }

    @Override
    public void onKeyguardChanged() {
        refreshState();
    }

    private void applyLockscreenState() {
        if (!mKeyguardBound) {
            // do nothing yet
            return;
        }

        mKeyguardViewMediator.setKeyguardEnabledInternal(mPersistedState);
    }

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

    private void setPersistedState(boolean enabled) {
        mPrefs.edit().putBoolean(KEY_ENABLED, enabled).apply();
        CMSettings.Secure.putIntForUser(mContext.getContentResolver(),
                CMSettings.Secure.LOCKSCREEN_INTERNALLY_ENABLED,
                enabled ? 1 : 0, UserHandle.USER_CURRENT);
        mPersistedState = enabled;
    }
}
 No newline at end of file