Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/BustoApp.java
| Show All 13 Lines | BusTO - Arrival times for Turin public transport. | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino; | package it.reyboz.bustorino; | ||||
| import android.content.Context; | import android.content.Context; | ||||
| import android.content.SharedPreferences; | |||||
| import androidx.appcompat.app.AppCompatDelegate; | |||||
| import androidx.multidex.MultiDexApplication; | import androidx.multidex.MultiDexApplication; | ||||
| import androidx.preference.PreferenceManager; | |||||
| import it.reyboz.bustorino.backend.utils; | |||||
| import it.reyboz.bustorino.data.PreferencesHolder; | |||||
| import it.reyboz.bustorino.fragments.SettingsFragment; | |||||
| import org.acra.ACRA; | import org.acra.ACRA; | ||||
| import org.acra.BuildConfig; | import org.acra.BuildConfig; | ||||
| import org.acra.ReportField; | import org.acra.ReportField; | ||||
| import org.acra.config.CoreConfigurationBuilder; | import org.acra.config.CoreConfigurationBuilder; | ||||
| import org.acra.config.DialogConfigurationBuilder; | import org.acra.config.DialogConfigurationBuilder; | ||||
| import org.acra.config.MailSenderConfigurationBuilder; | import org.acra.config.MailSenderConfigurationBuilder; | ||||
| import org.acra.data.StringFormat; | import org.acra.data.StringFormat; | ||||
| import java.lang.reflect.Array; | import java.lang.reflect.Array; | ||||
| import java.util.ArrayList; | import java.util.*; | ||||
| import java.util.List; | |||||
| import static org.acra.ReportField.*; | import static org.acra.ReportField.*; | ||||
| public class BustoApp extends MultiDexApplication { | public class BustoApp extends MultiDexApplication { | ||||
| private static final List<ReportField> REPORT_FIELDS = List.of(REPORT_ID, APP_VERSION_CODE, APP_VERSION_NAME, | private static final List<ReportField> REPORT_FIELDS = List.of(REPORT_ID, APP_VERSION_CODE, APP_VERSION_NAME, | ||||
| PACKAGE_NAME, PHONE_MODEL, BRAND, PRODUCT, ANDROID_VERSION, BUILD_CONFIG, CUSTOM_DATA, | PACKAGE_NAME, PHONE_MODEL, BRAND, PRODUCT, ANDROID_VERSION, BUILD_CONFIG, CUSTOM_DATA, | ||||
| IS_SILENT, STACK_TRACE, INITIAL_CONFIGURATION, CRASH_CONFIGURATION, DISPLAY, USER_COMMENT, | IS_SILENT, STACK_TRACE, INITIAL_CONFIGURATION, CRASH_CONFIGURATION, DISPLAY, USER_COMMENT, | ||||
| USER_APP_START_DATE, USER_CRASH_DATE, LOGCAT, SHARED_PREFERENCES); | USER_APP_START_DATE, USER_CRASH_DATE, LOGCAT, SHARED_PREFERENCES); | ||||
| private Integer currentNightDayStatus = -1; | |||||
| @Override | @Override | ||||
| protected void attachBaseContext(Context base) { | protected void attachBaseContext(Context base) { | ||||
| super.attachBaseContext(base); | super.attachBaseContext(base); | ||||
| CoreConfigurationBuilder builder = new CoreConfigurationBuilder(); | CoreConfigurationBuilder builder = new CoreConfigurationBuilder(); | ||||
| // mail stuff | // mail stuff | ||||
| MailSenderConfigurationBuilder mailConfig = new MailSenderConfigurationBuilder(); | MailSenderConfigurationBuilder mailConfig = new MailSenderConfigurationBuilder(); | ||||
| mailConfig.withMailTo("gtt@succhia.cz") | mailConfig.withMailTo("gtt@succhia.cz") | ||||
| Show All 15 Lines | protected void attachBaseContext(Context base) { | ||||
| builder.setReportContent(REPORT_FIELDS); | builder.setReportContent(REPORT_FIELDS); | ||||
| if (!it.reyboz.bustorino.BuildConfig.DEBUG) | if (!it.reyboz.bustorino.BuildConfig.DEBUG) | ||||
| ACRA.init(this, builder); | ACRA.init(this, builder); | ||||
| } | } | ||||
| private final SharedPreferences.OnSharedPreferenceChangeListener appSharedPreference = (sharedPreferences, key) -> { | |||||
| if(Objects.equals(key, PreferencesHolder.PREF_THEME_DAY_NIGHT)){ | |||||
| String value = sharedPreferences.getString(PreferencesHolder.PREF_THEME_DAY_NIGHT, ""); | |||||
| var state = PreferencesHolder.getAppThemeDayNight(value); | |||||
| if(state!=null && !state.equals(currentNightDayStatus)){ | |||||
| currentNightDayStatus = state; | |||||
| AppCompatDelegate.setDefaultNightMode(state); | |||||
| } | |||||
| } | |||||
| }; | |||||
| @Override | |||||
| public void onCreate() { | |||||
| super.onCreate(); | |||||
| checkApplyDefaultSettingsValues(); | |||||
| var dayorNight = PreferencesHolder.getAppThemeDayNight(this); | |||||
| currentNightDayStatus = dayorNight; | |||||
| AppCompatDelegate.setDefaultNightMode(dayorNight); | |||||
| PreferencesHolder.getAppPreferences(this).registerOnSharedPreferenceChangeListener(appSharedPreference); | |||||
| } | |||||
| /** | |||||
| * Adjust setting to match the default ones | |||||
| */ | |||||
| protected void checkApplyDefaultSettingsValues(){ | |||||
| SharedPreferences mainSharedPref = PreferenceManager.getDefaultSharedPreferences(this); | |||||
| SharedPreferences.Editor editor = mainSharedPref.edit(); | |||||
| boolean edit = false; | |||||
| //Day or night theme | |||||
| var str = mainSharedPref.getString(PreferencesHolder.PREF_THEME_DAY_NIGHT, ""); | |||||
| if(str.isEmpty()){ | |||||
| editor.putString(PreferencesHolder.PREF_THEME_DAY_NIGHT, "system"); | |||||
| edit = true; | |||||
| } | |||||
| //Main fragment to show | |||||
| String screen = mainSharedPref.getString(SettingsFragment.PREF_KEY_STARTUP_SCREEN, ""); | |||||
| if (screen.isEmpty()){ | |||||
| editor.putString(SettingsFragment.PREF_KEY_STARTUP_SCREEN, "arrivals"); | |||||
| edit=true; | |||||
| } | |||||
| //Fetchers | |||||
| final Set<String> setSelected = mainSharedPref.getStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, new HashSet<>()); | |||||
| if (setSelected.isEmpty()){ | |||||
| String[] defaultVals = getResources().getStringArray(R.array.arrivals_sources_values_default); | |||||
| editor.putStringSet(SettingsFragment.KEY_ARRIVALS_FETCHERS_USE, utils.convertArrayToSet(defaultVals)); | |||||
| edit=true; | |||||
| } | |||||
| //Live bus positions | |||||
| final String keySourcePositions=getString(R.string.pref_positions_source); | |||||
| final String positionsSource = mainSharedPref.getString(keySourcePositions, ""); | |||||
| if(positionsSource.isEmpty()){ | |||||
| String[] defaultVals = getResources().getStringArray(R.array.positions_source_values); | |||||
| editor.putString(keySourcePositions, defaultVals[0]); | |||||
| edit=true; | |||||
| } | |||||
| //Map style | |||||
| final String mapStylePref = mainSharedPref.getString(SettingsFragment.LIBREMAP_STYLE_PREF_KEY, ""); | |||||
| if(mapStylePref.isEmpty()){ | |||||
| final String[] defaultVals = getResources().getStringArray(R.array.map_style_pref_values); | |||||
| editor.putString(SettingsFragment.LIBREMAP_STYLE_PREF_KEY, defaultVals[0]); | |||||
| edit=true; | |||||
| } | |||||
| if (edit){ | |||||
| editor.apply(); | |||||
| } | |||||
| } | |||||
| } | } | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register