Fix crash on LineageWeatherManagerService for some devices

This commit is contained in:
José Rebelo
2026-03-19 21:32:57 +00:00
parent 85c3bb1886
commit 8b53389489
2 changed files with 16 additions and 13 deletions
@@ -88,14 +88,15 @@ public class LineageWeatherManager {
public static final int NO_MATCH_FOUND = -4;
}
private LineageWeatherManager(Context context) {
private LineageWeatherManager(Context context) throws Exception {
Context appContext = context.getApplicationContext();
mContext = (appContext != null) ? appContext : context;
sWeatherManagerService = getService();
if (context.getPackageManager().hasSystemFeature(
LineageContextConstants.Features.WEATHER_SERVICES) && (sWeatherManagerService == null)) {
Log.wtf(TAG, "Unable to bind the LineageWeatherManagerService");
// We replaced Log.wtf here, since it would crash some ROMs
throw new Exception("Unable to bind the LineageWeatherManagerService");
}
mHandler = new Handler(appContext.getMainLooper());
}
@@ -107,7 +108,11 @@ public class LineageWeatherManager {
*/
public static LineageWeatherManager getInstance(Context context) {
if (sInstance == null) {
sInstance = new LineageWeatherManager(context);
try {
sInstance = new LineageWeatherManager(context);
} catch (final Exception e) {
Log.e(TAG, "Unable to bind the LineageWeatherManagerService", e);
}
}
return sInstance;
}
@@ -124,20 +129,14 @@ public class LineageWeatherManager {
// This is a Gadgetbridge hack
IBinder binder = null;
try {
Class localClass = Class.forName("android.os.ServiceManager");
Class<?> localClass = Class.forName("android.os.ServiceManager");
Method getService = localClass.getMethod("getService", String.class);
Object result = getService.invoke(localClass, LineageContextConstants.LINEAGE_WEATHER_SERVICE);
if (result != null) {
binder = (IBinder) result;
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
Log.d(TAG, "Failed to get lineageos weather service", e);
}
if (binder != null) {
sWeatherManagerService = ILineageWeatherManager.Stub.asInterface(binder);
@@ -129,7 +129,11 @@ public class LineageOsWeatherReceiver extends BroadcastReceiver implements Linea
}
private void requestWeather() {
final LineageWeatherManager weatherManager = LineageWeatherManager.getInstance(GBApplication.getContext());
final LineageWeatherManager weatherManager = LineageWeatherManager.getInstance(mContext);
if (weatherManager == null) {
LOG.warn("Unable to request weather - weatherManager is null");
return;
}
if (weatherManager.getActiveWeatherServiceProviderLabel() != null && weatherLocation != null) {
weatherManager.requestWeatherUpdate(weatherLocation, this);
}