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

Commit db828323 authored by Roman Birg's avatar Roman Birg
Browse files

SystemUI: port lock screen toggle tile



Change-Id: Ibec9993f3bd339a2d48e00eea6442618a91b15c9
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent a24eee43
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ public class QSConstants {
    public static final String TILE_ADB_NETWORK = "adb_network";
    public static final String TILE_NFC = "nfc";
    public static final String TILE_COMPASS = "compass";
    public static final String TILE_LOCKSCREEN = "lockscreen";

    // Order matters
    protected static final ArrayList<String> TILES_DEFAULT = new ArrayList<String>();
@@ -72,5 +73,6 @@ public class QSConstants {
        TILES_AVAILABLE.add(TILE_ADB_NETWORK);
        TILES_AVAILABLE.add(TILE_NFC);
        TILES_AVAILABLE.add(TILE_COMPASS);
        TILES_AVAILABLE.add(TILE_LOCKSCREEN);
    }
}
+13 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="64dp"
    android:height="64dp"
    android:viewportWidth="48"
    android:viewportHeight="48">

    <path
        android:fillColor="#4DFFFFFF"
        android:pathData="M36,16h-2v-4c0-5.5-4.5-10-10-10S14,6.5,14,12h3.8c0-3.4,2.8-6.2,6.2-6.2
s6.2,2.8,6.2,6.2v4H12c-2.2,0-4,1.8-4,4v20c0,2.2,1.8,4,4,4h24c2.2,0,4-1.8,4-4V20C40,17.8,38.2,16,36,16Z
M36,40H12V20L36,40z" />
</vector>
 No newline at end of file
+14 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="64dp"
    android:height="64dp"
    android:viewportWidth="48"
    android:viewportHeight="48">

    <path
        android:fillColor="#FFFFFF"
        android:pathData="M36,16h-2v-4c0-5.5-4.5-10-10-10S14,6.5,14,12v4h-2c-2.2,0-4,1.8-4,4v20
c0,2.2,1.8,4,4,4h24c2.2,0,4-1.8,4-4V20C40,17.8,38.2,16,36,16Z
M24,5.8c3.4,0,6.2,2.8,6.2,6.2v4H17.8v-4C17.8,8.6,20.6,5.8,24,5.8z
M36,40H12V20L36,40z" />
</vector>
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -93,6 +93,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_off">Compass off</string>
    <string name="quick_settings_compass_init">Initializing\u2026</string>
    <string name="quick_settings_lockscreen">Lock screen</string>

    <!-- Task Manager -->
    <string name="tasklistview_title">Task Manager</string>
+144 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 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.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.qs.tiles;

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

import com.android.systemui.R;
import com.android.systemui.qs.QSTile;
import com.android.systemui.statusbar.policy.KeyguardMonitor;

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

    private static final String KEYGUARD_SERVICE_ACTION_STATE_CHANGE =
            "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 KeyguardMonitor mKeyguard;
    private KeyguardManager.KeyguardLock mLock;
    private boolean mLockscreenDisabled;
    private boolean mKeyguardBound;
    private SharedPreferences mPrefs;

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            updateBasedOnIntent(intent);
        }
    };

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

        mKeyguard = host.getKeyguardMonitor();
        mLockscreenDisabled = getPersistedState();

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

    @Override
    public void setListening(boolean listening) {
        if (listening) {
            mKeyguard.addCallback(this);
        } else {
            mKeyguard.removeCallback(this);
        }
    }

    @Override
    protected BooleanState newTileState() {
        return new BooleanState();
    }

    @Override
    protected void handleClick() {
        setLockscreenEnabled(!mLockscreenDisabled);
        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);
        state.iconId = mKeyguardBound && mLockscreenDisabled
                ? R.drawable.ic_qs_lock_screen_off
                : R.drawable.ic_qs_lock_screen_on;
    }

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

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

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

    private void applyLockscreenState() {
        if (!mKeyguardBound) {
            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();
    }

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

    private void setLockscreenEnabled(boolean disabled) {
        mPrefs.edit().putBoolean(KEY_DISABLED, disabled).apply();
        mLockscreenDisabled = disabled;
    }
}
Loading