Created
October 21, 2016 08:23
-
-
Save SimonMarquis/5be084d98a8c66e9b99e0a8be1279a6c to your computer and use it in GitHub Desktop.
Prevent screenshot on Activity, Dialog, Surface, etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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