Skip to content

Conversation

@m1ga
Copy link
Contributor

@m1ga m1ga commented Oct 12, 2025

Android parity for screenshotcaptured using Android 14 registerScreenCaptureCallback: https://developer.android.com/about/versions/14/features/screenshot-detection#java

Changes:

  • add Android parity for screenshotcaptured event
  • move Ti.App.iOS.screenshotcaptured to Ti.App.screenshotcaptured
  • add deprecation message for Ti.App.iOS.screenshotcaptured

Test:

var win = Ti.UI.createWindow({
	id: "win1",
	backgroundColor: "#fff"
});
var btn = Ti.UI.createButton({
	top: 140,
	title: "open"
})
btn.addEventListener("click", function() {
	var win2 = Ti.UI.createWindow({
		backgroundColor: "#fff",
		id: "win2"
	})
	var lbl2 = Ti.UI.createLabel({
		color: "#000",
		text: "app even is still fired. Close window and check the log"
	});
	win2.open();
	win2.add(lbl2);
})
var lbl = Ti.UI.createLabel({
	color: "#000",
	text: ""
});
win.add([btn, lbl]);
win.open();

// iOS parity
Ti.App.addEventListener("screenshotcaptured", function(e) {
	console.log("Ti.App event: " + JSON.stringify(e));
	lbl.text += "Ti.App event: " + e.source.id + "\n";
})
if (OS_IOS) {
	Ti.App.iOS.addEventListener("screenshotcaptured", function(e) {
		console.log("Old Ti.App.iOS event: " + JSON.stringify(e));
		lbl.text += "Old Ti.App.iOS event\n";
	})
}
  • and add <uses-permission android:name="android.permission.DETECT_SCREEN_CAPTURE"/> to your tiapp.xml
  • Run the app and take a screenshot.
  • log and label will show that the event is fired
  • click the open button to open a new window
  • take a screenshot
  • the app event is fired

@prashantsaini1
Copy link
Contributor

@m1ga

  1. We are firing Ti.App level events generated from per-activity level, it would be safe to test that the screen-capture callback is always invoked by the Android OS only when that activity is visible.
  2. After the above point, we should also let users know that which activity/ti-window this event is being fired for.
    • e.g. A window where some private content is shown and user takes screenshot, the app may only want to handle that specific use-case, without worrying about rest of the screens.
    • It can be achieved by firing ti-window level event also, and keeping the Ti.App event as is. So this covers both use cases - if app just needs to know app-wide, it'll be through Ti.App, and if required for only specific window, then using the window's screenshot event. Just need to add this info in docs?

@m1ga
Copy link
Contributor Author

m1ga commented Oct 14, 2025

Thanks for the comments!

I have to check how the iOS event is fired. I've added the window event and changed the example.
Currently it behaves like this:

  • Ti.App event: will fire in all windows
  • win event: will only fire if the window is open, if you open the 2nd window it won't fire in the 1st window

Update: the Ti.App event behaves the same now on both platforms. Android will also have the per Window event.

@m1ga m1ga marked this pull request as ready for review October 14, 2025 16:57
Copy link
Contributor

@prashantsaini1 prashantsaini1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

The listener for this event must be defined before this window
is opened.
- name: screenshotcaptured
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed on a window instance and Android only? Isn't it on Ti.App now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it came for free as it is registered on the baseactivity and that way you use it on one window if needed and don't need to make the checks yourself to see if it is active and in front (see this comment).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just pass the window proxy as the "source" then? That's how they are usually identified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, sounds better. The Android event will now have current window as a source. iOS still needs to update the source.

@hansemannn hansemannn merged commit ad0b76f into master Oct 21, 2025
9 checks passed
@hansemannn hansemannn deleted the androidScreenshotDetection branch October 21, 2025 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants