added callbacks
This commit is contained in:
		
							parent
							
								
									6d6278002a
								
							
						
					
					
						commit
						31eb89e2fb
					
				| 
						 | 
				
			
			@ -761,8 +761,13 @@
 | 
			
		|||
    <string name="search_countries">Search countries</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="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="back">back</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>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -63,6 +63,10 @@
 | 
			
		|||
        <item name="android:textSize">?TextSizeBody2</item>
 | 
			
		||||
    </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">
 | 
			
		||||
        <item name="android:textSize">?TextSizeCaption</item>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,6 +7,7 @@ import java.security.SecureRandom;
 | 
			
		|||
import java.util.Collections;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.WeakHashMap;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicBoolean;
 | 
			
		||||
 | 
			
		||||
import eu.siacs.conversations.Config;
 | 
			
		||||
import eu.siacs.conversations.entities.Account;
 | 
			
		||||
| 
						 | 
				
			
			@ -20,7 +21,9 @@ public class QuickConversationsService {
 | 
			
		|||
    private final XmppConnectionService service;
 | 
			
		||||
 | 
			
		||||
    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) {
 | 
			
		||||
        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) {
 | 
			
		||||
        String local = 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 {
 | 
			
		||||
        void onVerificationRequestFailed(int code);
 | 
			
		||||
        void onVerificationRequested();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public interface OnVerified {
 | 
			
		||||
    public interface OnVerification {
 | 
			
		||||
        void onVerificationFailed();
 | 
			
		||||
        void onVerificationSucceeded();
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,6 @@
 | 
			
		|||
package eu.siacs.conversations.ui;
 | 
			
		||||
 | 
			
		||||
import android.app.AlertDialog;
 | 
			
		||||
import android.content.ClipData;
 | 
			
		||||
import android.content.ClipDescription;
 | 
			
		||||
import android.content.ClipboardManager;
 | 
			
		||||
| 
						 | 
				
			
			@ -12,30 +13,31 @@ import android.support.v7.widget.Toolbar;
 | 
			
		|||
import android.text.Html;
 | 
			
		||||
import android.view.View;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
 | 
			
		||||
import eu.siacs.conversations.R;
 | 
			
		||||
import eu.siacs.conversations.databinding.ActivityVerifyBinding;
 | 
			
		||||
import eu.siacs.conversations.entities.Account;
 | 
			
		||||
import eu.siacs.conversations.services.QuickConversationsService;
 | 
			
		||||
import eu.siacs.conversations.ui.util.PinEntryWrapper;
 | 
			
		||||
import eu.siacs.conversations.utils.AccountUtils;
 | 
			
		||||
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
 | 
			
		||||
 | 
			
		||||
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 Account account;
 | 
			
		||||
    private PinEntryWrapper pinEntryWrapper;
 | 
			
		||||
    private ClipboardManager clipboardManager;
 | 
			
		||||
    private String pasted = null;
 | 
			
		||||
    private boolean verifying = false;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onCreate(final Bundle savedInstanceState) {
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
        String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
 | 
			
		||||
        boolean verifying = savedInstanceState != null && savedInstanceState.getBoolean("verifying");
 | 
			
		||||
        this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
 | 
			
		||||
        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
 | 
			
		||||
        setSupportActionBar((Toolbar) this.binding.toolbar);
 | 
			
		||||
| 
						 | 
				
			
			@ -44,18 +46,59 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
 | 
			
		|||
            this.pinEntryWrapper.setPin(pin);
 | 
			
		||||
        }
 | 
			
		||||
        binding.back.setOnClickListener(this::onBackButton);
 | 
			
		||||
        binding.next.setOnClickListener(this::onNextButton);
 | 
			
		||||
        clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
 | 
			
		||||
        setVerifyingState(verifying);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void onBackButton(View view) {
 | 
			
		||||
        if (this.verifying) {
 | 
			
		||||
            setVerifyingState(false);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        final Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
 | 
			
		||||
        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);
 | 
			
		||||
            Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
 | 
			
		||||
                startActivity(intent);
 | 
			
		||||
                finish();
 | 
			
		||||
            });
 | 
			
		||||
            builder.setNegativeButton(R.string.no, null);
 | 
			
		||||
            builder.create().show();
 | 
			
		||||
        } else {
 | 
			
		||||
            startActivity(intent);
 | 
			
		||||
            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
 | 
			
		||||
    protected void refreshUiReal() {
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -63,16 +106,19 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    void onBackendConnected() {
 | 
			
		||||
        xmppConnectionService.getQuickConversationsService().addOnVerificationListener(this);
 | 
			
		||||
        this.account = AccountUtils.getFirst(xmppConnectionService);
 | 
			
		||||
        if (this.account == null) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        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
 | 
			
		||||
    public void onSaveInstanceState(Bundle savedInstanceState) {
 | 
			
		||||
        savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
 | 
			
		||||
        savedInstanceState.putBoolean("verifying", this.verifying);
 | 
			
		||||
        if (this.pasted != null) {
 | 
			
		||||
            savedInstanceState.putString("pasted", this.pasted);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -89,6 +135,9 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
 | 
			
		|||
    public void onStop() {
 | 
			
		||||
        super.onStop();
 | 
			
		||||
        clipboardManager.removePrimaryClipChangedListener(this);
 | 
			
		||||
        if (xmppConnectionService != null) {
 | 
			
		||||
            xmppConnectionService.getQuickConversationsService().removeOnVerificationListener(this);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
| 
						 | 
				
			
			@ -105,7 +154,7 @@ public class VerifyActivity extends XmppActivity implements ClipboardManager.OnP
 | 
			
		|||
            final ClipData primaryClip = clipboardManager.getPrimaryClip();
 | 
			
		||||
            if (primaryClip != null && primaryClip.getItemCount() > 0) {
 | 
			
		||||
                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();
 | 
			
		||||
                    pinEntryWrapper.setPin(clip.toString());
 | 
			
		||||
                    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();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onVerificationFailed() {
 | 
			
		||||
        runOnUiThread(() -> {
 | 
			
		||||
            setVerifyingState(false);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onVerificationSucceeded() {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -64,7 +64,8 @@ public class PinEntryWrapper {
 | 
			
		|||
        }
 | 
			
		||||
        if (v instanceof EditText) {
 | 
			
		||||
            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);
 | 
			
		||||
                for (int i = current - 1; i >= 0; --i) {
 | 
			
		||||
                    if (digits.get(i).getText().length() > 0) {
 | 
			
		||||
| 
						 | 
				
			
			@ -100,7 +101,7 @@ public class PinEntryWrapper {
 | 
			
		|||
 | 
			
		||||
    public String getPin() {
 | 
			
		||||
        char[] chars = new char[digits.size()];
 | 
			
		||||
        for(int i = 0; i < chars.length; ++i) {
 | 
			
		||||
        for (int i = 0; i < chars.length; ++i) {
 | 
			
		||||
            final String input = digits.get(i).getText().toString();
 | 
			
		||||
            chars[i] = input.length() != 1 ? ' ' : input.charAt(0);
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			@ -109,7 +110,7 @@ public class PinEntryWrapper {
 | 
			
		|||
 | 
			
		||||
    public void setPin(String pin) {
 | 
			
		||||
        char[] chars = pin.toCharArray();
 | 
			
		||||
        for(int i = 0; i < digits.size(); ++i) {
 | 
			
		||||
        for (int i = 0; i < digits.size(); ++i) {
 | 
			
		||||
            if (i < chars.length) {
 | 
			
		||||
                final Editable editable = digits.get(i).getText();
 | 
			
		||||
                editable.clear();
 | 
			
		||||
| 
						 | 
				
			
			@ -118,8 +119,17 @@ public class PinEntryWrapper {
 | 
			
		|||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isEmpty() {
 | 
			
		||||
    public void setEnabled(boolean enabled) {
 | 
			
		||||
        for(EditText digit : digits) {
 | 
			
		||||
            digit.setEnabled(enabled);
 | 
			
		||||
            digit.setCursorVisible(enabled);
 | 
			
		||||
            digit.setFocusable(enabled);
 | 
			
		||||
            digit.setFocusableInTouchMode(enabled);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isEmpty() {
 | 
			
		||||
        for (EditText digit : digits) {
 | 
			
		||||
            if (digit.getText().length() > 0) {
 | 
			
		||||
                return false;
 | 
			
		||||
            }
 | 
			
		||||
| 
						 | 
				
			
			@ -127,12 +137,12 @@ public class PinEntryWrapper {
 | 
			
		|||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static boolean isPin(CharSequence pin) {
 | 
			
		||||
    public static boolean isValidPin(CharSequence pin) {
 | 
			
		||||
        return pin != null && PIN_STRING_PATTERN.matcher(pin).matches();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void clear() {
 | 
			
		||||
        for(int i = digits.size() - 1; i >= 0; --i) {
 | 
			
		||||
        for (int i = digits.size() - 1; i >= 0; --i) {
 | 
			
		||||
            digits.get(i).getText().clear();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -45,7 +45,7 @@
 | 
			
		|||
                        <TextView
 | 
			
		||||
                            android:layout_width="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" />
 | 
			
		||||
                    </LinearLayout>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -150,13 +150,12 @@
 | 
			
		|||
 | 
			
		||||
                    <Button
 | 
			
		||||
                        android:id="@+id/next"
 | 
			
		||||
                        style="@style/Widget.Conversations.Button.Borderless"
 | 
			
		||||
                        style="@style/Widget.Conversations.Button.Borderless.Primary"
 | 
			
		||||
                        android:layout_width="wrap_content"
 | 
			
		||||
                        android:layout_height="wrap_content"
 | 
			
		||||
                        android:layout_alignParentEnd="true"
 | 
			
		||||
                        android:layout_alignParentBottom="true"
 | 
			
		||||
                        android:text="@string/next"
 | 
			
		||||
                        android:textColor="?colorAccent" />
 | 
			
		||||
                        android:text="@string/next"/>
 | 
			
		||||
 | 
			
		||||
                    <Button
 | 
			
		||||
                        android:id="@+id/back"
 | 
			
		||||
| 
						 | 
				
			
			@ -177,6 +176,14 @@
 | 
			
		|||
                        android:layout_centerHorizontal="true"
 | 
			
		||||
                        android:text="@string/resend_sms"
 | 
			
		||||
                        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>
 | 
			
		||||
            </ScrollView>
 | 
			
		||||
        </android.support.design.widget.CoordinatorLayout>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue