mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-04-23 18:30:20 +01:00
Make CustomNotificationsDialogFragment work with recipients.
This commit is contained in:
committed by
Greyson Parrelli
parent
b3a59c3946
commit
f5626f678d
@@ -35,7 +35,6 @@ import org.thoughtcrime.securesms.groups.GroupId;
|
||||
import org.thoughtcrime.securesms.groups.ui.GroupMemberListView;
|
||||
import org.thoughtcrime.securesms.groups.ui.LeaveGroupDialog;
|
||||
import org.thoughtcrime.securesms.groups.ui.managegroup.dialogs.GroupRightsDialog;
|
||||
import org.thoughtcrime.securesms.groups.ui.notifications.CustomNotificationsDialogFragment;
|
||||
import org.thoughtcrime.securesms.groups.ui.pendingmemberinvites.PendingMemberInvitesActivity;
|
||||
import org.thoughtcrime.securesms.logging.Log;
|
||||
import org.thoughtcrime.securesms.mediaoverview.MediaOverviewActivity;
|
||||
@@ -45,6 +44,7 @@ import org.thoughtcrime.securesms.profiles.edit.EditProfileActivity;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.recipients.RecipientId;
|
||||
import org.thoughtcrime.securesms.recipients.ui.bottomsheet.RecipientBottomSheetDialogFragment;
|
||||
import org.thoughtcrime.securesms.recipients.ui.notifications.CustomNotificationsDialogFragment;
|
||||
import org.thoughtcrime.securesms.util.DateUtils;
|
||||
import org.thoughtcrime.securesms.util.LifecycleCursorWrapper;
|
||||
|
||||
@@ -205,6 +205,8 @@ public class ManageGroupFragment extends Fragment {
|
||||
activity.startActivity(AvatarPreviewActivity.intentFromRecipientId(activity, groupRecipient.getId()),
|
||||
AvatarPreviewActivity.createTransitionBundle(activity, avatar));
|
||||
});
|
||||
customNotificationsRow.setOnClickListener(v -> CustomNotificationsDialogFragment.create(groupRecipient.getId())
|
||||
.show(requireFragmentManager(), "CUSTOM_NOTIFICATIONS"));
|
||||
});
|
||||
|
||||
viewModel.getGroupViewState().observe(getViewLifecycleOwner(), vs -> {
|
||||
@@ -299,9 +301,6 @@ public class ManageGroupFragment extends Fragment {
|
||||
|
||||
customNotificationsRow.setVisibility(View.VISIBLE);
|
||||
|
||||
customNotificationsRow.setOnClickListener(v -> CustomNotificationsDialogFragment.create(groupId)
|
||||
.show(requireFragmentManager(), "CUSTOM_NOTIFICATIONS"));
|
||||
|
||||
//noinspection CodeBlock2Expr
|
||||
if (NotificationChannels.supported()) {
|
||||
viewModel.hasCustomNotifications().observe(getViewLifecycleOwner(), hasCustomNotifications -> {
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
package org.thoughtcrime.securesms.groups.ui.notifications;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.widget.SwitchCompat;
|
||||
import androidx.appcompat.widget.Toolbar;
|
||||
import androidx.fragment.app.DialogFragment;
|
||||
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 {
|
||||
|
||||
private static final short RINGTONE_PICKER_REQUEST_CODE = 13562;
|
||||
|
||||
private static final String ARG_GROUP_ID = "group_id";
|
||||
|
||||
private SwitchCompat customNotificationsSwitch;
|
||||
private View soundLabel;
|
||||
private TextView soundSelector;
|
||||
private View vibrateLabel;
|
||||
private SwitchCompat vibrateSwitch;
|
||||
|
||||
private CustomNotificationsViewModel viewModel;
|
||||
|
||||
public static DialogFragment create(@NonNull GroupId groupId) {
|
||||
DialogFragment fragment = new CustomNotificationsDialogFragment();
|
||||
Bundle args = new Bundle();
|
||||
|
||||
args.putString(ARG_GROUP_ID, groupId.toString());
|
||||
fragment.setArguments(args);
|
||||
|
||||
return fragment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (ThemeUtil.isDarkTheme(requireActivity())) {
|
||||
setStyle(STYLE_NO_FRAME, R.style.TextSecure_DarkTheme);
|
||||
} else {
|
||||
setStyle(STYLE_NO_FRAME, R.style.TextSecure_LightTheme);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable View onCreateView(@NonNull LayoutInflater inflater,
|
||||
@Nullable ViewGroup container,
|
||||
@Nullable Bundle savedInstanceState)
|
||||
{
|
||||
return inflater.inflate(R.layout.custom_notifications_dialog_fragment, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
initializeViewModel();
|
||||
initializeViews(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
if (requestCode == RINGTONE_PICKER_REQUEST_CODE && resultCode == Activity.RESULT_OK && data != null) {
|
||||
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
|
||||
|
||||
viewModel.setMessageSound(uri);
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeViewModel() {
|
||||
Bundle arguments = requireArguments();
|
||||
GroupId groupId = GroupId.parseOrThrow(arguments.getString(ARG_GROUP_ID, ""));
|
||||
CustomNotificationsRepository repository = new CustomNotificationsRepository(requireContext(), groupId);
|
||||
CustomNotificationsViewModel.Factory factory = new CustomNotificationsViewModel.Factory(groupId, repository);
|
||||
|
||||
viewModel = ViewModelProviders.of(this, factory).get(CustomNotificationsViewModel.class);
|
||||
}
|
||||
|
||||
private void initializeViews(@NonNull View view) {
|
||||
customNotificationsSwitch = view.findViewById(R.id.custom_notifications_enable_switch);
|
||||
soundLabel = view.findViewById(R.id.custom_notifications_sound_label);
|
||||
soundSelector = view.findViewById(R.id.custom_notifications_sound_selection);
|
||||
vibrateLabel = view.findViewById(R.id.custom_notifications_vibrate_label);
|
||||
vibrateSwitch = view.findViewById(R.id.custom_notifications_vibrate_switch);
|
||||
|
||||
Toolbar toolbar = view.findViewById(R.id.custom_notifications_toolbar);
|
||||
|
||||
toolbar.setNavigationOnClickListener(v -> dismissAllowingStateLoss());
|
||||
|
||||
CompoundButton.OnCheckedChangeListener onCustomNotificationsSwitchCheckChangedListener = (buttonView, isChecked) -> {
|
||||
viewModel.setHasCustomNotifications(isChecked);
|
||||
};
|
||||
|
||||
viewModel.isInitialLoadComplete().observe(getViewLifecycleOwner(), customNotificationsSwitch::setEnabled);
|
||||
|
||||
viewModel.hasCustomNotifications().observe(getViewLifecycleOwner(), hasCustomNotifications -> {
|
||||
if (customNotificationsSwitch.isChecked() != hasCustomNotifications) {
|
||||
customNotificationsSwitch.setOnCheckedChangeListener(null);
|
||||
customNotificationsSwitch.setChecked(hasCustomNotifications);
|
||||
}
|
||||
|
||||
customNotificationsSwitch.setOnCheckedChangeListener(onCustomNotificationsSwitchCheckChangedListener);
|
||||
|
||||
soundLabel.setEnabled(hasCustomNotifications);
|
||||
vibrateLabel.setEnabled(hasCustomNotifications);
|
||||
soundSelector.setVisibility(hasCustomNotifications ? View.VISIBLE : View.GONE);
|
||||
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);
|
||||
};
|
||||
|
||||
viewModel.getVibrateState().observe(getViewLifecycleOwner(), vibrateState -> {
|
||||
boolean vibrateEnabled = vibrateState != RecipientDatabase.VibrateState.DISABLED;
|
||||
|
||||
if (vibrateSwitch.isChecked() != vibrateEnabled) {
|
||||
vibrateSwitch.setOnCheckedChangeListener(null);
|
||||
vibrateSwitch.setChecked(vibrateEnabled);
|
||||
}
|
||||
|
||||
vibrateSwitch.setOnCheckedChangeListener(onVibrateSwitchCheckChangedListener);
|
||||
});
|
||||
|
||||
viewModel.getNotificationSound().observe(getViewLifecycleOwner(), sound -> {
|
||||
soundSelector.setText(getRingtoneSummary(requireContext(), sound));
|
||||
soundSelector.setTag(sound);
|
||||
});
|
||||
|
||||
soundSelector.setOnClickListener(v -> launchSoundSelector(viewModel.getNotificationSound().getValue()));
|
||||
}
|
||||
|
||||
private @NonNull String getRingtoneSummary(@NonNull Context context, @Nullable Uri ringtone) {
|
||||
if (ringtone == null) {
|
||||
return context.getString(R.string.preferences__default);
|
||||
} else if (ringtone.toString().isEmpty()) {
|
||||
return context.getString(R.string.preferences__silent);
|
||||
} else {
|
||||
Ringtone tone = RingtoneManager.getRingtone(getActivity(), ringtone);
|
||||
|
||||
if (tone != null) {
|
||||
return tone.getTitle(context);
|
||||
}
|
||||
}
|
||||
|
||||
return context.getString(R.string.preferences__default);
|
||||
}
|
||||
|
||||
private void launchSoundSelector(@Nullable Uri current) {
|
||||
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
|
||||
|
||||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
|
||||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
|
||||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
|
||||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Settings.System.DEFAULT_NOTIFICATION_URI);
|
||||
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, current);
|
||||
|
||||
startActivityForResult(intent, RINGTONE_PICKER_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
package org.thoughtcrime.securesms.groups.ui.notifications;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.WorkerThread;
|
||||
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.groups.GroupId;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
||||
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
|
||||
|
||||
class CustomNotificationsRepository {
|
||||
|
||||
private final Context context;
|
||||
private final GroupId groupId;
|
||||
|
||||
CustomNotificationsRepository(@NonNull Context context, @NonNull GroupId groupId) {
|
||||
this.context = context;
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
void onLoad(@NonNull Runnable onLoaded) {
|
||||
SignalExecutors.SERIAL.execute(() -> {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
void setHasCustomNotifications(final boolean hasCustomNotifications) {
|
||||
SignalExecutors.SERIAL.execute(() -> {
|
||||
if (hasCustomNotifications) {
|
||||
createCustomNotificationChannel();
|
||||
} else {
|
||||
deleteCustomNotificationChannel();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void setMessageVibrate(final RecipientDatabase.VibrateState vibrateState) {
|
||||
SignalExecutors.SERIAL.execute(() -> {
|
||||
Recipient recipient = getRecipient();
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setMessageVibrate(recipient.getId(), vibrateState);
|
||||
NotificationChannels.updateMessageVibrate(context, recipient, vibrateState);
|
||||
});
|
||||
}
|
||||
|
||||
void setMessageSound(@Nullable Uri sound) {
|
||||
SignalExecutors.SERIAL.execute(() -> {
|
||||
Recipient recipient = getRecipient();
|
||||
Uri defaultValue = TextSecurePreferences.getNotificationRingtone(context);
|
||||
Uri newValue;
|
||||
|
||||
if (defaultValue.equals(sound)) newValue = null;
|
||||
else if (sound == null) newValue = Uri.EMPTY;
|
||||
else newValue = sound;
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setMessageRingtone(recipient.getId(), newValue);
|
||||
NotificationChannels.updateMessageRingtone(context, recipient, newValue);
|
||||
});
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private void createCustomNotificationChannel() {
|
||||
Recipient recipient = getRecipient();
|
||||
String channelId = NotificationChannels.createChannelFor(context, recipient);
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient.getId(), channelId);
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private void deleteCustomNotificationChannel() {
|
||||
Recipient recipient = getRecipient();
|
||||
|
||||
DatabaseFactory.getRecipientDatabase(context).setNotificationChannel(recipient.getId(), null);
|
||||
NotificationChannels.deleteChannelFor(context, recipient);
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
private @NonNull Recipient getRecipient() {
|
||||
return Recipient.externalGroup(context, groupId).resolve();
|
||||
}
|
||||
}
|
||||
@@ -1,82 +0,0 @@
|
||||
package org.thoughtcrime.securesms.groups.ui.notifications;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
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 {
|
||||
|
||||
private final LiveGroup liveGroup;
|
||||
private final LiveData<Boolean> hasCustomNotifications;
|
||||
private final LiveData<RecipientDatabase.VibrateState> isVibrateEnabled;
|
||||
private final LiveData<Uri> notificationSound;
|
||||
private final CustomNotificationsRepository repository;
|
||||
private final MutableLiveData<Boolean> isInitialLoadComplete = new MutableLiveData<>();
|
||||
|
||||
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 || !NotificationChannels.supported());
|
||||
this.isVibrateEnabled = Transformations.map(liveGroup.getGroupRecipient(), Recipient::getMessageVibrate);
|
||||
this.notificationSound = Transformations.map(liveGroup.getGroupRecipient(), Recipient::getMessageRingtone);
|
||||
|
||||
repository.onLoad(() -> isInitialLoadComplete.postValue(true));
|
||||
}
|
||||
|
||||
public LiveData<Boolean> isInitialLoadComplete() {
|
||||
return isInitialLoadComplete;
|
||||
}
|
||||
|
||||
public LiveData<Boolean> hasCustomNotifications() {
|
||||
return hasCustomNotifications;
|
||||
}
|
||||
|
||||
public LiveData<RecipientDatabase.VibrateState> getVibrateState() {
|
||||
return isVibrateEnabled;
|
||||
}
|
||||
|
||||
public LiveData<Uri> getNotificationSound() {
|
||||
return notificationSound;
|
||||
}
|
||||
|
||||
public void setHasCustomNotifications(boolean hasCustomNotifications) {
|
||||
repository.setHasCustomNotifications(hasCustomNotifications);
|
||||
}
|
||||
|
||||
public void setMessageVibrate(@NonNull RecipientDatabase.VibrateState vibrateState) {
|
||||
repository.setMessageVibrate(vibrateState);
|
||||
}
|
||||
|
||||
public void setMessageSound(@Nullable Uri sound) {
|
||||
repository.setMessageSound(sound);
|
||||
}
|
||||
|
||||
public static final class Factory implements ViewModelProvider.Factory {
|
||||
|
||||
private final GroupId groupId;
|
||||
private final CustomNotificationsRepository repository;
|
||||
|
||||
public Factory(@NonNull GroupId groupId, @NonNull CustomNotificationsRepository repository) {
|
||||
this.groupId = groupId;
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
|
||||
//noinspection ConstantConditions
|
||||
return modelClass.cast(new CustomNotificationsViewModel(groupId, repository));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user