Search in sources :

Example 1 with NetUtil

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);
    }
}
Also used : Locale(java.util.Locale) Configuration(android.content.res.Configuration) ArrayList(java.util.ArrayList) Fragment(androidx.fragment.app.Fragment) NavHostFragment(androidx.navigation.fragment.NavHostFragment) BaseFragment(xyz.zedler.patrick.grocy.fragment.BaseFragment) BottomSheetDialogFragment(com.google.android.material.bottomsheet.BottomSheetDialogFragment) CompatibilityBottomSheet(xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.CompatibilityBottomSheet) Language(xyz.zedler.patrick.grocy.model.Language) NetUtil(xyz.zedler.patrick.grocy.util.NetUtil) MainRepository(xyz.zedler.patrick.grocy.repository.MainRepository) ClickUtil(xyz.zedler.patrick.grocy.util.ClickUtil) NavHostFragment(androidx.navigation.fragment.NavHostFragment) SSLContext(javax.net.ssl.SSLContext) Context(android.content.Context) IntentFilter(android.content.IntentFilter) Bundle(android.os.Bundle) Handler(android.os.Handler) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) DownloadHelper(xyz.zedler.patrick.grocy.helper.DownloadHelper) BottomAppBarRefreshScrollBehavior(xyz.zedler.patrick.grocy.behavior.BottomAppBarRefreshScrollBehavior) Resources(android.content.res.Resources)

Aggregations

BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Configuration (android.content.res.Configuration)1 Resources (android.content.res.Resources)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Fragment (androidx.fragment.app.Fragment)1 NavHostFragment (androidx.navigation.fragment.NavHostFragment)1 BottomSheetDialogFragment (com.google.android.material.bottomsheet.BottomSheetDialogFragment)1 ArrayList (java.util.ArrayList)1 Locale (java.util.Locale)1 SSLContext (javax.net.ssl.SSLContext)1 BottomAppBarRefreshScrollBehavior (xyz.zedler.patrick.grocy.behavior.BottomAppBarRefreshScrollBehavior)1 BaseFragment (xyz.zedler.patrick.grocy.fragment.BaseFragment)1 CompatibilityBottomSheet (xyz.zedler.patrick.grocy.fragment.bottomSheetDialog.CompatibilityBottomSheet)1 DownloadHelper (xyz.zedler.patrick.grocy.helper.DownloadHelper)1 Language (xyz.zedler.patrick.grocy.model.Language)1 MainRepository (xyz.zedler.patrick.grocy.repository.MainRepository)1