attempt to automatically detect pins in clipboard

This commit is contained in:
Daniel Gultsch 2018-10-20 23:38:03 +02:00
parent 5695236838
commit 6d6278002a
5 changed files with 295 additions and 147 deletions

View File

@ -764,4 +764,5 @@
<string name="please_enter_pin">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>
</resources>

View File

@ -1,12 +1,19 @@
package eu.siacs.conversations.ui;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
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;
@ -14,25 +21,36 @@ import eu.siacs.conversations.ui.util.PinEntryWrapper;
import eu.siacs.conversations.utils.AccountUtils;
import eu.siacs.conversations.utils.PhoneNumberUtilWrapper;
public class VerifyActivity extends XmppActivity {
import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;
public class VerifyActivity extends XmppActivity implements ClipboardManager.OnPrimaryClipChangedListener {
private ActivityVerifyBinding binding;
private Account account;
private PinEntryWrapper pinEntryWrapper;
private ClipboardManager clipboardManager;
private String pasted = null;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String pin = savedInstanceState != null ? savedInstanceState.getString("pin") : null;
this.pasted = savedInstanceState != null ? savedInstanceState.getString("pasted") : null;
this.binding = DataBindingUtil.setContentView(this, R.layout.activity_verify);
setSupportActionBar((Toolbar) this.binding.toolbar);
this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox);
if (pin != null) {
this.pinEntryWrapper.setPin(pin);
}
binding.back.setOnClickListener(this::onBackButton);
clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
}
private void onBackButton(View view) {
if (this.account != null) {
xmppConnectionService.deleteAccount(account);
Intent intent = new Intent(this,EnterPhoneNumberActivity.class);
Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
startActivity(intent);
finish();
}
@ -51,4 +69,58 @@ public class VerifyActivity extends XmppActivity {
}
this.binding.weHaveSent.setText(Html.fromHtml(getString(R.string.we_have_sent_you_an_sms, PhoneNumberUtilWrapper.prettyPhoneNumber(this, this.account.getJid()))));
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("pin", this.pinEntryWrapper.getPin());
if (this.pasted != null) {
savedInstanceState.putString("pasted", this.pasted);
}
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
clipboardManager.addPrimaryClipChangedListener(this);
}
@Override
public void onStop() {
super.onStop();
clipboardManager.removePrimaryClipChangedListener(this);
}
@Override
public void onResume() {
super.onResume();
if (pinEntryWrapper.isEmpty()) {
pastePinFromClipboard();
}
}
private void pastePinFromClipboard() {
final ClipDescription description = clipboardManager != null ? clipboardManager.getPrimaryClipDescription() : null;
if (description != null && description.hasMimeType(MIMETYPE_TEXT_PLAIN)) {
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)) {
this.pasted = clip.toString();
pinEntryWrapper.setPin(clip.toString());
final Snackbar snackbar = Snackbar.make(binding.coordinator, R.string.possible_pin, Snackbar.LENGTH_LONG);
snackbar.setAction(R.string.undo, v -> pinEntryWrapper.clear());
snackbar.show();
}
}
}
}
@Override
public void onPrimaryClipChanged() {
this.pasted = null;
if (pinEntryWrapper.isEmpty()) {
pastePinFromClipboard();
}
}
}

View File

