-
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.
- Loading branch information
1 parent
333a2c5
commit 53c5f73
Showing
21 changed files
with
1,128 additions
and
24 deletions.
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
150 changes: 150 additions & 0 deletions
150
sync/sync-impl/src/main/java/com/duckduckgo/sync/impl/SyncFeatureToggle.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,150 @@ | ||
/* | ||
* 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.sync.impl | ||
|
||
import android.content.Context | ||
import androidx.core.app.NotificationManagerCompat | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.appbuildconfig.api.isInternalBuild | ||
import com.duckduckgo.common.utils.DefaultDispatcherProvider | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.feature.toggles.api.Toggle | ||
import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin | ||
import com.duckduckgo.sync.impl.engine.SyncNotificationBuilder | ||
import com.duckduckgo.sync.store.SyncStore | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
interface SyncFeatureToggle { | ||
fun showSync(): Boolean | ||
|
||
fun allowDataSyncing(): Boolean | ||
|
||
fun allowDataSyncingOnNewerVersion(): Boolean | ||
|
||
fun allowSetupFlows(): Boolean | ||
|
||
fun allowSetupFlowsOnNewerVersion(): Boolean | ||
|
||
fun allowCreateAccount(): Boolean | ||
|
||
fun allowCreateAccountOnNewerVersion(): Boolean | ||
} | ||
|
||
@ContributesBinding( | ||
scope = AppScope::class, | ||
boundType = SyncFeatureToggle::class, | ||
) | ||
@ContributesMultibinding( | ||
scope = AppScope::class, | ||
boundType = PrivacyConfigCallbackPlugin::class, | ||
) | ||
@SingleInstanceIn(AppScope::class) | ||
class SyncRemoteFeatureToggle @Inject constructor( | ||
private val context: Context, | ||
private val syncFeature: SyncFeature, | ||
private val appBuildConfig: AppBuildConfig, | ||
private val notificationManager: NotificationManagerCompat, | ||
private val syncNotificationBuilder: SyncNotificationBuilder, | ||
private val syncStore: SyncStore, | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val coroutineDispatcher: DispatcherProvider = DefaultDispatcherProvider(), | ||
) : SyncFeatureToggle, PrivacyConfigCallbackPlugin { | ||
|
||
private fun isFeatureEnabled(): Boolean { | ||
return syncFeature.self().isEnabled() | ||
} | ||
|
||
override fun showSync(): Boolean { | ||
if (appBuildConfig.isInternalBuild()) return syncFeature.level0ShowSync().isEnabled() | ||
return isFeatureEnabled() && syncFeature.level0ShowSync().isEnabled() | ||
} | ||
|
||
override fun allowDataSyncing(): Boolean { | ||
if (!showSync()) return false | ||
return syncFeature.level1AllowDataSyncing().isEnabled() | ||
} | ||
|
||
override fun allowDataSyncingOnNewerVersion(): Boolean { | ||
return isToggleEnabledOnNewerVersion(syncFeature.level1AllowDataSyncing()) | ||
} | ||
|
||
override fun allowSetupFlows(): Boolean { | ||
if (!showSync()) return false | ||
if (!syncFeature.level1AllowDataSyncing().isEnabled()) return false | ||
return syncFeature.level2AllowSetupFlows().isEnabled() | ||
} | ||
|
||
override fun allowSetupFlowsOnNewerVersion(): Boolean { | ||
return isToggleEnabledOnNewerVersion(syncFeature.level2AllowSetupFlows()) | ||
} | ||
|
||
override fun allowCreateAccount(): Boolean { | ||
if (!showSync()) return false | ||
if (!syncFeature.level1AllowDataSyncing().isEnabled()) return false | ||
if (!syncFeature.level2AllowSetupFlows().isEnabled()) return false | ||
return syncFeature.level3AllowCreateAccount().isEnabled() | ||
} | ||
|
||
override fun allowCreateAccountOnNewerVersion(): Boolean { | ||
return isToggleEnabledOnNewerVersion(syncFeature.level3AllowCreateAccount()) | ||
} | ||
|
||
private fun isToggleEnabledOnNewerVersion(toggle: Toggle): Boolean { | ||
val rawStoredState = toggle.getRawStoredState() | ||
|
||
return rawStoredState?.remoteEnableState == true && | ||
appBuildConfig.versionCode < (rawStoredState.minSupportedVersion ?: 0) | ||
} | ||
|
||
override fun onPrivacyConfigDownloaded() { | ||
appCoroutineScope.launch(coroutineDispatcher.io()) { | ||
val canSyncData = allowDataSyncing() | ||
|
||
if (!canSyncData && syncStore.syncingDataEnabled && syncStore.isSignedIn()) { | ||
triggerNotification() | ||
} | ||
|
||
if (canSyncData && !syncStore.syncingDataEnabled) { | ||
cancelNotification() | ||
} | ||
|
||
syncStore.syncingDataEnabled = canSyncData | ||
} | ||
} | ||
|
||
private fun triggerNotification() { | ||
notificationManager.notify( | ||
SYNC_PAUSED_NOTIFICATION_ID, | ||
syncNotificationBuilder.buildSyncPausedNotification(context), | ||
) | ||
} | ||
private fun cancelNotification() { | ||
notificationManager.cancel(SYNC_PAUSED_NOTIFICATION_ID) | ||
} | ||
|
||
companion object { | ||
private const val SYNC_PAUSED_NOTIFICATION_ID = 6451 | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
sync/sync-impl/src/main/java/com/duckduckgo/sync/impl/engine/SyncNotificationBuilder.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,58 @@ | ||
/* | ||
* 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.sync.impl.engine | ||
|
||
import android.app.Notification | ||
import android.app.PendingIntent | ||
import android.content.Context | ||
import android.widget.RemoteViews | ||
import androidx.core.app.NotificationCompat | ||
import androidx.core.app.TaskStackBuilder | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.navigation.api.GlobalActivityStarter | ||
import com.duckduckgo.sync.api.SYNC_NOTIFICATION_CHANNEL_ID | ||
import com.duckduckgo.sync.api.SyncActivityWithEmptyParams | ||
import com.duckduckgo.sync.impl.R | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import javax.inject.Inject | ||
|
||
interface SyncNotificationBuilder { | ||
fun buildSyncPausedNotification(context: Context): Notification | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class AppCredentialsSyncNotificationBuilder @Inject constructor( | ||
private val globalGlobalActivityStarter: GlobalActivityStarter, | ||
) : SyncNotificationBuilder { | ||
override fun buildSyncPausedNotification(context: Context): Notification { | ||
return NotificationCompat.Builder(context, SYNC_NOTIFICATION_CHANNEL_ID) | ||
.setSmallIcon(com.duckduckgo.mobile.android.R.drawable.notification_logo) | ||
.setStyle(NotificationCompat.DecoratedCustomViewStyle()) | ||
.setContentIntent(getPendingIntent(context)) | ||
.setCustomContentView(RemoteViews(context.packageName, R.layout.notification_sync_paused)) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.setCategory(NotificationCompat.CATEGORY_STATUS) | ||
.build() | ||
} | ||
|
||
private fun getPendingIntent(context: Context): PendingIntent? = TaskStackBuilder.create(context).run { | ||
addNextIntentWithParentStack( | ||
globalGlobalActivityStarter.startIntent(context, SyncActivityWithEmptyParams)!!, | ||
) | ||
getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) | ||
} | ||
} |
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
Oops, something went wrong.