Skip to content

Commit

Permalink
Update for Karoo version 1.346.1384 (#43)
Browse files Browse the repository at this point in the history
* Fixes for version 1.346.1384

* Improved notification and audio

* Updated readme

* Removed new Api method call
  • Loading branch information
valterc authored Dec 2, 2022
1 parent d35bf4b commit 004e4df
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ private static boolean triggerLowBatteryAudioAlert_1(SdkContext context) {
Class<? extends Enum> audioAlertClass = (Class<? extends Enum>) 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;
}

Expand Down

0 comments on commit 004e4df

Please sign in to comment.