-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Feature Flag for Autofill Service
- Loading branch information
1 parent
5d63aa5
commit 1552747
Showing
8 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...utofill-impl/src/main/java/com/duckduckgo/autofill/impl/service/AutofillServiceFeature.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,41 @@ | ||
/* | ||
* Copyright (c) 2025 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.service | ||
|
||
import com.duckduckgo.anvil.annotations.ContributesRemoteFeature | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.feature.toggles.api.Toggle | ||
import com.duckduckgo.feature.toggles.api.Toggle.InternalAlwaysEnabled | ||
|
||
@ContributesRemoteFeature( | ||
scope = AppScope::class, | ||
featureName = "autofillService", | ||
) | ||
interface AutofillServiceFeature { | ||
|
||
@Toggle.DefaultValue(false) | ||
@InternalAlwaysEnabled | ||
fun self(): Toggle | ||
|
||
@Toggle.DefaultValue(false) | ||
@InternalAlwaysEnabled | ||
fun canUpdateAppToDomainDataset(): Toggle | ||
|
||
@Toggle.DefaultValue(false) | ||
@InternalAlwaysEnabled | ||
fun canMapAppToDomain(): Toggle | ||
} |
97 changes: 97 additions & 0 deletions
97
...pl/src/main/java/com/duckduckgo/autofill/impl/service/AutofillServiceLifecycleObserver.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,97 @@ | ||
/* | ||
* Copyright (c) 2025 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.service | ||
|
||
import android.content.ComponentName | ||
import android.content.Context | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER | ||
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED | ||
import android.content.pm.PackageManager.DONT_KILL_APP | ||
import androidx.lifecycle.LifecycleOwner | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
@ContributesMultibinding( | ||
scope = AppScope::class, | ||
boundType = MainProcessLifecycleObserver::class, | ||
) | ||
class AutofillServiceLifecycleObserver @Inject constructor( | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val context: Context, | ||
private val autofillServiceFeature: AutofillServiceFeature, | ||
) : MainProcessLifecycleObserver { | ||
|
||
override fun onCreate(owner: LifecycleOwner) { | ||
super.onCreate(owner) | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
runCatching { | ||
val currentState = getAutofillServiceState(context).toBoolean() | ||
autofillServiceFeature.self().isEnabled().let { remoteState -> | ||
if (currentState != remoteState) { | ||
Timber.d("DDGAutofillService: Updating state to $remoteState") | ||
newState(context, remoteState) | ||
} | ||
} | ||
}.onFailure { | ||
Timber.e("DDGAutofillService: Failed to update Service state: $it") | ||
} | ||
} | ||
} | ||
|
||
private fun getAutofillServiceState(context: Context): Int { | ||
val pm = context.packageManager | ||
val autofillServiceComponent = ComponentName(context, RealAutofillService::class.java) | ||
return pm.getComponentEnabledSetting(autofillServiceComponent) | ||
} | ||
|
||
private fun newState( | ||
context: Context, | ||
isEnabled: Boolean, | ||
) { | ||
val pm = context.packageManager | ||
val autofillServiceComponent = ComponentName(context, RealAutofillService::class.java) | ||
|
||
val value = when (isEnabled) { | ||
true -> COMPONENT_ENABLED_STATE_ENABLED | ||
false -> COMPONENT_ENABLED_STATE_DISABLED | ||
} | ||
|
||
pm.setComponentEnabledSetting(autofillServiceComponent, value, DONT_KILL_APP) | ||
} | ||
|
||
private fun Int.toBoolean(): Boolean? { | ||
return when (this) { | ||
COMPONENT_ENABLED_STATE_DEFAULT -> false // this is the current value in Manifest | ||
COMPONENT_ENABLED_STATE_ENABLED -> true | ||
COMPONENT_ENABLED_STATE_DISABLED -> false | ||
COMPONENT_ENABLED_STATE_DISABLED_USER -> true | ||
COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED -> true | ||
else -> null | ||
} | ||
} | ||
} |
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
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
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