-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Karoo themes (dark/white); Improved custom views (#27)
* Improve data types * Add support for white theme; Improved views
- Loading branch information
Showing
12 changed files
with
281 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/valterc/ki2/karoo/views/KarooTheme.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
86
app/src/main/java/com/valterc/ki2/karoo/views/Ki2SdkView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.