@ -9,9 +9,13 @@ import android.widget.LinearLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
public class PinEntryWrapper {
private static Pattern PIN_STRING_PATTERN = Pattern.compile("^[0-9]{6}$");
private final List<EditText> digits = new ArrayList<>();
private final TextWatcher textWatcher = new TextWatcher() {
@ -68,6 +72,10 @@ public class PinEntryWrapper {
return true;
}
}
if (current != 0) {
digits.get(0).requestFocus();
return true;
}
}
}
return false;
@ -90,4 +98,42 @@ public class PinEntryWrapper {
}
}
public String getPin() {
char[] chars = new char[digits.size()];
for(int i = 0; i < chars.length; ++i) {
final String input = digits.get(i).getText().toString();
chars[i] = input.length() != 1 ? ' ' : input.charAt(0);
}
return String.valueOf(chars);
}
public void setPin(String pin) {
char[] chars = pin.toCharArray();
for(int i = 0; i < digits.size(); ++i) {
if (i < chars.length) {
final Editable editable = digits.get(i).getText();
editable.clear();
editable.append(Character.isDigit(chars[i]) ? String.valueOf(chars[i]) : "");
}
}
}
public boolean isEmpty() {
for(EditText digit : digits) {
if (digit.getText().length() > 0) {
return false;
}
}
return true;
}
public static boolean isPin(CharSequence pin) {
return pin != null && PIN_STRING_PATTERN.matcher(pin).matches();
}
public void clear() {
for(int i = digits.size() - 1; i >= 0; --i) {
digits.get(i).getText().clear();
}
}
}

View File

@ -7,6 +7,10 @@
android:orientation="vertical">
<include android:id="@+id/toolbar" layout="@layout/toolbar" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
@ -36,12 +40,13 @@
android:orientation="vertical">
<EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/country"
style="@style/Widget.Conversations.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cursorVisible="false"
android:drawableRight="@drawable/ic_arrow_drop_down_black_18dp"
android:drawableEnd="@drawable/ic_arrow_drop_down_black_18dp"
android:focusable="false"
android:gravity="bottom|center_horizontal"
android:longClickable="false" />
@ -52,6 +57,7 @@
android:orientation="horizontal">
<EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/country_code"
style="@style/Widget.Conversations.EditText"
android:layout_width="0dp"
@ -64,6 +70,7 @@
android:maxLines="1" />
<EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/number"
style="@style/Widget.Conversations.EditText"
android:layout_width="0dp"
@ -87,5 +94,6 @@
android:textColor="?colorAccent"/>
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
</layout>

View File

@ -9,155 +9,176 @@
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"/>
<ScrollView
layout="@layout/toolbar" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<TextView
android:id="@+id/we_have_sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/we_have_sent_you_an_sms"
android:textAppearance="@style/TextAppearance.Conversations.Subhead" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_enter_pin"
android:textAppearance="@style/TextAppearance.Conversations.Body1" />
</LinearLayout>
<LinearLayout
android:id="@+id/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<LinearLayout
android:id="@+id/pin_box"
android:layout_width="230sp"
android:layout_height="wrap_content"
android:layout_below="@+id/instructions"
android:layout_centerHorizontal="true">
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"/>
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:inputType="number"
android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false"
/>
</LinearLayout>
<Button
android:id="@+id/next"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next"
android:textColor="?colorAccent"/>
<TextView
android:id="@+id/we_have_sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/we_have_sent_you_an_sms"
android:textAppearance="@style/TextAppearance.Conversations.Subhead" />
<Button
android:id="@+id/back"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back"
android:textColor="?android:textColorSecondary" />
<Button
android:layout_centerHorizontal="true"
android:layout_below="@+id/pin_box"
android:id="@+id/resend_sms"
style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/resend_sms"
android:textColor="?android:textColorSecondary"/>
</RelativeLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/please_enter_pin"
android:textAppearance="@style/TextAppearance.Conversations.Body1" />
</LinearLayout>
<LinearLayout
android:id="@+id/pin_box"
android:layout_width="230sp"
android:layout_height="wrap_content"
android:layout_below="@+id/instructions"
android:layout_centerHorizontal="true">
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<EditText
android:layout_width="35sp"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number"
android:maxLength="1"
android:textIsSelectable="false"
android:textSize="40sp" />
</LinearLayout>
<Button
android:id="@+id/next"
style="@style/Widget.Conversations.Button.Borderless"
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" />
<Button
android:id="@+id/back"
style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="@string/back"
android:textColor="?android:textColorSecondary" />
<Button
android:id="@+id/resend_sms"
style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pin_box"
android:layout_centerHorizontal="true"
android:text="@string/resend_sms"
android:textColor="?android:textColorSecondary" />
</RelativeLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
</layout>