Android Question Disable screen capture functionality

Nenad Trickovic

Member
Licensed User
Hi,

For security reasons I would like to prevent taking screenshot of my application.

I saw that in java the code could look like this

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

How could this functionality be implemented in B4A?
 

NJDude

Expert
Licensed User
Note that using a USB connection (ADB) won't prevent taking a screenshot, you could also take a picture of the device ;)
 
Upvote 0

Nenad Trickovic

Member
Licensed User
Try this:
B4X:
Dim r As Reflector
r.Target = r.GetActivity
r.Target = r.RunMethod("getWindow")
r.RunMethod3("setFlags", 8192 , "java.lang.int", 8192 , "java.lang.int")
Hi Erel,

this does not work for me.

I tried to put before loading layout and after, but screenshot works anyway.
 
Upvote 0

Nenad Trickovic

Member
Licensed User
Maybe this code snippet sheds more light.


B4X:
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.apis.app;
import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
/**
* <h3>Secure Window Activity</h3>
*
* <p>
* This activity demonstrates how to create an activity whose window is backed by
* a secure surface using {@link WindowManager.LayoutParams#FLAG_SECURE}.
* Because the surface is secure, its contents cannot be captured in screenshots
* and will not be visible on non-secure displays even when mirrored.
* </p><p>
* Here are a few things you can do to experiment with secure surfaces and
* observe their behavior.
* <ul>
* <li>Try taking a screenshot. Either the system will prevent you from taking
* a screenshot altogether or the screenshot should not contain the contents
* of the secure surface.
* <li>Try mirroring the secure surface onto a non-secure display such as an
* "Overlay Display" created using the "Simulate secondary displays" option in
* the "Developer options" section of the Settings application. The non-secure
* secondary display should not show the contents of the secure surface.
* <li>Try mirroring the secure surface onto a secure display such as an
* HDMI display with HDCP enabled. The contents of the secure surface should appear
* on the display.
* </ul>
* </p>
*/
public class SecureWindowActivity extends Activity {
/**
* Initialization of the Activity after it is first created. Must at least
* call {@link android.app.Activity#setContentView setContentView()} to
* describe what is to be displayed in the screen.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// Be sure to call the super class.
super.onCreate(savedInstanceState);
// See assets/res/any/layout/secure_window_activity.xml for this
// view layout definition, which is being set here as
// the content of our screen.
setContentView(R.layout.secure_window_activity);
// Make the window secure. This must be done at the time the activity
// is created. It cannot be changed later.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,
WindowManager.LayoutParams.FLAG_SECURE);
}
}
 
Upvote 0

bgsoft

Well-Known Member
Licensed User
Upvote 0

bgsoft

Well-Known Member
Licensed User
Thanks Erel
 
Upvote 0

Nenad Trickovic

Member
Licensed User
Seems like there is a timing issue here. The flag only affects the app after the program is once paused and then resumed. You can try to use a service to show the activity a few seconds after it was first started and then immediately finished.

Hi Erel,

you are great. I introduced dummy activity and coded something like this
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    StartActivity(dummy)
End Sub

Sub Activity_Resume
    Dim r As Reflector
    r.Target = r.GetActivity
    r.Target = r.RunMethod("getWindow")
    r.RunMethod3("setFlags", 8192 , "java.lang.int", 8192 , "java.lang.int")
End Sub

Now, the problem is that this works only for initial orientation (vertical) and screen capture is disabled.

In horizontal orientation I can capture the screen.
 
Upvote 0

Nenad Trickovic

Member
Licensed User
Hi Erel,

is it possible to code something like this in order to prevent screen capture?

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        Return False
    Else
        Activity.Color=Colors.Black
          Msgbox("You are not able to take screenshot.", "Warning")
        Activity.finish
        Return True
    End If
End Sub
 
Upvote 0

Javier Alonso

Member
Licensed User
This is solved in another post. Just copy the following code at the beginning of the Activity_create sub:

B4X:
#If Java
import android.annotation.TargetApi;
import android.content.Context;
import android.view.WindowManager.*;
public void _onCreate() {
    this.getWindow().setFlags(LayoutParams.FLAG_SECURE, LayoutParams.FLAG_SECURE);
}
#End If
 
Upvote 0
Top