From 01a438de09e71c9ab1cf8c8c388e6c0ebbcbbb71 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Wed, 12 Feb 2014 17:17:33 -0800 Subject: [PATCH] basic messaging when non-push users are added --- res/layout/group_create_activity.xml | 67 +++++++++---- res/values/strings.xml | 1 + .../securesms/GroupCreateActivity.java | 97 ++++++++++++++----- .../securesms/util/ActionBarUtil.java | 6 ++ .../util/SelectedRecipientsAdapter.java | 11 +++ 5 files changed, 142 insertions(+), 40 deletions(-) diff --git a/res/layout/group_create_activity.xml b/res/layout/group_create_activity.xml index 235660839..c23e087c9 100644 --- a/res/layout/group_create_activity.xml +++ b/res/layout/group_create_activity.xml @@ -3,30 +3,61 @@ + android:layout_height="match_parent"> - + android:layout_marginBottom="20dp"> - - - + + + + + + + + + + + + New Group + New MMS Group Import System SMS Database? diff --git a/src/org/thoughtcrime/securesms/GroupCreateActivity.java b/src/org/thoughtcrime/securesms/GroupCreateActivity.java index 7a8f883c6..48185aeed 100644 --- a/src/org/thoughtcrime/securesms/GroupCreateActivity.java +++ b/src/org/thoughtcrime/securesms/GroupCreateActivity.java @@ -1,6 +1,7 @@ package org.thoughtcrime.securesms; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; @@ -26,8 +27,13 @@ import org.thoughtcrime.securesms.util.ActionBarUtil; import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicTheme; import org.thoughtcrime.securesms.util.SelectedRecipientsAdapter; +import org.thoughtcrime.securesms.util.Util; +import org.whispersystems.textsecure.directory.Directory; +import org.whispersystems.textsecure.directory.NotInDirectoryException; +import org.whispersystems.textsecure.util.InvalidNumberException; import java.util.ArrayList; +import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -42,8 +48,6 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv private final DynamicTheme dynamicTheme = new DynamicTheme(); private final DynamicLanguage dynamicLanguage = new DynamicLanguage(); - private String defaultTitle; - private static final int PICK_CONTACT = 1; private static final int PICK_AVATAR = 2; @@ -58,12 +62,11 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv public void onCreate(Bundle state) { dynamicTheme.onCreate(this); dynamicLanguage.onCreate(this); - defaultTitle = getString(R.string.GroupCreateActivity_actionbar_title); super.onCreate(state); setContentView(R.layout.group_create_activity); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - ActionBarUtil.initializeDefaultActionBar(this, getSupportActionBar(), defaultTitle); + ActionBarUtil.initializeDefaultActionBar(this, getSupportActionBar(), R.string.GroupCreateActivity_actionbar_title); selectedContacts = new HashSet(); initializeResources(); @@ -75,6 +78,61 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv dynamicTheme.onResume(this); } + private void disableWhisperGroupUI() { + View pushDisabled = findViewById(R.id.push_disabled); + pushDisabled.setVisibility(View.VISIBLE); + avatar.setEnabled(false); + groupName.setEnabled(false); + getSupportActionBar().setTitle(R.string.GroupCreateActivity_actionbar_mms_title); + } + + private void enableWhisperGroupUI() { + findViewById(R.id.push_disabled).setVisibility(View.GONE); + avatar.setEnabled(true); + groupName.setEnabled(true); + final CharSequence groupNameText = groupName.getText(); + if (groupNameText.length() > 0) + getSupportActionBar().setTitle(groupNameText); + else + getSupportActionBar().setTitle(R.string.GroupCreateActivity_actionbar_title); + } + + private static boolean isActiveInDirectory(Context context, Recipient recipient) { + try { + if (!Directory.getInstance(context).isActiveNumber(Util.canonicalizeNumber(context, recipient.getNumber()))) { + return false; + } + } catch (NotInDirectoryException e) { + return false; + } catch (InvalidNumberException e ) { + return false; + } + return true; + } + + private void addSelectedContact(Recipient contact) { + selectedContacts.add(contact); + if (!isActiveInDirectory(this, contact)) disableWhisperGroupUI(); + } + + private void addAllSelectedContacts(Collection contacts) { + for (Recipient contact : contacts) { + addSelectedContact(contact); + } + } + + private void removeSelectedContact(Recipient contact) { + Log.i(TAG, "remoevSelectedContact: " + contact.getName() + "/" + contact.getNumber()); + selectedContacts.remove(contact); + if (!isActiveInDirectory(this, contact)) { + for (Recipient recipient : selectedContacts) { + if (!isActiveInDirectory(this, recipient)) + return; + } + enableWhisperGroupUI(); + } + } + private void initializeResources() { groupName = (EditText) findViewById(R.id.group_name); groupName.addTextChangedListener(new TextWatcher() { @@ -87,12 +145,19 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv if (editable.length() > 0) getSupportActionBar().setTitle(editable); else - getSupportActionBar().setTitle(defaultTitle); + getSupportActionBar().setTitle(R.string.GroupCreateActivity_actionbar_title); } }); lv = (ListView) findViewById(R.id.selected_contacts_list); - lv.setAdapter(new SelectedRecipientsAdapter(this, android.R.id.text1, new ArrayList())); + SelectedRecipientsAdapter adapter = new SelectedRecipientsAdapter(this, android.R.id.text1, new ArrayList()); + adapter.setOnRecipientDeletedListener(new SelectedRecipientsAdapter.OnRecipientDeletedListener() { + @Override + public void onRecipientDeleted(Recipient recipient) { + removeSelectedContact(recipient); + } + }); + lv.setAdapter(adapter); recipientsPanel = (PushRecipientsPanel) findViewById(R.id.recipients); recipientsPanel.setPanelChangeListener(new PushRecipientsPanel.RecipientsPanelChangedListener() { @@ -100,7 +165,7 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv public void onRecipientsPanelUpdate(Recipients recipients) { Log.i(TAG, "onRecipientsPanelUpdate received."); if (recipients != null) { - selectedContacts.addAll(recipients.getRecipientsList()); + addAllSelectedContacts(recipients.getRecipientsList()); syncAdapterWithSelectedContacts(); } } @@ -116,8 +181,8 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv photoPickerIntent.putExtra("crop", "true"); photoPickerIntent.putExtra("aspectX", 1); photoPickerIntent.putExtra("aspectY", 1); - photoPickerIntent.putExtra("outputX", 256); - photoPickerIntent.putExtra("outputY", 256); + photoPickerIntent.putExtra("outputX", 210); + photoPickerIntent.putExtra("outputY", 210); photoPickerIntent.putExtra("return-data", "true"); startActivityForResult(photoPickerIntent, PICK_AVATAR); } @@ -134,14 +199,6 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv return true; } - private List selectedContactsAsIdArray() { - final List ids = new ArrayList(); - for (Recipient recipient : selectedContacts) { - ids.add(String.valueOf(recipient.getRecipientId())); - } - return ids; - } - @Override public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); @@ -160,7 +217,6 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv adapter.clear(); for (Recipient contact : selectedContacts) { adapter.add(contact); - Log.i("GroupCreateActivity", "Adding " + contact.getName() + "/" + contact.getNumber()); } adapter.notifyDataSetChanged(); } @@ -176,9 +232,6 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv switch (reqCode) { case PICK_CONTACT: List selected = data.getParcelableArrayListExtra("contacts"); - for (ContactData cdata : selected) { - Log.i("PushContactSelect", "selected report: " + cdata.name); - } for (ContactData contact : selected) { for (ContactAccessor.NumberData numberData : contact.numbers) { try { @@ -186,7 +239,7 @@ public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActiv .getPrimaryRecipient(); if (!selectedContacts.contains(recipient)) { - selectedContacts.add(recipient); + addSelectedContact(recipient); } } catch (RecipientFormattingException e) { Log.w("GroupCreateActivity", e); diff --git a/src/org/thoughtcrime/securesms/util/ActionBarUtil.java b/src/org/thoughtcrime/securesms/util/ActionBarUtil.java index bdb08c398..613a98989 100644 --- a/src/org/thoughtcrime/securesms/util/ActionBarUtil.java +++ b/src/org/thoughtcrime/securesms/util/ActionBarUtil.java @@ -9,6 +9,12 @@ import com.actionbarsherlock.app.ActionBar; import org.thoughtcrime.securesms.R; public class ActionBarUtil { + + public static void initializeDefaultActionBar(final Context c, final ActionBar actionBar, final int title_resid) { + actionBar.setTitle(title_resid); + initializeDefaultActionBar(c, actionBar); + } + public static void initializeDefaultActionBar(final Context c, final ActionBar actionBar, final String title) { actionBar.setTitle(title); initializeDefaultActionBar(c, actionBar); diff --git a/src/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java b/src/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java index 5b6458980..52f957221 100644 --- a/src/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java +++ b/src/org/thoughtcrime/securesms/util/SelectedRecipientsAdapter.java @@ -17,6 +17,7 @@ import java.util.List; public class SelectedRecipientsAdapter extends ArrayAdapter { private ArrayList recipients; + private OnRecipientDeletedListener onRecipientDeletedListener; public SelectedRecipientsAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); @@ -59,6 +60,9 @@ public class SelectedRecipientsAdapter extends ArrayAdapter { delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { + if (onRecipientDeletedListener != null) { + onRecipientDeletedListener.onRecipientDeleted(recipients.get(position)); + } recipients.remove(position); SelectedRecipientsAdapter.this.notifyDataSetChanged(); } @@ -67,6 +71,13 @@ public class SelectedRecipientsAdapter extends ArrayAdapter { } return v; + } + public void setOnRecipientDeletedListener(OnRecipientDeletedListener listener) { + onRecipientDeletedListener = listener; + } + + public interface OnRecipientDeletedListener { + public void onRecipientDeleted(Recipient recipient); } } \ No newline at end of file