Android: Fix GPS and infrared crashes on devices without them

requestLocationUpdates() throws IllegalArgumentException for a provider
that doesn't exist (WiFi-only tablets, TVs, Chromebooks), which crashed
any game opening the GPS. Register each provider separately.

Without an IR blaster, the IR service is null, and InfraredHelper was
created anyway, so a sceSircs call NPE'd. Also catch transmit() failing,
like the SecurityException on API 21/22.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-12 14:41:19 +02:00
1 parent 46e576a402
commit 51a799623c
2 files changed
+19 -6

No files matched your search

@@ -17,8 +17,8 @@ class InfraredHelper {
InfraredHelper(Context context) throws Exception {
mConsumerIrManager = (ConsumerIrManager) context.getSystemService(Context.CONSUMER_IR_SERVICE);
if (mConsumerIrManager == null) {
Log.i(TAG, "No IR manager detected.");
return;
// Normal on devices without an IR blaster, the service isn't registered.
throw new Exception("No IR manager");
}
Log.d(TAG, "HasIrEmitter: " + mConsumerIrManager.hasIrEmitter());
if (!mConsumerIrManager.hasIrEmitter()) {
@@ -69,6 +69,11 @@ class InfraredHelper {
for (int i = 0; i < patternList.size(); i++) {
pattern[i] = patternList.get(i);
}
mConsumerIrManager.transmit(SIRC_FREQ, pattern);
try {
mConsumerIrManager.transmit(SIRC_FREQ, pattern);
} catch (Exception e) {
// SecurityException on API 21/22, where TRANSMIT_IR (declared for 23+) isn't granted.
Log.e(TAG, "IR transmit failed: " + e);
}
}
}
@@ -72,8 +72,16 @@ class LocationHelper implements LocationListener {
try {
isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
// Each provider can be missing (WiFi-only tablets, TVs, Chromebooks), which throws
// IllegalArgumentException. Set the flag as soon as one is registered, so stopListening removes it.
for (String provider : new String[] { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER }) {
try {
mLocationManager.requestLocationUpdates(provider, 1000, 0, this);
mLocationEnable = true;
} catch (IllegalArgumentException e) {
Log.i(TAG, "Location provider " + provider + " not available: " + e);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
mGnssStatusCallback = new GnssStatus.Callback() {
@Override
@@ -91,7 +99,7 @@ class LocationHelper implements LocationListener {
mLocationManager.addNmeaListener(mNmeaListener);
}
mLocationEnable = true;
} catch (SecurityException e) {
} catch (SecurityException | IllegalArgumentException e) {
Log.e(TAG, "Cannot start location updates: " + e);
}
if (!isGPSEnabled && !isNetworkEnabled) {