Add banner on home screen if notification permission is not granted (#6412)

This commit is contained in:
ByteHamster 2023-04-07 16:37:32 +02:00 committed by GitHub
parent da9bb8d578
commit 39d309e906
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 116 additions and 0 deletions

View File

@ -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<String> hiddenSections = getHiddenSections(getContext());
String[] sectionTags = getResources().getStringArray(R.array.home_section_tags);
for (String sectionTag : sectionTags) {

View File

@ -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<String> 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();
}
}

View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:padding="16dp">
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
app:cardCornerRadius="12dp"
app:cardElevation="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:text="@string/notification_permission_text" />
<Button
android:id="@+id/allowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@android:string/ok" />
<Button
android:id="@+id/openSettingsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:visibility="gone"
android:text="@string/open_settings" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>

View File

@ -60,6 +60,9 @@
<string name="home_downloads_title">Manage downloads</string>
<string name="home_welcome_title">Welcome to AntennaPod!</string>
<string name="home_welcome_text">You are not subscribed to any podcasts yet. Open the side menu to add a podcast.</string>
<string name="notification_permission_text">AntennaPod needs your permission to show notifications while downloading episodes.</string>
<string name="notification_permission_denied">You denied the permission.</string>
<string name="open_settings">Open settings</string>
<string name="configure_home">Configure Home Screen</string>
<!-- Download Statistics fragment -->