Show custom notifications for API < 26.

This commit is contained in:
Greyson Parrelli 2020-06-07 13:13:51 -04:00
parent d8f3e032c7
commit b48abb08d2
6 changed files with 25 additions and 14 deletions

View file

@ -314,19 +314,17 @@ public class ManageGroupFragment extends Fragment {
}
});
if (NotificationChannels.supported()) {
customNotificationsRow.setVisibility(View.VISIBLE);
customNotificationsRow.setOnClickListener(v -> CustomNotificationsDialogFragment.create(groupId)
.show(requireFragmentManager(), "CUSTOM_NOTIFICATIONS"));
//noinspection CodeBlock2Expr
if (NotificationChannels.supported()) {
viewModel.hasCustomNotifications().observe(getViewLifecycleOwner(), hasCustomNotifications -> {
customNotificationsButton.setText(hasCustomNotifications ? R.string.ManageGroupActivity_on
: R.string.ManageGroupActivity_off);
});
} else {
customNotificationsRow.setVisibility(View.GONE);
}
viewModel.getSnackbarEvents().observe(getViewLifecycleOwner(), this::handleSnackbarEvent);

View file

@ -31,6 +31,7 @@ import org.thoughtcrime.securesms.groups.ui.GroupChangeFailureReason;
import org.thoughtcrime.securesms.groups.ui.GroupErrors;
import org.thoughtcrime.securesms.groups.ui.GroupMemberEntry;
import org.thoughtcrime.securesms.groups.ui.addmembers.AddMembersActivity;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.recipients.RecipientId;
import org.thoughtcrime.securesms.recipients.RecipientUtil;
@ -100,7 +101,7 @@ public class ManageGroupViewModel extends ViewModel {
this.muteState = Transformations.map(this.groupRecipient,
recipient -> new MuteState(recipient.getMuteUntil(), recipient.isMuted()));
this.hasCustomNotifications = Transformations.map(this.groupRecipient,
recipient -> recipient.getNotificationChannel() != null);
recipient -> recipient.getNotificationChannel() != null || !NotificationChannels.supported());
this.canLeaveGroup = liveGroup.isActive();
this.canBlockGroup = Transformations.map(this.groupRecipient, recipient -> !recipient.isBlocked());
}

View file

@ -24,6 +24,7 @@ import androidx.lifecycle.ViewModelProviders;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.groups.GroupId;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.util.ThemeUtil;
public class CustomNotificationsDialogFragment extends DialogFragment {
@ -123,6 +124,11 @@ public class CustomNotificationsDialogFragment extends DialogFragment {
vibrateSwitch.setVisibility(hasCustomNotifications ? View.VISIBLE : View.GONE);
});
if (!NotificationChannels.supported()) {
customNotificationsSwitch.setVisibility(View.GONE);
view.findViewById(R.id.custom_notifications_enable_label).setVisibility(View.GONE);
}
CompoundButton.OnCheckedChangeListener onVibrateSwitchCheckChangedListener = (buttonView, isChecked) -> {
viewModel.setMessageVibrate(isChecked ? RecipientDatabase.VibrateState.ENABLED : RecipientDatabase.VibrateState.DISABLED);
};

View file

@ -30,11 +30,13 @@ class CustomNotificationsRepository {
Recipient recipient = getRecipient();
RecipientDatabase recipientDatabase = DatabaseFactory.getRecipientDatabase(context);
if (NotificationChannels.supported()) {
recipientDatabase.setMessageRingtone(recipient.getId(), NotificationChannels.getMessageRingtone(context, recipient));
recipientDatabase.setMessageVibrate(recipient.getId(), NotificationChannels.getMessageVibrate(context, recipient) ? RecipientDatabase.VibrateState.ENABLED
: RecipientDatabase.VibrateState.DISABLED);
NotificationChannels.ensureCustomChannelConsistency(context);
}
onLoaded.run();
});

View file

@ -13,6 +13,7 @@ import androidx.lifecycle.ViewModelProvider;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.groups.GroupId;
import org.thoughtcrime.securesms.groups.LiveGroup;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
import org.thoughtcrime.securesms.recipients.Recipient;
public final class CustomNotificationsViewModel extends ViewModel {
@ -27,7 +28,7 @@ public final class CustomNotificationsViewModel extends ViewModel {
private CustomNotificationsViewModel(@NonNull GroupId groupId, @NonNull CustomNotificationsRepository repository) {
this.liveGroup = new LiveGroup(groupId);
this.repository = repository;
this.hasCustomNotifications = Transformations.map(liveGroup.getGroupRecipient(), recipient -> recipient.getNotificationChannel() != null);
this.hasCustomNotifications = Transformations.map(liveGroup.getGroupRecipient(), recipient -> recipient.getNotificationChannel() != null || !NotificationChannels.supported());
this.isVibrateEnabled = Transformations.map(liveGroup.getGroupRecipient(), Recipient::getMessageVibrate);
this.notificationSound = Transformations.map(liveGroup.getGroupRecipient(), Recipient::getMessageRingtone);

View file

@ -400,6 +400,9 @@ public class NotificationChannels {
@TargetApi(26)
@WorkerThread
public static synchronized void ensureCustomChannelConsistency(@NonNull Context context) {
if (!supported()) {
return;
}
Log.d(TAG, "ensureCustomChannelConsistency()");
NotificationManager notificationManager = ServiceUtil.getNotificationManager(context);