2020-06-05 15:33:41 +00:00
|
|
|
package org.thoughtcrime.securesms.groups.ui.addmembers;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-06-05 15:33:41 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.View;
|
2020-07-30 03:04:35 +00:00
|
|
|
import android.widget.Toast;
|
2020-06-05 15:33:41 +00:00
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.lifecycle.ViewModelProviders;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ContactSelectionActivity;
|
2021-06-24 16:52:54 +00:00
|
|
|
import org.thoughtcrime.securesms.ContactSelectionListFragment;
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.PushContactSelectionActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2021-06-24 16:52:54 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactsCursorLoader;
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.groups.GroupId;
|
2021-06-24 16:52:54 +00:00
|
|
|
import org.thoughtcrime.securesms.groups.SelectionLimits;
|
2020-06-05 15:33:41 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
public class AddMembersActivity extends PushContactSelectionActivity {
|
|
|
|
|
|
|
|
public static final String GROUP_ID = "group_id";
|
|
|
|
|
|
|
|
private View done;
|
|
|
|
private AddMembersViewModel viewModel;
|
|
|
|
|
2021-06-24 16:52:54 +00:00
|
|
|
public static @NonNull Intent createIntent(@NonNull Context context,
|
|
|
|
@NonNull GroupId groupId,
|
|
|
|
int displayModeFlags,
|
|
|
|
int selectionWarning,
|
|
|
|
int selectionLimit,
|
|
|
|
@NonNull List<RecipientId> membersWithoutSelf) {
|
|
|
|
Intent intent = new Intent(context, AddMembersActivity.class);
|
|
|
|
|
|
|
|
intent.putExtra(AddMembersActivity.GROUP_ID, groupId.toString());
|
|
|
|
intent.putExtra(ContactSelectionListFragment.DISPLAY_MODE, displayModeFlags);
|
|
|
|
intent.putExtra(ContactSelectionListFragment.SELECTION_LIMITS, new SelectionLimits(selectionWarning, selectionLimit));
|
|
|
|
intent.putParcelableArrayListExtra(ContactSelectionListFragment.CURRENT_SELECTION, new ArrayList<>(membersWithoutSelf));
|
|
|
|
|
|
|
|
return intent;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle icicle, boolean ready) {
|
|
|
|
getIntent().putExtra(ContactSelectionActivity.EXTRA_LAYOUT_RES_ID, R.layout.add_members_activity);
|
|
|
|
super.onCreate(icicle, ready);
|
|
|
|
|
|
|
|
AddMembersViewModel.Factory factory = new AddMembersViewModel.Factory(getGroupId());
|
|
|
|
|
|
|
|
done = findViewById(R.id.done);
|
|
|
|
viewModel = ViewModelProviders.of(this, factory)
|
|
|
|
.get(AddMembersViewModel.class);
|
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
done.setOnClickListener(v ->
|
|
|
|
viewModel.getDialogStateForSelectedContacts(contactsFragment.getSelectedContacts(), this::displayAlertMessage)
|
|
|
|
);
|
2020-06-05 15:33:41 +00:00
|
|
|
|
|
|
|
disableDone();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void initializeToolbar() {
|
|
|
|
getToolbar().setNavigationIcon(R.drawable.ic_arrow_left_24);
|
|
|
|
getToolbar().setNavigationOnClickListener(v -> {
|
|
|
|
setResult(RESULT_CANCELED);
|
|
|
|
finish();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-08-31 14:33:50 +00:00
|
|
|
public boolean onBeforeContactSelected(Optional<RecipientId> recipientId, String number) {
|
2020-07-30 03:04:35 +00:00
|
|
|
if (getGroupId().isV1() && recipientId.isPresent() && !Recipient.resolved(recipientId.get()).hasE164()) {
|
|
|
|
Toast.makeText(this, R.string.AddMembersActivity__this_person_cant_be_added_to_legacy_groups, Toast.LENGTH_SHORT).show();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-05 15:33:41 +00:00
|
|
|
if (contactsFragment.hasQueryFilter()) {
|
|
|
|
getToolbar().clear();
|
|
|
|
}
|
|
|
|
|
2020-07-30 03:04:35 +00:00
|
|
|
enableDone();
|
|
|
|
|
|
|
|
return true;
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onContactDeselected(Optional<RecipientId> recipientId, String number) {
|
|
|
|
if (contactsFragment.hasQueryFilter()) {
|
|
|
|
getToolbar().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (contactsFragment.getSelectedContactsCount() < 1) {
|
|
|
|
disableDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableDone() {
|
|
|
|
done.setEnabled(true);
|
|
|
|
done.animate().alpha(1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disableDone() {
|
|
|
|
done.setEnabled(false);
|
|
|
|
done.animate().alpha(0.5f);
|
|
|
|
}
|
|
|
|
|
|
|
|
private GroupId getGroupId() {
|
|
|
|
return GroupId.parseOrThrow(getIntent().getStringExtra(GROUP_ID));
|
|
|
|
}
|
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
private void displayAlertMessage(@NonNull AddMembersViewModel.AddMemberDialogMessageState state) {
|
2020-06-05 15:33:41 +00:00
|
|
|
Recipient recipient = Util.firstNonNull(state.getRecipient(), Recipient.UNKNOWN);
|
|
|
|
|
2021-01-10 00:12:10 +00:00
|
|
|
String message = getResources().getQuantityString(R.plurals.AddMembersActivity__add_d_members_to_s, state.getSelectionCount(),
|
|
|
|
recipient.getDisplayName(this), state.getGroupTitle(), state.getSelectionCount());
|
|
|
|
|
|
|
|
new AlertDialog.Builder(this)
|
|
|
|
.setMessage(message)
|
|
|
|
.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel())
|
|
|
|
.setPositiveButton(R.string.AddMembersActivity__add, (dialog, which) -> {
|
|
|
|
dialog.dismiss();
|
|
|
|
onFinishedSelection();
|
|
|
|
})
|
|
|
|
.setCancelable(true)
|
|
|
|
.show();
|
2020-06-05 15:33:41 +00:00
|
|
|
}
|
|
|
|
}
|