use of xyz.zedler.patrick.grocy.util.NetUtil in project grocy-android by patzly.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
debug = PrefsUtil.isDebuggingEnabled(sharedPrefs);
insertConscrypt();
// DARK MODE
// this has to be placed before super.onCreate(savedInstanceState);
// https://stackoverflow.com/a/53356918
int theme = sharedPrefs.getInt(APPEARANCE.THEME, SETTINGS_DEFAULT.APPEARANCE.THEME);
AppCompatDelegate.setDefaultNightMode(theme);
// LANGUAGE
Locale userLocale = LocaleUtil.getUserLocale(this);
Locale.setDefault(userLocale);
// base
Resources resBase = getBaseContext().getResources();
Configuration configBase = resBase.getConfiguration();
configBase.setLocale(userLocale);
resBase.updateConfiguration(configBase, resBase.getDisplayMetrics());
// app
Resources resApp = getApplicationContext().getResources();
Configuration configApp = resApp.getConfiguration();
configApp.setLocale(userLocale);
resApp.updateConfiguration(configApp, getResources().getDisplayMetrics());
// set localized demo instance
String serverUrl = sharedPrefs.getString(Constants.PREF.SERVER_URL, null);
if (serverUrl != null && serverUrl.contains("demo.grocy.info")) {
List<Language> languages = LocaleUtil.getLanguages(this);
String demoDomain = null;
for (Language language : languages) {
if (language.getCode().equals(userLocale.getLanguage())) {
demoDomain = language.getDemoDomain();
}
}
if (demoDomain != null && !serverUrl.contains(demoDomain)) {
serverUrl = serverUrl.replaceAll("[a-z]+-?[a-z]*\\.demo\\.grocy\\.info", demoDomain);
sharedPrefs.edit().putString(Constants.PREF.SERVER_URL, serverUrl).apply();
}
}
super.onCreate(savedInstanceState);
// UTILS
clickUtil = new ClickUtil();
netUtil = new NetUtil(this);
// WEB
networkReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Fragment navHostFragment = fragmentManager.findFragmentById(R.id.nav_host_fragment);
assert navHostFragment != null;
if (navHostFragment.getChildFragmentManager().getFragments().size() == 0) {
return;
}
getCurrentFragment().updateConnectivity(netUtil.isOnline());
}
};
registerReceiver(networkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
boolean useTor = sharedPrefs.getBoolean(NETWORK.TOR, SETTINGS_DEFAULT.NETWORK.TOR);
if (useTor && !OrbotHelper.get(this).init()) {
OrbotHelper.get(this).installOrbot(this);
}
// API
updateGrocyApi();
repository = new MainRepository(getApplication());
// VIEWS
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
fragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.nav_host_fragment);
assert navHostFragment != null;
navController = navHostFragment.getNavController();
updateStartDestination();
navController.addOnDestinationChangedListener((controller, dest, args) -> {
if (isServerUrlEmpty() || dest.getId() == R.id.shoppingModeFragment || dest.getId() == R.id.onboardingFragment) {
binding.bottomAppBar.setVisibility(View.GONE);
binding.fabMain.hide();
new Handler().postDelayed(() -> setNavBarColor(R.color.background), 10);
} else {
binding.bottomAppBar.setVisibility(View.VISIBLE);
setNavBarColor(R.color.primary);
}
setProperNavBarDividerColor(dest);
});
// BOTTOM APP BAR
binding.bottomAppBar.setNavigationOnClickListener(v -> {
if (clickUtil.isDisabled()) {
return;
}
ViewUtil.startIcon(binding.bottomAppBar.getNavigationIcon());
navController.navigate(R.id.action_global_drawerBottomSheetDialogFragment);
});
scrollBehavior = new BottomAppBarRefreshScrollBehavior(this);
scrollBehavior.setUpBottomAppBar(binding.bottomAppBar);
scrollBehavior.setUpTopScroll(R.id.fab_scroll);
scrollBehavior.setHideOnScroll(true);
Runnable onSuccessConfigLoad = () -> {
String version = sharedPrefs.getString(Constants.PREF.GROCY_VERSION, null);
if (version == null || version.isEmpty()) {
return;
}
ArrayList<String> supportedVersions = new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.compatible_grocy_versions)));
if (supportedVersions.contains(version)) {
return;
}
// If user already ignored warning, do not display again
String ignoredVersion = sharedPrefs.getString(Constants.PREF.VERSION_COMPATIBILITY_IGNORED, null);
if (ignoredVersion != null && ignoredVersion.equals(version)) {
return;
}
Bundle bundle = new Bundle();
bundle.putString(Constants.ARGUMENT.VERSION, version);
bundle.putStringArrayList(Constants.ARGUMENT.SUPPORTED_VERSIONS, supportedVersions);
showBottomSheet(new CompatibilityBottomSheet(), bundle);
};
if (!isServerUrlEmpty()) {
ConfigUtil.loadInfo(new DownloadHelper(this, TAG), grocyApi, sharedPrefs, onSuccessConfigLoad, null);
}
}
Aggregations