9

Is it possible to disable screen capture from a fragment? I know the below works for an Activity class

onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
                WindowManager.LayoutParams.FLAG_SECURE);
}

But what if I have a fragment which shows up on top of an activity. Can I somehow disable screen capture? I've tried to set the FLAG_SECURE in the onCreate() or onCreateView() method of the fragment, but it doesn't work. I'm still able to take screen shot. Only when I add the flag in parent activity I can disable it.

On a related note, say, I've a method in ParentActivity.java (which extends Activity)

public void disableScreenCapture() {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);

}

And in my ChildFragment.java (which extends Fragment)

            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                ParentActivity parentActivity = (ParentActivity)getActivity();
                parentActivity.disableScreenCapture(); //doesn't work
        }
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
                ParentActivity parentActivity = (ParentActivity)getActivity();
                parentActivity.disableScreenCapture(); //doesn't work either
    }

Any ideas?

Thanks in advance

kdas
  • 602
  • 2
  • 7
  • 22
  • 2
    My expectation is that the "secure" flag applies to the entire View UI layer. A SurfaceView's Surface is a separate layer and can be handled independently, but the View UI is composited by the app and handed to the system compositor as a single entity. – fadden Sep 14 '15 at 22:01

3 Answers3

9

Performing your disableScreenCapture() call in onResume, onCreateView or onActivityAttached in your Fragment should all work - they did for me. Performing that call in onActivityCreated might not work as I believe that hook is only called when the Activity is being re-created, after it's destroyed. However, I didn't try that one.

If performing that call in onCreateView isn't working for you, are you 100% certain that your Fragment is actually being added to the Activity?

For a DialogFragment it's slightly different:

getDialog().getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
        WindowManager.LayoutParams.FLAG_SECURE);

A DialogFragment isn't a Dialog itself, but instead holds a reference to one and shows/dismisses it when the fragment is added and removed. Dialogs have their own Windows and must have the flag set individually.

tophyr
  • 1,658
  • 14
  • 20
  • What if my fragment extends from Fragment and not DialogFragment? – kdas Sep 14 '15 at 21:12
  • Oh sorry, I read the question wrong. Doing some research and will re-answer. – tophyr Sep 14 '15 at 21:39
  • Disabling screenshot on onResume() or onActivityCreated() doesn't work either, in fragment. – kdas Sep 14 '15 at 22:36
  • All of the methods you tried worked for me when I tested them. Are you certain your Fragment is actually being added to your Activity? – tophyr Sep 14 '15 at 23:57
  • I think so. Basically I've a single Activity and bunch of Fragments. I have a fragment switcher class which replaces current fragment with new fragment and then commits transaction. Looks okay to me on the surface. Let me dig deeper in my code. – kdas Sep 15 '15 at 04:31
  • SOmething to do with the way I replace fragments that simply setting the flag didn't work for me. However, I found a solution that works. Posted it as the answer. – kdas Sep 15 '15 at 23:07
6

The below code worked for me.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    getActivity().getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
    Window window = getActivity().getWindow();
    WindowManager wm = getActivity().getWindowManager();
    wm.removeViewImmediate(window.getDecorView());
    wm.addView(window.getDecorView(), window.getAttributes());

}
kdas
  • 602
  • 2
  • 7
  • 22
1

You can create a public function in Parent activity and set flag onAttach method of fragment and clear flag in onDetach Method.

Method in Parent Activity

fun disableScreenShot(isSecure:Boolean){
    if (isSecure) {
        window.setFlags(WindowManager.LayoutParams.FLAG_SECURE,
            WindowManager.LayoutParams.FLAG_SECURE);
    }
    else{
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE)
    }
}

Code in Fragment

override fun onAttach(context: Context) {
        super.onAttach(context)
        activity = context as HomeActivity
        activity.disableScreenShot(true)
    }

    override fun onDetach() {
        super.onDetach()
        activity.disableScreenShot(false)
    }
Gunavant Patel
  • 1,413
  • 1
  • 13
  • 17