From 683336d6204450df124a7e19870dad2f8d84da13 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 30 Sep 2018 19:43:44 +0200 Subject: [PATCH] handle incoming autocrypt setup messages, closes #43 --- res/layout/setup_code_grid.xml | 144 ++++++++++++++++++ res/values/strings.xml | 8 + .../securesms/ConversationAdapter.java | 2 +- .../securesms/ConversationFragment.java | 80 ++++++++++ .../securesms/ConversationItem.java | 23 ++- .../connect/ApplicationDcContext.java | 10 ++ 6 files changed, 253 insertions(+), 14 deletions(-) create mode 100644 res/layout/setup_code_grid.xml diff --git a/res/layout/setup_code_grid.xml b/res/layout/setup_code_grid.xml new file mode 100644 index 000000000..38a97f932 --- /dev/null +++ b/res/layout/setup_code_grid.xml @@ -0,0 +1,144 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 79f34ce52..1a3d5d791 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1449,6 +1449,14 @@ Send Autocrypt Setup Message An \"Autocrypt Setup Message\" securely shares your end-to-end setup with other Autocrypt-compliant apps.\n\nThe setup will be encrypted by a setup code which is displayed here and must be typed on the other device. Your setup has been sent to yourself. Switch to the other device and open the setup message. You should be prompted for a setup code. Type the following digits into the prompt:\n\n%1$s\n\nOnce you\'re done, your other device will be ready to use Autocrypt. + Autocrypt Setup Message + This is the Autocrypt Setup Message used to transfer your end-to-end setup between clients.\n\nTo decrypt and use your setup, open the message in an Autocrypt-compliant client and enter the setup code presented on the generating device. + This is the Autocrypt Setup Message used to transfer your end-to-end setup between clients.\n\nTo decrypt and use your setup, tap on this message. + Autocrypt Setup Message + Please enter the setup code that is displayed on the other device. + Bad setup code. Please try again.\n\nIf you do not remember the setup code, just send another Autocrypt Setup Message from the other device. + End-to-end setup transferred. This device is now ready to use Autocrypt with the same setup as the other device. + Retry Email address diff --git a/src/org/thoughtcrime/securesms/ConversationAdapter.java b/src/org/thoughtcrime/securesms/ConversationAdapter.java index f4c0e56cd..1f86a3892 100644 --- a/src/org/thoughtcrime/securesms/ConversationAdapter.java +++ b/src/org/thoughtcrime/securesms/ConversationAdapter.java @@ -245,7 +245,7 @@ public class ConversationAdapter else if (type==DcMsg.DC_MSG_AUDIO || type==DcMsg.DC_MSG_VOICE) { return dcMsg.isOutgoing()? MESSAGE_TYPE_AUDIO_OUTGOING : MESSAGE_TYPE_AUDIO_INCOMING; } - else if (type==DcMsg.DC_MSG_FILE) { + else if (type==DcMsg.DC_MSG_FILE && !dcMsg.isSetupMessage()) { return dcMsg.isOutgoing()? MESSAGE_TYPE_DOCUMENT_OUTGOING : MESSAGE_TYPE_DOCUMENT_INCOMING; } else if (type==DcMsg.DC_MSG_IMAGE || type==DcMsg.DC_MSG_GIF || type==DcMsg.DC_MSG_VIDEO) { diff --git a/src/org/thoughtcrime/securesms/ConversationFragment.java b/src/org/thoughtcrime/securesms/ConversationFragment.java index 1a220dd54..5a60eb162 100644 --- a/src/org/thoughtcrime/securesms/ConversationFragment.java +++ b/src/org/thoughtcrime/securesms/ConversationFragment.java @@ -37,7 +37,9 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView.OnScrollListener; import android.text.ClipboardManager; +import android.text.Editable; import android.text.TextUtils; +import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; @@ -48,6 +50,7 @@ import android.view.ViewGroup; import android.view.Window; import android.view.animation.Animation; import android.view.animation.AnimationUtils; +import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewSwitcher; @@ -651,6 +654,80 @@ public class ConversationFragment extends Fragment } } + + void querySetupCode(final DcMsg dcMsg, String[] preload) + { + if( !dcMsg.isSetupMessage()) { + return; + } + + View gl = View.inflate(getActivity(), R.layout.setup_code_grid, null); + final EditText[] editTexts = { + (EditText) gl.findViewById(R.id.setupCode0), (EditText) gl.findViewById(R.id.setupCode1), (EditText) gl.findViewById(R.id.setupCode2), + (EditText) gl.findViewById(R.id.setupCode3), (EditText) gl.findViewById(R.id.setupCode4), (EditText) gl.findViewById(R.id.setupCode5), + (EditText) gl.findViewById(R.id.setupCode6), (EditText) gl.findViewById(R.id.setupCode7), (EditText) gl.findViewById(R.id.setupCode8) + }; + android.app.AlertDialog.Builder builder1 = new android.app.AlertDialog.Builder(getActivity()); + builder1.setView(gl); + editTexts[0].setText(dcMsg.getSetupCodeBegin()); + editTexts[0].setSelection(editTexts[0].getText().length()); + + for( int i = 0; i < 9; i++ ) { + if( preload != null && i < preload.length ) { + editTexts[i].setText(preload[i]); + editTexts[i].setSelection(editTexts[i].getText().length()); + } + editTexts[i].addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + if( s.length()==4 ) { + for ( int i = 0; i < 8; i++ ) { + if( editTexts[i].hasFocus() && editTexts[i+1].getText().length()<4 ) { + editTexts[i+1].requestFocus(); + break; + } + } + } + } + + @Override + public void afterTextChanged(Editable s) { + } + }); + } + + builder1.setTitle(getActivity().getString(R.string.autocrypt__continue_transfer_title)); + builder1.setMessage(getActivity().getString(R.string.autocrypt__continue_transfer_please_enter_code)); + builder1.setNegativeButton(android.R.string.cancel, null); + builder1.setCancelable(false); // prevent the dialog from being dismissed accidentally (when the dialog is closed, the setup code is gone forever and the user has to create a new setup message) + builder1.setPositiveButton(android.R.string.ok, (dialog, which) -> { + String setup_code = ""; + final String[] preload1 = new String[9]; + for ( int i = 0; i < 9; i++ ) { + preload1[i] = editTexts[i].getText().toString(); + setup_code += preload1[i]; + } + boolean success = dcContext.continueKeyTransfer(dcMsg.getId(), setup_code); + + android.app.AlertDialog.Builder builder2 = new android.app.AlertDialog.Builder(getActivity()); + builder2.setTitle(getActivity().getString(R.string.autocrypt__continue_transfer_title)); + builder2.setMessage(getActivity().getString(success? R.string.autocrypt__continue_transfer_succeeded : R.string.autocrypt__continue_transfer_bad_code)); + if( success ) { + builder2.setPositiveButton(android.R.string.ok, null); + } + else { + builder2.setNegativeButton(android.R.string.cancel, null); + builder2.setPositiveButton(R.string.autocrypt__continue_button_retry, (dialog1, which1) -> querySetupCode(dcMsg, preload1)); + } + builder2.show(); + }); + builder1.show(); + } + private class ConversationFragmentItemClickListener implements ItemClickListener { @Override @@ -666,6 +743,9 @@ public class ConversationFragment extends Fragment actionMode.setTitle(String.valueOf(getListAdapter().getSelectedItems().size())); } } + else if(messageRecord.isSetupMessage()) { + querySetupCode(messageRecord,null); + } } @Override diff --git a/src/org/thoughtcrime/securesms/ConversationItem.java b/src/org/thoughtcrime/securesms/ConversationItem.java index e2db04b7f..9e407dfbe 100644 --- a/src/org/thoughtcrime/securesms/ConversationItem.java +++ b/src/org/thoughtcrime/securesms/ConversationItem.java @@ -350,11 +350,6 @@ public class ConversationItem extends LinearLayout } } - private boolean isCaptionlessMms(DcMsg messageRecord) { - return false; -// return TextUtils.isEmpty(messageRecord.getText()) && messageRecord.isMms(); - } - private boolean hasAudio(DcMsg messageRecord) { return false; // return messageRecord.isMms() && ((MmsDcMsg)messageRecord).getSlideDeck().getAudioSlide() != null; @@ -389,12 +384,14 @@ public class ConversationItem extends LinearLayout bodyText.setFocusable(false); bodyText.setTextSize(TypedValue.COMPLEX_UNIT_SP, TextSecurePreferences.getMessageBodyTextSize(context)); - if (isCaptionlessMms(messageRecord)) { - bodyText.setVisibility(View.GONE); - } else { - bodyText.setText(linkifyMessageBody(new SpannableString(messageRecord.getText()), batchSelected.isEmpty())); - bodyText.setVisibility(View.VISIBLE); + if (messageRecord.isSetupMessage()) { + bodyText.setText(context.getString(R.string.autocrypt__asm_tap_body)); } + else { + bodyText.setText(linkifyMessageBody(new SpannableString(messageRecord.getText()), batchSelected.isEmpty())); + } + + bodyText.setVisibility(View.VISIBLE); } private void setMediaAttributes(@NonNull DcMsg messageRecord, @@ -584,7 +581,8 @@ public class ConversationItem extends LinearLayout } private void setStatusIcons(DcMsg messageRecord) { - bodyText.setCompoundDrawablesWithIntrinsicBounds(0, 0, messageRecord.isSetupMessage() ? R.drawable.ic_menu_login : 0, 0); + // disable the icon for for, we should use the same ui on desktop/ios here, so, for now + //bodyText.setCompoundDrawablesWithIntrinsicBounds(0, 0, messageRecord.isSetupMessage() ? R.drawable.ic_menu_login : 0, 0); if (messageRecord.isFailed()) { alertView.setFailed(); @@ -680,8 +678,7 @@ public class ConversationItem extends LinearLayout } private boolean shouldInterceptClicks(DcMsg messageRecord) { - return batchSelected.isEmpty() && (messageRecord.isFailed() || - messageRecord.isSetupMessage()); + return batchSelected.isEmpty() && (messageRecord.isFailed()); } @SuppressLint("SetTextI18n") diff --git a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java index abae115f0..08fd4019c 100644 --- a/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java +++ b/src/org/thoughtcrime/securesms/connect/ApplicationDcContext.java @@ -32,6 +32,7 @@ import com.b44t.messenger.DcEventCenter; import com.b44t.messenger.DcLot; import org.thoughtcrime.securesms.ApplicationContext; +import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.database.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; @@ -343,6 +344,15 @@ public class ApplicationDcContext extends DcContext { } return stringToData(httpContent); + case DC_EVENT_GET_STRING: + String s; + switch( (int)data1 ) { // the integers are defined in the core and used only here, an enum or sth. like that won't have a big benefit + case 42: s = context.getString(R.string.autocrypt__asm_subject); break; + case 43: s = context.getString(R.string.autocrypt__asm_general_body); break; + default: s = null; break; + } + return stringToData(s); + default: { final Object data1obj = data1IsString(event)? dataToString(data1) : data1;