Skip to content

Instantly share code, notes, and snippets.

@SimonMarquis
Created October 21, 2016 08:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimonMarquis/5be084d98a8c66e9b99e0a8be1279a6c to your computer and use it in GitHub Desktop.
Save SimonMarquis/5be084d98a8c66e9b99e0a8be1279a6c to your computer and use it in GitHub Desktop.
Prevent screenshot on Activity, Dialog, Surface, etc.
import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
/**
* Prevent screenshot on multiple levels.
*/
public final class PreventScreenshot {
private PreventScreenshot() {
}
public static void on(Window window) {
window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);
}
public static void on(Activity activity) {
PreventScreenshot.on(activity.getWindow());
}
public static void on(Fragment fragment) {
PreventScreenshot.on(fragment.getActivity());
}
public static void on(WindowManager.LayoutParams layoutParams) {
layoutParams.flags |= WindowManager.LayoutParams.FLAG_SECURE;
}
public static void on(Dialog dialog) {
on(dialog.getWindow());
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
public static void on(SurfaceView surfaceView) {
surfaceView.setSecure(true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment