[Sony Cross-Device General] serajr:
Exactly... thank you!

For those who don't know what android development is (nobody needs to know it), here is a sample (the new Lock Screen tile related code only, but not entirely) in which illustrates you what happens in the background when you simply toggle (tap) a quick settings tile, and you must ask your self "But who would write such things? "... Me!! ;)

package serajr.xx.lp.hooks.systemui.tiles;

import serajr.xx.lp.Xposed;
import serajr.xx.lp.hooks.systemui.SystemUI_ModRes;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.provider.Settings;
import com.android.systemui.SystemUIApplication;
import com.android.systemui.keyguard.KeyguardViewMediator;
import com.android.systemui.qs.QSTile;
import com.android.systemui.qs.QSTile.BooleanState;
import com.android.systemui.statusbar.policy.KeyguardMonitor;

import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class LockscreenTile extends QSTile<BooleanState> {

private KeyguardMonitor mKeyguard;
private boolean mListening = false;
private Controller mController = new Controller();

public LockscreenTile(Host host) {
super(host);

// guarda
mKeyguard = host.getKeyguardMonitor();

}

@Override
public void setListening(boolean listening) {

if (mListening == listening)
return;

// guarda
mListening = listening;

if (listening) {

// registra
mController.register();
mKeyguard.addCallback(mController);

} else {

// desregistra
mController.unregister();
mKeyguard.removeCallback(mController);

}
}

@Override
protected void handleClick() {

// toogle
mController.toggleState();

}

@Override
protected void handleUpdateState(BooleanState state, Object arg) {

boolean enabled = mController.isEnabled();
boolean enforced = lockscreenEnforcedByDevicePolicy(mContext);

state.visible = mController.isBound() && !(mKeyguard.isShowing() && mKeyguard.isSecure());
state.value = enabled;

if (enabled) {

// on
state.iconId = -1;
state.icon = new BitmapDrawable(SystemUI_ModRes.getBitmap("ic_qs_lockscreen_on"));
state.label = enforced ? SystemUI_ModRes.getString("qs_lockscreen_enforced_text") : SystemUI_ModRes.getString("qs_lockscreen_text");
state.contentDescription = state.label;

} else {

// off
state.iconId = -1;
state.icon = new BitmapDrawable(SystemUI_ModRes.getBitmap("ic_qs_lockscreen_off"));
state.label = enforced ? SystemUI_ModRes.getString("qs_lockscreen_enforced_text") : SystemUI_ModRes.getString("qs_lockscreen_text");
state.contentDescription = state.label;

}
}

@Override
protected BooleanState newTileState() {

return new BooleanState();

}

private class Controller implements KeyguardMonitor.Callback {

private static final String KEY_ENABLED = "lockscreen_enabled";
//private static final String KEYGUARD_SERVICE_EXTRA_ACTIVE = "active";
private static final String KEYGUARD_SERVICE_ACTION_STATE_CHANGE = "xx_lp_com.android.internal.action.KEYGUARD_SERVICE_STATE_CHANGED";

private boolean mKeyguardBound;
private boolean mPersistedState;
private SharedPreferences mPrefs;
private KeyguardViewMediator mKeyguardViewMediator;
private ContentObserver mContentObserver = new ContentObserver(new Handler()) {

@Override
public void onChange(boolean selfChange) {

mKeyguardBound = Settings.System.getInt(mContext.getContentResolver(), KEYGUARD_SERVICE_ACTION_STATE_CHANGE, 0) == 1;
applyLockscreenState();

// atualiza
refreshState();

}
};

public Controller() {

// guarda
mPrefs = mContext.getSharedPreferences("xx_lp_quick_settings", Context.MODE_PRIVATE);
mPersistedState = getPersistedState();
mKeyguardViewMediator = ((SystemUIApplication) mContext.getApplicationContext()).getComponent(KeyguardViewMediator.class);

}

public void register() {

// registra
mContext.getContentResolver().registerContentObserver(Settings.System.getUriFor(KEYGUARD_SERVICE_ACTION_STATE_CHANGE), false, mContentObserver);

// 5 segundos
mHandler.postDelayed(new Runnable() {

@Override
public void run() {

// atualiza
mContentObserver.onChange(false);

}
}, 5000);
}

public void unregister() {

// desregistra
mContext.getContentResolver().unregisterContentObserver(mContentObserver);

}

public boolean isEnabled() {

return lockscreenEnforcedByDevicePolicy(mContext) || mPersistedState || getKeyguardEnabledInternal();

}

public boolean isBound() {

return mKeyguardBound;

}

public void toggleState() {

setPersistedState(!mPersistedState);
applyLockscreenState();

// atualiza
refreshState();

}

private void applyLockscreenState() {

// continua ?
if (!mKeyguardBound)
return;

setKeyguardEnabledInternal(mKeyguardViewMediator, mPersistedState);

}

private boolean getPersistedState() {

return mPrefs.getBoolean(KEY_ENABLED, true);

}

private void setPersistedState(boolean enabled) {

mPrefs.edit().putBoolean(KEY_ENABLED, enabled).apply();
mPersistedState = enabled;

}

@Override
public void onKeyguardChanged() {

// atualiza
refreshState();

}
}

// ------
// xposed
// ------

private static boolean mInternallyDisabled = false;

public static void hook() {

// habilitado?
if (!Xposed.mXSharedPreferences.getBoolean("xx_system_ui_enabled_pref", false))
return;

try {

// KeyguardViewMediator - setKeyguardEnabled
XposedHelpers.findAndHookMethod(KeyguardViewMediator.class, "setKeyguardEnabled", boolean.class, new XC_MethodReplacement() {

@Override
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {

// parâmetros
boolean enabled = (Boolean) param.args[0];

// obtém os campos
Context mContext = (Context) XposedHelpers.getObjectField(param.thisObject, "mContext");

// o keyguard foi desabilitado pelo tile (internamente) ?
if (mInternallyDisabled && enabled && !lockscreenEnforcedByDevicePolicy(mContext)) {

// não permite o keyguard ser habilitado externamente
// a não ser pelo próprio DevicePolicyManager !!!!!!!
return null;

} else {

// invoca o método original
XposedBridge.invokeOriginalMethod(param.method, param.thisObject, param.args);

}

return null;

}
});

} catch (Exception e) {

XposedBridge.log(e);

}
}

private static void setKeyguardEnabledInternal(KeyguardViewMediator kvm, boolean enabled) {

// guarda
mInternallyDisabled = !enabled;
kvm.setKeyguardEnabled(enabled);

if (mInternallyDisabled)
XposedHelpers.setBooleanField(kvm, "mNeedToReshowWhenReenabled", false);

}

private static boolean getKeyguardEnabledInternal() {

return !mInternallyDisabled;

}

private static boolean lockscreenEnforcedByDevicePolicy(Context context) {

DevicePolicyManager dpm = (DevicePolicyManager) context.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;

}
}

Remember that "to write" isn't the hard work, but "to know" what's you're writing up!