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="please_enter_pin">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>
</resources> </resources>

View file

@ -1,12 +1,19 @@
package eu.siacs.conversations.ui; 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.content.Intent;
import android.databinding.DataBindingUtil; import android.databinding.DataBindingUtil;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar; 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;
@ -14,25 +21,36 @@ 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;
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 ActivityVerifyBinding binding;
private Account account; private Account account;
private PinEntryWrapper pinEntryWrapper; private PinEntryWrapper pinEntryWrapper;
private ClipboardManager clipboardManager;
private String pasted = null;
@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;
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);
this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox); this.pinEntryWrapper = new PinEntryWrapper(binding.pinBox);
if (pin != null) {
this.pinEntryWrapper.setPin(pin);
}
binding.back.setOnClickListener(this::onBackButton); binding.back.setOnClickListener(this::onBackButton);
clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
} }
private void onBackButton(View view) { private void onBackButton(View view) {
if (this.account != null) { if (this.account != null) {
xmppConnectionService.deleteAccount(account); xmppConnectionService.deleteAccount(account);
Intent intent = new Intent(this,EnterPhoneNumberActivity.class); Intent intent = new Intent(this, EnterPhoneNumberActivity.class);
startActivity(intent); startActivity(intent);
finish(); 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())))); 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern;
public class PinEntryWrapper { public class PinEntryWrapper {
private static Pattern PIN_STRING_PATTERN = Pattern.compile("^[0-9]{6}$");
private final List<EditText> digits = new ArrayList<>(); private final List<EditText> digits = new ArrayList<>();
private final TextWatcher textWatcher = new TextWatcher() { private final TextWatcher textWatcher = new TextWatcher() {
@ -68,6 +72,10 @@ public class PinEntryWrapper {
return true; return true;
} }
} }
if (current != 0) {
digits.get(0).requestFocus();
return true;
}
} }
} }
return false; 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"> android:orientation="vertical">
<include android:id="@+id/toolbar" layout="@layout/toolbar" /> <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 <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
@ -36,12 +40,13 @@
android:orientation="vertical"> android:orientation="vertical">
<EditText <EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/country" android:id="@+id/country"
style="@style/Widget.Conversations.EditText" style="@style/Widget.Conversations.EditText"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:cursorVisible="false" 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:focusable="false"
android:gravity="bottom|center_horizontal" android:gravity="bottom|center_horizontal"
android:longClickable="false" /> android:longClickable="false" />
@ -52,6 +57,7 @@
android:orientation="horizontal"> android:orientation="horizontal">
<EditText <EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/country_code" android:id="@+id/country_code"
style="@style/Widget.Conversations.EditText" style="@style/Widget.Conversations.EditText"
android:layout_width="0dp" android:layout_width="0dp"
@ -64,6 +70,7 @@
android:maxLines="1" /> android:maxLines="1" />
<EditText <EditText
android:imeOptions="flagNoExtractUi"
android:id="@+id/number" android:id="@+id/number"
style="@style/Widget.Conversations.EditText" style="@style/Widget.Conversations.EditText"
android:layout_width="0dp" android:layout_width="0dp"
@ -87,5 +94,6 @@
android:textColor="?colorAccent"/> android:textColor="?colorAccent"/>
</RelativeLayout> </RelativeLayout>
</ScrollView> </ScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout> </LinearLayout>
</layout> </layout>

View file

@ -9,11 +9,18 @@
<include <include
android:id="@+id/toolbar" android:id="@+id/toolbar"
layout="@layout/toolbar"/> layout="@layout/toolbar" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:fillViewport="true"> android:fillViewport="true">
<RelativeLayout <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content">
@ -48,116 +55,130 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/instructions" android:layout_below="@+id/instructions"
android:layout_centerHorizontal="true"> android:layout_centerHorizontal="true">
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"/> android:layout_weight="1" />
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"/> android:layout_weight="1" />
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"/> android:layout_weight="1" />
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"/> android:layout_weight="1" />
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
<Space <Space
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1"/> android:layout_weight="1" />
<EditText <EditText
android:layout_width="35sp" android:layout_width="35sp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:digits="1234567890" android:digits="1234567890"
android:gravity="center"
android:imeOptions="flagNoExtractUi"
android:inputType="number" android:inputType="number"
android:maxLength="1" android:maxLength="1"
android:textSize="40sp"
android:gravity="center"
android:textIsSelectable="false" android:textIsSelectable="false"
/> android:textSize="40sp" />
</LinearLayout> </LinearLayout>
<Button <Button
android:id="@+id/next" android:id="@+id/next"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
style="@style/Widget.Conversations.Button.Borderless" style="@style/Widget.Conversations.Button.Borderless"
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_alignParentBottom="true"
android:text="@string/next" android:text="@string/next"
android:textColor="?colorAccent"/> android:textColor="?colorAccent" />
<Button <Button
android:id="@+id/back" android:id="@+id/back"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
style="@style/Widget.Conversations.Button.Borderless" style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:text="@string/back" android:text="@string/back"
android:textColor="?android:textColorSecondary" /> android:textColor="?android:textColorSecondary" />
<Button <Button
android:layout_centerHorizontal="true"
android:layout_below="@+id/pin_box"
android:id="@+id/resend_sms" android:id="@+id/resend_sms"
style="@style/Widget.Conversations.Button.Borderless" style="@style/Widget.Conversations.Button.Borderless"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_below="@+id/pin_box"
android:layout_centerHorizontal="true"
android:text="@string/resend_sms" android:text="@string/resend_sms"
android:textColor="?android:textColorSecondary"/> android:textColor="?android:textColorSecondary" />
</RelativeLayout> </RelativeLayout>
</ScrollView> </ScrollView>
</android.support.design.widget.CoordinatorLayout>
</LinearLayout> </LinearLayout>
</layout> </layout>