Browse Source

working on autofill

minhaj 1 week ago
parent
commit
7810113587

+ 36 - 37
app/src/main/java/com/fastest/pass/autofill/FastestPassAutofillService.kt

@@ -10,6 +10,7 @@ import android.service.autofill.FillResponse
 import android.view.autofill.AutofillId
 import android.view.autofill.AutofillValue
 import android.widget.RemoteViews
+import com.fastest.pass.R
 import com.fastest.pass.home.domain.model.AddPassword
 import com.fastest.pass.sharedpref.CredentialManager
 
@@ -32,9 +33,9 @@ class FastestPassAutofillService : AutofillService() {
         }
 
         // Fetch credentials matching the package name or domain
-       /* val data = getCredentials().filter {
-            it.url.contains(packageName)
-        }*/
+        /* val data = getCredentials().filter {
+             it.url.contains(packageName)
+         }*/
         val data = getCredentials()
 
         if (data.isEmpty()) {
@@ -46,48 +47,48 @@ class FastestPassAutofillService : AutofillService() {
         for (i in 0 until structure.windowNodeCount) {
             val windowNode = structure.getWindowNodeAt(i)
             val viewNode = windowNode.rootViewNode
-         parseViewNode(viewNode, data, fillResponseBuilder)
+            parseViewNode(viewNode, data, fillResponseBuilder)
 
         }
 
-    callback.onSuccess(fillResponseBuilder.build())
+        callback.onSuccess(fillResponseBuilder.build())
     }
 
     override fun onSaveRequest(
         request: android.service.autofill.SaveRequest,
         callback: android.service.autofill.SaveCallback
     ) {
-      /*  val credentialManager = CredentialManager(applicationContext)
+        /*  val credentialManager = CredentialManager(applicationContext)
 
-        // Extracting the FillContexts from the SaveRequest
-        val fillContexts = request.fillContexts
+          // Extracting the FillContexts from the SaveRequest
+          val fillContexts = request.fillContexts
 
-        // For each field in the SaveRequest, extract the autofill values (email and password)
-        val credentials = mutableListOf<AddPassword>()
+          // For each field in the SaveRequest, extract the autofill values (email and password)
+          val credentials = mutableListOf<AddPassword>()
 
-        for (context in fillContexts) {
-            val structure = context.structure
+          for (context in fillContexts) {
+              val structure = context.structure
 
-            // Loop through the views in the structure and extract the autofill values
-            for (i in 0 until structure.windowNodeCount) {
-                val windowNode = structure.getWindowNodeAt(i)
-                val rootNode = windowNode.rootViewNode
+              // Loop through the views in the structure and extract the autofill values
+              for (i in 0 until structure.windowNodeCount) {
+                  val windowNode = structure.getWindowNodeAt(i)
+                  val rootNode = windowNode.rootViewNode
 
-                // Extract the values from the autofill fields
-                val title = extractAutofillValue(rootNode, "personFirstName") ?: ""
-                val url = extractAutofillValue(rootNode, "personLastName") ?: ""
-                val username = extractAutofillValue(rootNode, "username") ?: ""
-                val password = extractAutofillValue(rootNode, "password") ?: ""
-                val notes = extractAutofillValue(rootNode, "addressStreet") ?: ""
+                  // Extract the values from the autofill fields
+                  val title = extractAutofillValue(rootNode, "personFirstName") ?: ""
+                  val url = extractAutofillValue(rootNode, "personLastName") ?: ""
+                  val username = extractAutofillValue(rootNode, "username") ?: ""
+                  val password = extractAutofillValue(rootNode, "password") ?: ""
+                  val notes = extractAutofillValue(rootNode, "addressStreet") ?: ""
 
-                // Create a new Credential object and add it to the list
-                val credential = AddPassword(title, url, username, password, notes)
-                credentials.add(credential)
-            }
-        }
+                  // Create a new Credential object and add it to the list
+                  val credential = AddPassword(title, url, username, password, notes)
+                  credentials.add(credential)
+              }
+          }
 
-        // Save the credentials in SharedPreferences
-        credentialManager.saveCredentials(credentials)*/
+          // Save the credentials in SharedPreferences
+          credentialManager.saveCredentials(credentials)*/
 
         callback.onSuccess()
     }
@@ -132,13 +133,11 @@ class FastestPassAutofillService : AutofillService() {
 
             if (username != null || emailAddress != null || passwordId != null) {
                 credentials.forEach { credential ->
-                    val presentation =
-                        RemoteViews(packageName, android.R.layout.simple_list_item_1).apply {
-                            setTextViewText(
-                                android.R.id.text1,
-                                "${credential.username} @ ${credential.url}"
-                            )
-                        }
+                    val presentation = RemoteViews(packageName, R.layout.item_auto_fill).apply {
+                        // Set the TextView text for the suggestion
+                        setTextViewText(R.id.text, "${credential.username} @ ${credential.url}")
+                        // Optionally, set an icon/image
+                    }
                     val datasetBuilder = Dataset.Builder()
                     emailAddress?.let {
                         datasetBuilder.setValue(
@@ -170,7 +169,7 @@ class FastestPassAutofillService : AutofillService() {
 
         // Recursively parse child nodes
         for (i in 0 until node.childCount) {
-           parseViewNode(node.getChildAt(i), credentials, fillResponseBuilder)
+            parseViewNode(node.getChildAt(i), credentials, fillResponseBuilder)
         }
     }
 

+ 3 - 4
app/src/main/java/com/fastest/pass/login/presentation/ui/components/LoginScreen.kt

@@ -98,13 +98,11 @@ fun LoginScreen(
     var passwordLogin by remember { mutableStateOf("") }
     val snackBarStateRed = remember { SnackbarHostState() }
 
-
     LaunchedEffect(true) {
         emailLogin = "eng.asix@gmail.com"
         passwordLogin = "password"
     }
 
-
     Box(
         modifier = Modifier
             .background(colorResource(id = R.color.blue_login))
@@ -145,8 +143,8 @@ fun LoginScreen(
                 LoginTextField(
                     emailLogin,
                     keyboardController, focusManager, onEmailText = {
-                    emailLogin = it
-                })
+                        emailLogin = it
+                    })
                 Spacer(modifier = Modifier.height(20.dp))
                 PasswordTextField(passwordLogin,keyboardController, focusManager, onPasswordText = {
                     passwordLogin = it
@@ -268,6 +266,7 @@ fun Modifier.autoFill(
         }
 }
 
+@OptIn(ExperimentalComposeUiApi::class)
 @Composable
 fun ColumnScope.LoginTextField(
     emailText:String,

+ 20 - 2
app/src/main/res/layout/item_auto_fill.xml

@@ -1,7 +1,25 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:orientation="vertical"
+    android:orientation="horizontal"
+    android:padding="2dp"
     android:layout_width="match_parent"
-    android:layout_height="match_parent">
+    android:layout_height="wrap_content">
+
+    <ImageView
+        android:id="@+id/icon"
+        android:layout_width="48dp"
+        android:layout_height="48dp"
+        android:layout_marginEnd="16dp"
+        android:src="@mipmap/ic_launcher" />
+
+    <TextView
+        android:id="@+id/text"
+        android:text="Username"
+        android:textSize="16sp"
+        android:layout_width="0dp"
+        android:layout_gravity="center_vertical"
+        android:layout_height="wrap_content"
+        android:layout_weight="1"
+        android:gravity="start" />
 
 </LinearLayout>

+ 1 - 1
app/src/main/res/xml/autofill_service_configuration.xml

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <autofill-service xmlns:android="http://schemas.android.com/apk/res/android"
-    android:settingsActivity=".MainActivity"
+    android:settingsActivity=".SettingsActivity"
     android:summary="@string/about"
     android:description="@string/account_number" />