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

Commit 61d68f9a authored by Lars Greiss's avatar Lars Greiss Committed by Michael Bestas
Browse files

Keyguard: do not allow to delete the system widget if widgets are disabled

We overwrite the default widget with our own one. In this case it is detected
as a normal widget even though it widgets are disabled and starts listening to
longpress events

Whatever check if widgets are disabled. If this is the case do not allow any
longpress events

Change-Id: I471664bac6ca1f8e57efe4904a385904c40486db
parent bf04b664
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
@@ -19,6 +19,8 @@ package com.android.keyguard;
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.content.Context;
@@ -37,6 +39,8 @@ import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.android.internal.widget.LockPatternUtils;

public class KeyguardWidgetFrame extends FrameLayout {
    private final static PorterDuffXfermode sAddBlendMode =
            new PorterDuffXfermode(PorterDuff.Mode.ADD);
@@ -82,6 +86,12 @@ public class KeyguardWidgetFrame extends FrameLayout {

    private boolean mIsHoveringOverDeleteDropTarget;

    // Even though we know already that this is a widget
    // which the user only can add when widgets are enabled
    // we need to cover the case that the system clock widget
    // was overwritten via overlays with a normal widget.
    private boolean mWidgetsDisabled;

    // Multiple callers may try and adjust the alpha of the frame. When a caller shows
    // the outlines, we give that caller control, and nobody else can fade them out.
    // This prevents animation conflicts.
@@ -100,6 +110,8 @@ public class KeyguardWidgetFrame extends FrameLayout {

        mLongPressHelper = new CheckLongPressHelper(this);

        mWidgetsDisabled = widgetsDisabled(context);

        Resources res = context.getResources();
        // TODO: this padding should really correspond to the padding embedded in the background
        // drawable (ie. outlines).
@@ -118,6 +130,30 @@ public class KeyguardWidgetFrame extends FrameLayout {
        mGradientPaint.setXfermode(sAddBlendMode);
    }

    private boolean widgetsDisabled(Context context) {
        int disabledFeatures = 0;
        LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
        DevicePolicyManager dpm =
                (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        if (dpm != null) {
            disabledFeatures = getDisabledFeatures(dpm, lockPatternUtils);
        }
        boolean disabledByDpm =
                (disabledFeatures & DevicePolicyManager.KEYGUARD_DISABLE_WIDGETS_ALL) != 0;
        boolean disabledByUser = !lockPatternUtils.getWidgetsEnabled();
        boolean disabledByLowRamDevice = ActivityManager.isLowRamDeviceStatic();
        return disabledByLowRamDevice || disabledByDpm || disabledByUser;
    }

    private int getDisabledFeatures(DevicePolicyManager dpm, LockPatternUtils lockPatternUtils) {
        int disabledFeatures = DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NONE;
        if (dpm != null) {
            final int currentUser = lockPatternUtils.getCurrentUser();
            disabledFeatures = dpm.getKeyguardDisabledFeatures(null, currentUser);
        }
        return disabledFeatures;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
@@ -158,6 +194,13 @@ public class KeyguardWidgetFrame extends FrameLayout {

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // Widgets are disabled. This widget is a system one
        // do not check for longpress events and let the
        // touch events fall through to the children
        if (mWidgetsDisabled) {
            return false;
        }

        // Watch for longpress events at this level to make sure
        // users can always pick up this widget
        switch (ev.getAction()) {
@@ -180,6 +223,11 @@ public class KeyguardWidgetFrame extends FrameLayout {

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Widgets are disabled. This widget is a system one
        // do not check for longpress events
        if (mWidgetsDisabled) {
            return true;
        }
        // Watch for longpress events at this level to make sure
        // users can always pick up this widget
        switch (ev.getAction()) {