added callbacks

This commit is contained in:
Daniel Gultsch 2018-10-21 16:27:56 +02:00
parent 6d6278002a
commit 31eb89e2fb
6 changed files with 144 additions and 19 deletions

View File

@ -761,8 +761,13 @@
<string name="search_countries">Search countries</string> <string name="search_countries">Search countries</string>
<string name="verify_x">Verify %s</string> <string name="verify_x">Verify %s</string>
<string name="we_have_sent_you_an_sms"><![CDATA[We have sent you an SMS to <b>%s</b>.]]></string> <string name="we_have_sent_you_an_sms"><![CDATA[We have sent you an SMS to <b>%s</b>.]]></string>
<string name="please_enter_pin">Please enter the 6 digit pin below.</string> <string name="please_enter_pin_below">Please enter the 6 digit pin below.</string>
<string name="resend_sms">Resend SMS</string> <string name="resend_sms">Resend SMS</string>
<string name="back">back</string> <string name="back">back</string>
<string name="possible_pin">Automatically pasted possible pin from clipboard.</string> <string name="possible_pin">Automatically pasted possible pin from clipboard.</string>
<string name="please_enter_pin">Please enter your 6 digit pin.</string>
<string name="abort_registration_procedure">Are you sure you want to abort the registration procedure?</string>
<string name="yes">Yes</string>
<string name="no">No</string>
<string name="verifying">Verifying…</string>
</resources> </resources>

View File

@ -63,6 +63,10 @@
<item name="android:textSize">?TextSizeBody2</item> <item name="android:textSize">?TextSizeBody2</item>
</style> </style>
<style name="Widget.Conversations.Button.Borderless.Primary" parent="Widget.Conversations.Button.Borderless">
<item name="android:textColor">?colorAccent</item>
</style>
<style name="TextAppearance.Conversations.Design.Hint" parent="TextAppearance.Design.Hint"> <style name="TextAppearance.Conversations.Design.Hint" parent="TextAppearance.Design.Hint">
<item name="android:textSize">?TextSizeCaption</item> <item name="android:textSize">?TextSizeCaption</item>

View File

