app/src/main/java/jp/aosystem/gearcombination/MainActivity.kt
package jp.aosystem.gearcombination
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.LocaleList
import android.util.DisplayMetrics
import android.widget.LinearLayout
import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var vAdContainer: LinearLayout
private lateinit var vTextSetting: TextView
private lateinit var vTextQuest: List<TextView>
private lateinit var vTextClear: List<TextView>
private var destroyFlag: Boolean = false //Activity破棄された場合など
private var themeNumber: Int = 0
private var localeLanguage: String = ""
//adMob
private lateinit var adView: AdView //adMob
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = this.vAdContainer.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
companion object {
//internal const val AD_UNIT_ID: String = "ca-app-pub-0000000000000000/0000000000" //adMob Test
internal const val AD_UNIT_ID: String = "ca-app-pub-0000000000000000/0000000000"
internal const val SETTINGS: String = "settings"
internal const val THEME_NUMBER: String = "themeNumber"
internal const val LOCALE_LANGUAGE: String = "localeLanguage"
internal const val GAME_DATA: String = "gameData"
internal const val QUEST_NUMBER: String = "questNumber"
}
private val settingStartForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent: Intent? = result.data
if (intent != null) {
val lastThemeNumber: Int = this.themeNumber
this.themeNumber = intent.getIntExtra(THEME_NUMBER, 0)
if (lastThemeNumber != this.themeNumber) {
this.saveThemeNumber()
}
val lastLocaleLanguage = this.localeLanguage
this.localeLanguage = intent.getStringExtra(LOCALE_LANGUAGE) ?: ""
if (this.localeLanguage != lastLocaleLanguage) {
this.saveLocaleLanguage()
}
recreate()
}
}
}
private val gameStartForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
if (result.resultCode == Activity.RESULT_OK) {
val intent: Intent? = result.data
if (intent != null) {
val questNumber: Int = intent.getIntExtra(QUEST_NUMBER, 0)
this.saveGameData(questNumber)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//タイトルバー非表示
supportActionBar?.hide()
//
this.vAdContainer = findViewById(R.id.ad_container)
this.vTextSetting = findViewById(R.id.textSetting)
this.vTextQuest = listOf(
findViewById(R.id.textQuest1),
findViewById(R.id.textQuest2),
findViewById(R.id.textQuest3),
findViewById(R.id.textQuest4),
findViewById(R.id.textQuest5),
findViewById(R.id.textQuest6),
findViewById(R.id.textQuest7),
findViewById(R.id.textQuest8),
findViewById(R.id.textQuest9),
findViewById(R.id.textQuest10),
findViewById(R.id.textQuest11),
findViewById(R.id.textQuest12),
findViewById(R.id.textQuest13),
findViewById(R.id.textQuest14),
findViewById(R.id.textQuest15),
findViewById(R.id.textQuest16),
findViewById(R.id.textQuest17),
findViewById(R.id.textQuest18),
findViewById(R.id.textQuest19),
findViewById(R.id.textQuest20),
findViewById(R.id.textQuest21),
findViewById(R.id.textQuest22),
findViewById(R.id.textQuest23),
findViewById(R.id.textQuest24),
findViewById(R.id.textQuest25),
findViewById(R.id.textQuest26),
findViewById(R.id.textQuest27),
findViewById(R.id.textQuest28),
findViewById(R.id.textQuest29),
findViewById(R.id.textQuest30),
findViewById(R.id.textQuest31),
findViewById(R.id.textQuest32),
findViewById(R.id.textQuest33),
findViewById(R.id.textQuest34),
findViewById(R.id.textQuest35),
findViewById(R.id.textQuest36),
findViewById(R.id.textQuest37),
findViewById(R.id.textQuest38),
findViewById(R.id.textQuest39),
findViewById(R.id.textQuest40),
)
this.vTextClear = listOf(
findViewById(R.id.textClear1),
findViewById(R.id.textClear2),
findViewById(R.id.textClear3),
findViewById(R.id.textClear4),
findViewById(R.id.textClear5),
findViewById(R.id.textClear6),
findViewById(R.id.textClear7),
findViewById(R.id.textClear8),
findViewById(R.id.textClear9),
findViewById(R.id.textClear10),
findViewById(R.id.textClear11),
findViewById(R.id.textClear12),
findViewById(R.id.textClear13),
findViewById(R.id.textClear14),
findViewById(R.id.textClear15),
findViewById(R.id.textClear16),
findViewById(R.id.textClear17),
findViewById(R.id.textClear18),
findViewById(R.id.textClear19),
findViewById(R.id.textClear20),
findViewById(R.id.textClear21),
findViewById(R.id.textClear22),
findViewById(R.id.textClear23),
findViewById(R.id.textClear24),
findViewById(R.id.textClear25),
findViewById(R.id.textClear26),
findViewById(R.id.textClear27),
findViewById(R.id.textClear28),
findViewById(R.id.textClear29),
findViewById(R.id.textClear30),
findViewById(R.id.textClear31),
findViewById(R.id.textClear32),
findViewById(R.id.textClear33),
findViewById(R.id.textClear34),
findViewById(R.id.textClear35),
findViewById(R.id.textClear36),
findViewById(R.id.textClear37),
findViewById(R.id.textClear38),
findViewById(R.id.textClear39),
findViewById(R.id.textClear40),
)
//テーマ読み込みと設定
this.loadThemeNumber()
this.setTheme()
//設定読み出し
this.loadGameData()
//onClickListener
this.vTextSetting.setOnClickListener {
this.onClickSetting()
}
for (i in this.vTextQuest.indices) {
this.vTextQuest[i].setOnClickListener {
this.onClickQuest(i + 1)
}
}
//adMob
MobileAds.initialize(this) {}
this.adView = AdView(this)
this.vAdContainer.addView(this.adView)
this.loadBanner()
}
//adMob
private fun loadBanner() {
this.adView.adUnitId = AD_UNIT_ID
this.adView.adSize = this.adSize
val adRequest = AdRequest
.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build()
this.adView.loadAd(adRequest)
}
override fun onResume() {
super.onResume()
}
override fun onDestroy() {
this.destroyFlag = true
super.onDestroy()
}
//-------------------------------------------------
private fun onClickSetting() {
this.destroyFlag = true
val intent = Intent(applicationContext, SettingActivity::class.java)
intent.putExtra(THEME_NUMBER, this.themeNumber)
intent.putExtra(LOCALE_LANGUAGE, this.localeLanguage)
this.settingStartForResult.launch(intent)
}
private fun onClickQuest(num: Int) {
this.destroyFlag = true
val intent = Intent(applicationContext, GameActivity::class.java)
intent.putExtra(QUEST_NUMBER, num)
this.gameStartForResult.launch(intent)
}
//-------------------------------------------------
//GameDataを保存
private fun saveGameData(questNumber: Int) {
val pref = getSharedPreferences(SETTINGS, MODE_PRIVATE)
val gameData: String = pref.getString(GAME_DATA, "") ?: ""
val gameDataList: List<String> = gameData.split(",")
val gameDataListInt: ArrayList<Int> = arrayListOf()
for (i in gameDataList.indices) {
if (gameDataList[i] != "") {
gameDataListInt.add(gameDataList[i].toInt())
}
}
val ary: ArrayList<Int> = arrayListOf(questNumber)
ary += gameDataListInt
ary.sort()
val ary2: ArrayList<Int> = arrayListOf()
ary2.add(ary[0])
for (i in 1 until ary.size) {
if (ary[i] != ary[i - 1]) {
ary2.add(ary[i])
}
}
getSharedPreferences(SETTINGS, MODE_PRIVATE).edit().apply {
putString(GAME_DATA, ary2.joinToString(","))
apply()
this@MainActivity.drawGameData(ary2)
}
}
//GameDataを読み出し
private fun loadGameData() {
val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
val gameData: String = pref.getString(GAME_DATA, "") ?: ""
val gameDataList: List<String> = gameData.split(",")
val gameDataListInt: ArrayList<Int> = arrayListOf()
for (i in gameDataList.indices) {
if (gameDataList[i] != "") {
gameDataListInt.add(gameDataList[i].toInt())
}
}
this.drawGameData(gameDataListInt)
}
private fun drawGameData(gameDataListInt: ArrayList<Int>) {
for (i in this.vTextClear.indices) {
this.vTextClear[i].alpha = 0F
}
for (i in gameDataListInt.indices) {
this.vTextClear[gameDataListInt[i] - 1].alpha = 1F
}
}
//-------------------------------------------------
//テーマを保存
private fun saveThemeNumber() {
getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
putInt(THEME_NUMBER, this@MainActivity.themeNumber)
apply()
}
}
//テーマを読み出し
private fun loadThemeNumber() {
val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.themeNumber = pref.getInt(THEME_NUMBER, 0)
}
//テーマを設定
private fun setTheme() {
when (this.themeNumber) {
0 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
1 -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
}
//----------------------------------------------
//localeLanguageを保存
private fun saveLocaleLanguage() {
getSharedPreferences(SETTINGS, Context.MODE_PRIVATE).edit().apply {
putString(LOCALE_LANGUAGE, this@MainActivity.localeLanguage)
apply()
}
}
//localeLanguageを読み出し
private fun loadLocaleLanguage() {
val pref = getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
}
//言語設定
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //minSdkVersion 24
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
} else { //minSdkVersion 17 16はダメ
config.setLocale(loc)
}
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
//----------------------------------------------
}
app/src/main/java/jp/aosystem/gearcombination/SettingActivity.kt
package jp.aosystem.gearcombination
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.widget.RadioButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SwitchCompat
import java.util.*
class SettingActivity : AppCompatActivity() {
private lateinit var vTextApply: TextView
private lateinit var vTextCancel: TextView
private lateinit var vSwitchTheme: SwitchCompat
private lateinit var vRadioLanguageSystem: RadioButton
private lateinit var vRadioLanguageEn: RadioButton
private lateinit var vRadioLanguageBg: RadioButton
private lateinit var vRadioLanguageCs: RadioButton
private lateinit var vRadioLanguageDa: RadioButton
private lateinit var vRadioLanguageDe: RadioButton
private lateinit var vRadioLanguageEl: RadioButton
private lateinit var vRadioLanguageEs: RadioButton
private lateinit var vRadioLanguageEt: RadioButton
private lateinit var vRadioLanguageFi: RadioButton
private lateinit var vRadioLanguageFr: RadioButton
private lateinit var vRadioLanguageHu: RadioButton
private lateinit var vRadioLanguageIt: RadioButton
private lateinit var vRadioLanguageJa: RadioButton
private lateinit var vRadioLanguageLt: RadioButton
private lateinit var vRadioLanguageLv: RadioButton
private lateinit var vRadioLanguageNl: RadioButton
private lateinit var vRadioLanguagePl: RadioButton
private lateinit var vRadioLanguagePt: RadioButton
private lateinit var vRadioLanguageRo: RadioButton
private lateinit var vRadioLanguageRu: RadioButton
private lateinit var vRadioLanguageSk: RadioButton
private lateinit var vRadioLanguageSv: RadioButton
private lateinit var vRadioLanguageTh: RadioButton
private lateinit var vRadioLanguageZh: RadioButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_setting)
//タイトルバー非表示
supportActionBar?.hide()
//
this.vTextApply = findViewById(R.id.textApply)
this.vTextCancel = findViewById(R.id.textCancel)
this.vSwitchTheme = findViewById(R.id.switchTheme)
this.vRadioLanguageSystem = findViewById(R.id.radioLanguageSystem)
this.vRadioLanguageEn = findViewById(R.id.radioLanguageEn)
//this.vRadioLanguageBg = findViewById(R.id.radioLanguageBg)
//this.vRadioLanguageCs = findViewById(R.id.radioLanguageCs)
//this.vRadioLanguageDa = findViewById(R.id.radioLanguageDa)
//this.vRadioLanguageDe = findViewById(R.id.radioLanguageDe)
//this.vRadioLanguageEl = findViewById(R.id.radioLanguageEl)
//this.vRadioLanguageEs = findViewById(R.id.radioLanguageEs)
//this.vRadioLanguageEt = findViewById(R.id.radioLanguageEt)
//this.vRadioLanguageFi = findViewById(R.id.radioLanguageFi)
//this.vRadioLanguageFr = findViewById(R.id.radioLanguageFr)
//this.vRadioLanguageHu = findViewById(R.id.radioLanguageHu)
//this.vRadioLanguageIt = findViewById(R.id.radioLanguageIt)
this.vRadioLanguageJa = findViewById(R.id.radioLanguageJa)
//this.vRadioLanguageLt = findViewById(R.id.radioLanguageLt)
//this.vRadioLanguageLv = findViewById(R.id.radioLanguageLv)
//this.vRadioLanguageNl = findViewById(R.id.radioLanguageNl)
//this.vRadioLanguagePl = findViewById(R.id.radioLanguagePl)
//this.vRadioLanguagePt = findViewById(R.id.radioLanguagePt)
//this.vRadioLanguageRo = findViewById(R.id.radioLanguageRo)
//this.vRadioLanguageRu = findViewById(R.id.radioLanguageRu)
//this.vRadioLanguageSk = findViewById(R.id.radioLanguageSk)
//this.vRadioLanguageSv = findViewById(R.id.radioLanguageSv)
//this.vRadioLanguageTh = findViewById(R.id.radioLanguageTh)
//this.vRadioLanguageZh = findViewById(R.id.radioLanguageZh)
//データ受け取り
val intent = this.intent
val themeNumber: Int = intent.getIntExtra(MainActivity.THEME_NUMBER,0)
this.vSwitchTheme.isChecked = themeNumber != 0
val localeLanguage: String = intent.getStringExtra(MainActivity.LOCALE_LANGUAGE) ?: ""
when (localeLanguage) {
"en" -> this.vRadioLanguageEn.isChecked = true
//"bg" -> this.vRadioLanguageBg.isChecked = true
//"cs" -> this.vRadioLanguageCs.isChecked = true
//"da" -> this.vRadioLanguageDa.isChecked = true
//"de" -> this.vRadioLanguageDe.isChecked = true
//"el" -> this.vRadioLanguageEl.isChecked = true
//"es" -> this.vRadioLanguageEs.isChecked = true
//"et" -> this.vRadioLanguageEt.isChecked = true
//"fi" -> this.vRadioLanguageFi.isChecked = true
//"fr" -> this.vRadioLanguageFr.isChecked = true
//"hu" -> this.vRadioLanguageHu.isChecked = true
//"it" -> this.vRadioLanguageIt.isChecked = true
"ja" -> this.vRadioLanguageJa.isChecked = true
//"lt" -> this.vRadioLanguageLt.isChecked = true
//"lv" -> this.vRadioLanguageLv.isChecked = true
//"nl" -> this.vRadioLanguageNl.isChecked = true
//"pl" -> this.vRadioLanguagePl.isChecked = true
//"pt" -> this.vRadioLanguagePt.isChecked = true
//"ro" -> this.vRadioLanguageRo.isChecked = true
//"ru" -> this.vRadioLanguageRu.isChecked = true
//"sk" -> this.vRadioLanguageSk.isChecked = true
//"sv" -> this.vRadioLanguageSv.isChecked = true
//"th" -> this.vRadioLanguageTh.isChecked = true
//"zh" -> this.vRadioLanguageZh.isChecked = true
else -> this.vRadioLanguageSystem.isChecked = true
}
//onClickListener
this.vTextApply.setOnClickListener {
this.onClickApply()
}
this.vTextCancel.setOnClickListener {
this.onClickCancel()
}
}
private fun onClickApply() {
val themeNumber: Int = if (this.vSwitchTheme.isChecked) 1 else 0
var localeLanguage: String = ""
if (this.vRadioLanguageEn.isChecked) {
localeLanguage = "en"
//} else if (this.vRadioLanguageBg.isChecked) {
// localeLanguage = "bg"
//} else if (this.vRadioLanguageCs.isChecked) {
// localeLanguage = "cs"
//} else if (this.vRadioLanguageDa.isChecked) {
// localeLanguage = "da"
//} else if (this.vRadioLanguageDe.isChecked) {
// localeLanguage = "de"
//} else if (this.vRadioLanguageEl.isChecked) {
// localeLanguage = "el"
//} else if (this.vRadioLanguageEs.isChecked) {
// localeLanguage = "es"
//} else if (this.vRadioLanguageEt.isChecked) {
// localeLanguage = "et"
//} else if (this.vRadioLanguageFi.isChecked) {
// localeLanguage = "fi"
//} else if (this.vRadioLanguageFr.isChecked) {
// localeLanguage = "fr"
//} else if (this.vRadioLanguageHu.isChecked) {
// localeLanguage = "hu"
//} else if (this.vRadioLanguageIt.isChecked) {
// localeLanguage = "it"
} else if (this.vRadioLanguageJa.isChecked) {
localeLanguage = "ja"
//} else if (this.vRadioLanguageLt.isChecked) {
// localeLanguage = "lt"
//} else if (this.vRadioLanguageLv.isChecked) {
// localeLanguage = "lv"
//} else if (this.vRadioLanguageNl.isChecked) {
// localeLanguage = "nl"
//} else if (this.vRadioLanguagePl.isChecked) {
// localeLanguage = "pl"
//} else if (this.vRadioLanguagePt.isChecked) {
// localeLanguage = "pt"
//} else if (this.vRadioLanguageRo.isChecked) {
// localeLanguage = "ro"
//} else if (this.vRadioLanguageRu.isChecked) {
// localeLanguage = "ru"
//} else if (this.vRadioLanguageSk.isChecked) {
// localeLanguage = "sk"
//} else if (this.vRadioLanguageSv.isChecked) {
// localeLanguage = "sv"
//} else if (this.vRadioLanguageTh.isChecked) {
// localeLanguage = "th"
//} else if (this.vRadioLanguageZh.isChecked) {
// localeLanguage = "zh"
}
val intent = Intent()
intent.putExtra(MainActivity.THEME_NUMBER, themeNumber)
intent.putExtra(MainActivity.LOCALE_LANGUAGE, localeLanguage)
setResult(Activity.RESULT_OK, intent)
finish()
}
private fun onClickCancel() {
val intent = Intent()
setResult(Activity.RESULT_CANCELED, intent)
finish()
}
//--------------------------------------------------
//言語設定
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE)
val localeLanguage: String = pref.getString(MainActivity.LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (localeLanguage != "") Locale(localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //minSdkVersion 24
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
} else { //minSdkVersion 17 16はダメ
config.setLocale(loc)
}
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
//--------------------------------------------------
}
app/src/main/java/jp/aosystem/gearcombination/GameActivity.kt
package jp.aosystem.gearcombination
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.LocaleList
import android.util.DisplayMetrics
import android.webkit.JsResult
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import java.util.*
class GameActivity : AppCompatActivity() {
private lateinit var vAdContainer: LinearLayout
private lateinit var vTextReload: TextView
private lateinit var vTextBack: TextView
private lateinit var vWebView: WebView
private var destroyFlag: Boolean = false //Activity破棄された場合など
private var questNumber: Int = 0
//adMob
private lateinit var adView: AdView //adMob
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = this.vAdContainer.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game)
//タイトルバー非表示
supportActionBar?.hide()
//
this.vAdContainer = findViewById(R.id.ad_container)
this.vTextReload = findViewById(R.id.textReload)
this.vTextBack = findViewById(R.id.textBack)
this.vWebView = findViewById(R.id.webView)
//onClickListener
this.vTextReload.setOnClickListener {
this.onClickReload()
}
this.vTextBack.setOnClickListener {
this.onClickBack()
}
//データ受け取り
val intent = this.intent
this.questNumber = intent.getIntExtra(MainActivity.QUEST_NUMBER,1)
//adMob
MobileAds.initialize(this) {}
this.adView = AdView(this)
this.vAdContainer.addView(this.adView)
this.loadBanner()
}
//adMob
private fun loadBanner() {
this.adView.adUnitId = MainActivity.AD_UNIT_ID
this.adView.adSize = this.adSize
val adRequest = AdRequest
.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build()
this.adView.loadAd(adRequest)
}
override fun onResume() {
super.onResume()
this.game(this.questNumber)
}
override fun onDestroy() {
this.destroyFlag = true
super.onDestroy()
}
private fun onClickReload() {
this.game(this.questNumber)
}
private fun onClickBack() {
val intent = Intent()
setResult(Activity.RESULT_CANCELED, intent)
finish()
}
private fun onClickClear(questNum: Int) {
val intent = Intent()
intent.putExtra(MainActivity.QUEST_NUMBER, questNum)
setResult(Activity.RESULT_OK, intent)
finish()
}
//-------------------------------------------------
private fun game(questNumber: Int) {
val locale: String = resources.getString(R.string.locale)
val webSettings: WebSettings = this.vWebView.settings
webSettings.javaScriptEnabled = true //setJavaScriptEnabled を使用すると、アプリケーションに XSS の脆弱性が侵入する可能性があります。と表示されているだけ。
//webSettings.cacheMode = WebSettings.LOAD_CACHE_ONLY //WebSettings.LOAD_NO_CACHE
this.vWebView.loadUrl("file:///android_asset/web/index.html?q=$questNumber&locale=$locale")
this.vWebView.webChromeClient = object : WebChromeClient() {
override fun onJsAlert(view: WebView, url: String, message: String, result: JsResult): Boolean {
if (message.substring(0,6) == "clear:") {
// do something
onClickClear(message.substring(6).toInt())
}
result.cancel()
return true
}
}
}
//--------------------------------------------------
//言語設定
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE)
val localeLanguage: String = pref.getString(MainActivity.LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (localeLanguage != "") Locale(localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //minSdkVersion 24
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
} else { //minSdkVersion 17 16はダメ
config.setLocale(loc)
}
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
//--------------------------------------------------
}