diff --git a/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/MainActivity.kt b/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/MainActivity.kt index 3c458cebe3..a91f916800 100644 --- a/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/MainActivity.kt +++ b/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/MainActivity.kt @@ -16,15 +16,111 @@ package com.google.android.fhir.datacapture.gallery +import android.app.Activity +import android.content.Context +import android.content.res.Configuration import android.os.Bundle +import android.view.MenuItem +import android.view.View +import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import com.google.android.fhir.datacapture.gallery.databinding.ActivityMainBinding +import com.google.android.fhir.datacapture.gallery.utils.LanguageSwitcherUtils +import com.google.android.material.navigation.NavigationView +import java.util.Locale -class MainActivity : AppCompatActivity() { +class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener { private lateinit var binding: ActivityMainBinding + private lateinit var drawerToggle: ActionBarDrawerToggle + private lateinit var languageList: List + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) + + languageList = LanguageSwitcherUtils.getLanguageList(this) + + setUpNavigationView() + } + + private fun refreshToSelectedLanguage( + language: LanguageSwitcherUtils.Language, + context: Activity + ) { + + getNavigationView().menu.findItem(R.id.switch_language).title = language.displayName + + val sharedPref = + context?.getSharedPreferences( + BuildConfig.APPLICATION_ID.plus(this.javaClass.canonicalName), + Context.MODE_PRIVATE + ) + ?: return + with(sharedPref.edit()) { + putString(LanguageSwitcherUtils.SHARED_PREF_KEY_LANG, language.tag) + commit() + } + + LanguageSwitcherUtils.refreshActivity(context) + } + + override fun attachBaseContext(base: Context) { + val sharedPref = + base?.getSharedPreferences( + BuildConfig.APPLICATION_ID.plus(this.javaClass.canonicalName), + Context.MODE_PRIVATE + ) + ?: return + val languageTag = + sharedPref.getString( + LanguageSwitcherUtils.SHARED_PREF_KEY_LANG, + Locale.getDefault().toLanguageTag() + ) + val newConfiguration: Configuration? = LanguageSwitcherUtils.setAppLocale(base, languageTag) + super.attachBaseContext(base) + applyOverrideConfiguration(newConfiguration) } + + override fun onNavigationItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.switch_language -> + LanguageSwitcherUtils.renderSelectLanguageDialog(this, languageList) { _, i -> + refreshToSelectedLanguage(languageList[i], this) + } + else -> false + } + + return true + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + + if (drawerToggle.onOptionsItemSelected(item)) { + return true + } + return super.onOptionsItemSelected(item) + } + + private fun setUpNavigationView() { + + drawerToggle = + ActionBarDrawerToggle( + this, + binding.drawerLayout, + R.string.nav_app_bar_open_drawer_description, + R.string.nav_app_bar_navigate_up_description + ) + binding.drawerLayout.addDrawerListener(drawerToggle) + drawerToggle.syncState() + + supportActionBar?.setDisplayHomeAsUpEnabled(true) + + val navigationView = getNavigationView() + navigationView.setNavigationItemSelectedListener(this) + + navigationView.menu.findItem(R.id.switch_language).title = Locale.getDefault().displayName + } + + private fun getNavigationView() = findViewById(R.id.nav_view) as NavigationView } diff --git a/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/utils/LanguageSwitcherUtils.kt b/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/utils/LanguageSwitcherUtils.kt new file mode 100644 index 0000000000..64164c96f9 --- /dev/null +++ b/datacapturegallery/src/main/java/com/google/android/fhir/datacapture/gallery/utils/LanguageSwitcherUtils.kt @@ -0,0 +1,85 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.fhir.datacapture.gallery.utils + +import android.app.Activity +import android.content.Context +import android.content.DialogInterface +import android.content.Intent +import android.content.res.Configuration +import android.content.res.Resources +import android.os.Build +import android.os.LocaleList +import android.widget.ArrayAdapter +import androidx.appcompat.app.AlertDialog +import com.google.android.fhir.datacapture.gallery.R +import java.util.Locale + +object LanguageSwitcherUtils { + + const val SHARED_PREF_KEY_LANG = "shared_pref_key_lang" + + fun setAppLocale(context: Context, language: String?): Configuration? { + val res: Resources = context.resources + val configuration: Configuration = res.configuration + try { + val locale = Locale.forLanguageTag(language) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + configuration.setLocale(locale) + val localeList = LocaleList(locale) + LocaleList.setDefault(localeList) + configuration.setLocales(localeList) + context.createConfigurationContext(configuration) + } else { + configuration.locale = locale + res.updateConfiguration(configuration, res.displayMetrics) + } + } catch (e: Exception) { + e.printStackTrace() + } + return configuration + } + + fun getLanguageList(activity: Activity): List { + return activity.resources.getStringArray(R.array.languages).toList().map { + Language(it, Locale.forLanguageTag(it).displayName) + } + } + + fun refreshActivity(activity: Activity) { + val intent = Intent(activity, activity.javaClass) + activity.startActivity(intent) + } + + fun renderSelectLanguageDialog( + context: Activity, + languageList: List, + listener: DialogInterface.OnClickListener + ) { + + val adapter: ArrayAdapter = + ArrayAdapter(context, android.R.layout.simple_list_item_1, languageList) + + val builder: AlertDialog.Builder = AlertDialog.Builder(context) + builder.setTitle(context.getString(R.string.select_language)) + builder.setIcon(R.drawable.outline_language_24) + builder.setAdapter(adapter, listener).create().show() + } + data class Language(val tag: String, val displayName: String) { + override fun toString() = displayName + } +} diff --git a/datacapturegallery/src/main/res/drawable-v24/outline_language_24.xml b/datacapturegallery/src/main/res/drawable-v24/outline_language_24.xml new file mode 100755 index 0000000000..8aeb4d8db0 --- /dev/null +++ b/datacapturegallery/src/main/res/drawable-v24/outline_language_24.xml @@ -0,0 +1,13 @@ + + + diff --git a/datacapturegallery/src/main/res/layout/activity_main.xml b/datacapturegallery/src/main/res/layout/activity_main.xml index 9d1ab6d9ec..01579b23d0 100644 --- a/datacapturegallery/src/main/res/layout/activity_main.xml +++ b/datacapturegallery/src/main/res/layout/activity_main.xml @@ -14,20 +14,34 @@ See the License for the specific language governing permissions and limitations under the License. --> - + - + + + + diff --git a/datacapturegallery/src/main/res/layout/nav_header.xml b/datacapturegallery/src/main/res/layout/nav_header.xml new file mode 100644 index 0000000000..0f5903068d --- /dev/null +++ b/datacapturegallery/src/main/res/layout/nav_header.xml @@ -0,0 +1,9 @@ + + + + diff --git a/datacapturegallery/src/main/res/menu/nav_drawer_menu.xml b/datacapturegallery/src/main/res/menu/nav_drawer_menu.xml new file mode 100644 index 0000000000..c127ed0785 --- /dev/null +++ b/datacapturegallery/src/main/res/menu/nav_drawer_menu.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/datacapturegallery/src/main/res/values-vi/strings.xml b/datacapturegallery/src/main/res/values-vi/strings.xml new file mode 100644 index 0000000000..5d5669091a --- /dev/null +++ b/datacapturegallery/src/main/res/values-vi/strings.xml @@ -0,0 +1,20 @@ + + + Thư viện thu thập dữ liệu + Gửi + Chọn ngôn ngữ + diff --git a/datacapturegallery/src/main/res/values/languages.xml b/datacapturegallery/src/main/res/values/languages.xml new file mode 100644 index 0000000000..c084e84d4a --- /dev/null +++ b/datacapturegallery/src/main/res/values/languages.xml @@ -0,0 +1,7 @@ + + + + en + vi-VN + + diff --git a/datacapturegallery/src/main/res/values/strings.xml b/datacapturegallery/src/main/res/values/strings.xml index 34c006c5ba..ce873e83fe 100644 --- a/datacapturegallery/src/main/res/values/strings.xml +++ b/datacapturegallery/src/main/res/values/strings.xml @@ -16,4 +16,5 @@ Data Capture Gallery Submit + Select Language