@ -7,6 +7,7 @@ import java.security.SecureRandom;
import java.util.Collections; import java.util.Collections;
import java.util.Set; import java.util.Set;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import java.util.concurrent.atomic.AtomicBoolean;
import eu.siacs.conversations.Config; import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
@ -20,7 +21,9 @@ public class QuickConversationsService {
private final XmppConnectionService service; private final XmppConnectionService service;
private final Set<OnVerificationRequested> mOnVerificationRequested = Collections.newSetFromMap(new WeakHashMap<>()); private final Set<OnVerificationRequested> mOnVerificationRequested = Collections.newSetFromMap(new WeakHashMap<>());
private final Set<OnVerified> mOnVerified = Collections.newSetFromMap(new WeakHashMap<>()); private final Set<OnVerification> mOnVerification = Collections.newSetFromMap(new WeakHashMap<>());
private final AtomicBoolean mVerificationInProgress = new AtomicBoolean(false);
QuickConversationsService(XmppConnectionService xmppConnectionService) { QuickConversationsService(XmppConnectionService xmppConnectionService) {
this.service = xmppConnectionService; this.service = xmppConnectionService;
@ -38,6 +41,18 @@ public class QuickConversationsService {
} }
} }
public void addOnVerificationListener(OnVerification onVerification) {
synchronized (mOnVerification) {
mOnVerification.add(onVerification);
}
}
public void removeOnVerificationListener(OnVerification onVerification) {
synchronized (mOnVerification) {
mOnVerification.remove(onVerification);
}
}
public void requestVerification(Phonenumber.PhoneNumber phoneNumber) { public void requestVerification(Phonenumber.PhoneNumber phoneNumber) {
String local = PhoneNumberUtilWrapper.normalize(service, phoneNumber); String local = PhoneNumberUtilWrapper.normalize(service, phoneNumber);
Log.d(Config.LOGTAG,"requesting verification for "+PhoneNumberUtilWrapper.normalize(service,phoneNumber)); Log.d(Config.LOGTAG,"requesting verification for "+PhoneNumberUtilWrapper.normalize(service,phoneNumber));
@ -52,12 +67,35 @@ public class QuickConversationsService {
} }
} }
public void verify(Account account, String pin) {
if (mVerificationInProgress.compareAndSet(false, true)) {
new Thread(() -> {
try {
Thread.sleep(5000);
synchronized (mOnVerification) {
for (OnVerification onVerification : mOnVerification) {
onVerification.onVerificationFailed();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
mVerificationInProgress.set(false);
}
}).start();
}
}
public boolean isVerifying() {
return mVerificationInProgress.get();
}
public interface OnVerificationRequested { public interface OnVerificationRequested {
void onVerificationRequestFailed(int code); void onVerificationRequestFailed(int code);
void onVerificationRequested(); void onVerificationRequested();
} }
public interface OnVerified { public interface OnVerification {
void onVerificationFailed(); void onVerificationFailed();
void onVerificationSucceeded(); void onVerificationSucceeded();
} }

View File

@ -1,5 +1,6 @@
package eu.siacs.conversations.ui; package eu.siacs.conversations.ui;
import android.app.AlertDialog;
import android.content.ClipData; import android.content.ClipData;
import android.content.ClipDescription; import android.content.ClipDescription;
import android.content.ClipboardManager; import android.content.ClipboardManager;
@ -12,30 +13,31 @@ import android.support.v7.widget.Toolbar;
import android.text.Html; import android.text.Html;
import android.view.View; import android.view.View;
import java.util.ArrayList;
import eu.siacs.conversations.R; import eu.siacs.conversations.R;
import eu.siacs.conversations.databinding.ActivityVerifyBinding; import eu.siacs.conversations.databinding.ActivityVerifyBinding;
import eu.siacs.conversations.entities.Account; import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.services.QuickConversationsService;
import eu.siacs.conversations.ui.util.PinEntryWrapper; import eu.siacs.conversations.ui.util.PinEntryWrapper;
import eu.siacs.conversations.utils.AccountUtils; import eu.siacs.conversations.utils.AccountUtils;
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper; import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN; import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener { public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener, QuickConversationsService.OnVerification {
private ActivityVerifyBinding binding; private ActivityVerifyBinding binding;
private Account account; private Account account;
private PinEntryWrapper pinEntryWrapper; private PinEntryWrapper pinEntryWrapper;
private ClipboardManager clipboardManager; private ClipboardManager clipboardManager;
private String pasted = null; private String pasted = null;
private boolean verifying = false;
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null; String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying");
this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null; this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify); this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
setSupportActionBar((Toolbar) this.binding.toolbar); setSupportActionBar((Toolbar) this.binding.toolbar);
@ -44,18 +46,59 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
this.pinEntryWrapper.setPin(pin); this.pinEntryWrapper.setPin(pin);
} }
binding.back.setOnClickListener(this::onBackButton); binding.back.setOnClickListener(this::onBackButton);
binding.next.setOnClickListener(this::onNextButton);
clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
setVerifyingState(verifying);
} }
private void onBackButton(View view) { private void onBackButton(View view) {
if (this.verifying) {
setVerifyingState(false);
return;
}
final Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
if (this.account != null) { if (this.account != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.abort_registration_procedure);
builder.setPositiveButton(R.string.yes, (dialog, which) -> {
xmppConnectionService.deleteAccount(account); xmppConnectionService.deleteAccount(account);
Intent intent = new Intent(this, EnterPhoneNumberActivity.class); startActivity(intent);
finish();
});
builder.setNegativeButton(R.string.no, null);
builder.create().show();
} else {
startActivity(intent); startActivity(intent);
finish(); finish();
} }
} }
private void onNextButton(View view) {
final String pin = pinEntryWrapper.getPin();
if (PinEntryWrapper.isValidPin(pin)) {
if (account != null && xmppConnectionService != null) {
setVerifyingState(true);
xmppConnectionService.getQuickConversationsService().verify(account, pin);
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.please_enter_pin);
builder.setPositiveButton(R.string.ok, null);
builder.create().show();
}
}
private void setVerifyingState(boolean verifying) {
this.verifying = verifying;
this.binding.back.setText(verifying ? R.string.cancel : R.string.back);
this.binding.next.setEnabled(!verifying);
this.binding.next.setText(verifying ? R.string.verifying : R.string.next);
this.binding.resendSms.setVisibility(verifying ? View.GONE : View.VISIBLE);
pinEntryWrapper.setEnabled(!verifying);
this.binding.progressBar.setVisibility(verifying ? View.VISIBLE : View.GONE);
this.binding.progressBar.setIndeterminate(verifying);
}
@Override @Override
protected void refreshUiReal() { protected void refreshUiReal() {
@ -63,16 +106,19 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
@Override @Override
void onBackendConnected() { void onBackendConnected() {
xmppConnectionService.getQuickConversationsService().addOnVerificationListener(this);
this.account = AccountUtils.getFirst(xmppConnectionService); this.account = AccountUtils.getFirst(xmppConnectionService);
if (this.account == null) { if (this.account == null) {
return; return;
} }
this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms, PhoneNumberUtilWrapper.prettyPhoneNumber(this, this.account.getJid())))); this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms, PhoneNumberUtilWrapper.prettyPhoneNumber(this, this.account.getJid()))));
setVerifyingState(xmppConnectionService.getQuickConversationsService().isVerifying());
} }
@Override @Override
public void onSaveInstanceState(Bundle savedInstanceState) { public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("pin", this.pinEntryWrapper.getPin()); savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
savedInstanceState.putBoolean("verifying", this.verifying);
if (this.pasted != null) { if (this.pasted != null) {
savedInstanceState.putString("pasted", this.pasted); savedInstanceState.putString("pasted", this.pasted);
} }
@ -89,6 +135,9 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
clipboardManager.removePrimaryClipChangedListener(this); clipboardManager.removePrimaryClipChangedListener(this);
if (xmppConnectionService != null) {
xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
}
} }
@Override @Override
@ -105,7 +154,7 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
final ClipData primaryClip = clipboardManager.getPrimaryClip(); final ClipData primaryClip = clipboardManager.getPrimaryClip();
if (primaryClip != null && primaryClip.getItemCount() > 0) { if (primaryClip != null && primaryClip.getItemCount() > 0) {
final CharSequence clip = primaryClip.getItemAt(0).getText(); final CharSequence clip = primaryClip.getItemAt(0).getText();
if (PinEntryWrapper.isPin(clip) && !clip.toString().equals(this.pasted)) { if (PinEntryWrapper.isValidPin(clip) && !clip.toString().equals(this.pasted)) {
this.pasted = clip.toString(); this.pasted = clip.toString();
pinEntryWrapper.setPin(clip.toString()); pinEntryWrapper.setPin(clip.toString());
final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG); final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
@ -123,4 +172,16 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
pastePinFromClipboard(); pastePinFromClipboard();
} }
} }
@Override
public void onVerificationFailed() {
runOnUiThread(() -> {
setVerifyingState(false);
});
}
@Override
public void onVerificationSucceeded() {
}
} }

