How to clear WindowManager flags in Android from Fragment
Asked Answered
A

1

7

I have a FLAG_SECURE set for an activity (it contains sensitive data), but in one particular Fragment I need to clear it (because od Android Beam).

Window window = getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);

This code clears the flag (I have it on the Fragment's onResume callback), but the issue is, that it doesn't go into effect till next configuration change (screen rotation,...) The same problem is with setting the flag once again when leaving the Fragment.

Does anybody know, what should I do to fix this? (I thought about Activity.recreate(), that could work but I don't like the solution) I don't want to create a separate Activity for this particular screen if possible.

Agace answered 31/3, 2015 at 13:46 Comment(0)
P
2

EDIT: added example code.

I am late here, but I will post it anyway. It is not the best solution (in my honest opinion) since it makes visible "re-draw" like effect on Android 4.x (5+ is good), but it works at least. I am using it like this:

/**
 * @param flagSecure adds/removes FLAG_SECURE from Activity this Fragment is attached to/from.
 */
public void applyFlagSecure(boolean flagSecure)
{
    Window window = getActivity().getWindow();
    WindowManager wm = getActivity().getWindowManager();

    // is change needed?
    int flags = window.getAttributes().flags;
    if (flagSecure && (flags & WindowManager.LayoutParams.FLAG_SECURE) != 0) {
        // already set, change is not needed.
        return;
    } else if (!flagSecure && (flags & WindowManager.LayoutParams.FLAG_SECURE) == 0) {
        // already cleared, change is not needed.
        return;
    }

    // apply (or clear) the FLAG_SECURE flag to/from Activity this Fragment is attached to.
    boolean flagsChanged = false;
    if (flagSecure) {
        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
        flagsChanged = true;
    } else {
        // FIXME Do NOT unset FLAG_SECURE flag from Activity's Window if Activity explicitly set it itself.
        if (!(getActivity() instanceof YourFlagSecureActivity)) {
            // Okay, it is safe to clear FLAG_SECURE flag from Window flags.
            // Activity is (probably) not showing any secure content.
            window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);
            flagsChanged = true;
        }
    }

    // Re-apply (re-draw) Window's DecorView so the change to the Window flags will be in place immediately.
    if (flagsChanged && ViewCompat.isAttachedToWindow(window.getDecorView())) {
        // FIXME Removing the View and attaching it back makes visible re-draw on Android 4.x, 5+ is good.
        wm.removeViewImmediate(window.getDecorView());
        wm.addView(window.getDecorView(), window.getAttributes());
    }
}

Source: this solution is based on kdas's example: How to disable screen capture in Android fragment?

Postimpressionism answered 21/4, 2017 at 14:39 Comment(1)
please add some content from the linkLaine

© 2022 - 2024 — McMap. All rights reserved.