refactored options menu handling a bit

This commit is contained in:
iNPUTmice 2014-09-23 15:06:49 +02:00
parent a399c0c3df
commit 248a1693ea
1 changed files with 151 additions and 142 deletions

View File

@ -154,10 +154,12 @@ public class ConversationActivity extends XmppActivity implements
if (ab != null) { if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true); ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true); ab.setHomeButtonEnabled(true);
if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE || activity.useSubjectToIdentifyConference()) { if (getSelectedConversation().getMode() == Conversation.MODE_SINGLE
|| activity.useSubjectToIdentifyConference()) {
ab.setTitle(getSelectedConversation().getName()); ab.setTitle(getSelectedConversation().getName());
} else { } else {
ab.setTitle(getSelectedConversation().getContactJid().split("/")[0]); ab.setTitle(getSelectedConversation()
.getContactJid().split("/")[0]);
} }
} }
invalidateOptionsMenu(); invalidateOptionsMenu();
@ -312,40 +314,16 @@ public class ConversationActivity extends XmppActivity implements
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { if (item.getItemId() == android.R.id.home) {
case android.R.id.home:
spl.openPane(); spl.openPane();
return true; return true;
case R.id.action_attach_file: } else if (item.getItemId() == R.id.action_add) {
View menuAttachFile = findViewById(R.id.action_attach_file);
if (menuAttachFile == null) {
break;
}
PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
attachFilePopup.inflate(R.menu.attachment_choices);
attachFilePopup
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.attach_choose_picture:
attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case R.id.attach_take_picture:
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
}
return false;
}
});
attachFilePopup.show();
break;
case R.id.action_add:
startActivity(new Intent(this, StartConversationActivity.class)); startActivity(new Intent(this, StartConversationActivity.class));
return true;
} else if (getSelectedConversation() != null) {
switch (item.getItemId()) {
case R.id.action_attach_file:
attachFileDialog();
break; break;
case R.id.action_archive: case R.id.action_archive:
this.endConversation(getSelectedConversation()); this.endConversation(getSelectedConversation());
@ -359,7 +337,8 @@ public class ConversationActivity extends XmppActivity implements
} }
break; break;
case R.id.action_muc_details: case R.id.action_muc_details:
Intent intent = new Intent(this, ConferenceDetailsActivity.class); Intent intent = new Intent(this,
ConferenceDetailsActivity.class);
intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC); intent.setAction(ConferenceDetailsActivity.ACTION_VIEW_MUC);
intent.putExtra("uuid", getSelectedConversation().getUuid()); intent.putExtra("uuid", getSelectedConversation().getUuid());
startActivity(intent); startActivity(intent);
@ -368,85 +347,7 @@ public class ConversationActivity extends XmppActivity implements
inviteToConversation(getSelectedConversation()); inviteToConversation(getSelectedConversation());
break; break;
case R.id.action_security: case R.id.action_security:
final Conversation conversation = getSelectedConversation(); selectEncryptionDialog(getSelectedConversation());
View menuItemView = findViewById(R.id.action_security);
if (menuItemView == null) {
break;
}
PopupMenu popup = new PopupMenu(this, menuItemView);
final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
.findFragmentByTag("conversation");
if (fragment != null) {
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.encryption_choice_none:
conversation
.setNextEncryption(Message.ENCRYPTION_NONE);
item.setChecked(true);
break;
case R.id.encryption_choice_otr:
conversation
.setNextEncryption(Message.ENCRYPTION_OTR);
item.setChecked(true);
break;
case R.id.encryption_choice_pgp:
if (hasPgp()) {
if (conversation.getAccount().getKeys()
.has("pgp_signature")) {
conversation
.setNextEncryption(Message.ENCRYPTION_PGP);
item.setChecked(true);
} else {
announcePgp(conversation.getAccount(),
conversation);
}
} else {
showInstallPgpDialog();
}
break;
default:
conversation
.setNextEncryption(Message.ENCRYPTION_NONE);
break;
}
fragment.updateChatMsgHint();
return true;
}
});
popup.inflate(R.menu.encryption_choices);
MenuItem otr = popup.getMenu().findItem(
R.id.encryption_choice_otr);
MenuItem none = popup.getMenu().findItem(
R.id.encryption_choice_none);
if (conversation.getMode() == Conversation.MODE_MULTI) {
otr.setEnabled(false);
} else {
if (forceEncryption()) {
none.setVisible(false);
}
}
switch (conversation.getNextEncryption(forceEncryption())) {
case Message.ENCRYPTION_NONE:
none.setChecked(true);
break;
case Message.ENCRYPTION_OTR:
otr.setChecked(true);
break;
case Message.ENCRYPTION_PGP:
popup.getMenu().findItem(R.id.encryption_choice_pgp)
.setChecked(true);
break;
default:
popup.getMenu().findItem(R.id.encryption_choice_none)
.setChecked(true);
break;
}
popup.show();
}
break; break;
case R.id.action_clear_history: case R.id.action_clear_history:
clearHistoryDialog(getSelectedConversation()); clearHistoryDialog(getSelectedConversation());
@ -458,6 +359,9 @@ public class ConversationActivity extends XmppActivity implements
break; break;
} }
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} else {
return super.onOptionsItemSelected(item);
}
} }
public void endConversation(Conversation conversation) { public void endConversation(Conversation conversation) {
@ -496,6 +400,111 @@ public class ConversationActivity extends XmppActivity implements
builder.create().show(); builder.create().show();
} }
protected void attachFileDialog() {
View menuAttachFile = findViewById(R.id.action_attach_file);
if (menuAttachFile == null) {
return;
}
PopupMenu attachFilePopup = new PopupMenu(this, menuAttachFile);
attachFilePopup.inflate(R.menu.attachment_choices);
attachFilePopup
.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.attach_choose_picture:
attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case R.id.attach_take_picture:
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
}
return false;
}
});
attachFilePopup.show();
}
protected void selectEncryptionDialog(final Conversation conversation) {
View menuItemView = findViewById(R.id.action_security);
if (menuItemView == null) {
return;
}
PopupMenu popup = new PopupMenu(this, menuItemView);
final ConversationFragment fragment = (ConversationFragment) getFragmentManager()
.findFragmentByTag("conversation");
if (fragment != null) {
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.encryption_choice_none:
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
item.setChecked(true);
break;
case R.id.encryption_choice_otr:
conversation.setNextEncryption(Message.ENCRYPTION_OTR);
item.setChecked(true);
break;
case R.id.encryption_choice_pgp:
if (hasPgp()) {
if (conversation.getAccount().getKeys()
.has("pgp_signature")) {
conversation
.setNextEncryption(Message.ENCRYPTION_PGP);
item.setChecked(true);
} else {
announcePgp(conversation.getAccount(),
conversation);
}
} else {
showInstallPgpDialog();
}
break;
default:
conversation.setNextEncryption(Message.ENCRYPTION_NONE);
break;
}
fragment.updateChatMsgHint();
return true;
}
});
popup.inflate(R.menu.encryption_choices);
MenuItem otr = popup.getMenu().findItem(R.id.encryption_choice_otr);
MenuItem none = popup.getMenu().findItem(
R.id.encryption_choice_none);
if (conversation.getMode() == Conversation.MODE_MULTI) {
otr.setEnabled(false);
} else {
if (forceEncryption()) {
none.setVisible(false);
}
}
switch (conversation.getNextEncryption(forceEncryption())) {
case Message.ENCRYPTION_NONE:
none.setChecked(true);
break;
case Message.ENCRYPTION_OTR:
otr.setChecked(true);
break;
case Message.ENCRYPTION_PGP:
popup.getMenu().findItem(R.id.encryption_choice_pgp)
.setChecked(true);
break;
default:
popup.getMenu().findItem(R.id.encryption_choice_none)
.setChecked(true);
break;
}
popup.show();
}
}
protected void muteConversationDialog(final Conversation conversation) { protected void muteConversationDialog(final Conversation conversation) {
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.disable_notifications_for_this_conversation); builder.setTitle(R.string.disable_notifications_for_this_conversation);