Skip to content

Commit

Permalink
Add support for Karoo themes (dark/white); Improved custom views (#27)
Browse files Browse the repository at this point in the history
* Improve data types

* Add support for white theme; Improved views
  • Loading branch information
valterc authored Oct 15, 2022
1 parent ef3c9c3 commit c6f8444
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class ShiftCountTextFormatter extends SdkFormatter {
private int shiftCount;

private final Consumer<ShiftingInfo> shiftingInfoConsumer = shiftingInfo -> {
if (shiftingInfo == null) {
return;
}

if (lastShiftingInfo == null){
lastShiftingInfo = shiftingInfo;
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public String formatValue(double value) {
return NumericTextFormatterConstants.NOT_AVAILABLE;
}

if (shiftingInfo == null) {
if (shiftingInfo == null || shiftingInfo.getShiftingMode() == null) {
return NumericTextFormatterConstants.WAITING_FOR_DATA;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,45 @@

import java.util.function.Consumer;

import io.hammerhead.sdk.v0.datatype.view.SdkView;

public class DrivetrainSdkView extends SdkView {
public class DrivetrainSdkView extends Ki2SdkView {

private final Consumer<ConnectionInfo> connectionInfoConsumer = connectionInfo -> {
connectionStatus = connectionInfo.getConnectionStatus();
};

private final Consumer<ShiftingInfo> shiftingInfoConsumer = shiftingInfo -> {
this.shiftingInfo = shiftingInfo;
updateDrivetrainView();
};

private ConnectionStatus connectionStatus;
private ShiftingInfo shiftingInfo;

private TextView textView;
private DrivetrainView drivetrainView;

public DrivetrainSdkView(@NonNull Ki2Context context) {
super(context.getSdkContext());
super(context);
context.getServiceClient().registerConnectionInfoListener(connectionInfoConsumer);
context.getServiceClient().registerShiftingInfoListener(shiftingInfoConsumer);
}

@NonNull
@Override
protected View createView(@NonNull LayoutInflater layoutInflater, @NonNull ViewGroup parent) {
return layoutInflater.inflate(R.layout.view_karoo_drivetrain, parent, false);
View inflatedView = layoutInflater.inflate(R.layout.view_karoo_drivetrain, parent, false);
textView = inflatedView.findViewById(R.id.textview_karoo_drivetrain_waiting_for_data);
drivetrainView = inflatedView.findViewById(R.id.drivetrainview_karoo_drivetrain);

KarooTheme karooTheme = getKarooTheme(parent);

if (karooTheme == KarooTheme.WHITE) {
textView.setTextColor(getContext().getColor(R.color.hh_black));
drivetrainView.setTextColor(getContext().getColor(R.color.hh_black));
drivetrainView.setChainColor(getContext().getColor(R.color.hh_black));
}

return inflatedView;
}

@Override
Expand All @@ -50,21 +64,25 @@ public void onInvalid(@NonNull View view) {

@Override
public void onUpdate(@NonNull View view, double value, @Nullable String formattedValue) {
TextView textView = view.findViewById(R.id.textview_karoo_drivetrain_waiting_for_data);
DrivetrainView drivetrainView = view.findViewById(R.id.drivetrainview_karoo_drivetrain);

if (connectionStatus != ConnectionStatus.ESTABLISHED || shiftingInfo == null) {
drivetrainView.setVisibility(View.INVISIBLE);
textView.setVisibility(View.VISIBLE);
textView.setTextColor(getContext().getColor(R.color.hh_red));
} else {
textView.setVisibility(View.INVISIBLE);
drivetrainView.setVisibility(View.VISIBLE);
updateDrivetrainView();
}
}

drivetrainView.setRearGearMax(shiftingInfo.getRearGearMax());
drivetrainView.setFrontGearMax(shiftingInfo.getFrontGearMax());
drivetrainView.setRearGear(shiftingInfo.getRearGear());
drivetrainView.setFrontGear(shiftingInfo.getFrontGear());
private void updateDrivetrainView(){
if (drivetrainView == null || shiftingInfo == null) {
return;
}

drivetrainView.setGears(
shiftingInfo.getFrontGearMax(),
shiftingInfo.getFrontGear(),
shiftingInfo.getRearGearMax(),
shiftingInfo.getRearGear());
}
}
44 changes: 31 additions & 13 deletions app/src/main/java/com/valterc/ki2/karoo/views/GearsSdkView.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,45 @@

import java.util.function.Consumer;

import io.hammerhead.sdk.v0.datatype.view.SdkView;

public class GearsSdkView extends SdkView {
public class GearsSdkView extends Ki2SdkView {

private final Consumer<ConnectionInfo> connectionInfoConsumer = connectionInfo -> {
connectionStatus = connectionInfo.getConnectionStatus();
};

private final Consumer<ShiftingInfo> shiftingInfoConsumer = shiftingInfo -> {
this.shiftingInfo = shiftingInfo;
updateGearsView();
};

private ConnectionStatus connectionStatus;
private ShiftingInfo shiftingInfo;

private TextView textView;
private GearsView gearsView;

public GearsSdkView(@NonNull Ki2Context context) {
super(context.getSdkContext());
super(context);
context.getServiceClient().registerConnectionInfoListener(connectionInfoConsumer);
context.getServiceClient().registerShiftingInfoListener(shiftingInfoConsumer);
}

@NonNull
@Override
protected View createView(@NonNull LayoutInflater layoutInflater, @NonNull ViewGroup parent) {
return layoutInflater.inflate(R.layout.view_karoo_gears, parent, false);
View inflatedView = layoutInflater.inflate(R.layout.view_karoo_gears, parent, false);
textView = inflatedView.findViewById(R.id.textview_karoo_gears_waiting_for_data);
gearsView = inflatedView.findViewById(R.id.gearsview_karoo_gears);

KarooTheme karooTheme = getKarooTheme(parent);

if (karooTheme == KarooTheme.WHITE) {
textView.setTextColor(getContext().getColor(R.color.hh_black));
gearsView.setTextColor(getContext().getColor(R.color.hh_black));
gearsView.setUnselectedGearBorderColor(getContext().getColor(R.color.hh_gears_border_dark));
}

return inflatedView;
}

@Override
Expand All @@ -50,21 +64,25 @@ public void onInvalid(@NonNull View view) {

@Override
public void onUpdate(@NonNull View view, double value, @Nullable String formattedValue) {
TextView textView = view.findViewById(R.id.textview_karoo_gears_waiting_for_data);
GearsView gearsView = view.findViewById(R.id.gearsview_karoo_gears);

if (connectionStatus != ConnectionStatus.ESTABLISHED || shiftingInfo == null) {
gearsView.setVisibility(View.INVISIBLE);
textView.setVisibility(View.VISIBLE);
textView.setTextColor(getContext().getColor(R.color.hh_red));
} else {
textView.setVisibility(View.INVISIBLE);
gearsView.setVisibility(View.VISIBLE);
updateGearsView();
}
}

gearsView.setRearGearMax(shiftingInfo.getRearGearMax());
gearsView.setFrontGearMax(shiftingInfo.getFrontGearMax());
gearsView.setRearGear(shiftingInfo.getRearGear());
gearsView.setFrontGear(shiftingInfo.getFrontGear());
private void updateGearsView() {
if (gearsView == null || shiftingInfo == null) {
return;
}

gearsView.setGears(
shiftingInfo.getFrontGearMax(),
shiftingInfo.getFrontGear(),
shiftingInfo.getRearGearMax(),
shiftingInfo.getRearGear());
}
}
22 changes: 22 additions & 0 deletions app/src/main/java/com/valterc/ki2/karoo/views/KarooTheme.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.valterc.ki2.karoo.views;

/**
* Karoo ride theme.
*/
public enum KarooTheme {

/**
* Unknown theme.
*/
UNKNOWN,

/**
* Dark theme. Ride application has a dark background and white text.
*/
DARK,

/**
* White theme. Ride application has a white background and dark text.
*/
WHITE
}
86 changes: 86 additions & 0 deletions app/src/main/java/com/valterc/ki2/karoo/views/Ki2SdkView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.valterc.ki2.karoo.views;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewParent;

import androidx.annotation.NonNull;
import androidx.core.graphics.ColorUtils;

import com.valterc.ki2.karoo.Ki2Context;

import io.hammerhead.sdk.v0.datatype.view.SdkView;

@SuppressLint("LogNotTimber")
public abstract class Ki2SdkView extends SdkView {

public static final int COLOR_MAX_DISTANCE = 50;

private final Ki2Context ki2Context;

public Ki2SdkView(@NonNull Ki2Context ki2Context) {
super(ki2Context.getSdkContext());
this.ki2Context = ki2Context;
}

private Integer getFirstNonTransparentBackgroundColor(View view) {
if (view == null) {
return null;
}

Drawable backgroundDrawable = view.getBackground();

if (backgroundDrawable instanceof ColorDrawable) {
ColorDrawable colorDrawable = ((ColorDrawable) backgroundDrawable);
if (colorDrawable.getAlpha() != 0) {
return colorDrawable.getColor();
}
}

ViewParent viewParent = view.getParent();
if (viewParent instanceof View) {
return getFirstNonTransparentBackgroundColor((View) viewParent);
}

return null;
}

protected KarooTheme getKarooTheme(View karooHierarchyView){
if (karooHierarchyView == null) {
return KarooTheme.UNKNOWN;
}

Integer backgroundColor = getFirstNonTransparentBackgroundColor(karooHierarchyView);
if (backgroundColor == null){
return KarooTheme.UNKNOWN;
}

double[] backgroundColorLAB = new double[3];
double[] testColorLAB = new double[3];
ColorUtils.colorToLAB(backgroundColor, backgroundColorLAB);
ColorUtils.colorToLAB(Color.BLACK, testColorLAB);

if (ColorUtils.distanceEuclidean(backgroundColorLAB, testColorLAB) <= COLOR_MAX_DISTANCE) {
Log.d("KI2", "Karoo theme: " + KarooTheme.DARK);
return KarooTheme.DARK;
}

ColorUtils.colorToLAB(Color.WHITE, testColorLAB);
if (ColorUtils.distanceEuclidean(backgroundColorLAB, testColorLAB) <= COLOR_MAX_DISTANCE) {
Log.d("KI2", "Karoo theme: " + KarooTheme.WHITE);
return KarooTheme.WHITE;
}

Log.d("KI2", "Karoo theme: " + KarooTheme.UNKNOWN);
return KarooTheme.UNKNOWN;
}

public Ki2Context getKi2Context() {
return ki2Context;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private void initTextPaint() {
private int measureTextHeight(float padding) {
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.getTextBounds(STRING_MEASURE, 0, STRING_MEASURE.length(), tempRect);
return (int) (tempRect.height() + padding);
return (int) (tempRect.height() + padding + 0.5);
}

@Override
Expand Down
Loading

0 comments on commit c6f8444

Please sign in to comment.