From f25e871a91baf79fb257d63269b7b9fd4c784554 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 29 Mar 2020 23:38:58 +0200 Subject: [PATCH 1/3] improve leap day behavior --- .../nightscout/androidaps/data/Profile.java | 9 +++++--- .../general/automation/elements/InputTime.kt | 2 +- .../automation/elements/InputTimeRange.kt | 2 +- .../triggers/TriggerRecurringTime.kt | 2 +- .../automation/triggers/TriggerTimeRange.kt | 2 +- .../smsCommunicator/SmsCommunicatorPlugin.kt | 6 ++--- .../nightscout/androidaps/utils/DateUtil.java | 22 +++++++++++++++++++ .../androidaps/utils/MidnightTime.java | 11 ++++++++++ .../androidaps/utils/stats/TddCalculator.kt | 2 +- .../androidaps/utils/stats/TirCalculator.kt | 2 +- .../triggers/TriggerTimeRangeTest.kt | 4 ++-- 11 files changed, 49 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/data/Profile.java b/app/src/main/java/info/nightscout/androidaps/data/Profile.java index bf570f08a7..b6f22aae07 100644 --- a/app/src/main/java/info/nightscout/androidaps/data/Profile.java +++ b/app/src/main/java/info/nightscout/androidaps/data/Profile.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.data; import androidx.collection.LongSparseArray; +import org.joda.time.DateTime; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -607,13 +608,15 @@ public class Profile { } public static int secondsFromMidnight() { - long passed = DateUtil.now() - MidnightTime.calc(); + // long passed = DateUtil.now() - MidnightTime.calc(); + long passed = new DateTime().getMillisOfDay(); return (int) (passed / 1000); } public static int secondsFromMidnight(long date) { - long midnight = MidnightTime.calc(date); - long passed = date - midnight; + //long midnight = MidnightTime.calc(date); + //long passed = date - midnight; + long passed = new DateTime(date).getMillisOfDay(); return (int) (passed / 1000); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt index 34bc74560c..01a7854e35 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTime.kt @@ -57,7 +57,7 @@ class InputTime(injector: HasAndroidInjector) : Element(injector) { root.addView(l) } - private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() + private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt index 18cf98fc73..4775990d16 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/elements/InputTimeRange.kt @@ -80,7 +80,7 @@ class InputTimeRange(injector: HasAndroidInjector) : Element(injector) { root.addView(l) } - private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() + private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt index 11dd1e0475..8982a12669 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerRecurringTime.kt @@ -93,7 +93,7 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) { override fun duplicate(): Trigger = TriggerRecurringTime(injector, this) - private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() + private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt index 46edd9a0b2..2c565fa733 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRange.kt @@ -76,7 +76,7 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) { override fun duplicate(): Trigger = TriggerTimeRange(injector, range.start, range.end) - private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calc() + T.mins(minutesSinceMidnight.toLong()).msecs() + private fun toMills(minutesSinceMidnight: Int): Long = MidnightTime.calcPlusMinutes(minutesSinceMidnight) private fun getMinSinceMidnight(time: Long): Int = Profile.secondsFromMidnight(time) / 60 diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt index 78a0bf0ffc..350e5f08db 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/smsCommunicator/SmsCommunicatorPlugin.kt @@ -703,13 +703,11 @@ class SmsCommunicatorPlugin @Inject constructor( var grams = SafeParse.stringToInt(splitted[1]) var time = DateUtil.now() if (splitted.size > 2) { - val seconds = DateUtil.toSeconds(splitted[2].toUpperCase(Locale.getDefault())) - val midnight = MidnightTime.calc() - if (seconds == 0 && (!splitted[2].startsWith("00:00") || !splitted[2].startsWith("12:00"))) { + time = DateUtil.toTodayTime(splitted[2].toUpperCase(Locale.getDefault())) + if (time == 0L) { sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) return } - time = midnight + T.secs(seconds.toLong()).msecs() } grams = constraintChecker.applyCarbsConstraints(Constraint(grams)).value() if (grams == 0) sendSMS(Sms(receivedSms.phoneNumber, resourceHelper.gs(R.string.wrongformat))) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java index 7d2790990f..82fabc52b7 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/DateUtil.java @@ -108,6 +108,28 @@ public class DateUtil { return retval; } + public static long toTodayTime(String hh_colon_mm) { + Pattern p = Pattern.compile("(\\d+):(\\d+)( a.m.| p.m.| AM| PM|AM|PM|)"); + Matcher m = p.matcher(hh_colon_mm); + long retval = 0; + + if (m.find()) { + int hours = SafeParse.stringToInt(m.group(1)); + int minutes = SafeParse.stringToInt(m.group(2)); + if ((m.group(3).equals(" a.m.") || m.group(3).equals(" AM") || m.group(3).equals("AM")) && m.group(1).equals("12")) + hours -= 12; + if ((m.group(3).equals(" p.m.") || m.group(3).equals(" PM") || m.group(3).equals("PM")) && !(m.group(1).equals("12"))) + hours += 12; + Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, hours); + c.set(Calendar.MINUTE, minutes); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + retval = c.getTimeInMillis(); + } + return retval; + } + public static String dateString(Date date) { DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT); return df.format(date); diff --git a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java index 235bb9c6e9..501f6147e7 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java @@ -21,6 +21,17 @@ public class MidnightTime { return c.getTimeInMillis(); } + public static long calcPlusMinutes(int minutes) { + int h = minutes / 60; + int m = minutes % 60; + Calendar c = Calendar.getInstance(); + c.set(Calendar.HOUR_OF_DAY, h); + c.set(Calendar.MINUTE, m); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + return c.getTimeInMillis(); + } + public static long calc(long time) { Long m; synchronized (times) { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt index 0444513952..247d4c22d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TddCalculator.kt @@ -40,7 +40,7 @@ class TddCalculator @Inject constructor( fun calculate(days: Long): LongSparseArray { val range = T.days(days + 1).msecs() - val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs() + val startTime = MidnightTime.calc(DateUtil.now() - T.days(days).msecs()) val endTime = MidnightTime.calc(DateUtil.now()) initializeData(range) diff --git a/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt b/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt index b8d29af3d5..f1f13cbf0d 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/stats/TirCalculator.kt @@ -23,7 +23,7 @@ class TirCalculator @Inject constructor( fun calculate(days: Long, lowMgdl: Double, highMgdl: Double): LongSparseArray { if (lowMgdl < 39) throw RuntimeException("Low below 39") if (lowMgdl > highMgdl) throw RuntimeException("Low > High") - val startTime = MidnightTime.calc(DateUtil.now()) - T.days(days).msecs() + val startTime = MidnightTime.calc(DateUtil.now() - T.days(days).msecs()) val endTime = MidnightTime.calc(DateUtil.now()) val bgReadings = MainApp.getDbHelper().getBgreadingsDataFromTime(startTime, endTime, true) diff --git a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt index fc1dbbf690..dba3ddce80 100644 --- a/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt +++ b/app/src/test/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerTimeRangeTest.kt @@ -22,9 +22,9 @@ class TriggerTimeRangeTest : TriggerTestBase() { @Before fun mock() { - var realNow = System.currentTimeMillis() PowerMockito.mockStatic(DateUtil::class.java) - PowerMockito.`when`(DateUtil.now()).thenReturn(now.toLong() * 60000 + MidnightTime.calc(realNow)) + val nowMills = MidnightTime.calcPlusMinutes(now) + PowerMockito.`when`(DateUtil.now()).thenReturn(nowMills) } @Test From d1ee12fafc819af11cc3552ce080322a68df5655 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 29 Mar 2020 23:52:24 +0200 Subject: [PATCH 2/3] replace deprecated calls --- .../plugins/configBuilder/PluginStore.kt | 1 + .../general/tidepool/TidepoolPlugin.kt | 5 ++-- .../general/tidepool/comm/InfoInterceptor.kt | 24 ++++++------------- .../general/tidepool/comm/TidepoolUploader.kt | 2 +- .../tidepool/events/EventTidepoolStatus.kt | 11 --------- .../general/tidepool/utils/RateLimit.kt | 17 ++++++------- 6 files changed, 21 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginStore.kt b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginStore.kt index 30014d38e3..0bce3e34eb 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginStore.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/PluginStore.kt @@ -190,6 +190,7 @@ class PluginStore @Inject constructor( */ private fun determineActivePlugin(pluginsInCategory: ArrayList, pluginType: PluginType): T? { + @Suppress("UNCHECKED_CAST") val activePlugin = getTheOneEnabledInArray(pluginsInCategory, pluginType) as T? if (activePlugin != null) { setFragmentVisiblities((activePlugin as PluginBase).name, pluginsInCategory, pluginType) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt index ce535e41fd..e21f67957d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/TidepoolPlugin.kt @@ -50,7 +50,8 @@ class TidepoolPlugin @Inject constructor( private val fabricPrivacy: FabricPrivacy, private val tidepoolUploader: TidepoolUploader, private val uploadChunk: UploadChunk, - private val sp: SP + private val sp: SP, + private val rateLimit: RateLimit ) : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) .pluginName(R.string.tidepool) @@ -103,7 +104,7 @@ class TidepoolPlugin @Inject constructor( if (isEnabled(PluginType.GENERAL) && (!sp.getBoolean(R.string.key_tidepool_only_while_charging, false) || ChargingStateReceiver.isCharging()) && (!sp.getBoolean(R.string.key_tidepool_only_while_unmetered, false) || NetworkChangeReceiver.isWifiConnected()) - && RateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt())) + && rateLimit.rateLimit("tidepool-new-data-upload", T.mins(4).secs().toInt())) doUpload() }, { fabricPrivacy.logException(it) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/InfoInterceptor.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/InfoInterceptor.kt index c1a9f55dc6..59268f49b0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/InfoInterceptor.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/InfoInterceptor.kt @@ -1,32 +1,22 @@ package info.nightscout.androidaps.plugins.general.tidepool.comm -import info.nightscout.androidaps.logging.L -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag import okhttp3.Interceptor import okhttp3.Response import okio.Buffer -import org.slf4j.LoggerFactory import java.io.IOException -class InfoInterceptor(tag: String) : Interceptor { - - private val log = StacktraceLoggerWrapper.getLogger(L.TIDEPOOL) - private var tag = "interceptor" - - init { - this.tag = tag - } +class InfoInterceptor(val tag: String = "interceptor", val aapsLogger: AAPSLogger) : Interceptor { @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { val request = chain.request() request.body?.let { - if (L.isEnabled(L.TIDEPOOL)) { - log.debug("Interceptor Body size: " + it.contentLength()) - val requestBuffer = Buffer() - it.writeTo(requestBuffer) - log.debug("Interceptor Body: " + requestBuffer.readUtf8()) - } + aapsLogger.debug(LTag.TIDEPOOL, "Interceptor Body size: " + it.contentLength()) + val requestBuffer = Buffer() + it.writeTo(requestBuffer) + aapsLogger.debug(LTag.TIDEPOOL, "Interceptor Body: " + requestBuffer.readUtf8()) } return chain.proceed(request) } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt index a6d89f8971..51c16af342 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/comm/TidepoolUploader.kt @@ -68,7 +68,7 @@ class TidepoolUploader @Inject constructor( val client = OkHttpClient.Builder() .addInterceptor(httpLoggingInterceptor) - .addInterceptor(InfoInterceptor(TidepoolUploader::class.java.name)) + .addInterceptor(InfoInterceptor(TidepoolUploader::class.java.name, aapsLogger)) .build() retrofit = Retrofit.Builder() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt index bbb86c45b8..aff358ba08 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/events/EventTidepoolStatus.kt @@ -1,23 +1,13 @@ package info.nightscout.androidaps.plugins.general.tidepool.events import info.nightscout.androidaps.events.Event -import info.nightscout.androidaps.logging.L -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper import info.nightscout.androidaps.utils.DateUtil -import org.slf4j.LoggerFactory import java.text.SimpleDateFormat import java.util.* class EventTidepoolStatus(val status: String) : Event() { - private val log = StacktraceLoggerWrapper.getLogger(L.TIDEPOOL) - var date: Long = DateUtil.now() - init { - if (L.isEnabled(L.TIDEPOOL)) - log.debug("New status: $status") - } - private var timeFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault()) fun toPreparedHtml(): StringBuilder { @@ -29,5 +19,4 @@ class EventTidepoolStatus(val status: String) : Event() { stringBuilder.append("
") return stringBuilder } - } \ No newline at end of file diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/utils/RateLimit.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/utils/RateLimit.kt index 6f23a3f9c1..9e48d0b90f 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/utils/RateLimit.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/tidepool/utils/RateLimit.kt @@ -1,26 +1,27 @@ package info.nightscout.androidaps.plugins.general.tidepool.utils -import info.nightscout.androidaps.logging.L -import info.nightscout.androidaps.logging.StacktraceLoggerWrapper +import info.nightscout.androidaps.logging.AAPSLogger +import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.T -import org.slf4j.LoggerFactory import java.util.* +import javax.inject.Inject +import javax.inject.Singleton -object RateLimit { +@Singleton +class RateLimit @Inject constructor( + val aapsLogger: AAPSLogger +) { private val rateLimits = HashMap() - private val log = StacktraceLoggerWrapper.getLogger(L.TIDEPOOL) - // return true if below rate limit @Synchronized fun rateLimit(name: String, seconds: Int): Boolean { // check if over limit rateLimits[name]?.let { if (DateUtil.now() - it < T.secs(seconds.toLong()).msecs()) { - if (L.isEnabled(L.TIDEPOOL)) - log.debug("$name rate limited: $seconds seconds") + aapsLogger.debug(LTag.TIDEPOOL, "$name rate limited: $seconds seconds") return false } } From ddd91109124ae1684b6139c5713e87149ee226b4 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Mon, 30 Mar 2020 00:57:33 +0200 Subject: [PATCH 3/3] LocaleHelper dependency remove --- .../nightscout/androidaps/MainActivity.java | 1 - .../activities/NoSplashAppCompatActivity.kt | 2 +- .../nightscout/androidaps/utils/LocaleHelper.kt | 17 ++++++++++------- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/MainActivity.java b/app/src/main/java/info/nightscout/androidaps/MainActivity.java index 0d65cbb834..f6a250dc4a 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/MainActivity.java @@ -92,7 +92,6 @@ public class MainActivity extends NoSplashAppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { - AndroidInjection.inject(this); super.onCreate(savedInstanceState); Iconify.with(new FontAwesomeModule()); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/NoSplashAppCompatActivity.kt b/app/src/main/java/info/nightscout/androidaps/activities/NoSplashAppCompatActivity.kt index b4ac7eff51..aaf85c6adf 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/NoSplashAppCompatActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/activities/NoSplashAppCompatActivity.kt @@ -10,8 +10,8 @@ import info.nightscout.androidaps.utils.LocaleHelper open class NoSplashAppCompatActivity : DaggerAppCompatActivity() { public override fun onCreate(savedInstanceState: Bundle?) { - setTheme(R.style.AppTheme_NoActionBar) super.onCreate(savedInstanceState) + setTheme(R.style.AppTheme_NoActionBar) } public override fun attachBaseContext(newBase: Context) { diff --git a/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt index 447d076d93..62da88a15d 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/LocaleHelper.kt @@ -4,16 +4,19 @@ import android.content.Context import android.content.ContextWrapper import android.os.Build import android.os.LocaleList +import androidx.preference.PreferenceManager import info.nightscout.androidaps.R import java.util.* - object LocaleHelper { - fun currentLanguage(): String = - SP.getString(R.string.key_language, Locale.getDefault().language) + private fun currentLanguage(context: Context): String = + PreferenceManager.getDefaultSharedPreferences(context).getString(context.getString(R.string.key_language), "en") + ?: "en" + // injection not possible because of use in attachBaseContext + //SP.getString(R.string.key_language, Locale.getDefault().language) - private fun currentLocale(): Locale { - val language = currentLanguage() + private fun currentLocale(context: Context): Locale { + val language = currentLanguage(context) var locale = Locale(language) if (language.contains("_")) { // language with country like pt_BR defined in arrays.xml @@ -26,7 +29,7 @@ object LocaleHelper { @Suppress("DEPRECATION") fun update(context: Context) { - val locale = currentLocale() + val locale = currentLocale(context) Locale.setDefault(locale) val resources = context.resources val configuration = resources.configuration @@ -39,7 +42,7 @@ object LocaleHelper { fun wrap(ctx: Context): ContextWrapper { val res = ctx.resources val configuration = res.configuration - val newLocale = currentLocale() + val newLocale = currentLocale(ctx) val context = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { configuration.setLocale(newLocale) val localeList = LocaleList(newLocale)