Skip to content

Commit

Permalink
feat: Lock the app when the screen turns off (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirambd authored Dec 26, 2024
2 parents 51d5bd8 + dfb8149 commit 357c958
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<activity
android:name="com.infomaniak.lib.applock.LockActivity"
android:exported="false"
android:launchMode="singleTop"
android:theme="@style/AppThemeLock" />

<activity
Expand Down
8 changes: 0 additions & 8 deletions app/src/main/java/com/infomaniak/mail/MainApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.matomo.sdk.Tracker
import java.util.Date
import java.util.UUID
import javax.inject.Inject

Expand All @@ -79,8 +78,6 @@ open class MainApplication : Application(), ImageLoaderFactory, DefaultLifecycle
val matomoTracker: Tracker by lazy { buildTracker(shouldOptOut = !localSettings.isMatomoTrackingEnabled) }
var isAppInBackground = true
private set
var lastAppClosingTime: Long? = FIRST_LAUNCH_TIME
private set

@Inject
lateinit var workerFactory: HiltWorkerFactory
Expand Down Expand Up @@ -145,7 +142,6 @@ open class MainApplication : Application(), ImageLoaderFactory, DefaultLifecycle
}

override fun onStop(owner: LifecycleOwner) {
lastAppClosingTime = Date().time
isAppInBackground = true
owner.lifecycleScope.launch {
syncMailboxesWorkerScheduler.scheduleWorkIfNeeded()
Expand Down Expand Up @@ -289,10 +285,6 @@ open class MainApplication : Application(), ImageLoaderFactory, DefaultLifecycle

override fun newImageLoader(): ImageLoader = CoilUtils.newImageLoader(applicationContext, tokenInterceptorListener())

fun resetLastAppClosing() {
lastAppClosingTime = null
}

fun createSvgImageLoader(): ImageLoader {
return CoilUtils.newImageLoader(
applicationContext,
Expand Down
23 changes: 5 additions & 18 deletions app/src/main/java/com/infomaniak/mail/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import android.os.Bundle
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import com.infomaniak.lib.applock.LockActivity
import com.infomaniak.lib.applock.Utils.isKeyguardSecure
import com.infomaniak.mail.MatomoMail.trackScreen
import com.infomaniak.mail.data.LocalSettings
import com.infomaniak.mail.utils.AccountUtils
import com.infomaniak.mail.utils.extensions.getMainApplication
import io.sentry.Sentry
import kotlinx.coroutines.runBlocking

Expand All @@ -49,23 +47,12 @@ open class BaseActivity : AppCompatActivity() {

super.onCreate(savedInstanceState)
trackScreen()
}

override fun onResume() {
super.onResume()

if (localSettings.isAppLocked && isKeyguardSecure()) with(getMainApplication()) {
lastAppClosingTime?.let {
LockActivity.lockAfterTimeout(
context = this@BaseActivity,
destinationClass = this::class.java,
lastAppClosingTime = it,
primaryColor = localSettings.accentColor.getPrimary(this),
)
}

resetLastAppClosing()
}
LockActivity.scheduleLockIfNeeded(
targetActivity = this,
primaryColor = localSettings.accentColor.getPrimary(this),
isAppLockEnabled = { localSettings.isAppLocked }
)
}

fun getCurrentFragment(@IdRes fragmentContainerViewId: Int) = supportFragmentManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.infomaniak.lib.applock.Utils.isKeyguardSecure
import com.infomaniak.lib.applock.LockActivity
import com.infomaniak.lib.applock.Utils.silentlyReverseSwitch
import com.infomaniak.lib.core.utils.openAppNotificationSettings
import com.infomaniak.lib.core.utils.safeBinding
Expand Down Expand Up @@ -116,12 +116,15 @@ class SettingsFragment : Fragment() {
}

settingsAppLock.apply {
isVisible = context.isKeyguardSecure()
isVisible = LockActivity.hasBiometrics()
isChecked = localSettings.isAppLocked
setOnClickListener {
trackEvent("settingsGeneral", "lock", value = isChecked.toFloat())
// Reverse switch (before official parameter changed) by silent click
requireActivity().silentlyReverseSwitch(toggle!!) { isChecked -> localSettings.isAppLocked = isChecked }
requireActivity().silentlyReverseSwitch(toggle!!) { isChecked ->
localSettings.isAppLocked = isChecked
if (isChecked) LockActivity.unlock()
}
}
}

Expand Down

0 comments on commit 357c958

Please sign in to comment.