Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import android.service.autofill.FillCallback
import android.service.autofill.FillRequest
import android.service.autofill.FillResponse
import android.service.autofill.SaveCallback
import android.service.autofill.SaveInfo
import android.service.autofill.SaveRequest
import android.view.autofill.AutofillValue
import android.widget.RemoteViews
import android.widget.Toast
import com.jeeldobariya.passcodes.database.master.PasswordEntity
import com.jeeldobariya.passcodes.database.master.PasswordsDao
import kotlinx.coroutines.CoroutineScope
Expand All @@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject

// TODO: currently the code here serves as a foundation for autofill features..
// TODO: Docs for this autofill is need in @github:PasscodesApp/Passcodes-Docs
class PasswordAutofillService : AutofillService() {

private val serviceScope = CoroutineScope(Dispatchers.IO)
Expand All @@ -35,6 +35,8 @@ class PasswordAutofillService : AutofillService() {
val viewNodes = mutableMapOf<String, AssistStructure.ViewNode>()
parseStructure(structure.getWindowNodeAt(0).rootViewNode, viewNodes)

// TODO: Add support for newUsername & newPassword autofill hints.
// https://developer.android.com/reference/androidx/autofill/HintConstants#AUTOFILL_HINT_NEW_USERNAME()
val usernameNode = viewNodes["username"] ?: viewNodes["emailAddress"]
val passwordNode = viewNodes["password"]

Expand All @@ -47,16 +49,10 @@ class PasswordAutofillService : AutofillService() {
val passwordId = passwordNode.autofillId!!

cancellationSignal.setOnCancelListener {
// Handle cancellation
// TODO: Handle cancellation
}

serviceScope.launch {
Toast.makeText(
applicationContext,
"Passcodes autofill is preview feature!!!",
Toast.LENGTH_LONG
).show()

val passwordsDao by inject<PasswordsDao>()
val passwords = passwordsDao.getAllPasswords().first()
val responseBuilder = FillResponse.Builder()
Expand All @@ -73,13 +69,22 @@ class PasswordAutofillService : AutofillService() {
)
}

// TODO: Migrate to Presentations
// @docs:developer.android.com/reference/android/service/autofill/Presentations.Builder
val dataset = android.service.autofill.Dataset.Builder(presentation)
.setValue(usernameId, AutofillValue.forText(password.username))
.setValue(passwordId, AutofillValue.forText(password.password))
.build()
responseBuilder.addDataset(dataset)
}

responseBuilder.setSaveInfo(
SaveInfo.Builder(
SaveInfo.SAVE_DATA_TYPE_EMAIL_ADDRESS or SaveInfo.SAVE_DATA_TYPE_USERNAME or SaveInfo.SAVE_DATA_TYPE_PASSWORD,
arrayOf(usernameId, passwordId)
).build()
)

callback.onSuccess(responseBuilder.build())
}
}
Expand All @@ -91,6 +96,7 @@ class PasswordAutofillService : AutofillService() {
val viewNodes = mutableMapOf<String, AssistStructure.ViewNode>()
parseStructure(structure.getWindowNodeAt(0).rootViewNode, viewNodes)

// TODO: Add support for newUsername & newPassword autofill hints.
val usernameNode = viewNodes["username"] ?: viewNodes["emailAddress"]
val passwordNode = viewNodes["password"]

Expand All @@ -108,11 +114,6 @@ class PasswordAutofillService : AutofillService() {
notes = "Save using autofill service..."
)
)
Toast.makeText(
applicationContext,
"Open passcodes app and configure the saved password!!!",
Toast.LENGTH_LONG
).show()
}

callback.onSuccess()
Expand Down
Loading