From 004e4df06fca739036c08e2c74b49678e172d73b Mon Sep 17 00:00:00 2001 From: Valter Costa Date: Fri, 2 Dec 2022 16:38:43 +0000 Subject: [PATCH] Update for Karoo version 1.346.1384 (#43) * Fixes for version 1.346.1384 * Improved notification and audio * Updated readme * Removed new Api method call --- README.md | 1 + ...vityServiceNotificationControllerHook.java | 63 ++++++++++++++++++- .../ki2/karoo/hooks/KarooAudioAlertHook.java | 14 ++++- 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 68318197..9eebeeef 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Karoo software may change with new updates, for best experience please use the l | 1.335.1353 (Oct 20, 2022) | ✅ | 0.5 | | 1.342.1374 (Nov 17, 2022) | ✅ | 0.6 | | 1.344.1384 (Nov 18, 2022) | ✅ | 0.6 | +| 1.346.1384 (Dec 01, 2022) | ✅ | 0.7 | ## Known issues diff --git a/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooActivityServiceNotificationControllerHook.java b/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooActivityServiceNotificationControllerHook.java index 35a87738..6793b979 100644 --- a/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooActivityServiceNotificationControllerHook.java +++ b/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooActivityServiceNotificationControllerHook.java @@ -18,6 +18,11 @@ private KarooActivityServiceNotificationControllerHook() { } public static boolean showSensorLowBatteryNotification(SdkContext context, String deviceName) { + boolean result = showSensorLowBatteryNotification_1(context, deviceName); + return result || showSensorLowBatteryNotification_2(context, deviceName); + } + + private static boolean showSensorLowBatteryNotification_1(SdkContext context, String deviceName) { try { Class classActivityServiceApplication = Class.forName("io.hammerhead.activityservice.ActivityServiceApplication"); Method methodGetActivityComponent = classActivityServiceApplication.getMethod("getActivityComponent"); @@ -45,7 +50,63 @@ public static boolean showSensorLowBatteryNotification(SdkContext context, Strin throw new Exception("Unable to hook into notification publisher"); } catch (Exception e) { - Log.e("KI2", "Unable to publish notification: " + e); + Log.w("KI2", "Unable to publish notification using method 1: " + e); + } + + return false; + } + + private static boolean showSensorLowBatteryNotification_2(SdkContext context, String deviceName) { + try { + Class classActivityServiceApplication = Class.forName("io.hammerhead.activityservice.ActivityServiceApplication"); + Field[] fieldsInActivityServiceApplication = classActivityServiceApplication.getDeclaredFields(); + + for (Field fieldActivityComponent: fieldsInActivityServiceApplication) { + fieldActivityComponent.setAccessible(true); + Object activityComponent = fieldActivityComponent.get(context.getBaseContext()); + + if (activityComponent == null) { + continue; + } + + Method[] methodsInActivityComponent = activityComponent.getClass().getDeclaredMethods(); + for (Method method: methodsInActivityComponent) { + Field[] fieldsInNotificationController = method.getReturnType().getDeclaredFields(); + + if (fieldsInNotificationController.length == 0) { + continue; + } + + Object notificationController = method.invoke(activityComponent); + for (Field fieldNotificationSubject: fieldsInNotificationController) { + + if (fieldNotificationSubject.getType().getTypeParameters().length == 1 && + fieldNotificationSubject.getGenericType().toString().contains("Notification")) { + + fieldNotificationSubject.setAccessible(true); + Object notificationSubject = fieldNotificationSubject.get(notificationController); + + if (notificationSubject == null) { + continue; + } + + Method[] methodsInPublishSubject = notificationSubject.getClass().getDeclaredMethods(); + + for (Method methodOnNext: methodsInPublishSubject) { + Type[] types = methodOnNext.getGenericParameterTypes(); + if (types.length == 1 && types[0].toString().equals("T")) { + methodOnNext.invoke(notificationSubject, KarooNotificationHook.buildSensorLowBatteryNotification(context.getString(R.string.text_param_di2_name, deviceName))); + return true; + } + } + } + } + } + } + + throw new Exception("Unable to hook into notification publisher"); + } catch (Exception e) { + Log.w("KI2", "Unable to publish notification using method 2: " + e); } return false; diff --git a/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooAudioAlertHook.java b/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooAudioAlertHook.java index 747c88e8..2f5a30d4 100644 --- a/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooAudioAlertHook.java +++ b/app/src/main/java/com/valterc/ki2/karoo/hooks/KarooAudioAlertHook.java @@ -29,10 +29,18 @@ private static boolean triggerLowBatteryAudioAlert_1(SdkContext context) { Class audioAlertClass = (Class) Class.forName("io.hammerhead.datamodels.profiles.AudioAlert"); Enum audioAlertSensorBatteryLow = Enum.valueOf(audioAlertClass, "SENSOR_BATTERY_LOW"); - Method handleAlertMethod = audioAlertClass.getMethod("broadcast", Context.class, String.class); - handleAlertMethod.invoke(audioAlertSensorBatteryLow, context.getBaseContext(), null); + Method[] methodsAudioAlert = audioAlertClass.getMethods(); + + for (Method methodBroadcast: methodsAudioAlert) { + if (methodBroadcast.getParameterCount() == 2) { + Class[] parameterTypes = methodBroadcast.getParameterTypes(); + if (parameterTypes[0] == Context.class && parameterTypes[1] == String.class) { + methodBroadcast.invoke(audioAlertSensorBatteryLow, context.getBaseContext(), null); + } + } + } } catch (Exception e) { - Log.e("KI2", "Unable to trigger audio alert: " + e); + Log.e("KI2", "Unable to trigger audio alert using method 1: " + e); return false; }