View File

@ -64,7 +64,8 @@ public class PinEntryWrapper {
} }
if (v instanceof EditText) { if (v instanceof EditText) {
final EditText editText = (EditText) v; final EditText editText = (EditText) v;
if (keyCode == KeyEvent.KEYCODE_DEL && editText.getText().length() == 0) { final boolean cursorAtZero = editText.getSelectionEnd() == 0 && editText.getSelectionStart() == 0;
if (keyCode == KeyEvent.KEYCODE_DEL && (cursorAtZero || editText.getText().length() == 0)) {
final int current = digits.indexOf(editText); final int current = digits.indexOf(editText);
for (int i = current - 1; i >= 0; --i) { for (int i = current - 1; i >= 0; --i) {
if (digits.get(i).getText().length() > 0) { if (digits.get(i).getText().length() > 0) {
@ -118,6 +119,15 @@ public class PinEntryWrapper {
} }
} }
public void setEnabled(boolean enabled) {
for(EditText digit : digits) {
digit.setEnabled(enabled);
digit.setCursorVisible(enabled);
digit.setFocusable(enabled);
digit.setFocusableInTouchMode(enabled);
}
}
public boolean isEmpty() { public boolean isEmpty() {
for (EditText digit : digits) { for (EditText digit : digits) {
if (digit.getText().length() > 0) { if (digit.getText().length() > 0) {
@ -127,7 +137,7 @@ public class PinEntryWrapper {
return true; return true;
} }
public static boolean isPin(CharSequence pin) { public static boolean isValidPin(CharSequence pin) {
return pin != null && PIN_STRING_PATTERN.matcher(pin).matches(); return pin != null && PIN_STRING_PATTERN.matcher(pin).matches();
} }

View File

@ -45,7 +45,7 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/please_enter_pin" android:text="@string/please_enter_pin_below"
android:textAppearance="@style/TextAppearance.Conversations.Body1" /> android:textAppearance="@style/TextAppearance.Conversations.Body1" />
</LinearLayout> </LinearLayout>
@ -150,13 +150,12 @@
<Button <Button
android:id="@+id/next" android:id="@+id/next"
style="@style/Widget.Conversations.Button.Borderless" style="@style/Widget.Conversations.Button.Borderless.Primary"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:text="@string/next" android:text="@string/next"/>
android:textColor="?colorAccent" />
<Button <Button
android:id="@+id/back" android:id="@+id/back"
@ -177,6 +176,14 @@
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:text="@string/resend_sms" android:text="@string/resend_sms"
android:textColor="?android:textColorSecondary" /> android:textColor="?android:textColorSecondary" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/pin_box" />
</RelativeLayout> </RelativeLayout>
</ScrollView> </ScrollView>
</android.support.design.widget.CoordinatorLayout> </android.support.design.widget.CoordinatorLayout>