Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime notification permisson to support android 13 and above #424

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<queries>
<!-- Specific apps Watomatic interacts with. Required for Android 11+ -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.checkbox.MaterialCheckBox;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.parishod.watomatic.BuildConfig;
import com.parishod.watomatic.NotificationService;
Expand Down Expand Up @@ -68,6 +71,7 @@
public class MainFragment extends Fragment {

private static final int REQ_NOTIFICATION_LISTENER = 100;
private static final int NOTIFICATION_REQUEST_CODE = 101;
CardView autoReplyTextPreviewCard, timePickerCard;
TextView autoReplyTextPreview, timeSelectedTextPreview, timePickerSubTitleTextPreview;
CustomRepliesData customRepliesData;
Expand Down Expand Up @@ -131,6 +135,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
// launchNotificationAccessSettings();
showPermissionsDialog();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (!isPostNotificationPermissionGranted()) {
checkNotificationPermission();
return;
}
}
preferencesManager.setServicePref(isChecked);
if (isChecked) {
startNotificationService();
Expand Down Expand Up @@ -179,10 +189,49 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
});

setNumDays();

if (!isPostNotificationPermissionGranted()) {
checkNotificationPermission();
}
return view;
}

private void checkNotificationPermission(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.requestPermissions(mActivity, new String[]{android.Manifest.permission.POST_NOTIFICATIONS}, NOTIFICATION_REQUEST_CODE);
}
}

private boolean isPostNotificationPermissionGranted(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return ContextCompat.checkSelfPermission(mActivity, android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED;
}
return true;
}

private void showPostNotificationPermissionDeniedSnackbar(View view){
Snackbar.make(view, mActivity.getResources().getString(R.string.post_notification_permission_snackbar_text), Snackbar.LENGTH_INDEFINITE)
.setAction(mActivity.getResources().getString(R.string.post_notification_permission_snackbar_setting), view1 -> {
// Open app settings
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", view1.getContext().getPackageName(), null);
intent.setData(uri);
view1.getContext().startActivity(intent);
}).show();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == NOTIFICATION_REQUEST_CODE){
// If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Displaying a toast
} else {
// Displaying another toast if permission is not granted
showPostNotificationPermissionDeniedSnackbar(mainAutoReplySwitch);
}
}
}

private List<App> getEnabledApps() {
if (enabledApps != null) {
enabledApps.clear();
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,7 @@
<string name="current_donation_progress">Current donation progress</string>
<string name="error_name_cannot_be_blank">Name cannot be blank</string>
<string name="error_name_cannot_be_duplicate">Name already present</string>

<string name="post_notification_permission_snackbar_text">Notification permissions are required for this app. Enable it in settings.</string>
<string name="post_notification_permission_snackbar_setting">Settings</string>
</resources>
Loading