Created
January 2, 2018 15:54
-
-
Save slm/4184b5bb00e13dadcf44699d88f8171d to your computer and use it in GitHub Desktop.
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
package com.taxi724.taximeterv2.navigation; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.preference.PreferenceManager; | |
import com.google.gson.GsonBuilder; | |
import com.mapbox.api.directions.v5.DirectionsAdapterFactory; | |
import com.mapbox.services.android.navigation.ui.v5.NavigationActivity; | |
import com.mapbox.services.android.navigation.ui.v5.NavigationViewOptions; | |
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants; | |
import com.mapbox.services.android.navigation.v5.navigation.NavigationUnitType; | |
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_VIEW_DESTINATION_LAT_KEY; | |
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_VIEW_ORIGIN_LNG_KEY; | |
/** | |
* Created by selimyildiz on 28.12.2017. | |
*/ | |
public class NavigationUtils | |
{ | |
public static void setNavigation(Activity activity, NavigationViewOptions options) { | |
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activity); | |
SharedPreferences.Editor editor = preferences.edit(); | |
storeRouteOptions(options, editor); | |
editor.putString(NavigationConstants.NAVIGATION_VIEW_AWS_POOL_ID, options.awsPoolId()); | |
editor.putBoolean(NavigationConstants.NAVIGATION_VIEW_SIMULATE_ROUTE, false); | |
editor.putInt(NavigationConstants.NAVIGATION_VIEW_UNIT_TYPE, NavigationUnitType.TYPE_METRIC); | |
editor.apply(); | |
} | |
private static void setThemePreferences(NavigationViewOptions options, SharedPreferences.Editor editor) { | |
boolean preferenceThemeSet = options.lightThemeResId() != null || options.darkThemeResId() != null; | |
editor.putBoolean(NavigationConstants.NAVIGATION_VIEW_PREFERENCE_SET_THEME, preferenceThemeSet); | |
if (preferenceThemeSet) { | |
editor.putInt(NavigationConstants.NAVIGATION_VIEW_LIGHT_THEME, options.lightThemeResId()); | |
editor.putInt(NavigationConstants.NAVIGATION_VIEW_DARK_THEME, options.darkThemeResId()); | |
} | |
} | |
private static void storeRouteOptions(NavigationViewOptions options, SharedPreferences.Editor editor) { | |
if (options.directionsRoute() != null) { | |
storeDirectionsRouteValue(options, editor); | |
} else if (options.origin() != null && options.destination() != null) { | |
storeCoordinateValues(options, editor); | |
} else { | |
throw new RuntimeException("A valid DirectionsRoute or origin and " | |
+ "destination must be provided in NavigationViewOptions"); | |
} | |
} | |
private static void storeDirectionsRouteValue(NavigationViewOptions options, SharedPreferences.Editor editor) { | |
editor.putString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, new GsonBuilder() | |
.registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create().toJson(options.directionsRoute())); | |
} | |
private static void storeCoordinateValues(NavigationViewOptions options, SharedPreferences.Editor editor) { | |
// Reset any previous value for the route json | |
editor.putString(NavigationConstants.NAVIGATION_VIEW_ROUTE_KEY, ""); | |
// Store the coordinates | |
editor.putLong(NavigationConstants.NAVIGATION_VIEW_ORIGIN_LAT_KEY, | |
Double.doubleToRawLongBits(options.origin().latitude())); | |
editor.putLong(NAVIGATION_VIEW_ORIGIN_LNG_KEY, | |
Double.doubleToRawLongBits(options.origin().longitude())); | |
editor.putLong(NAVIGATION_VIEW_DESTINATION_LAT_KEY, | |
Double.doubleToRawLongBits(options.destination().latitude())); | |
editor.putLong(NavigationConstants.NAVIGATION_VIEW_DESTINATION_LNG_KEY, | |
Double.doubleToRawLongBits(options.destination().longitude())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment