Refactoring cc names

This commit is contained in:
J-Jamet
2021-07-20 15:53:02 +02:00
parent ea7221c39a
commit bef179187f
3 changed files with 57 additions and 73 deletions

View File

@@ -129,26 +129,19 @@ object AutofillHelper {
// get year (year in database entry is stored as String in the format YY) // get year (year in database entry is stored as String in the format YY)
val year = entryInfo.expiryTime.getYearInt() val year = entryInfo.expiryTime.getYearInt()
struct.ccExpDateId?.let { struct.creditCardExpirationDateId?.let {
if (struct.isWebView) { if (struct.isWebView) {
// set date string as defined in https://html.spec.whatwg.org // set date string as defined in https://html.spec.whatwg.org
val dateString = "$year\u002D$month" builder.setValue(it, AutofillValue.forText("$year\u002D$month"))
builder.setValue(it, AutofillValue.forText(dateString))
} else { } else {
val calendar = Calendar.getInstance() builder.setValue(it, AutofillValue.forDate(entryInfo.expiryTime.date.time))
calendar.clear()
calendar[Calendar.YEAR] = year
// Month value is 0-based. e.g., 0 for January
calendar[Calendar.MONTH] = month - 1
val date = calendar.timeInMillis
builder.setValue(it, AutofillValue.forDate(date))
} }
} }
struct.ccExpDateMonthId?.let { struct.creditCardExpirationMonthId?.let {
if (struct.isWebView) { if (struct.isWebView) {
builder.setValue(it, AutofillValue.forText(month.toString())) builder.setValue(it, AutofillValue.forText(month.toString()))
} else { } else {
if (struct.ccExpMonthOptions != null) { if (struct.creditCardExpirationMonthOptions != null) {
// index starts at 0 // index starts at 0
builder.setValue(it, AutofillValue.forList(month - 1)) builder.setValue(it, AutofillValue.forList(month - 1))
} else { } else {
@@ -156,10 +149,10 @@ object AutofillHelper {
} }
} }
} }
struct.ccExpDateYearId?.let { struct.creditCardExpirationYearId?.let {
var autofillValue: AutofillValue? = null var autofillValue: AutofillValue? = null
struct.ccExpYearOptions?.let { options -> struct.creditCardExpirationYearOptions?.let { options ->
var yearIndex = options.indexOf(year.toString().substring(0, 2)) var yearIndex = options.indexOf(year.toString().substring(0, 2))
if (yearIndex == -1) { if (yearIndex == -1) {
@@ -178,17 +171,17 @@ object AutofillHelper {
} }
for (field in entryInfo.customFields) { for (field in entryInfo.customFields) {
if (field.name == TemplateField.LABEL_HOLDER) { if (field.name == TemplateField.LABEL_HOLDER) {
struct.ccNameId?.let { ccNameId -> struct.creditCardHolderId?.let { ccNameId ->
builder.setValue(ccNameId, AutofillValue.forText(field.protectedValue.stringValue)) builder.setValue(ccNameId, AutofillValue.forText(field.protectedValue.stringValue))
} }
} }
if (field.name == TemplateField.LABEL_NUMBER) { if (field.name == TemplateField.LABEL_NUMBER) {
struct.ccnId?.let { ccnId -> struct.creditCardNumberId?.let { ccnId ->
builder.setValue(ccnId, AutofillValue.forText(field.protectedValue.stringValue)) builder.setValue(ccnId, AutofillValue.forText(field.protectedValue.stringValue))
} }
} }
if (field.name == TemplateField.LABEL_CVV) { if (field.name == TemplateField.LABEL_CVV) {
struct.cvvId?.let { cvvId -> struct.cardVerificationValueId?.let { cvvId ->
builder.setValue(cvvId, AutofillValue.forText(field.protectedValue.stringValue)) builder.setValue(cvvId, AutofillValue.forText(field.protectedValue.stringValue))
} }
} }

View File

@@ -110,15 +110,6 @@ class KeeAutofillService : AutofillService() {
} }
} }
} }
// TODO does it make sense to disable autofill here? how long?
// else {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
// val builder = FillResponse.Builder()
// // disable for a while (duration in ms)
// builder.disableAutofill(5*60*1000)
// callback.onSuccess(builder.build())
// }
// }
} }
} }
} }
@@ -191,16 +182,16 @@ class KeeAutofillService : AutofillService() {
} }
// or a credit card form // or a credit card form
if (requiredIds.isEmpty()) { if (requiredIds.isEmpty()) {
parseResult.ccnId?.let { numberId -> parseResult.creditCardNumberId?.let { numberId ->
types = types or SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD types = types or SaveInfo.SAVE_DATA_TYPE_CREDIT_CARD
requiredIds.add(numberId) requiredIds.add(numberId)
Log.d(TAG, "Asking to save credit card number") Log.d(TAG, "Asking to save credit card number")
} }
parseResult.ccExpDateId?.let { id -> optionalIds.add(id) } parseResult.creditCardExpirationDateId?.let { id -> optionalIds.add(id) }
parseResult.ccExpDateYearId?.let { id -> optionalIds.add(id) } parseResult.creditCardExpirationYearId?.let { id -> optionalIds.add(id) }
parseResult.ccExpDateMonthId?.let { id -> optionalIds.add(id) } parseResult.creditCardExpirationMonthId?.let { id -> optionalIds.add(id) }
parseResult.ccNameId?.let { id -> optionalIds.add(id) } parseResult.creditCardHolderId?.let { id -> optionalIds.add(id) }
parseResult.cvvId?.let { id -> optionalIds.add(id) } parseResult.cardVerificationValueId?.let { id -> optionalIds.add(id) }
} }
if (requiredIds.isNotEmpty()) { if (requiredIds.isNotEmpty()) {
val builder = SaveInfo.Builder(types, requiredIds.toTypedArray()) val builder = SaveInfo.Builder(types, requiredIds.toTypedArray())
@@ -260,14 +251,14 @@ class KeeAutofillService : AutofillService() {
Log.d(TAG, "autofill onSaveRequest password") Log.d(TAG, "autofill onSaveRequest password")
if (parseResult.ccExpirationValue == null) { if (parseResult.creditCardExpirationValue == null) {
if (parseResult.ccExpDateMonthValue != 0 && parseResult.ccExpDateYearValue != 0) { if (parseResult.creditCardExpirationMonthValue != 0 && parseResult.creditCardExpirationYearValue != 0) {
parseResult.ccExpirationValue = parseResult.ccExpDateMonthValue.toString().padStart(2, '0') + parseResult.ccExpDateYearValue.toString() parseResult.creditCardExpirationValue = parseResult.creditCardExpirationMonthValue.toString().padStart(2, '0') + parseResult.creditCardExpirationYearValue.toString()
} }
} }
val creditCard = CreditCard(parseResult.ccName, parseResult.ccNumber, val creditCard = CreditCard(parseResult.creditCardHolder, parseResult.creditCardNumber,
parseResult.ccExpirationValue, parseResult.cvv) parseResult.creditCardExpirationValue, parseResult.cardVerificationValue)
// Show UI to save data // Show UI to save data
val registerInfo = RegisterInfo( val registerInfo = RegisterInfo(

View File

@@ -64,7 +64,7 @@ class StructureParser(private val structure: AssistStructure) {
} }
} }
return if (result?.passwordId != null || result?.ccnId != null) return if (result?.passwordId != null || result?.creditCardNumberId != null)
result result
else else
null null
@@ -144,30 +144,30 @@ class StructureParser(private val structure: AssistStructure) {
} }
it.contains("cc-name", true) -> { it.contains("cc-name", true) -> {
Log.d(TAG, "Autofill credit card name hint") Log.d(TAG, "Autofill credit card name hint")
result?.ccNameId = autofillId result?.creditCardHolderId = autofillId
result?.ccName = node.autofillValue?.textValue?.toString() result?.creditCardHolder = node.autofillValue?.textValue?.toString()
} }
it.contains(View.AUTOFILL_HINT_CREDIT_CARD_NUMBER, true) it.contains(View.AUTOFILL_HINT_CREDIT_CARD_NUMBER, true)
|| it.contains("cc-number", true) -> { || it.contains("cc-number", true) -> {
Log.d(TAG, "Autofill credit card number hint") Log.d(TAG, "Autofill credit card number hint")
result?.ccnId = autofillId result?.creditCardNumberId = autofillId
result?.ccNumber = node.autofillValue?.textValue?.toString() result?.creditCardNumber = node.autofillValue?.textValue?.toString()
} }
// expect date string as defined in https://html.spec.whatwg.org, e.g. 2014-12 // expect date string as defined in https://html.spec.whatwg.org, e.g. 2014-12
it.contains("cc-exp", true) -> { it.contains("cc-exp", true) -> {
Log.d(TAG, "Autofill credit card expiration date hint") Log.d(TAG, "Autofill credit card expiration date hint")
result?.ccExpDateId = autofillId result?.creditCardExpirationDateId = autofillId
node.autofillValue?.let { value -> node.autofillValue?.let { value ->
if (value.isText && value.textValue.length == 7) { if (value.isText && value.textValue.length == 7) {
value.textValue.let { date -> value.textValue.let { date ->
result?.ccExpirationValue = date.substring(5, 7) + date.substring(2, 4) result?.creditCardExpirationValue = date.substring(5, 7) + date.substring(2, 4)
} }
} }
} }
} }
it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE, true) -> { it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE, true) -> {
Log.d(TAG, "Autofill credit card expiration date hint") Log.d(TAG, "Autofill credit card expiration date hint")
result?.ccExpDateId = autofillId result?.creditCardExpirationDateId = autofillId
node.autofillValue?.let { value -> node.autofillValue?.let { value ->
if (value.isDate) { if (value.isDate) {
val calendar = Calendar.getInstance() val calendar = Calendar.getInstance()
@@ -175,16 +175,16 @@ class StructureParser(private val structure: AssistStructure) {
calendar.timeInMillis = value.dateValue calendar.timeInMillis = value.dateValue
val year = calendar.get(Calendar.YEAR).toString().substring(2,4) val year = calendar.get(Calendar.YEAR).toString().substring(2,4)
val month = calendar.get(Calendar.MONTH).inc().toString().padStart(2, '0') val month = calendar.get(Calendar.MONTH).inc().toString().padStart(2, '0')
result?.ccExpirationValue = month + year result?.creditCardExpirationValue = month + year
} }
} }
} }
it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR, true) it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR, true)
|| it.contains("cc-exp-year", true) -> { || it.contains("cc-exp-year", true) -> {
Log.d(TAG, "Autofill credit card expiration year hint") Log.d(TAG, "Autofill credit card expiration year hint")
result?.ccExpDateYearId = autofillId result?.creditCardExpirationYearId = autofillId
if (node.autofillOptions != null) { if (node.autofillOptions != null) {
result?.ccExpYearOptions = node.autofillOptions result?.creditCardExpirationYearOptions = node.autofillOptions
} }
node.autofillValue?.let { value -> node.autofillValue?.let { value ->
var year = 0 var year = 0
@@ -198,15 +198,15 @@ class StructureParser(private val structure: AssistStructure) {
} catch (e: Exception) { } catch (e: Exception) {
year = 0 year = 0
} }
result?.ccExpDateYearValue = year % 100 result?.creditCardExpirationYearValue = year % 100
} }
} }
it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH, true) it.contains(View.AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH, true)
|| it.contains("cc-exp-month", true) -> { || it.contains("cc-exp-month", true) -> {
Log.d(TAG, "AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH hint") Log.d(TAG, "Autofill credit card expiration month hint")
result?.ccExpDateMonthId = autofillId result?.creditCardExpirationMonthId = autofillId
if (node.autofillOptions != null) { if (node.autofillOptions != null) {
result?.ccExpMonthOptions = node.autofillOptions result?.creditCardExpirationMonthOptions = node.autofillOptions
} }
node.autofillValue?.let { value -> node.autofillValue?.let { value ->
var month = 0 var month = 0
@@ -221,14 +221,14 @@ class StructureParser(private val structure: AssistStructure) {
// assume list starts with January (index 0) // assume list starts with January (index 0)
month = value.listValue + 1 month = value.listValue + 1
} }
result?.ccExpDateMonthValue = month result?.creditCardExpirationMonthValue = month
} }
} }
it.contains(View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE, true) it.contains(View.AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE, true)
|| it.contains("cc-csc", true) -> { || it.contains("cc-csc", true) -> {
Log.d(TAG, "AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE hint") Log.d(TAG, "Autofill card security code hint")
result?.cvvId = autofillId result?.cardVerificationValueId = autofillId
result?.cvv = node.autofillValue?.textValue?.toString() result?.cardVerificationValue = node.autofillValue?.textValue?.toString()
} }
// Ignore autocomplete="off" // Ignore autocomplete="off"
// https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion // https://developer.mozilla.org/en-US/docs/Web/Security/Securing_your_site/Turning_off_form_autocompletion
@@ -390,8 +390,8 @@ class StructureParser(private val structure: AssistStructure) {
// if the user selects the credit card expiration date from a list of options // if the user selects the credit card expiration date from a list of options
// all options are stored here // all options are stored here
var ccExpMonthOptions: Array<CharSequence>? = null var creditCardExpirationMonthOptions: Array<CharSequence>? = null
var ccExpYearOptions: Array<CharSequence>? = null var creditCardExpirationYearOptions: Array<CharSequence>? = null
var usernameId: AutofillId? = null var usernameId: AutofillId? = null
set(value) { set(value) {
@@ -405,37 +405,37 @@ class StructureParser(private val structure: AssistStructure) {
field = value field = value
} }
var ccNameId: AutofillId? = null var creditCardHolderId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
} }
var ccnId: AutofillId? = null var creditCardNumberId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
} }
var ccExpDateId: AutofillId? = null var creditCardExpirationDateId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
} }
var ccExpDateYearId: AutofillId? = null var creditCardExpirationYearId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
} }
var ccExpDateMonthId: AutofillId? = null var creditCardExpirationMonthId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
} }
var cvvId: AutofillId? = null var cardVerificationValueId: AutofillId? = null
set(value) { set(value) {
if (field == null) if (field == null)
field = value field = value
@@ -449,13 +449,13 @@ class StructureParser(private val structure: AssistStructure) {
passwordId?.let { passwordId?.let {
all.add(it) all.add(it)
} }
ccNameId?.let { creditCardHolderId?.let {
all.add(it) all.add(it)
} }
ccnId?.let { creditCardNumberId?.let {
all.add(it) all.add(it)
} }
cvvId?.let { cardVerificationValueId?.let {
all.add(it) all.add(it)
} }
return all.toTypedArray() return all.toTypedArray()
@@ -476,41 +476,41 @@ class StructureParser(private val structure: AssistStructure) {
field = value field = value
} }
var ccName: String? = null var creditCardHolder: String? = null
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value
} }
var ccNumber: String? = null var creditCardNumber: String? = null
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value
} }
// format MMYY // format MMYY
var ccExpirationValue: String? = null var creditCardExpirationValue: String? = null
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value
} }
// for year of CC expiration date: YY // for year of CC expiration date: YY
var ccExpDateYearValue = 0 var creditCardExpirationYearValue = 0
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value
} }
// for month of CC expiration date: MM // for month of CC expiration date: MM
var ccExpDateMonthValue = 0 var creditCardExpirationMonthValue = 0
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value
} }
// the security code for the credit card (also called CVV) // the security code for the credit card (also called CVV)
var cvv: String? = null var cardVerificationValue: String? = null
set(value) { set(value) {
if (allowSaveValues) if (allowSaveValues)
field = value field = value