-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect System AutofillService usage within WebView (#3643)
<!-- Note: This checklist is a reminder of our shared engineering expectations. The items in Bold are required If your PR involves UI changes: 1. Upload screenshots or screencasts that illustrate the changes before / after 2. Add them under the UI changes section (feel free to add more columns if needed) If your PR does not involve UI changes, you can remove the **UI changes** section At a minimum, make sure your changes are tested in API 23 and one of the more recent API levels available. --> Task/Issue URL: https://app.asana.com/0/488551667048375/1205669280706362/f ### Description Detect system-level `AutofillService` autofill event to gain insights into usage. ### Steps to test this PR - [ ] Ensure you have a system-level Autofill provider set up (search for `Autofill` in system settings) - [ ] Visit https://fill.dev/form/registration-email and tap on the email field - [ ] Choose to autofill using the system-level provider, which will either be an inline suggestion on the form field or a button at the top of the keyboard - [ ] Verify a pixel is sent - [ ] Repeat, and verify the pixel is not sent (it's limited to once per day) - [ ] Set system clock one day forward and repeat; verify pixel is sent
- Loading branch information
Showing
5 changed files
with
142 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...pi/src/main/java/com/duckduckgo/autofill/api/systemautofill/SystemAutofillUsageMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.api.systemautofill | ||
|
||
/** | ||
* Interface for monitoring system autofill usage | ||
*/ | ||
interface SystemAutofillUsageMonitor { | ||
|
||
/** | ||
* Used to notify that the user has used system AutofillService to fill in any type of data into a form in the WebView | ||
*/ | ||
fun onSystemAutofillUsed() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...c/main/java/com/duckduckgo/autofill/impl/systemautofill/RealSystemAutofillUsageMonitor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.systemautofill | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.global.DispatcherProvider | ||
import com.duckduckgo.app.statistics.pixels.Pixel | ||
import com.duckduckgo.autofill.api.systemautofill.SystemAutofillUsageMonitor | ||
import com.duckduckgo.autofill.impl.pixel.AutofillPixelNames | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import org.threeten.bp.Instant | ||
import org.threeten.bp.ZoneOffset | ||
import org.threeten.bp.format.DateTimeFormatter | ||
|
||
@SingleInstanceIn(AppScope::class) | ||
@ContributesBinding(AppScope::class) | ||
class RealSystemAutofillUsageMonitor @Inject constructor( | ||
private val pixel: Pixel, | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatchers: DispatcherProvider, | ||
private val context: Context, | ||
) : SystemAutofillUsageMonitor { | ||
|
||
private val preferences: SharedPreferences by lazy { | ||
context.getSharedPreferences(FILENAME, Context.MODE_PRIVATE) | ||
} | ||
|
||
override fun onSystemAutofillUsed() { | ||
appCoroutineScope.launch(dispatchers.io()) { | ||
tryToFireDailyPixel(AutofillPixelNames.SYSTEM_AUTOFILL_USED.pixelName) | ||
} | ||
} | ||
|
||
private fun tryToFireDailyPixel(pixelName: String) { | ||
val now = getUtcIsoLocalDate() | ||
val timestamp = preferences.getString(pixelName.appendTimestampSuffix(), null) | ||
|
||
if (canFirePixelToday(timestamp, now)) { | ||
pixel.fire(pixelName).also { preferences.edit { putString(pixelName.appendTimestampSuffix(), now) } } | ||
} | ||
} | ||
|
||
private fun canFirePixelToday( | ||
timestamp: String?, | ||
now: String, | ||
): Boolean { | ||
return timestamp == null || now > timestamp | ||
} | ||
|
||
private fun String.appendTimestampSuffix(): String = "${this}_timestamp" | ||
|
||
/** | ||
* returns today's date in YYYY-MM-dd format | ||
*/ | ||
private fun getUtcIsoLocalDate(): String { | ||
return Instant.now().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_LOCAL_DATE) | ||
} | ||
|
||
companion object { | ||
private const val FILENAME = "com.duckduckgo.autofill.impl.systemautofill.RealSystemAutofillUsageMonitor" | ||
} | ||
} |