refactoring. move functionality from activity to fragment

This commit is contained in:
Daniel Gultsch 2018-02-20 08:21:07 +01:00
parent d55a3842d3
commit c17f902be2
3 changed files with 186 additions and 156 deletions

View File

@ -59,15 +59,15 @@ import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdat
import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.ui.adapter.ConversationAdapter; import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.ui.service.EmojiService; import eu.siacs.conversations.ui.service.EmojiService;
import eu.siacs.conversations.ui.util.SendButtonAction;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
public class ConversationActivity extends XmppActivity public class ConversationActivity extends XmppActivity
implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast { implements OnAccountUpdate, OnConversationUpdate, OnRosterUpdate, OnUpdateBlocklist, XmppConnectionService.OnShowErrorToast {
public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action"; public static final String RECENTLY_USED_QUICK_ACTION = "recently_used_quick_action";
@ -101,7 +101,7 @@ public class ConversationActivity extends XmppActivity
private String mOpenConversation = null; private String mOpenConversation = null;
private boolean mPanelOpen = true; private boolean mPanelOpen = true;
private AtomicBoolean mShouldPanelBeOpen = new AtomicBoolean(false); private AtomicBoolean mShouldPanelBeOpen = new AtomicBoolean(false);
private Pair<Integer,Integer> mScrollPosition = null; private Pair<Integer, Integer> mScrollPosition = null;
final private List<Uri> mPendingImageUris = new ArrayList<>(); final private List<Uri> mPendingImageUris = new ArrayList<>();
final private List<Uri> mPendingFileUris = new ArrayList<>(); final private List<Uri> mPendingFileUris = new ArrayList<>();
private Uri mPendingGeoUri = null; private Uri mPendingGeoUri = null;
@ -188,14 +188,14 @@ public class ConversationActivity extends XmppActivity
int pos = savedInstanceState.getInt(STATE_FIRST_VISIBLE, -1); int pos = savedInstanceState.getInt(STATE_FIRST_VISIBLE, -1);
int offset = savedInstanceState.getInt(STATE_OFFSET_FROM_TOP, 1); int offset = savedInstanceState.getInt(STATE_OFFSET_FROM_TOP, 1);
if (pos >= 0 && offset <= 0) { if (pos >= 0 && offset <= 0) {
Log.d(Config.LOGTAG,"retrieved scroll position from instanceState "+pos+":"+offset); Log.d(Config.LOGTAG, "retrieved scroll position from instanceState " + pos + ":" + offset);
mScrollPosition = new Pair<>(pos,offset); mScrollPosition = new Pair<>(pos, offset);
} else { } else {
mScrollPosition = null; mScrollPosition = null;
} }
String pending = savedInstanceState.getString(STATE_PENDING_URI, null); String pending = savedInstanceState.getString(STATE_PENDING_URI, null);
if (pending != null) { if (pending != null) {
Log.d(Config.LOGTAG,"ConversationsActivity.onCreate() - restoring pending image uri"); Log.d(Config.LOGTAG, "ConversationsActivity.onCreate() - restoring pending image uri");
mPendingImageUris.clear(); mPendingImageUris.clear();
mPendingImageUris.add(Uri.parse(pending)); mPendingImageUris.add(Uri.parse(pending));
} }
@ -222,7 +222,7 @@ public class ConversationActivity extends XmppActivity
@Override @Override
public void onItemClick(AdapterView<?> arg0, View clickedView, public void onItemClick(AdapterView<?> arg0, View clickedView,
int position, long arg3) { int position, long arg3) {
if (getSelectedConversation() != conversationList.get(position)) { if (getSelectedConversation() != conversationList.get(position)) {
ConversationActivity.this.mConversationFragment.stopScrolling(); ConversationActivity.this.mConversationFragment.stopScrolling();
setSelectedConversation(conversationList.get(position)); setSelectedConversation(conversationList.get(position));
@ -443,7 +443,7 @@ public class ConversationActivity extends XmppActivity
} }
menuAttachLocation.setVisible(showLocationAttachment); menuAttachLocation.setVisible(showLocationAttachment);
menuAttachSoundRecorder.setVisible(showSoundRecorderAttachment); menuAttachSoundRecorder.setVisible(showSoundRecorderAttachment);
configureEncryptionMenu(getSelectedConversation(),menu); configureEncryptionMenu(getSelectedConversation(), menu);
} }
} }
return super.onCreateOptionsMenu(menu); return super.onCreateOptionsMenu(menu);
@ -556,29 +556,12 @@ public class ConversationActivity extends XmppActivity
return; return;
} }
} }
final ConversationFragment.SendButtonAction action; try {
switch (attachmentChoice) { getPreferences().edit()
case ATTACHMENT_CHOICE_LOCATION: .putString(RECENTLY_USED_QUICK_ACTION, SendButtonAction.of(attachmentChoice).toString())
action = ConversationFragment.SendButtonAction.SEND_LOCATION; .apply();
break; } catch (IllegalArgumentException e) {
case ATTACHMENT_CHOICE_RECORD_VOICE: //just do not save
action = ConversationFragment.SendButtonAction.RECORD_VOICE;
break;
case ATTACHMENT_CHOICE_RECORD_VIDEO:
action = ConversationFragment.SendButtonAction.RECORD_VIDEO;
break;
case ATTACHMENT_CHOICE_TAKE_PHOTO:
action = ConversationFragment.SendButtonAction.TAKE_PHOTO;
break;
case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
action = ConversationFragment.SendButtonAction.CHOOSE_PICTURE;
break;
default:
action = null;
break;
}
if (action != null) {
getPreferences().edit().putString(RECENTLY_USED_QUICK_ACTION,action.toString()).apply();
} }
final Conversation conversation = getSelectedConversation(); final Conversation conversation = getSelectedConversation();
final int encryption = conversation.getNextEncryption(); final int encryption = conversation.getNextEncryption();
@ -624,7 +607,7 @@ public class ConversationActivity extends XmppActivity
@Override @Override
public void onClick(DialogInterface dialog, public void onClick(DialogInterface dialog,
int which) { int which) {
conversation.setNextEncryption(Message.ENCRYPTION_NONE); conversation.setNextEncryption(Message.ENCRYPTION_NONE);
xmppConnectionService.updateConversation(conversation); xmppConnectionService.updateConversation(conversation);
selectPresenceToAttachFile(attachmentChoice, Message.ENCRYPTION_NONE); selectPresenceToAttachFile(attachmentChoice, Message.ENCRYPTION_NONE);
@ -693,22 +676,12 @@ public class ConversationActivity extends XmppActivity
handleEncryptionSelection(item); handleEncryptionSelection(item);
break; break;
case R.id.attach_choose_picture: case R.id.attach_choose_picture:
attachFile(ATTACHMENT_CHOICE_CHOOSE_IMAGE);
break;
case R.id.attach_take_picture: case R.id.attach_take_picture:
attachFile(ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case R.id.attach_record_video: case R.id.attach_record_video:
attachFile(ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case R.id.attach_choose_file: case R.id.attach_choose_file:
attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
break;
case R.id.attach_record_voice: case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case R.id.attach_location: case R.id.attach_location:
attachFile(ATTACHMENT_CHOICE_LOCATION); handleAttachmentSelection(item);
break; break;
case R.id.action_archive: case R.id.action_archive:
this.endConversation(getSelectedConversation()); this.endConversation(getSelectedConversation());
@ -783,19 +756,41 @@ public class ConversationActivity extends XmppActivity
final CheckBox endConversationCheckBox = dialogView.findViewById(R.id.end_conversation_checkbox); final CheckBox endConversationCheckBox = dialogView.findViewById(R.id.end_conversation_checkbox);
builder.setView(dialogView); builder.setView(dialogView);
builder.setNegativeButton(getString(R.string.cancel), null); builder.setNegativeButton(getString(R.string.cancel), null);
builder.setPositiveButton(getString(R.string.delete_messages), builder.setPositiveButton(getString(R.string.delete_messages), (dialog, which) -> {
(dialog, which) -> { ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation);
ConversationActivity.this.xmppConnectionService.clearConversationHistory(conversation); if (endConversationCheckBox.isChecked()) {
if (endConversationCheckBox.isChecked()) { endConversation(conversation);
endConversation(conversation); } else {
} else { updateConversationList();
updateConversationList(); ConversationActivity.this.mConversationFragment.updateMessages();
ConversationActivity.this.mConversationFragment.updateMessages(); }
} });
});
builder.create().show(); builder.create().show();
} }
private void handleAttachmentSelection(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_video:
attachFile(ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case R.id.attach_choose_file:
attachFile(ATTACHMENT_CHOICE_CHOOSE_FILE);
break;
case R.id.attach_record_voice:
attachFile(ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case R.id.attach_location:
attachFile(ATTACHMENT_CHOICE_LOCATION);
break;
}
}
private void handleEncryptionSelection(MenuItem item) { private void handleEncryptionSelection(MenuItem item) {
Conversation conversation = getSelectedConversation(); Conversation conversation = getSelectedConversation();
if (conversation == null) { if (conversation == null) {
@ -813,7 +808,7 @@ public class ConversationActivity extends XmppActivity
conversation.setNextEncryption(Message.ENCRYPTION_PGP); conversation.setNextEncryption(Message.ENCRYPTION_PGP);
item.setChecked(true); item.setChecked(true);
} else { } else {
announcePgp(conversation.getAccount(), conversation,null, onOpenPGPKeyPublished); announcePgp(conversation.getAccount(), conversation, null, onOpenPGPKeyPublished);
} }
} else { } else {
showInstallPgpDialog(); showInstallPgpDialog();
@ -839,24 +834,19 @@ public class ConversationActivity extends XmppActivity
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.disable_notifications); builder.setTitle(R.string.disable_notifications);
final int[] durations = getResources().getIntArray(R.array.mute_options_durations); final int[] durations = getResources().getIntArray(R.array.mute_options_durations);
builder.setItems(R.array.mute_options_descriptions, builder.setItems(R.array.mute_options_descriptions, (dialog, which) -> {
new OnClickListener() { final long till;
if (durations[which] == -1) {
@Override till = Long.MAX_VALUE;
public void onClick(final DialogInterface dialog, final int which) { } else {
final long till; till = System.currentTimeMillis() + (durations[which] * 1000);
if (durations[which] == -1) { }
till = Long.MAX_VALUE; conversation.setMutedTill(till);
} else { ConversationActivity.this.xmppConnectionService.updateConversation(conversation);
till = System.currentTimeMillis() + (durations[which] * 1000); updateConversationList();
} ConversationActivity.this.mConversationFragment.updateMessages();
conversation.setMutedTill(till); invalidateOptionsMenu();
ConversationActivity.this.xmppConnectionService.updateConversation(conversation); });
updateConversationList();
ConversationActivity.this.mConversationFragment.updateMessages();
invalidateOptionsMenu();
}
});
builder.create().show(); builder.create().show();
} }
@ -1047,7 +1037,7 @@ public class ConversationActivity extends XmppActivity
Conversation conversation = getSelectedConversation(); Conversation conversation = getSelectedConversation();
if (conversation != null) { if (conversation != null) {
savedInstanceState.putString(STATE_OPEN_CONVERSATION, conversation.getUuid()); savedInstanceState.putString(STATE_OPEN_CONVERSATION, conversation.getUuid());
Pair<Integer,Integer> scrollPosition = mConversationFragment.getScrollPosition(); Pair<Integer, Integer> scrollPosition = mConversationFragment.getScrollPosition();
if (scrollPosition != null) { if (scrollPosition != null) {
savedInstanceState.putInt(STATE_FIRST_VISIBLE, scrollPosition.first); savedInstanceState.putInt(STATE_FIRST_VISIBLE, scrollPosition.first);
savedInstanceState.putInt(STATE_OFFSET_FROM_TOP, scrollPosition.second); savedInstanceState.putInt(STATE_OFFSET_FROM_TOP, scrollPosition.second);
@ -1057,7 +1047,7 @@ public class ConversationActivity extends XmppActivity
} }
savedInstanceState.putBoolean(STATE_PANEL_OPEN, isConversationsOverviewVisable()); savedInstanceState.putBoolean(STATE_PANEL_OPEN, isConversationsOverviewVisable());
if (this.mPendingImageUris.size() >= 1) { if (this.mPendingImageUris.size() >= 1) {
Log.d(Config.LOGTAG,"ConversationsActivity.onSaveInstanceState() - saving pending image uri"); Log.d(Config.LOGTAG, "ConversationsActivity.onSaveInstanceState() - saving pending image uri");
savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString()); savedInstanceState.putString(STATE_PENDING_URI, this.mPendingImageUris.get(0).toString());
} else { } else {
savedInstanceState.remove(STATE_PENDING_URI); savedInstanceState.remove(STATE_PENDING_URI);
@ -1083,7 +1073,7 @@ public class ConversationActivity extends XmppActivity
startConversationActivity.putExtra("init", true); startConversationActivity.putExtra("init", true);
startActivity(startConversationActivity); startActivity(startConversationActivity);
if (noAnimation) { if (noAnimation) {
overridePendingTransition(0,0); overridePendingTransition(0, 0);
} }
} else { } else {
switchToAccount(pendingAccount, true); switchToAccount(pendingAccount, true);
@ -1111,15 +1101,15 @@ public class ConversationActivity extends XmppActivity
Intent redirectionIntent = new Intent(this, ManageAccountActivity.class); Intent redirectionIntent = new Intent(this, ManageAccountActivity.class);
redirectionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); redirectionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(redirectionIntent); startActivity(redirectionIntent);
overridePendingTransition(0,0); overridePendingTransition(0, 0);
} else if (Config.MAGIC_CREATE_DOMAIN != null) { } else if (Config.MAGIC_CREATE_DOMAIN != null) {
WelcomeActivity.launch(this); WelcomeActivity.launch(this);
} else { } else {
Intent editAccount = new Intent(this, EditAccountActivity.class); Intent editAccount = new Intent(this, EditAccountActivity.class);
editAccount.putExtra("init",true); editAccount.putExtra("init", true);
editAccount.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION); editAccount.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(editAccount); startActivity(editAccount);
overridePendingTransition(0,0); overridePendingTransition(0, 0);
} }
} }
} else if (conversationList.size() <= 0) { } else if (conversationList.size() <= 0) {
@ -1136,7 +1126,7 @@ public class ConversationActivity extends XmppActivity
} }
} }
if (this.mConversationFragment.reInit(getSelectedConversation())) { if (this.mConversationFragment.reInit(getSelectedConversation())) {
Log.d(Config.LOGTAG,"setting scroll position on fragment"); Log.d(Config.LOGTAG, "setting scroll position on fragment");
this.mConversationFragment.setScrollPosition(mScrollPosition); this.mConversationFragment.setScrollPosition(mScrollPosition);
} }
mOpenConversation = null; mOpenConversation = null;
@ -1161,12 +1151,12 @@ public class ConversationActivity extends XmppActivity
if (!forbidProcessingPendings) { if (!forbidProcessingPendings) {
for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) { for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
Uri foo = i.next(); Uri foo = i.next();
Log.d(Config.LOGTAG,"ConversationsActivity.onBackendConnected() - attaching image to conversations. stopping="+Boolean.toString(stopping)); Log.d(Config.LOGTAG, "ConversationsActivity.onBackendConnected() - attaching image to conversations. stopping=" + Boolean.toString(stopping));
attachImageToConversation(getSelectedConversation(), foo); attachImageToConversation(getSelectedConversation(), foo);
} }
for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) { for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
Log.d(Config.LOGTAG,"ConversationsActivity.onBackendConnected() - attaching file to conversations. stopping="+Boolean.toString(stopping)); Log.d(Config.LOGTAG, "ConversationsActivity.onBackendConnected() - attaching file to conversations. stopping=" + Boolean.toString(stopping));
attachFileToConversation(getSelectedConversation(), i.next()); attachFileToConversation(getSelectedConversation(), i.next());
} }
@ -1305,7 +1295,7 @@ public class ConversationActivity extends XmppActivity
} }
} else if (requestCode == REQUEST_ANNOUNCE_PGP) { } else if (requestCode == REQUEST_ANNOUNCE_PGP) {
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
announcePgp(mSelectedConversation.getAccount(), mSelectedConversation,data, onOpenPGPKeyPublished); announcePgp(mSelectedConversation.getAccount(), mSelectedConversation, data, onOpenPGPKeyPublished);
this.mPostponedActivityResult = null; this.mPostponedActivityResult = null;
} else { } else {
this.mPostponedActivityResult = new Pair<>(requestCode, data); this.mPostponedActivityResult = new Pair<>(requestCode, data);
@ -1315,13 +1305,13 @@ public class ConversationActivity extends XmppActivity
mPendingImageUris.addAll(extractUriFromIntent(data)); mPendingImageUris.addAll(extractUriFromIntent(data));
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) { for (Iterator<Uri> i = mPendingImageUris.iterator(); i.hasNext(); i.remove()) {
Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching image to conversations. CHOOSE_IMAGE"); Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching image to conversations. CHOOSE_IMAGE");
attachImageToConversation(getSelectedConversation(), i.next()); attachImageToConversation(getSelectedConversation(), i.next());
} }
} }
} else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE || requestCode == ATTACHMENT_CHOICE_RECORD_VIDEO) { } else if (requestCode == ATTACHMENT_CHOICE_CHOOSE_FILE || requestCode == ATTACHMENT_CHOICE_RECORD_VOICE || requestCode == ATTACHMENT_CHOICE_RECORD_VIDEO) {
final List<Uri> uris = extractUriFromIntent(data); final List<Uri> uris = extractUriFromIntent(data);
Log.d(Config.LOGTAG,"uris "+uris.toString()); Log.d(Config.LOGTAG, "uris " + uris.toString());
final Conversation c = getSelectedConversation(); final Conversation c = getSelectedConversation();
final OnPresenceSelected callback = new OnPresenceSelected() { final OnPresenceSelected callback = new OnPresenceSelected() {
@Override @Override
@ -1330,7 +1320,7 @@ public class ConversationActivity extends XmppActivity
mPendingFileUris.addAll(uris); mPendingFileUris.addAll(uris);
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) { for (Iterator<Uri> i = mPendingFileUris.iterator(); i.hasNext(); i.remove()) {
Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO"); Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching file to conversations. CHOOSE_FILE/RECORD_VOICE/RECORD_VIDEO");
attachFileToConversation(c, i.next()); attachFileToConversation(c, i.next());
} }
} }
@ -1347,7 +1337,7 @@ public class ConversationActivity extends XmppActivity
Uri uri = FileBackend.getIndexableTakePhotoUri(mPendingImageUris.get(0)); Uri uri = FileBackend.getIndexableTakePhotoUri(mPendingImageUris.get(0));
mPendingImageUris.set(0, uri); mPendingImageUris.set(0, uri);
if (xmppConnectionServiceBound) { if (xmppConnectionServiceBound) {
Log.d(Config.LOGTAG,"ConversationsActivity.onActivityResult() - attaching image to conversations. TAKE_PHOTO"); Log.d(Config.LOGTAG, "ConversationsActivity.onActivityResult() - attaching image to conversations. TAKE_PHOTO");
attachImageToConversation(getSelectedConversation(), uri); attachImageToConversation(getSelectedConversation(), uri);
mPendingImageUris.clear(); mPendingImageUris.clear();
} }
@ -1396,7 +1386,7 @@ public class ConversationActivity extends XmppActivity
private String getBatteryOptimizationPreferenceKey() { private String getBatteryOptimizationPreferenceKey() {
@SuppressLint("HardwareIds") String device = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID); @SuppressLint("HardwareIds") String device = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
return "show_battery_optimization"+(device == null ? "" : device); return "show_battery_optimization" + (device == null ? "" : device);
} }
private void setNeverAskForBatteryOptimizationsAgain() { private void setNeverAskForBatteryOptimizationsAgain() {
@ -1430,7 +1420,7 @@ public class ConversationActivity extends XmppActivity
} }
private boolean hasAccountWithoutPush() { private boolean hasAccountWithoutPush() {
for(Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() == Account.State.ONLINE && !xmppConnectionService.getPushManagementService().available(account)) { if (account.getStatus() == Account.State.ONLINE && !xmppConnectionService.getPushManagementService().available(account)) {
return true; return true;
} }
@ -1442,7 +1432,7 @@ public class ConversationActivity extends XmppActivity
if (conversation == null) { if (conversation == null) {
return; return;
} }
xmppConnectionService.attachLocationToConversation(conversation,uri, new UiCallback<Message>() { xmppConnectionService.attachLocationToConversation(conversation, uri, new UiCallback<Message>() {
@Override @Override
public void success(Message message) { public void success(Message message) {
@ -1465,29 +1455,19 @@ public class ConversationActivity extends XmppActivity
if (conversation == null) { if (conversation == null) {
return; return;
} }
final Toast prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG); final Toast prepareFileToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_file), Toast.LENGTH_LONG);
prepareFileToast.show(); prepareFileToast.show();
delegateUriPermissionsToService(uri); delegateUriPermissionsToService(uri);
xmppConnectionService.attachFileToConversation(conversation, uri, new UiInformableCallback<Message>() { xmppConnectionService.attachFileToConversation(conversation, uri, new UiInformableCallback<Message>() {
@Override @Override
public void inform(final String text) { public void inform(final String text) {
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
runOnUiThread(new Runnable() { runOnUiThread(() -> replaceToast(text));
@Override
public void run() {
replaceToast(text);
}
});
} }
@Override @Override
public void success(Message message) { public void success(Message message) {
runOnUiThread(new Runnable() { runOnUiThread(() -> hideToast());
@Override
public void run() {
hideToast();
}
});
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
xmppConnectionService.sendMessage(message); xmppConnectionService.sendMessage(message);
} }
@ -1495,12 +1475,7 @@ public class ConversationActivity extends XmppActivity
@Override @Override
public void error(final int errorCode, Message message) { public void error(final int errorCode, Message message) {
hidePrepareFileToast(prepareFileToast); hidePrepareFileToast(prepareFileToast);
runOnUiThread(new Runnable() { runOnUiThread(() -> replaceToast(getString(errorCode)));
@Override
public void run() {
replaceToast(getString(errorCode));
}
});
} }
@ -1519,7 +1494,7 @@ public class ConversationActivity extends XmppActivity
if (conversation == null) { if (conversation == null) {
return; return;
} }
final Toast prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_image), Toast.LENGTH_LONG); final Toast prepareFileToast = Toast.makeText(getApplicationContext(), getText(R.string.preparing_image), Toast.LENGTH_LONG);
prepareFileToast.show(); prepareFileToast.show();
delegateUriPermissionsToService(uri); delegateUriPermissionsToService(uri);
xmppConnectionService.attachImageToConversation(conversation, uri, xmppConnectionService.attachImageToConversation(conversation, uri,
@ -1551,13 +1526,7 @@ public class ConversationActivity extends XmppActivity
private void hidePrepareFileToast(final Toast prepareFileToast) { private void hidePrepareFileToast(final Toast prepareFileToast) {
if (prepareFileToast != null) { if (prepareFileToast != null) {
runOnUiThread(new Runnable() { runOnUiThread(() -> prepareFileToast.cancel());
@Override
public void run() {
prepareFileToast.cancel();
}
});
} }
} }
@ -1589,8 +1558,8 @@ public class ConversationActivity extends XmppActivity
new UiCallback<Message>() { new UiCallback<Message>() {
@Override @Override
public void userInputRequried(PendingIntent pi,Message message) { public void userInputRequried(PendingIntent pi, Message message) {
ConversationActivity.this.runIntent(pi,ConversationActivity.REQUEST_SEND_MESSAGE); ConversationActivity.this.runIntent(pi, ConversationActivity.REQUEST_SEND_MESSAGE);
} }
@Override @Override
@ -1631,7 +1600,7 @@ public class ConversationActivity extends XmppActivity
} }
public boolean useGreenBackground() { public boolean useGreenBackground() {
return getPreferences().getBoolean("use_green_background",getResources().getBoolean(R.bool.use_green_background)); return getPreferences().getBoolean("use_green_background", getResources().getBoolean(R.bool.use_green_background));
} }
protected boolean trustKeysIfNeeded(int requestCode) { protected boolean trustKeysIfNeeded(int requestCode) {
@ -1646,17 +1615,17 @@ public class ConversationActivity extends XmppActivity
boolean hasUndecidedContacts = !axolotlService.getKeysWithTrust(FingerprintStatus.createActiveUndecided(), targets).isEmpty(); boolean hasUndecidedContacts = !axolotlService.getKeysWithTrust(FingerprintStatus.createActiveUndecided(), targets).isEmpty();
boolean hasPendingKeys = !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty(); boolean hasPendingKeys = !axolotlService.findDevicesWithoutSession(mSelectedConversation).isEmpty();
boolean hasNoTrustedKeys = axolotlService.anyTargetHasNoTrustedKeys(targets); boolean hasNoTrustedKeys = axolotlService.anyTargetHasNoTrustedKeys(targets);
if(hasUndecidedOwn || hasUndecidedContacts || hasPendingKeys || hasNoTrustedKeys || hasUnaccepted) { if (hasUndecidedOwn || hasUndecidedContacts || hasPendingKeys || hasNoTrustedKeys || hasUnaccepted) {
axolotlService.createSessionsIfNeeded(mSelectedConversation); axolotlService.createSessionsIfNeeded(mSelectedConversation);
Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class); Intent intent = new Intent(getApplicationContext(), TrustKeysActivity.class);
String[] contacts = new String[targets.size()]; String[] contacts = new String[targets.size()];
for(int i = 0; i < contacts.length; ++i) { for (int i = 0; i < contacts.length; ++i) {
contacts[i] = targets.get(i).toString(); contacts[i] = targets.get(i).toString();
} }
intent.putExtra("contacts", contacts); intent.putExtra("contacts", contacts);
intent.putExtra(EXTRA_ACCOUNT, mSelectedConversation.getAccount().getJid().toBareJid().toString()); intent.putExtra(EXTRA_ACCOUNT, mSelectedConversation.getAccount().getJid().toBareJid().toString());
intent.putExtra("choice", attachmentChoice); intent.putExtra("choice", attachmentChoice);
intent.putExtra("conversation",mSelectedConversation.getUuid()); intent.putExtra("conversation", mSelectedConversation.getUuid());
startActivityForResult(intent, requestCode); startActivityForResult(intent, requestCode);
return true; return true;
} else { } else {
@ -1669,7 +1638,7 @@ public class ConversationActivity extends XmppActivity
updateConversationList(); updateConversationList();
if (conversationList.size() > 0) { if (conversationList.size() > 0) {
if (!this.mConversationFragment.isAdded()) { if (!this.mConversationFragment.isAdded()) {
Log.d(Config.LOGTAG,"fragment NOT added to activity. detached="+Boolean.toString(mConversationFragment.isDetached())); Log.d(Config.LOGTAG, "fragment NOT added to activity. detached=" + Boolean.toString(mConversationFragment.isDetached()));
} }
if (getSelectedConversation() == null) { if (getSelectedConversation() == null) {
reInitLatestConversation(); reInitLatestConversation();
@ -1682,7 +1651,7 @@ public class ConversationActivity extends XmppActivity
if (!isStopping() && mRedirected.compareAndSet(false, true)) { if (!isStopping() && mRedirected.compareAndSet(false, true)) {
redirectToStartConversationActivity(false); redirectToStartConversationActivity(false);
} }
Log.d(Config.LOGTAG,"not updating conversations fragment because conversations list size was 0"); Log.d(Config.LOGTAG, "not updating conversations fragment because conversations list size was 0");
} }
} }
@ -1711,17 +1680,12 @@ public class ConversationActivity extends XmppActivity
} }
public boolean enterIsSend() { public boolean enterIsSend() {
return getPreferences().getBoolean("enter_is_send",getResources().getBoolean(R.bool.enter_is_send)); return getPreferences().getBoolean("enter_is_send", getResources().getBoolean(R.bool.enter_is_send));
} }
@Override @Override
public void onShowErrorToast(final int resId) { public void onShowErrorToast(final int resId) {
runOnUiThread(new Runnable() { runOnUiThread(() -> Toast.makeText(ConversationActivity.this, resId, Toast.LENGTH_SHORT).show());
@Override
public void run() {
Toast.makeText(ConversationActivity.this,resId,Toast.LENGTH_SHORT).show();
}
});
} }
public boolean highlightSelectedConversations() { public boolean highlightSelectedConversations() {

View File

@ -71,8 +71,7 @@ import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected; import eu.siacs.conversations.ui.XmppActivity.OnPresenceSelected;
import eu.siacs.conversations.ui.XmppActivity.OnValueEdited; import eu.siacs.conversations.ui.XmppActivity.OnValueEdited;
import eu.siacs.conversations.ui.adapter.MessageAdapter; import eu.siacs.conversations.ui.adapter.MessageAdapter;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureClicked; import eu.siacs.conversations.ui.util.SendButtonAction;
import eu.siacs.conversations.ui.adapter.MessageAdapter.OnContactPictureLongClicked;
import eu.siacs.conversations.ui.widget.EditMessage; import eu.siacs.conversations.ui.widget.EditMessage;
import eu.siacs.conversations.utils.MessageUtils; import eu.siacs.conversations.utils.MessageUtils;
import eu.siacs.conversations.utils.NickValidityChecker; import eu.siacs.conversations.utils.NickValidityChecker;
@ -82,6 +81,12 @@ import eu.siacs.conversations.xmpp.XmppConnection;
import eu.siacs.conversations.xmpp.chatstate.ChatState; import eu.siacs.conversations.xmpp.chatstate.ChatState;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_LOCATION;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO;
public class ConversationFragment extends Fragment implements EditMessage.KeyboardListener { public class ConversationFragment extends Fragment implements EditMessage.KeyboardListener {
final protected List<Message> messageList = new ArrayList<>(); final protected List<Message> messageList = new ArrayList<>();
@ -357,19 +362,11 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
SendButtonAction action = (SendButtonAction) tag; SendButtonAction action = (SendButtonAction) tag;
switch (action) { switch (action) {
case TAKE_PHOTO: case TAKE_PHOTO:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO);
break;
case RECORD_VIDEO: case RECORD_VIDEO:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO);
break;
case SEND_LOCATION: case SEND_LOCATION:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_LOCATION);
break;
case RECORD_VOICE: case RECORD_VOICE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE);
break;
case CHOOSE_PICTURE: case CHOOSE_PICTURE:
activity.attachFile(ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE); activity.attachFile(action.toChoice());
break; break;
case CANCEL: case CANCEL:
if (conversation != null) { if (conversation != null) {
@ -1674,8 +1671,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, public void onActivityResult(int requestCode, int resultCode, final Intent data) {
final Intent data) {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) { if (requestCode == ConversationActivity.REQUEST_DECRYPT_PGP) {
activity.getSelectedConversation().getAccount().getPgpDecryptionService().continueDecryption(data); activity.getSelectedConversation().getAccount().getPgpDecryptionService().continueDecryption(data);
@ -1695,16 +1691,4 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
} }
enum SendButtonAction {
TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE, RECORD_VIDEO;
public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) {
try {
return valueOf(setting);
} catch (IllegalArgumentException e) {
return TEXT;
}
}
}
} }

View File

@ -0,0 +1,82 @@
/*
* Copyright (c) 2018, Daniel Gultsch All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package eu.siacs.conversations.ui.util;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_CHOOSE_IMAGE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_LOCATION;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VIDEO;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_RECORD_VOICE;
import static eu.siacs.conversations.ui.ConversationActivity.ATTACHMENT_CHOICE_TAKE_PHOTO;
public enum SendButtonAction {
TEXT, TAKE_PHOTO, SEND_LOCATION, RECORD_VOICE, CANCEL, CHOOSE_PICTURE, RECORD_VIDEO;
public static SendButtonAction valueOfOrDefault(String setting, SendButtonAction text) {
try {
return valueOf(setting);
} catch (IllegalArgumentException e) {
return TEXT;
}
}
public static SendButtonAction of(int attachmentChoice) {
switch (attachmentChoice) {
case ATTACHMENT_CHOICE_LOCATION:
return SEND_LOCATION;
case ATTACHMENT_CHOICE_RECORD_VOICE:
return RECORD_VOICE;
case ATTACHMENT_CHOICE_RECORD_VIDEO:
return RECORD_VIDEO;
case ATTACHMENT_CHOICE_TAKE_PHOTO:
return TAKE_PHOTO;
case ATTACHMENT_CHOICE_CHOOSE_IMAGE:
return CHOOSE_PICTURE;
default:
throw new IllegalArgumentException("Not a known attachment choice");
}
}
public int toChoice() {
switch (this) {
case TAKE_PHOTO:
return ATTACHMENT_CHOICE_TAKE_PHOTO;
case SEND_LOCATION:
return ATTACHMENT_CHOICE_LOCATION;
case RECORD_VOICE:
return ATTACHMENT_CHOICE_RECORD_VOICE;
case CHOOSE_PICTURE:
return ATTACHMENT_CHOICE_CHOOSE_IMAGE;
case RECORD_VIDEO:
return ATTACHMENT_CHOICE_RECORD_VIDEO;
default:
return 0;
}
}
}