Skip to content

Commit fe01676

Browse files
authored
fix(android): fix screenCaptured crash on Android 13 and lower (#14325)
1 parent 16c3122 commit fe01676

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

android/titanium/src/java/org/appcelerator/titanium/TiBaseActivity.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,8 @@ public abstract class TiBaseActivity extends AppCompatActivity implements TiActi
108108
private TiActivitySafeAreaMonitor safeAreaMonitor;
109109
private Context baseContext;
110110
public boolean keyboardVisible = false;
111-
final Activity.ScreenCaptureCallback screenCaptureCallback =
112-
new Activity.ScreenCaptureCallback()
113-
{
114-
@Override
115-
public void onScreenCaptured()
116-
{
117-
KrollDict kd = new KrollDict();
118-
kd.put("source", getWindowProxy());
119-
getTiApp().fireAppEvent("screenshotcaptured", kd);
120-
}
121-
};
111+
112+
Activity.ScreenCaptureCallback screenCaptureCallback = null;
122113

123114
/**
124115
* Callback to be invoked when the TiBaseActivity.onRequestPermissionsResult() has been called,
@@ -690,6 +681,19 @@ protected void onCreate(Bundle savedInstanceState)
690681
TiApplication tiApp = getTiApp();
691682
TiApplication.addToActivityStack(this);
692683

684+
if (Build.VERSION.SDK_INT >= 34) {
685+
screenCaptureCallback = new Activity.ScreenCaptureCallback()
686+
{
687+
@Override
688+
public void onScreenCaptured()
689+
{
690+
KrollDict kd = new KrollDict();
691+
kd.put("source", getWindowProxy());
692+
getTiApp().fireAppEvent("screenshotcaptured", kd);
693+
}
694+
};
695+
}
696+
693697
this.safeAreaMonitor = new TiActivitySafeAreaMonitor(this, tiApp);
694698

695699
// Fetch the current UI mode flags. Used to determine light/dark theme being used.
@@ -1575,7 +1579,8 @@ protected void onStart()
15751579
applyNightMode();
15761580

15771581
if (Build.VERSION.SDK_INT >= 34 && TiApplication.getInstance().checkCallingOrSelfPermission(
1578-
Manifest.permission.DETECT_SCREEN_CAPTURE) == PackageManager.PERMISSION_GRANTED) {
1582+
Manifest.permission.DETECT_SCREEN_CAPTURE) == PackageManager.PERMISSION_GRANTED
1583+
&& screenCaptureCallback != null) {
15791584
registerScreenCaptureCallback(ContextCompat.getMainExecutor(this), screenCaptureCallback);
15801585
}
15811586
}
@@ -1604,8 +1609,9 @@ protected void onStop()
16041609
}
16051610
}
16061611
}
1607-
1608-
unregisterScreenCaptureCallback(screenCaptureCallback);
1612+
if (Build.VERSION.SDK_INT >= 34 && screenCaptureCallback != null) {
1613+
unregisterScreenCaptureCallback(screenCaptureCallback);
1614+
}
16091615
}
16101616

16111617
@Override

0 commit comments

Comments
 (0)