Skip to content

Commit

Permalink
Fix crash in settings when trying to get notification permission (#212)
Browse files Browse the repository at this point in the history
* Fix crash in Settings when reminders are enabled but notifications are not enabled

registerForActivityResult must be called from onCreate or we get a crash. Fixes the crash and tests that the notifications dialog is shown. Leaves as TODO to tell the user they must go into settings to turn notifications on after notifications are disabled the first time. This could do with some polish but fixes #211

* Add string for reminders setting when notifications disabled, rev version for release
  • Loading branch information
dektar authored Feb 26, 2025
1 parent db3035e commit 457757a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
4 changes: 2 additions & 2 deletions 5calls/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "org.a5calls.android.a5calls"
minSdkVersion 23
targetSdkVersion 34
versionCode 63
versionName '2.3.5'
versionCode 64
versionName '2.3.6'
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
signingConfig signingConfigs.debug
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public void onClick(DialogInterface dialogInterface, int i) {
if (mSelectedOption == 0) {
OneSignal.getUser().getPushSubscription().optIn();
OneSignal.getNotifications().requestPermission(true, Continue.none());
// TODO(#139): Do not turn on notifications preference if they did not enable
// permissions.
}
SettingsActivity.updateNotificationsPreference(
(FiveCallsApplication) getActivity().getApplication(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationManagerCompat;
Expand Down Expand Up @@ -112,7 +113,7 @@ public static void updateNotificationsPreference(FiveCallsApplication applicatio
if (TextUtils.equals("0", result)) {
OneSignal.getNotifications().requestPermission(true, Continue.none());
OneSignal.getUser().getPushSubscription().optIn();
// TODO: Wait for permission request result before opting in
// TODO(#139): Wait for permission request result before opting in
} else if (TextUtils.equals("1", result)) {
OneSignal.getUser().getPushSubscription().optOut();
}
Expand All @@ -134,6 +135,26 @@ public static void updateNotificationsPreference(FiveCallsApplication applicatio
public static class SettingsFragment extends PreferenceFragmentCompat implements
SharedPreferences.OnSharedPreferenceChangeListener {
private final AccountManager accountManager = AccountManager.Instance;
private ActivityResultLauncher<String> mNotificationPermissionRequest;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestNotificationPermission((isGranted) -> {
// If the user denied the notification permission, set the preference to false
// Otherwise they granted and we will set the permission to true
accountManager.setAllowReminders(getActivity(), isGranted);
if (!isGranted) {
SwitchPreference remindersPref =
findPreference(AccountManager.KEY_ALLOW_REMINDERS);
if (remindersPref == null) {
return;
}
remindersPref.setChecked(false);
remindersPref.setSummary(R.string.reminders_disabled_summary);
}
});
}

@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
Expand Down Expand Up @@ -191,11 +212,10 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
boolean result = sharedPreferences.getBoolean(key, false);
if (result && !NotificationManagerCompat.from(requireContext()).areNotificationsEnabled()) {
// Trying to enable reminders and notification permission is not granted
requestNotificationPermission((isGranted) -> {
// If the user denied the notification permission, set the preference to false
// Otherwise they granted and we will set the permission to true
accountManager.setAllowReminders(getActivity(), isGranted);
});
if (mNotificationPermissionRequest != null
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
mNotificationPermissionRequest.launch(Manifest.permission.POST_NOTIFICATIONS);
}
} else {
// Either disabling reminders or notification permission is already granted
// so we don't need to prompt
Expand Down Expand Up @@ -242,7 +262,7 @@ private void requestNotificationPermission(Consumer<Boolean> isGranted) {
== PackageManager.PERMISSION_GRANTED) {
return;
}
registerForActivityResult(
mNotificationPermissionRequest = registerForActivityResult(
new ActivityResultContracts.RequestPermission(), isGranted::accept
);
}
Expand Down
3 changes: 3 additions & 0 deletions 5calls/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@
<!-- Summary for settings option to enable reminders [CHAR_LIMIT=NONE] -->
<string name="settings_reminders_enable_summary">Enable reminders to make 5 calls.</string>

<!-- Summary string for settings option to enable reminders, when the notification permission has been denied [CHAR_LIMIT=NONE] -->
<string name="reminders_disabled_summary">Notifications are disabled. Turn on notifications in your device\'s settings to allow reminders.</string>

<!-- Title for settings option to enable weekday reminders [CHAR_LIMIT=50] -->
<string name="settings_reminder_days_title">Remind me on these days</string>

Expand Down

0 comments on commit 457757a

Please sign in to comment.