Search code examples
androidflutterscreen-recording

Stop pre-installed screen recording apps from recording screen in android


I am using flutter and have disabled normal apps from recording the screen. Here is the code

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

The problem is there are some phones where screen recordings apps are pre-installed and above code can't stop them from recording the screen. So is there any other way to stop these apps from recording the screen? On other answers I saw that this was not possible but there are some apps on playstore which successfully achieve this. So there must be a way. I was thinking, as screen recording apps are drawn over , they might be detected through a piece of code hence we can show a pop up while screen recording app is drawn over. Is it possible ? If yes how can we detect if the app is drawn over our app. Thanks.


Solution

  • Adding this code to my MainActivity.java solved the problem:

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!this.setSecureSurfaceView()) {
            Log.e("MainActivity", "Could not secure the MainActivity!");
        }
    
    }
    
    
    
    private final boolean setSecureSurfaceView() {
        ViewGroup content = (ViewGroup) this.findViewById(android.R.id.content);
        //Intrinsics.checkExpressionValueIsNotNull(content, "content");
        if (!this.isNonEmptyContainer((View) content)) {
            return false;
        } else {
            View splashView = content.getChildAt(0);
            //Intrinsics.checkExpressionValueIsNotNull(splashView, "splashView");
            if (!this.isNonEmptyContainer(splashView)) {
                return false;
            } else {
                View flutterView = ((ViewGroup) splashView).getChildAt(0);
                //Intrinsics.checkExpressionValueIsNotNull(flutterView,          "flutterView");
                if (!this.isNonEmptyContainer(flutterView)) {
                    return false;
                } else {
                    View surfaceView = ((ViewGroup) flutterView).getChildAt(0);
                    if (!(surfaceView instanceof SurfaceView)) {
                        return false;
                    } else {
                        ((SurfaceView) surfaceView).setSecure(true);
                        this.getWindow().setFlags(8192, 8192);
                        return true;
                    }
                }
            }
        }
    
    }
    
        private final boolean isNonEmptyContainer(View view) {
            if (!(view instanceof ViewGroup)) {
                return false;
            } else {
                return ((ViewGroup) view).getChildCount() >= 1;
            }
    }
    

    Import the required things.