diff --git a/app/src/main/java/de/danoeh/antennapod/ui/home/HomeFragment.java b/app/src/main/java/de/danoeh/antennapod/ui/home/HomeFragment.java index 778b57c8c..f65e9fee7 100644 --- a/app/src/main/java/de/danoeh/antennapod/ui/home/HomeFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/ui/home/HomeFragment.java @@ -1,7 +1,10 @@ package de.danoeh.antennapod.ui.home; +import android.Manifest; import android.content.Context; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; @@ -13,6 +16,7 @@ import android.view.View; import android.view.ViewGroup; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; +import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentContainerView; import de.danoeh.antennapod.R; @@ -24,6 +28,7 @@ import de.danoeh.antennapod.databinding.HomeFragmentBinding; import de.danoeh.antennapod.event.FeedListUpdateEvent; import de.danoeh.antennapod.event.FeedUpdateRunningEvent; import de.danoeh.antennapod.fragment.SearchFragment; +import de.danoeh.antennapod.ui.home.sections.AllowNotificationsSection; import de.danoeh.antennapod.ui.home.sections.DownloadsSection; import de.danoeh.antennapod.ui.home.sections.EpisodesSurpriseSection; import de.danoeh.antennapod.ui.home.sections.InboxSection; @@ -84,6 +89,11 @@ public class HomeFragment extends Fragment implements Toolbar.OnMenuItemClickLis private void populateSectionList() { viewBinding.homeContainer.removeAllViews(); + if (Build.VERSION.SDK_INT >= 33 && ContextCompat.checkSelfPermission(getContext(), + Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + addSection(new AllowNotificationsSection()); + } + List hiddenSections = getHiddenSections(getContext()); String[] sectionTags = getResources().getStringArray(R.array.home_section_tags); for (String sectionTag : sectionTags) { diff --git a/app/src/main/java/de/danoeh/antennapod/ui/home/sections/AllowNotificationsSection.java b/app/src/main/java/de/danoeh/antennapod/ui/home/sections/AllowNotificationsSection.java new file mode 100644 index 000000000..b023a19bf --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/ui/home/sections/AllowNotificationsSection.java @@ -0,0 +1,54 @@ +package de.danoeh.antennapod.ui.home.sections; + +import android.Manifest; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; +import android.os.Bundle; +import android.provider.Settings; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.Fragment; +import de.danoeh.antennapod.R; +import de.danoeh.antennapod.activity.MainActivity; +import de.danoeh.antennapod.databinding.HomeSectionNotificationBinding; +import de.danoeh.antennapod.ui.home.HomeFragment; + +public class AllowNotificationsSection extends Fragment { + HomeSectionNotificationBinding viewBinding; + + private final ActivityResultLauncher requestPermissionLauncher = + registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { + if (isGranted) { + ((MainActivity) getActivity()).loadFragment(HomeFragment.TAG, null); + } else { + viewBinding.openSettingsButton.setVisibility(View.VISIBLE); + Toast.makeText(getContext(), R.string.notification_permission_denied, Toast.LENGTH_LONG).show(); + } + }); + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, + @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + viewBinding = HomeSectionNotificationBinding.inflate(inflater); + viewBinding.allowButton.setOnClickListener(v -> { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS); + } + }); + viewBinding.openSettingsButton.setOnClickListener(view -> { + Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); + Uri uri = Uri.fromParts("package", getContext().getPackageName(), null); + intent.setData(uri); + startActivity(intent); + }); + return viewBinding.getRoot(); + } +} diff --git a/app/src/main/res/layout/home_section_notification.xml b/app/src/main/res/layout/home_section_notification.xml new file mode 100644 index 000000000..7e2182176 --- /dev/null +++ b/app/src/main/res/layout/home_section_notification.xml @@ -0,0 +1,49 @@ + + + + + + + + + +