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

Commit 47a25227 authored by Danesh M's avatar Danesh M Committed by Gerrit Code Review
Browse files

QuickSettings : Properly restore lockscreen tile state

The keyguard lock relies on device policy manager service to be active,
currently it fails because it tries to request before its up. Fix that
by registering the request in a receiver

Change-Id: I15e0d980ecf21c8316f4a771e93f4b520c556115
parent e037c1fe
Loading
Loading
Loading
Loading
+119 −0
Original line number Diff line number Diff line
package com.android.systemui.quicksettings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
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 com.android.systemui.statusbar.phone.KeyguardTouchDelegate;

@SuppressWarnings("deprecation")
public class LockscreenStateChanger {

    private static final String KEY_DISABLED = "lockscreen_disabled";
    public interface LockStateChangeListener {
        public void onLockStateChange(boolean enabled);
    }

    private List<LockStateChangeListener> mListeners;
    private boolean mLockscreenDisabled;
    private static LockscreenStateChanger sInstance;
    private Context mContext;
    private KeyguardLock mLock;
    private boolean mInitialized;
    private SharedPreferences mPrefs;

    public static synchronized LockscreenStateChanger getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new LockscreenStateChanger(context);
        }
        return sInstance;
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mContext.unregisterReceiver(this);
            mInitialized = true;
            updateForCurrentState();
        }
    };

    private LockscreenStateChanger(Context context) {
        mListeners = Collections.synchronizedList(new ArrayList<LockStateChangeListener>());
        mContext = context.getApplicationContext();
        // Acquire lock
        KeyguardManager keyguardManager = (KeyguardManager)
                mContext.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
        mLock = keyguardManager.newKeyguardLock("LockscreenTile");
        // Fetch last state
        mPrefs = mContext.getSharedPreferences("quicksettings", Context.MODE_PRIVATE);
        mLockscreenDisabled = mPrefs.getBoolean(KEY_DISABLED, false);
        if (!isInitialized()) {
            // Register receiver
            IntentFilter filter = new IntentFilter(DevicePolicyManager
                    .ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
            mContext.registerReceiver(mReceiver, filter);
        } else {
            updateForCurrentState();
        }
    }

    public synchronized void addListener(LockStateChangeListener listener) {
        mListeners.add(listener);
    }

    public synchronized void removeListener(LockStateChangeListener listener) {
        mListeners.remove(listener);
        if (mListeners.isEmpty() && mLockscreenDisabled) {
            // No more tiles left, lets re-enable keyguard
            toggleState();
        }
    }

    public synchronized void toggleState() {
        mLockscreenDisabled = !mLockscreenDisabled;
        updateForCurrentState();
    }

    private synchronized void updateForCurrentState() {
        boolean dpmInitialized = isInitialized();
        if (dpmInitialized) {
            mPrefs.edit().putBoolean(KEY_DISABLED, mLockscreenDisabled).apply();
            if (mLockscreenDisabled) {
                mLock.disableKeyguard();
            } else {
                mLock.reenableKeyguard();
            }
            informListeners();
        }
    }

    private boolean isInitialized() {
        if (!mInitialized) {
            KeyguardTouchDelegate keyguard = KeyguardTouchDelegate.getInstance(mContext);
            if (keyguard.isServiceInitialized()) {
                mInitialized = true;
            }
        }
        return mInitialized;
    }

    private void informListeners() {
        for (LockStateChangeListener listener : mListeners) {
            listener.onLockStateChange(mLockscreenDisabled);
        }
    }

    public synchronized boolean isDisabled() {
        return mLockscreenDisabled;
    }
}
+14 −63
Original line number Diff line number Diff line
package com.android.systemui.quicksettings;

import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

import com.android.systemui.R;
import com.android.systemui.statusbar.phone.QuickSettingsController;
import com.android.systemui.statusbar.phone.QuickSettingsContainerView;

@SuppressWarnings("deprecation")
public class ToggleLockscreenTile extends QuickSettingsTile
        implements OnSharedPreferenceChangeListener {
public class ToggleLockscreenTile extends QuickSettingsTile implements
    LockscreenStateChanger.LockStateChangeListener {

    private static final String KEY_DISABLED = "lockscreen_disabled";

    private static KeyguardLock sLock = null;
    private static int sLockTileCount = 0;
    private static boolean sDisabledLockscreen = false;
    private LockscreenStateChanger mLockscreenChanger;

    public ToggleLockscreenTile(Context context, QuickSettingsController qsc) {
        super(context, qsc);

        mLockscreenChanger = LockscreenStateChanger.getInstance(context);
        mLockscreenChanger.addListener(this);
        mOnClick = new OnClickListener() {

            @Override
            public void onClick(View v) {
                sDisabledLockscreen = !sDisabledLockscreen;
                mPrefs.edit().putBoolean(KEY_DISABLED, sDisabledLockscreen).apply();
                updateLockscreenState();
                mLockscreenChanger.toggleState();
            }
        };

        mOnLongClick = new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                startSettingsActivity("android.settings.SECURITY_SETTINGS");
                return true;
            }
        };
    }

    @Override
    void onPostCreate() {
        mPrefs.registerOnSharedPreferenceChangeListener(this);
        if (sLockTileCount == 0) {
            sDisabledLockscreen = mPrefs.getBoolean(KEY_DISABLED, false);
            updateLockscreenState();
        }
        sLockTileCount++;
        updateTile();
        super.onPostCreate();
        updateResources();
    }

    @Override
    public void onDestroy() {
        mPrefs.unregisterOnSharedPreferenceChangeListener(this);
        sLockTileCount--;
        if (sLock != null && sLockTileCount < 1 && sDisabledLockscreen) {
            sLock.reenableKeyguard();
            sLock = null;
        }
        mLockscreenChanger.removeListener(this);
        super.onDestroy();
    }

    @Override
    public void updateResources() {
        updateTile();
        super.updateResources();
    }

    private synchronized void updateTile() {
        mLabel = mContext.getString(R.string.quick_settings_lockscreen);
        mDrawable = sDisabledLockscreen ?
                R.drawable.ic_qs_lock_screen_off : R.drawable.ic_qs_lock_screen_on;
    }

    private void updateLockscreenState() {
        if (sLock == null) {
            KeyguardManager keyguardManager = (KeyguardManager)
                    mContext.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
            sLock = keyguardManager.newKeyguardLock("LockscreenTile");
        }
        if (sDisabledLockscreen) {
            sLock.disableKeyguard();
        } else {
            sLock.reenableKeyguard();
        }
        mDrawable = mLockscreenChanger.isDisabled() ?
                R.drawable.ic_qs_lock_screen_on : R.drawable.ic_qs_lock_screen_off;
        super.updateResources();
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (KEY_DISABLED.equals(key)) {
    public void onLockStateChange(boolean enabled) {
        updateResources();
    }
}
 No newline at end of file
}
+3 −0
Original line number Diff line number Diff line
@@ -186,4 +186,7 @@ public class KeyguardTouchDelegate {
        }
    }

    public boolean isServiceInitialized() {
        return mService != null;
    }
}