From 962720145e3678f5ffd17ea6c4a9e277a665b144 Mon Sep 17 00:00:00 2001 From: cpfeiffer Date: Sun, 3 Sep 2017 01:02:31 +0200 Subject: [PATCH] Fix custom language being lost for newly created activities So we do need to set the language both on change and onCreate() For some reason, the title bar of the SettingsActivity is not updated on recreate(). Closes #787 --- .../gadgetbridge/GBApplication.java | 15 ++- .../activities/AbstractGBActivity.java | 101 ++++++++++++++++++ .../AbstractGBFragmentActivity.java | 2 +- .../activities/AbstractSettingsActivity.java | 15 +-- .../gadgetbridge/activities/AlarmDetails.java | 2 +- .../activities/AndroidPairingActivity.java | 2 +- .../activities/AppBlacklistActivity.java | 2 +- .../activities/CalBlacklistActivity.java | 2 +- .../activities/ConfigureAlarms.java | 2 +- .../activities/ControlCenterv2.java | 16 ++- .../activities/DbManagementActivity.java | 2 +- .../activities/DebugActivity.java | 2 +- .../activities/DiscoveryActivity.java | 2 +- .../activities/ExternalPebbleJSActivity.java | 2 +- .../activities/FwAppInstallerActivity.java | 2 +- .../gadgetbridge/activities/GBActivity.java | 77 +------------ .../activities/VibrationActivity.java | 2 +- .../devices/miband/MiBandPairingActivity.java | 4 +- .../devices/pebble/PebblePairingActivity.java | 4 +- .../WeatherNotificationConfig.java | 4 +- .../gadgetbridge/util/AndroidUtils.java | 13 ++- 21 files changed, 154 insertions(+), 119 deletions(-) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBActivity.java diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java index 1fc6f8a65..7ee81ed53 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/GBApplication.java @@ -57,6 +57,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService; import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser; import nodomain.freeyourgadget.gadgetbridge.model.DeviceService; import nodomain.freeyourgadget.gadgetbridge.service.NotificationCollectorMonitorService; +import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; import nodomain.freeyourgadget.gadgetbridge.util.FileUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs; @@ -532,11 +533,11 @@ public class GBApplication extends Application { } else { language = new Locale(lang); } - Configuration config = new Configuration(); - config.setLocale(language); + updateLanguage(language); + } - // FIXME: I have no idea what I am doing - context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); + public static void updateLanguage(Locale locale) { + AndroidUtils.setLanguage(context, locale); Intent intent = new Intent(); intent.setAction(ACTION_LANGUAGE_CHANGE); @@ -558,6 +559,12 @@ public class GBApplication extends Application { return typedValue.data; } + @Override + public void onConfigurationChanged(Configuration newConfig) { + super.onConfigurationChanged(newConfig); + updateLanguage(getLanguage()); + } + public static int getBackgroundColor(Context context) { TypedValue typedValue = new TypedValue(); Resources.Theme theme = context.getTheme(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBActivity.java new file mode 100644 index 000000000..3ee9e20dc --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBActivity.java @@ -0,0 +1,101 @@ +/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo + + This file is part of Gadgetbridge. + + Gadgetbridge is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Gadgetbridge is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . */ +package nodomain.freeyourgadget.gadgetbridge.activities; + + +import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.res.Configuration; +import android.os.Bundle; +import android.os.LocaleList; +import android.support.v4.content.LocalBroadcastManager; +import android.support.v7.app.AppCompatActivity; + +import java.util.Locale; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; +import nodomain.freeyourgadget.gadgetbridge.R; +import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; +import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; +import nodomain.freeyourgadget.gadgetbridge.util.Prefs; + + +public class AbstractGBActivity extends AppCompatActivity implements GBActivity { + + public static final int NONE = 0; + public static final int NO_ACTIONBAR = 1; + + private final BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + switch (action) { + case GBApplication.ACTION_LANGUAGE_CHANGE: + setLanguage(GBApplication.getLanguage(), true); + break; + case GBApplication.ACTION_QUIT: + finish(); + break; + } + } + }; + + public void setLanguage(Locale language, boolean recreate) { + AndroidUtils.setLanguage(this, language, recreate); + } + + public static void init(GBActivity activity) { + init(activity, NONE); + } + + public static void init(GBActivity activity, int flags) { + if (GBApplication.isDarkThemeEnabled()) { + if ((flags & NO_ACTIONBAR) != 0) { + activity.setTheme(R.style.GadgetbridgeThemeDark_NoActionBar); + } else { + activity.setTheme(R.style.GadgetbridgeThemeDark); + } + } else { + if ((flags & NO_ACTIONBAR) != 0) { + activity.setTheme(R.style.GadgetbridgeTheme_NoActionBar); + } else { + activity.setTheme(R.style.GadgetbridgeTheme); + } + } + activity.setLanguage(GBApplication.getLanguage(), false); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + IntentFilter filterLocal = new IntentFilter(); + filterLocal.addAction(GBApplication.ACTION_QUIT); + filterLocal.addAction(GBApplication.ACTION_LANGUAGE_CHANGE); + LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); + + init(this); + super.onCreate(savedInstanceState); + } + + @Override + protected void onDestroy() { + LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); + super.onDestroy(); + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBFragmentActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBFragmentActivity.java index 087954de7..2390aedff 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBFragmentActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractGBFragmentActivity.java @@ -33,7 +33,7 @@ import android.support.v4.app.FragmentPagerAdapter; * * @see AbstractGBFragment */ -public abstract class AbstractGBFragmentActivity extends GBActivity { +public abstract class AbstractGBFragmentActivity extends AbstractGBActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java index 14d37112a..a1ca99131 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AbstractSettingsActivity.java @@ -37,7 +37,6 @@ import org.slf4j.LoggerFactory; import java.util.Locale; import nodomain.freeyourgadget.gadgetbridge.GBApplication; -import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; /** @@ -46,7 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; * to set that listener in #onCreate, *not* in #onPostCreate, otherwise the value will * not be displayed. */ -public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivity { +public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivity implements GBActivity { private static final Logger LOG = LoggerFactory.getLogger(AbstractSettingsActivity.class); @@ -56,7 +55,7 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi String action = intent.getAction(); switch (action) { case GBApplication.ACTION_LANGUAGE_CHANGE: - setLanguage(GBApplication.getLanguage()); + setLanguage(GBApplication.getLanguage(), true); break; case GBApplication.ACTION_QUIT: finish(); @@ -129,11 +128,7 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi @Override protected void onCreate(Bundle savedInstanceState) { - if (GBApplication.isDarkThemeEnabled()) { - setTheme(R.style.GadgetbridgeThemeDark); - } else { - setTheme(R.style.GadgetbridgeTheme); - } + AbstractGBActivity.init(this); IntentFilter filterLocal = new IntentFilter(); filterLocal.addAction(GBApplication.ACTION_QUIT); @@ -215,7 +210,7 @@ public abstract class AbstractSettingsActivity extends AppCompatPreferenceActivi return super.onOptionsItemSelected(item); } - private void setLanguage(Locale language) { - AndroidUtils.setLanguage(this, language); + public void setLanguage(Locale language, boolean recreate) { + AndroidUtils.setLanguage(this, language, recreate); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AlarmDetails.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AlarmDetails.java index 543673de3..d0cfa085d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AlarmDetails.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AlarmDetails.java @@ -31,7 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBAlarm; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; -public class AlarmDetails extends GBActivity { +public class AlarmDetails extends AbstractGBActivity { private GBAlarm alarm; private TimePicker timePicker; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AndroidPairingActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AndroidPairingActivity.java index f26dd3b04..a2c39bbcf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AndroidPairingActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AndroidPairingActivity.java @@ -20,7 +20,7 @@ import android.os.Bundle; import nodomain.freeyourgadget.gadgetbridge.R; -public class AndroidPairingActivity extends GBActivity { +public class AndroidPairingActivity extends AbstractGBActivity { @Override protected void onCreate(Bundle savedInstanceState) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java index 5c3c10146..20ea96cc9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/AppBlacklistActivity.java @@ -31,7 +31,7 @@ import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.adapter.AppBlacklistAdapter; -public class AppBlacklistActivity extends GBActivity { +public class AppBlacklistActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(AppBlacklistActivity.class); private AppBlacklistAdapter appBlacklistAdapter; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/CalBlacklistActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/CalBlacklistActivity.java index 15307a32c..54e4ae15e 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/CalBlacklistActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/CalBlacklistActivity.java @@ -47,7 +47,7 @@ import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class CalBlacklistActivity extends GBActivity { +public class CalBlacklistActivity extends AbstractGBActivity { private final String[] EVENT_PROJECTION = new String[]{ CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java index 584ac07da..3528c82be 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ConfigureAlarms.java @@ -38,7 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.Prefs; import static nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandConst.PREF_MIBAND_ALARMS; -public class ConfigureAlarms extends GBActivity { +public class ConfigureAlarms extends AbstractGBActivity { private static final int REQ_CONFIGURE_ALARM = 1; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java index 7e889efa1..377663d83 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ControlCenterv2.java @@ -62,9 +62,9 @@ import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; -//TODO: extend GBActivity, but it requires actionbar that is not available +//TODO: extend AbstractGBActivity, but it requires actionbar that is not available public class ControlCenterv2 extends AppCompatActivity - implements NavigationView.OnNavigationItemSelectedListener { + implements NavigationView.OnNavigationItemSelectedListener, GBActivity { //needed for KK compatibility static { @@ -84,7 +84,7 @@ public class ControlCenterv2 extends AppCompatActivity String action = intent.getAction(); switch (action) { case GBApplication.ACTION_LANGUAGE_CHANGE: - setLanguage(GBApplication.getLanguage()); + setLanguage(GBApplication.getLanguage(), true); break; case GBApplication.ACTION_QUIT: finish(); @@ -98,11 +98,7 @@ public class ControlCenterv2 extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { - if (GBApplication.isDarkThemeEnabled()) { - setTheme(R.style.GadgetbridgeThemeDark_NoActionBar); - } else { - setTheme(R.style.GadgetbridgeTheme_NoActionBar); - } + AbstractGBActivity.init(this, AbstractGBActivity.NO_ACTIONBAR); super.onCreate(savedInstanceState); setContentView(R.layout.activity_controlcenterv2); @@ -315,7 +311,7 @@ public class ControlCenterv2 extends AppCompatActivity ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[wantedPermissions.size()]), 0); } - private void setLanguage(Locale language) { - AndroidUtils.setLanguage(this, language); + public void setLanguage(Locale language, boolean recreate) { + AndroidUtils.setLanguage(this, language, recreate); } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DbManagementActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DbManagementActivity.java index 2085f7615..782f43a24 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DbManagementActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DbManagementActivity.java @@ -45,7 +45,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.ImportExportSharedPreferences; -public class DbManagementActivity extends GBActivity { +public class DbManagementActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(DbManagementActivity.class); private static SharedPreferences sharedPrefs; private ImportExportSharedPreferences shared_file = new ImportExportSharedPreferences(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java index a88df172e..03e3cecdf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DebugActivity.java @@ -53,7 +53,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.NotificationType; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class DebugActivity extends GBActivity { +public class DebugActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(DebugActivity.class); private static final String EXTRA_REPLY = "reply"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java index 6c80cf76a..b7af301cf 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/DiscoveryActivity.java @@ -69,7 +69,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; import static android.bluetooth.le.ScanSettings.MATCH_MODE_STICKY; import static android.bluetooth.le.ScanSettings.SCAN_MODE_LOW_LATENCY; -public class DiscoveryActivity extends GBActivity implements AdapterView.OnItemClickListener { +public class DiscoveryActivity extends AbstractGBActivity implements AdapterView.OnItemClickListener { private static final Logger LOG = LoggerFactory.getLogger(DiscoveryActivity.class); private static final long SCAN_DURATION = 60000; // 60s diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java index 094639856..07befc2ea 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/ExternalPebbleJSActivity.java @@ -64,7 +64,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.PebbleUtils; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; -public class ExternalPebbleJSActivity extends GBActivity { +public class ExternalPebbleJSActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(ExternalPebbleJSActivity.class); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java index f1f5ac229..28b57e464 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/FwAppInstallerActivity.java @@ -51,7 +51,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class FwAppInstallerActivity extends GBActivity implements InstallActivity { +public class FwAppInstallerActivity extends AbstractGBActivity implements InstallActivity { private static final Logger LOG = LoggerFactory.getLogger(FwAppInstallerActivity.class); private static final String ITEM_DETAILS = "details"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java index 09246d6dc..7600c8f66 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/GBActivity.java @@ -1,80 +1,9 @@ -/* Copyright (C) 2015-2017 Andreas Shimokawa, Carsten Pfeiffer, Lem Dulfo - - This file is part of Gadgetbridge. - - Gadgetbridge is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Gadgetbridge is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.activities; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.content.res.Configuration; -import android.os.Bundle; -import android.os.LocaleList; -import android.support.v4.content.LocalBroadcastManager; -import android.support.v7.app.AppCompatActivity; - import java.util.Locale; -import nodomain.freeyourgadget.gadgetbridge.GBApplication; -import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.devices.DeviceManager; -import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils; -import nodomain.freeyourgadget.gadgetbridge.util.Prefs; +public interface GBActivity { + void setLanguage(Locale language, boolean recreate); + void setTheme(int themeId); - -public class GBActivity extends AppCompatActivity { - - private final BroadcastReceiver mReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String action = intent.getAction(); - switch (action) { - case GBApplication.ACTION_LANGUAGE_CHANGE: - setLanguage(GBApplication.getLanguage()); - break; - case GBApplication.ACTION_QUIT: - finish(); - break; - } - } - }; - - private void setLanguage(Locale language) { - AndroidUtils.setLanguage(this, language); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - IntentFilter filterLocal = new IntentFilter(); - filterLocal.addAction(GBApplication.ACTION_QUIT); - filterLocal.addAction(GBApplication.ACTION_LANGUAGE_CHANGE); - LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal); - - if (GBApplication.isDarkThemeEnabled()) { - setTheme(R.style.GadgetbridgeThemeDark); - } else { - setTheme(R.style.GadgetbridgeTheme); - } - super.onCreate(savedInstanceState); - } - - @Override - protected void onDestroy() { - LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver); - super.onDestroy(); - } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/VibrationActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/VibrationActivity.java index ea92116a6..076d8d1c3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/VibrationActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/VibrationActivity.java @@ -26,7 +26,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; -public class VibrationActivity extends GBActivity { +public class VibrationActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(VibrationActivity.class); private SeekBar seekBar; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java index deed2c2e0..4e49ae911 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/miband/MiBandPairingActivity.java @@ -37,7 +37,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2; import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity; -import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate; @@ -46,7 +46,7 @@ import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; -public class MiBandPairingActivity extends GBActivity { +public class MiBandPairingActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(MiBandPairingActivity.class); private static final int REQ_CODE_USER_SETTINGS = 52; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebblePairingActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebblePairingActivity.java index f7823a01e..30d0b6d39 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebblePairingActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/pebble/PebblePairingActivity.java @@ -38,7 +38,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.ControlCenterv2; import nodomain.freeyourgadget.gadgetbridge.activities.DiscoveryActivity; -import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator; import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; @@ -50,7 +50,7 @@ import nodomain.freeyourgadget.gadgetbridge.model.DeviceType; import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper; import nodomain.freeyourgadget.gadgetbridge.util.GB; -public class PebblePairingActivity extends GBActivity { +public class PebblePairingActivity extends AbstractGBActivity { private static final Logger LOG = LoggerFactory.getLogger(PebblePairingActivity.class); private TextView message; private boolean isPairing; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationConfig.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationConfig.java index d7caa1e8a..93e4dcb2c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationConfig.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/WeatherNotificationConfig.java @@ -19,9 +19,9 @@ package nodomain.freeyourgadget.gadgetbridge.externalevents; import android.os.Bundle; import nodomain.freeyourgadget.gadgetbridge.R; -import nodomain.freeyourgadget.gadgetbridge.activities.GBActivity; +import nodomain.freeyourgadget.gadgetbridge.activities.AbstractGBActivity; -public class WeatherNotificationConfig extends GBActivity { +public class WeatherNotificationConfig extends AbstractGBActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/AndroidUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/AndroidUtils.java index a3258b19f..06bafd277 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/AndroidUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/AndroidUtils.java @@ -70,13 +70,20 @@ public class AndroidUtils { } } - public static void setLanguage(Activity activity, Locale language) { + public static void setLanguage(Activity activity, Locale language, boolean recreate) { + setLanguage(activity.getBaseContext(), language); + + if (recreate) { + activity.recreate(); + } + } + + public static void setLanguage(Context context, Locale language) { Configuration config = new Configuration(); config.setLocale(language); // FIXME: I have no idea what I am doing - activity.getBaseContext().getResources().updateConfiguration(config, activity.getBaseContext().getResources().getDisplayMetrics()); - activity.recreate(); + context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics()); } /**