2014-01-19 02:17:08 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2014-02-13 01:17:33 +00:00
|
|
|
import android.content.Context;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.os.Bundle;
|
2014-02-12 20:05:18 +00:00
|
|
|
import android.text.Editable;
|
|
|
|
import android.text.TextWatcher;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2014-02-12 20:05:18 +00:00
|
|
|
import android.widget.EditText;
|
2014-02-07 02:06:23 +00:00
|
|
|
import android.widget.ImageView;
|
2014-01-19 02:17:08 +00:00
|
|
|
import android.widget.ListView;
|
|
|
|
|
|
|
|
import com.actionbarsherlock.view.Menu;
|
|
|
|
import com.actionbarsherlock.view.MenuInflater;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
import org.thoughtcrime.securesms.components.PushRecipientsPanel;
|
2014-02-03 19:52:27 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
2014-01-19 02:17:08 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2014-02-03 19:52:27 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
|
2014-01-19 02:17:08 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.ActionBarUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
|
|
|
import org.thoughtcrime.securesms.util.SelectedRecipientsAdapter;
|
2014-02-13 01:17:33 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.textsecure.directory.Directory;
|
|
|
|
import org.whispersystems.textsecure.directory.NotInDirectoryException;
|
|
|
|
import org.whispersystems.textsecure.util.InvalidNumberException;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2014-02-13 01:17:33 +00:00
|
|
|
import java.util.Collection;
|
2014-01-19 02:17:08 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2014-02-03 19:52:27 +00:00
|
|
|
import static org.thoughtcrime.securesms.contacts.ContactAccessor.ContactData;
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
|
|
|
|
public class GroupCreateActivity extends PassphraseRequiredSherlockFragmentActivity {
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
private final static String TAG = "GroupCreateActivity";
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private static final int PICK_CONTACT = 1;
|
2014-02-07 02:06:23 +00:00
|
|
|
private static final int PICK_AVATAR = 2;
|
|
|
|
|
2014-02-12 20:05:18 +00:00
|
|
|
private EditText groupName;
|
2014-02-07 02:06:23 +00:00
|
|
|
private ListView lv;
|
|
|
|
private PushRecipientsPanel recipientsPanel;
|
|
|
|
private ImageView avatar;
|
2014-01-19 02:17:08 +00:00
|
|
|
|
|
|
|
private Set<Recipient> selectedContacts;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle state) {
|
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
dynamicLanguage.onCreate(this);
|
|
|
|
super.onCreate(state);
|
|
|
|
|
|
|
|
setContentView(R.layout.group_create_activity);
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
2014-02-13 01:17:33 +00:00
|
|
|
ActionBarUtil.initializeDefaultActionBar(this, getSupportActionBar(), R.string.GroupCreateActivity_actionbar_title);
|
2014-01-19 02:17:08 +00:00
|
|
|
|
|
|
|
selectedContacts = new HashSet<Recipient>();
|
|
|
|
initializeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
}
|
|
|
|
|
2014-02-13 01:17:33 +00:00
|
|
|
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<Recipient> 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
private void initializeResources() {
|
2014-02-12 20:05:18 +00:00
|
|
|
groupName = (EditText) findViewById(R.id.group_name);
|
|
|
|
groupName.addTextChangedListener(new TextWatcher() {
|
|
|
|
@Override
|
|
|
|
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
|
|
|
|
@Override
|
|
|
|
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { }
|
|
|
|
@Override
|
|
|
|
public void afterTextChanged(Editable editable) {
|
|
|
|
if (editable.length() > 0)
|
|
|
|
getSupportActionBar().setTitle(editable);
|
|
|
|
else
|
2014-02-13 01:17:33 +00:00
|
|
|
getSupportActionBar().setTitle(R.string.GroupCreateActivity_actionbar_title);
|
2014-02-12 20:05:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
lv = (ListView) findViewById(R.id.selected_contacts_list);
|
2014-02-13 01:17:33 +00:00
|
|
|
SelectedRecipientsAdapter adapter = new SelectedRecipientsAdapter(this, android.R.id.text1, new ArrayList<Recipient>());
|
|
|
|
adapter.setOnRecipientDeletedListener(new SelectedRecipientsAdapter.OnRecipientDeletedListener() {
|
|
|
|
@Override
|
|
|
|
public void onRecipientDeleted(Recipient recipient) {
|
|
|
|
removeSelectedContact(recipient);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
lv.setAdapter(adapter);
|
2014-02-07 02:06:23 +00:00
|
|
|
|
|
|
|
recipientsPanel = (PushRecipientsPanel) findViewById(R.id.recipients);
|
|
|
|
recipientsPanel.setPanelChangeListener(new PushRecipientsPanel.RecipientsPanelChangedListener() {
|
|
|
|
@Override
|
|
|
|
public void onRecipientsPanelUpdate(Recipients recipients) {
|
|
|
|
Log.i(TAG, "onRecipientsPanelUpdate received.");
|
|
|
|
if (recipients != null) {
|
2014-02-13 01:17:33 +00:00
|
|
|
addAllSelectedContacts(recipients.getRecipientsList());
|
2014-02-07 02:06:23 +00:00
|
|
|
syncAdapterWithSelectedContacts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
(findViewById(R.id.contacts_button)).setOnClickListener(new AddRecipientButtonListener());
|
|
|
|
|
|
|
|
avatar = (ImageView) findViewById(R.id.avatar);
|
|
|
|
avatar.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View view) {
|
|
|
|
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
|
|
|
|
photoPickerIntent.setType("image/*");
|
|
|
|
photoPickerIntent.putExtra("crop", "true");
|
|
|
|
photoPickerIntent.putExtra("aspectX", 1);
|
|
|
|
photoPickerIntent.putExtra("aspectY", 1);
|
2014-02-13 01:17:33 +00:00
|
|
|
photoPickerIntent.putExtra("outputX", 210);
|
|
|
|
photoPickerIntent.putExtra("outputY", 210);
|
2014-02-07 02:06:23 +00:00
|
|
|
photoPickerIntent.putExtra("return-data", "true");
|
|
|
|
startActivityForResult(photoPickerIntent, PICK_AVATAR);
|
|
|
|
}
|
|
|
|
});
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = this.getSupportMenuInflater();
|
|
|
|
menu.clear();
|
|
|
|
|
|
|
|
inflater.inflate(R.menu.group_create, menu);
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home:
|
|
|
|
case R.id.menu_create_group:
|
|
|
|
finish(); // TODO not this
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-07 02:06:23 +00:00
|
|
|
private void syncAdapterWithSelectedContacts() {
|
|
|
|
SelectedRecipientsAdapter adapter = (SelectedRecipientsAdapter)lv.getAdapter();
|
|
|
|
adapter.clear();
|
|
|
|
for (Recipient contact : selectedContacts) {
|
|
|
|
adapter.add(contact);
|
|
|
|
}
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2014-01-19 02:17:08 +00:00
|
|
|
@Override
|
|
|
|
public void onActivityResult(int reqCode, int resultCode, Intent data) {
|
|
|
|
Log.w("ComposeMessageActivity", "onActivityResult called: " + resultCode + " , " + data);
|
|
|
|
super.onActivityResult(reqCode, resultCode, data);
|
|
|
|
|
|
|
|
if (data == null || resultCode != Activity.RESULT_OK)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (reqCode) {
|
|
|
|
case PICK_CONTACT:
|
2014-02-03 19:52:27 +00:00
|
|
|
List<ContactData> selected = data.getParcelableArrayListExtra("contacts");
|
|
|
|
for (ContactData contact : selected) {
|
|
|
|
for (ContactAccessor.NumberData numberData : contact.numbers) {
|
|
|
|
try {
|
2014-02-07 02:06:23 +00:00
|
|
|
Recipient recipient = RecipientFactory.getRecipientsFromString(this, numberData.number, false)
|
2014-02-03 19:52:27 +00:00
|
|
|
.getPrimaryRecipient();
|
|
|
|
|
|
|
|
if (!selectedContacts.contains(recipient)) {
|
2014-02-13 01:17:33 +00:00
|
|
|
addSelectedContact(recipient);
|
2014-02-03 19:52:27 +00:00
|
|
|
}
|
|
|
|
} catch (RecipientFormattingException e) {
|
|
|
|
Log.w("GroupCreateActivity", e);
|
|
|
|
}
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-07 02:06:23 +00:00
|
|
|
syncAdapterWithSelectedContacts();
|
2014-01-19 02:17:08 +00:00
|
|
|
break;
|
2014-02-07 02:06:23 +00:00
|
|
|
case PICK_AVATAR:
|
|
|
|
if(resultCode == RESULT_OK) {
|
|
|
|
Bitmap avatarBmp = data.getParcelableExtra("data");
|
|
|
|
avatar.setImageBitmap(avatarBmp);
|
|
|
|
//Uri selectedImage = data.getData();
|
|
|
|
//avatar.setImageURI(selectedImage);
|
2014-01-19 02:17:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class AddRecipientButtonListener implements View.OnClickListener {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent intent = new Intent(GroupCreateActivity.this, PushContactSelectionActivity.class);
|
|
|
|
startActivityForResult(intent, PICK_CONTACT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|