From 2c7762dfae64577fb14f0c3fab1e109ee5ca52da Mon Sep 17 00:00:00 2001 From: danesfeder Date: Wed, 15 Nov 2017 18:17:05 -0500 Subject: [PATCH 1/4] Add custom themes via XML for light / dark mode --- .../android/navigation/ui/v5/NavigationView.java | 2 +- .../android/navigation/ui/v5/ThemeSwitcher.java | 12 ++++++++++-- .../src/main/res/layout/activity_navigation.xml | 2 ++ .../src/main/res/values/attrs.xml | 8 ++++++++ .../src/main/res/values/styles.xml | 4 ++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java index 1fa5d03be54..cda9824b983 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java @@ -94,7 +94,7 @@ public NavigationView(Context context, @Nullable AttributeSet attrs) { public NavigationView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO); - ThemeSwitcher.setTheme(getContext()); + ThemeSwitcher.setTheme(getContext(), attrs); init(); } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index 2200be94404..03efed7419b 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -7,6 +7,7 @@ import android.content.res.TypedArray; import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; +import android.util.AttributeSet; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; @@ -23,8 +24,9 @@ public class ThemeSwitcher { * and set the theme colors accordingly. * * @param context {@link NavigationView} where the theme will be set + * @param attrs holding custom styles if any are set */ - static void setTheme(Context context) { + static void setTheme(Context context, AttributeSet attrs) { int uiMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; boolean darkThemeEnabled = uiMode == Configuration.UI_MODE_NIGHT_YES; @@ -32,7 +34,13 @@ static void setTheme(Context context) { SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled); editor.apply(); - context.setTheme(darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight); + + TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NavigationView); + int lightTheme = array.getResourceId(R.styleable.NavigationView_navigationLightTheme, R.style.NavigationViewLight); + int darkTheme = array.getResourceId(R.styleable.NavigationView_navigationDarkTheme, R.style.NavigationViewDark); + array.recycle(); + + context.setTheme(darkThemeEnabled ? darkTheme : lightTheme); } /** diff --git a/libandroid-navigation-ui/src/main/res/layout/activity_navigation.xml b/libandroid-navigation-ui/src/main/res/layout/activity_navigation.xml index 2edb5b1bfbe..40bd1841a6f 100644 --- a/libandroid-navigation-ui/src/main/res/layout/activity_navigation.xml +++ b/libandroid-navigation-ui/src/main/res/layout/activity_navigation.xml @@ -9,6 +9,8 @@ android:id="@+id/navigationView" android:layout_width="0dp" android:layout_height="0dp" + app:navigationLightTheme="@style/NavigationViewLight" + app:navigationDarkTheme="@style/NavigationViewDark" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" diff --git a/libandroid-navigation-ui/src/main/res/values/attrs.xml b/libandroid-navigation-ui/src/main/res/values/attrs.xml index 94163376033..aa9ad5c7382 100644 --- a/libandroid-navigation-ui/src/main/res/values/attrs.xml +++ b/libandroid-navigation-ui/src/main/res/values/attrs.xml @@ -41,8 +41,16 @@ + + + + + + + + \ No newline at end of file diff --git a/libandroid-navigation-ui/src/main/res/values/styles.xml b/libandroid-navigation-ui/src/main/res/values/styles.xml index 1035fc846fc..fd8802e0eba 100644 --- a/libandroid-navigation-ui/src/main/res/values/styles.xml +++ b/libandroid-navigation-ui/src/main/res/values/styles.xml @@ -29,6 +29,8 @@ @color/mapbox_navigation_view_color_progress @color/mapbox_navigation_view_color_progress_background + + @string/navigation_guidance_day_v2 \ No newline at end of file From a1e9f6330855288b5bc2c16fd533dbe4a9ec6e7f Mon Sep 17 00:00:00 2001 From: danesfeder Date: Thu, 16 Nov 2017 10:46:21 -0500 Subject: [PATCH 2/4] Update theme switcher to get attributes from the current set theme --- .../navigation/ui/v5/ThemeSwitcher.java | 111 +++++------------- 1 file changed, 32 insertions(+), 79 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index 03efed7419b..56204973bbb 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -8,6 +8,7 @@ import android.preference.PreferenceManager; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; +import android.util.TypedValue; import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.IconFactory; @@ -35,10 +36,12 @@ static void setTheme(Context context, AttributeSet attrs) { editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled); editor.apply(); - TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.NavigationView); - int lightTheme = array.getResourceId(R.styleable.NavigationView_navigationLightTheme, R.style.NavigationViewLight); - int darkTheme = array.getResourceId(R.styleable.NavigationView_navigationDarkTheme, R.style.NavigationViewDark); - array.recycle(); + TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.NavigationView); + int lightTheme = styledAttributes.getResourceId(R.styleable.NavigationView_navigationLightTheme, + R.style.NavigationViewLight); + int darkTheme = styledAttributes.getResourceId(R.styleable.NavigationView_navigationDarkTheme, + R.style.NavigationViewDark); + styledAttributes.recycle(); context.setTheme(darkThemeEnabled ? darkTheme : lightTheme); } @@ -64,11 +67,10 @@ static void toggleTheme(Activity activity) { * @param map the style will be set on */ static void setMapStyle(Context context, MapboxMap map, MapboxMap.OnStyleLoadedListener listener) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - String nightThemeUrl = context.getString(R.string.navigation_guidance_night_v2); - String dayThemeUrl = context.getString(R.string.navigation_guidance_day_v2); - map.setStyleUrl(darkThemeEnabled ? nightThemeUrl : dayThemeUrl, listener); + TypedValue mapStyleAttr = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewMapStyle, mapStyleAttr, true); + String styleUrl = mapStyleAttr.string.toString(); + map.setStyleUrl(styleUrl, listener); } /** @@ -92,16 +94,9 @@ static Icon retrieveMapMarker(Context context) { * @return color resource identifier for primary theme color */ public static int retrieveNavigationViewPrimaryColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int navigationViewPrimary = styleArray.getColor(R.styleable.NavigationView_navigationViewPrimary, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_primary)); - styleArray.recycle(); - return navigationViewPrimary; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewPrimary, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -112,16 +107,9 @@ public static int retrieveNavigationViewPrimaryColor(Context context) { * @return color resource identifier for secondary theme color */ public static int retrieveNavigationViewSecondaryColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int navigationViewSecondary = styleArray.getColor(R.styleable.NavigationView_navigationViewSecondary, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_secondary)); - styleArray.recycle(); - return navigationViewSecondary; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewSecondary, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -132,16 +120,9 @@ public static int retrieveNavigationViewSecondaryColor(Context context) { * @return color resource identifier for banner background color */ public static int retrieveNavigationViewBannerBackgroundColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int bannerBackground = styleArray.getColor(R.styleable.NavigationView_navigationViewBannerBackground, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_banner_background)); - styleArray.recycle(); - return bannerBackground; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewBannerBackground, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -152,16 +133,9 @@ public static int retrieveNavigationViewBannerBackgroundColor(Context context) { * @return color resource identifier for banner maneuver primary theme color */ public static int retrieveNavigationViewBannerManeuverPrimaryColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int bannerManeuverPrimary = styleArray.getColor(R.styleable.NavigationView_navigationViewBannerManeuverPrimary, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_banner_maneuver_primary)); - styleArray.recycle(); - return bannerManeuverPrimary; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverPrimary, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -172,16 +146,9 @@ public static int retrieveNavigationViewBannerManeuverPrimaryColor(Context conte * @return color resource identifier for banner maneuver secondary theme color */ public static int retrieveNavigationViewBannerManeuverSecondaryColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int bannerManeuverSecondary = styleArray.getColor(R.styleable.NavigationView_navigationViewBannerManeuverSecondary, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_banner_maneuver_secondary)); - styleArray.recycle(); - return bannerManeuverSecondary; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverSecondary, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -192,16 +159,9 @@ public static int retrieveNavigationViewBannerManeuverSecondaryColor(Context con * @return color resource identifier for progress color */ public static int retrieveNavigationViewProgressColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int progress = styleArray.getColor(R.styleable.NavigationView_navigationViewProgress, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_progress)); - styleArray.recycle(); - return progress; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewProgress, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } /** @@ -212,15 +172,8 @@ public static int retrieveNavigationViewProgressColor(Context context) { * @return color resource identifier for progress background color */ public static int retrieveNavigationViewProgressBackgroundColor(Context context) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - boolean darkThemeEnabled = preferences.getBoolean(context.getString(R.string.dark_theme_enabled), false); - TypedArray styleArray = context.obtainStyledAttributes( - darkThemeEnabled ? R.style.NavigationViewDark : R.style.NavigationViewLight, - R.styleable.NavigationView - ); - int progressBackground = styleArray.getColor(R.styleable.NavigationView_navigationViewProgressBackground, - ContextCompat.getColor(context, R.color.mapbox_navigation_view_color_progress_background)); - styleArray.recycle(); - return progressBackground; + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(R.attr.navigationViewProgressBackground, outValue, true); + return ContextCompat.getColor(context, outValue.resourceId); } } \ No newline at end of file From ce3d1b23fae7d15f9ea18fd86a3be1378a86dd5d Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 21 Nov 2017 11:02:46 -0500 Subject: [PATCH 3/4] Consolidated theme methods and created attribute for route style --- .../navigation/ui/v5/NavigationView.java | 4 +- .../navigation/ui/v5/ThemeSwitcher.java | 108 +++--------------- .../navigation/ui/v5/alert/AlertView.java | 6 +- .../ui/v5/feedback/FeedbackBottomSheet.java | 6 +- .../ui/v5/feedback/FeedbackViewHolder.java | 3 +- .../ui/v5/instruction/InstructionView.java | 6 +- .../v5/instruction/maneuver/ManeuverView.java | 7 +- .../v5/instruction/turnlane/TurnLaneView.java | 7 +- .../src/main/res/values/attrs.xml | 3 + .../src/main/res/values/styles.xml | 4 + 10 files changed, 50 insertions(+), 104 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java index cda9824b983..16b18a4ba5d 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/NavigationView.java @@ -359,7 +359,9 @@ private void resetBottomSheetState(int bottomSheetState) { * route. */ private void initRoute() { - mapRoute = new NavigationMapRoute(mapView, map, NavigationConstants.ROUTE_BELOW_LAYER); + int routeStyleRes = ThemeSwitcher.retrieveNavigationViewRouteStyle(getContext()); + mapRoute = new NavigationMapRoute(null, mapView, map, + routeStyleRes, NavigationConstants.ROUTE_BELOW_LAYER); } /** diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index 56204973bbb..f22d77d14d5 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -1,6 +1,5 @@ package com.mapbox.services.android.navigation.ui.v5; -import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.res.Configuration; @@ -31,10 +30,7 @@ static void setTheme(Context context, AttributeSet attrs) { int uiMode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; boolean darkThemeEnabled = uiMode == Configuration.UI_MODE_NIGHT_YES; - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); - SharedPreferences.Editor editor = preferences.edit(); - editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled); - editor.apply(); + updatePreferencesDarkEnabled(context, darkThemeEnabled); TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.NavigationView); int lightTheme = styledAttributes.getResourceId(R.styleable.NavigationView_navigationLightTheme, @@ -46,20 +42,6 @@ static void setTheme(Context context, AttributeSet attrs) { context.setTheme(darkThemeEnabled ? darkTheme : lightTheme); } - /** - * Can be called to toggle the theme based on the current theme setting. - * - * @param activity {@link NavigationView} where the theme will be set - */ - static void toggleTheme(Activity activity) { - SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); - boolean darkThemeEnabled = preferences.getBoolean(activity.getString(R.string.dark_theme_enabled), false); - SharedPreferences.Editor editor = preferences.edit(); - editor.putBoolean(activity.getString(R.string.dark_theme_enabled), !darkThemeEnabled); - editor.apply(); - activity.recreate(); - } - /** * Sets the {@link MapView} style based on the current theme setting. * @@ -87,93 +69,35 @@ static Icon retrieveMapMarker(Context context) { } /** - * Looks are current theme and retrieves the primary color + * Looks are current theme and retrieves the color attribute * for the given set theme. * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} + * @param context to retrieve the set theme and resolved attribute and then color res Id with {@link ContextCompat} * @return color resource identifier for primary theme color */ - public static int retrieveNavigationViewPrimaryColor(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewPrimary, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); - } - - /** - * Looks are current theme and retrieves the secondary color - * for the given set theme. - * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for secondary theme color - */ - public static int retrieveNavigationViewSecondaryColor(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewSecondary, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); - } - - /** - * Looks are current theme and retrieves the banner background color - * for the given set theme. - * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for banner background color - */ - public static int retrieveNavigationViewBannerBackgroundColor(Context context) { + public static int retrieveNavigationViewThemeColor(Context context, int resId) { TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewBannerBackground, outValue, true); + context.getTheme().resolveAttribute(resId, outValue, true); return ContextCompat.getColor(context, outValue.resourceId); } /** - * Looks are current theme and retrieves the banner maneuver primary color + * Looks are current theme and retrieves the route style * for the given set theme. * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for banner maneuver primary theme color + * @param context to retrieve the resolved attribute + * @return style resource Id for the route */ - public static int retrieveNavigationViewBannerManeuverPrimaryColor(Context context) { + public static int retrieveNavigationViewRouteStyle(Context context) { TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverPrimary, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); - } - - /** - * Looks are current theme and retrieves the banner maneuver secondary color - * for the given set theme. - * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for banner maneuver secondary theme color - */ - public static int retrieveNavigationViewBannerManeuverSecondaryColor(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewBannerManeuverSecondary, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); + context.getTheme().resolveAttribute(R.attr.navigationViewRouteStyle, outValue, true); + return outValue.resourceId; } - /** - * Looks are current theme and retrieves the progress color - * for the given set theme. - * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for progress color - */ - public static int retrieveNavigationViewProgressColor(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewProgress, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); - } - - /** - * Looks are current theme and retrieves the progress background color - * for the given set theme. - * - * @param context to retrieve {@link SharedPreferences} and color with {@link ContextCompat} - * @return color resource identifier for progress background color - */ - public static int retrieveNavigationViewProgressBackgroundColor(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewProgressBackground, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); + private static void updatePreferencesDarkEnabled(Context context, boolean darkThemeEnabled) { + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences.Editor editor = preferences.edit(); + editor.putBoolean(context.getString(R.string.dark_theme_enabled), darkThemeEnabled); + editor.apply(); } } \ No newline at end of file diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/alert/AlertView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/alert/AlertView.java index 9eb86ed5cc4..1f3f7b4ceb6 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/alert/AlertView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/alert/AlertView.java @@ -125,8 +125,10 @@ private void initAnimations() { private void initBackground() { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - int progressColor = ThemeSwitcher.retrieveNavigationViewProgressColor(getContext()); - int progressBackgroundColor = ThemeSwitcher.retrieveNavigationViewProgressBackgroundColor(getContext()); + int progressColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewProgress); + int progressBackgroundColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewProgressBackground); LayerDrawable progressBarDrawable = (LayerDrawable) alertProgressBar.getProgressDrawable(); // ProgressBar progress color diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackBottomSheet.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackBottomSheet.java index 79dc4c0ac94..8cc3a49ef4e 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackBottomSheet.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackBottomSheet.java @@ -167,8 +167,10 @@ public void onAnimationRepeat(Animator animation) { private void initBackground(View view) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewPrimaryColor(getContext()); - int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewSecondaryColor(getContext()); + int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewPrimary); + int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewSecondary); // BottomSheet background Drawable bottomSheetBackground = DrawableCompat.wrap(view.getBackground()).mutate(); DrawableCompat.setTint(bottomSheetBackground, navigationViewPrimaryColor); diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackViewHolder.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackViewHolder.java index 796d7436388..8152324551a 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackViewHolder.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/feedback/FeedbackViewHolder.java @@ -31,7 +31,8 @@ void setFeedbackText(String feedbackText) { private void initTextColor(TextView feedbackText) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewSecondaryColor(feedbackText.getContext()); + int navigationViewSecondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(feedbackText.getContext(), + R.attr.navigationViewSecondary); // Text color feedbackText.setTextColor(navigationViewSecondaryColor); } diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/InstructionView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/InstructionView.java index 885d35bc227..6f613740379 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/InstructionView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/InstructionView.java @@ -350,8 +350,10 @@ private void bind() { */ private void initBackground() { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) { - int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewPrimaryColor(getContext()); - int navigationViewBannerBackgroundColor = ThemeSwitcher.retrieveNavigationViewBannerBackgroundColor(getContext()); + int navigationViewPrimaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewPrimary); + int navigationViewBannerBackgroundColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewBannerBackground); // Instruction Layout landscape - banner background if (getContext().getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) { View instructionLayoutManeuver = findViewById(R.id.instructionManeuverLayout); diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/maneuver/ManeuverView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/maneuver/ManeuverView.java index b2acaa9c9d2..c8c8f14f6d2 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/maneuver/ManeuverView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/maneuver/ManeuverView.java @@ -7,6 +7,7 @@ import android.util.AttributeSet; import android.view.View; +import com.mapbox.services.android.navigation.ui.v5.R; import com.mapbox.services.android.navigation.ui.v5.ThemeSwitcher; import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants; @@ -65,8 +66,10 @@ protected void onFinishInflate() { } private void initManeuverColor() { - this.primaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverPrimaryColor(getContext()); - this.secondaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverSecondaryColor(getContext()); + this.primaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewBannerManeuverPrimary); + this.secondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewBannerManeuverSecondary); } public void setManeuverType(String maneuverType) { diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/turnlane/TurnLaneView.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/turnlane/TurnLaneView.java index f44028091e4..ff85628ad9f 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/turnlane/TurnLaneView.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/instruction/turnlane/TurnLaneView.java @@ -9,6 +9,7 @@ import android.view.View; import com.mapbox.directions.v5.models.IntersectionLanes; +import com.mapbox.services.android.navigation.ui.v5.R; import com.mapbox.services.android.navigation.ui.v5.ThemeSwitcher; import com.mapbox.services.commons.utils.TextUtils; @@ -114,7 +115,9 @@ public void updateLaneView(@NonNull IntersectionLanes lane, @NonNull String mane } private void initManeuverColor() { - this.primaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverPrimaryColor(getContext()); - this.secondaryColor = ThemeSwitcher.retrieveNavigationViewBannerManeuverSecondaryColor(getContext()); + this.primaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewBannerManeuverPrimary); + this.secondaryColor = ThemeSwitcher.retrieveNavigationViewThemeColor(getContext(), + R.attr.navigationViewBannerManeuverSecondary); } } diff --git a/libandroid-navigation-ui/src/main/res/values/attrs.xml b/libandroid-navigation-ui/src/main/res/values/attrs.xml index aa9ad5c7382..d566736b544 100644 --- a/libandroid-navigation-ui/src/main/res/values/attrs.xml +++ b/libandroid-navigation-ui/src/main/res/values/attrs.xml @@ -48,6 +48,9 @@ + + + diff --git a/libandroid-navigation-ui/src/main/res/values/styles.xml b/libandroid-navigation-ui/src/main/res/values/styles.xml index fd8802e0eba..0ba51c00505 100644 --- a/libandroid-navigation-ui/src/main/res/values/styles.xml +++ b/libandroid-navigation-ui/src/main/res/values/styles.xml @@ -30,6 +30,8 @@ @color/mapbox_navigation_view_color_progress @color/mapbox_navigation_view_color_progress_background + @style/NavigationMapRoute + @string/navigation_guidance_day_v2 @@ -51,6 +53,8 @@ @color/mapbox_navigation_view_color_progress_dark @color/mapbox_navigation_view_color_progress_background_dark + @style/NavigationMapRoute + @string/navigation_guidance_night_v2 From 5e37e0f60350c3354f7e722abfd656025eb0fc27 Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 21 Nov 2017 11:52:54 -0500 Subject: [PATCH 4/4] Extract typed value logic to own method --- .../navigation/ui/v5/ThemeSwitcher.java | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java index f22d77d14d5..463f80fc757 100644 --- a/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java +++ b/libandroid-navigation-ui/src/main/java/com/mapbox/services/android/navigation/ui/v5/ThemeSwitcher.java @@ -5,6 +5,7 @@ import android.content.res.Configuration; import android.content.res.TypedArray; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.support.v4.content.ContextCompat; import android.util.AttributeSet; import android.util.TypedValue; @@ -19,6 +20,18 @@ */ public class ThemeSwitcher { + /** + * Looks are current theme and retrieves the color attribute + * for the given set theme. + * + * @param context to retrieve the set theme and resolved attribute and then color res Id with {@link ContextCompat} + * @return color resource identifier for primary theme color + */ + public static int retrieveNavigationViewThemeColor(Context context, int resId) { + TypedValue outValue = obtainTypedValue(context, resId); + return ContextCompat.getColor(context, outValue.resourceId); + } + /** * Called in onCreate() to check the UI Mode (day or night) * and set the theme colors accordingly. @@ -49,8 +62,7 @@ static void setTheme(Context context, AttributeSet attrs) { * @param map the style will be set on */ static void setMapStyle(Context context, MapboxMap map, MapboxMap.OnStyleLoadedListener listener) { - TypedValue mapStyleAttr = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewMapStyle, mapStyleAttr, true); + TypedValue mapStyleAttr = obtainTypedValue(context, R.attr.navigationViewMapStyle); String styleUrl = mapStyleAttr.string.toString(); map.setStyleUrl(styleUrl, listener); } @@ -68,19 +80,6 @@ static Icon retrieveMapMarker(Context context) { return iconFactory.fromResource(darkThemeEnabled ? R.drawable.map_marker_dark : R.drawable.map_marker_light); } - /** - * Looks are current theme and retrieves the color attribute - * for the given set theme. - * - * @param context to retrieve the set theme and resolved attribute and then color res Id with {@link ContextCompat} - * @return color resource identifier for primary theme color - */ - public static int retrieveNavigationViewThemeColor(Context context, int resId) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(resId, outValue, true); - return ContextCompat.getColor(context, outValue.resourceId); - } - /** * Looks are current theme and retrieves the route style * for the given set theme. @@ -88,12 +87,18 @@ public static int retrieveNavigationViewThemeColor(Context context, int resId) { * @param context to retrieve the resolved attribute * @return style resource Id for the route */ - public static int retrieveNavigationViewRouteStyle(Context context) { - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(R.attr.navigationViewRouteStyle, outValue, true); + static int retrieveNavigationViewRouteStyle(Context context) { + TypedValue outValue = obtainTypedValue(context, R.attr.navigationViewRouteStyle); return outValue.resourceId; } + @NonNull + private static TypedValue obtainTypedValue(Context context, int resId) { + TypedValue outValue = new TypedValue(); + context.getTheme().resolveAttribute(resId, outValue, true); + return outValue; + } + private static void updatePreferencesDarkEnabled(Context context, boolean darkThemeEnabled) { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences.Editor editor = preferences.edit();