Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize android app preview when it is in background with secure flag

In my Android app I need to use the SECURE_FLAG to say to Android: "hey you don't take a screenshoot of my app in background!". Ok it works simply using the following lines of code in my activities:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

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

What I obtained is explained in the following screenshot:

screenshot

Now.. how can I:

  1. change the icon on the top of the activity preview?
  2. change the preview of the activity, with another colour or an image?

I look for a solution, but I didn't find anything.. except that it is impossible to do it (if true I think is a pity, for Android). Tnx in advance.

like image 535
xcesco Avatar asked Oct 28 '22 06:10

xcesco


1 Answers

You can use two layouts.

  1. In your xml code of the layout, make a separate layout and cut & paste the entire ui code in it and give it the id mainLayout . Make another layout which will be used for the securing UI. Give this layout the id protectLayout and there you can customize what you want like image background and etc...

  2. Then follow this in the activity you want to be secure.

    LinearLayout mainLayout,secureLayout;
    
    @Override
    protected void onPause() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mainLayout.setVisibility(View.GONE);
        secureLayout.setVisibility(View.VISIBLE);
        super.onPause();
    }
    
    @Override
    protected void onStart() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE,WindowManager.LayoutParams.FLAG_SECURE);
        mainLayout.setVisibility(View.VISIBLE);
        secureLayout.setVisibility(View.GONE);
        super.onResume();
    }
    

NOTE: This will best work on Android versions 12 and above as in others the display at the recent does not update immediately.

like image 54
Sambhav Khandelwal Avatar answered Nov 13 '22 14:11

Sambhav Khandelwal