app/src/main/java/com/futsch1/medtimer/MainActivity.java
package com.futsch1.medtimer;
import static android.Manifest.permission.POST_NOTIFICATIONS;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.preference.PreferenceManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
result -> {
if (Boolean.FALSE.equals(result)) {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("show_notification", false).apply();
}
}
);
private AppBarConfiguration appBarConfiguration;
private OptionsMenu optionsMenu;
private final ActivityResultLauncher<Intent> requestFileLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
optionsMenu.fileSelected(result.getData().getData());
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Select theme
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String theme = sharedPref.getString("theme", "0");
if (theme.equals("1")) {
setTheme(R.style.Theme_MedTimer2);
}
boolean introShown = sharedPref.getBoolean("intro_shown", false);
if (!introShown && !BuildConfig.DEBUG) {
startActivity(new Intent(getApplicationContext(), MedTimerAppIntro.class));
sharedPref.edit().putBoolean("intro_shown", true).apply();
} else {
checkPermissions();
}
ReminderNotificationChannelManager.Companion.initialize(this);
setContentView(R.layout.activity_main);
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHost);
assert navHostFragment != null;
NavController navController = navHostFragment.getNavController();
appBarConfiguration = new AppBarConfiguration.Builder(
R.id.overviewFragment, R.id.medicinesFragment, R.id.statisticsFragment)
.build();
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);
NavigationUI.setupWithNavController(bottomNavigationView, navController);
optionsMenu = new OptionsMenu(this,
new MedicineViewModel(this.getApplication()),
requestFileLauncher,
navController);
addMenuProvider(optionsMenu);
}
@Override
protected void onResume() {
super.onResume();
startService(new Intent(getApplicationContext(), ReminderSchedulerService.class));
}
private void checkPermissions() {
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
requestPermissionLauncher.launch(POST_NOTIFICATIONS);
}
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.navHost);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
}