2014-01-19 02:17:08 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
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;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
ActionBarUtil.initializeDefaultActionBar(this, getSupportActionBar(), "New Group");
|
|
|
|
|
|
|
|
selectedContacts = new HashSet<Recipient>();
|
|
|
|
initializeResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initializeResources() {
|
|
|
|
lv = (ListView) findViewById(R.id.selected_contacts_list);
|
|
|
|
lv.setAdapter(new SelectedRecipientsAdapter(this, android.R.id.text1, new ArrayList<Recipient>()));
|
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) {
|
|
|
|
selectedContacts.addAll(recipients.getRecipientsList());
|
|
|
|
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);
|
|
|
|
photoPickerIntent.putExtra("outputX", 256);
|
|
|
|
photoPickerIntent.putExtra("outputY", 256);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> selectedContactsAsIdArray() {
|
|
|
|
final List<String> ids = new ArrayList<String>();
|
|
|
|
for (Recipient recipient : selectedContacts) {
|
2014-02-03 19:52:27 +00:00
|
|
|
ids.add(String.valueOf(recipient.getRecipientId()));
|
2014-01-19 02:17:08 +00:00
|
|
|
}
|
|
|
|
return ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
Log.i("GroupCreateActivity", "Adding " + contact.getName() + "/" + contact.getNumber());
|
|
|
|
}
|
|
|
|
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");
|
2014-02-07 02:06:23 +00:00
|
|
|
for (ContactData cdata : selected) {
|
|
|
|
Log.i("PushContactSelect", "selected report: " + cdata.name);
|
|
|
|
}
|
2014-02-03 19:52:27 +00:00
|
|
|
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)) {
|
|
|
|
selectedContacts.add(recipient);
|
|
|
|
}
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|