Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK] Add locale example #230

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
android:name=".DownloadTerritoriesActivity"
android:theme="@style/AppTheme"
android:windowSoftInputMode="adjustNothing" />
<activity
android:name=".LocaleSwitchActivity"
android:screenOrientation="portrait"
android:theme="@style/GenericActivityTheme"
tools:ignore="DiscouragedApi,LockedOrientationActivity" />

<meta-data
android:name="com.google.android.gms.car.application"
Expand Down
110 changes: 110 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/LocaleSwitchActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package ru.dgis.sdk.demo

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.LinearLayout
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
import ru.dgis.sdk.Context
import ru.dgis.sdk.coordinates.GeoPoint
import ru.dgis.sdk.map.BearingSource
import ru.dgis.sdk.map.CameraPosition
import ru.dgis.sdk.map.DgisSource
import ru.dgis.sdk.map.Map
import ru.dgis.sdk.map.MapOptions
import ru.dgis.sdk.map.MapView
import ru.dgis.sdk.map.MyLocationControllerSettings
import ru.dgis.sdk.map.MyLocationMapObjectSource
import ru.dgis.sdk.map.Zoom
import ru.dgis.sdk.platform.Locale
import ru.dgis.sdk.platform.getLocaleManager

/**
* Activity that demonstrates how to dynamically switch map locales in a DGis-based application.
* The locale changes are applied through a dropdown menu, and the map is recreated to reflect the
* changes.
*/
class LocaleSwitchActivity : AppCompatActivity() {
Sameri11 marked this conversation as resolved.
Show resolved Hide resolved
private val sdkContext: Context by lazy { application.sdkContext }
private lateinit var mapView: MapView
private lateinit var mapContainer: LinearLayout
private var map: Map? = null
private lateinit var mapSource: MyLocationMapObjectSource

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_locale_switch)

mapContainer = findViewById(R.id.map_container)
initSpinner(findViewById(R.id.spinnerLocale), R.array.locales_list, this::onLocaleItemSelected)
recreateMap()
}

/**
* Handles selection changes in the locale dropdown. Updates the locale settings in the
* DGis SDK and recreates the map to apply the changes.
*
* @param item The selected locale as a string.
*/
private fun onLocaleItemSelected(item: String) {
if (item == "System") {
getLocaleManager(sdkContext).overrideLocales(emptyList())
} else {
Locale.makeLocale(item)?.let {
getLocaleManager(sdkContext).overrideLocales(listOf(it))
}
}
recreateMap()
}

/**
* Recreates the map to reflect any changes in settings (e.g., locale). The current map view
* is removed, and a new one is initialized with the updated options.
*/
private fun recreateMap() {
map?.let { lifecycle.removeObserver(mapView) }
mapContainer.removeAllViews()

val mapOptions = MapOptions().apply {
position = CameraPosition(
GeoPoint(40.37741938, 49.87862621),
Zoom(9.0f)
)
sources = listOf(DgisSource.createDgisSource(sdkContext))
}

mapView = MapView(this, mapOptions).also {
it.getMapAsync { map ->
this.map = map
mapSource = MyLocationMapObjectSource(
sdkContext,
MyLocationControllerSettings(BearingSource.MAGNETIC)
)
map.addSource(mapSource)
}
mapContainer.addView(it)
lifecycle.addObserver(it)
}
}

/**
* Initializes a dropdown spinner with a list of items and a callback for selection changes.
*
* @param onItemSelected Callback invoked when an item is selected.
*/
private fun initSpinner(spinner: Spinner, itemsResource: Int, onItemSelected: (String) -> Unit) {
ArrayAdapter.createFromResource(this, itemsResource, R.layout.spinner_item).also { adapter ->
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
}

spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, pos: Int, id: Long) {
onItemSelected(parent?.getItemAtPosition(pos)?.toString() ?: "")
}
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/java/ru/dgis/sdk/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ class MainActivity : AppCompatActivity() {
Page("Download Territories") {
val intent = Intent(this@MainActivity, DownloadTerritoriesActivity::class.java)
startActivity(intent)
},
Page("Locale switch") {
val intent = Intent(this@MainActivity, LocaleSwitchActivity::class.java)
startActivity(intent)
}
)

Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/drawable/spinner_item_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/colorAccent"/>
<corners android:radius="8dp"/>
<padding android:left="4dp" android:right="4dp"/>
</shape>
62 changes: 62 additions & 0 deletions app/src/main/res/layout/activity_locale_switch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:id="@+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:fitsSystemWindows="true"
tools:context=".LocaleSwitchActivity">

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineFollowModeCenter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45" />

<androidx.constraintlayout.widget.Guideline
android:id="@+id/guidelineFollowModeSpinners"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.66" />

<TextView
android:id="@+id/textViewLocale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/locale"
android:textColor="@color/text_foreground_day"
app:layout_constraintBaseline_toBaselineOf="@id/spinnerLocale"
app:layout_constraintStart_toEndOf="@id/guidelineFollowModeCenter"
app:layout_constraintEnd_toStartOf="@id/guidelineFollowModeSpinners"/>

<Spinner
android:id="@+id/spinnerLocale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginBottom="26dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@id/guidelineFollowModeSpinners"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

</RelativeLayout>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/spinner_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/text_foreground_night"
android:background="@drawable/spinner_item_background"/>
7 changes: 7 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,11 @@
<item>EVENT_CENTER</item>
<item>MAP_POSITION</item>
</string-array>
<string-array name="locales_list">
<item>az_AZ</item>
<item>en-En</item>
<item>ru-Ru</item>
<item>System</item>
</string-array>
<string name="locale">Locale</string>
</resources>