Skip to content

Commit

Permalink
Kotlinizing PatternManager
Browse files Browse the repository at this point in the history
  • Loading branch information
JuancaG05 committed Sep 27, 2021
1 parent ebe5e44 commit 928bec6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 121 deletions.
4 changes: 2 additions & 2 deletions owncloudApp/src/main/java/com/owncloud/android/MainApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class MainApp : Application() {
override fun onActivityStarted(activity: Activity) {
Timber.v("${activity.javaClass.simpleName} onStart() starting")
PassCodeManager.onActivityStarted(activity)
PatternManager.getPatternManager().onActivityStarted(activity)
PatternManager.onActivityStarted(activity)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
BiometricManager.getBiometricManager(activity).onActivityStarted(activity)
}
Expand All @@ -133,7 +133,7 @@ class MainApp : Application() {
override fun onActivityStopped(activity: Activity) {
Timber.v("${activity.javaClass.simpleName} onStop() ending")
PassCodeManager.onActivityStopped(activity)
PatternManager.getPatternManager().onActivityStopped(activity)
PatternManager.onActivityStopped(activity)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
BiometricManager.getBiometricManager(activity).onActivityStopped(activity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public void onActivityStarted(Activity activity) {
// Cancel biometric lock and use passcode unlock method
PassCodeManager.INSTANCE.onBiometricCancelled(activity);
mVisibleActivitiesCounter++;
} else if (PatternManager.getPatternManager().isPatternEnabled()) {
} else if (PatternManager.INSTANCE.isPatternEnabled()) {
// Cancel biometric lock and use pattern unlock method
PatternManager.getPatternManager().onBiometricCancelled(activity);
PatternManager.INSTANCE.onBiometricCancelled(activity);
mVisibleActivitiesCounter++;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* ownCloud Android client application
*
* @author Juan Carlos Garrote Gascón
*
* Copyright (C) 2021 ownCloud GmbH.
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.owncloud.android.presentation.ui.security

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.PowerManager
import com.owncloud.android.MainApp.Companion.appContext
import com.owncloud.android.authentication.BiometricManager
import com.owncloud.android.data.preferences.datasources.implementation.SharedPreferencesProviderImpl

object PatternManager {

private val exemptOfPatternActivities: MutableSet<Class<*>> = mutableSetOf(PatternActivity::class.java)
private var visibleActivitiesCounter = 0
private val preferencesProvider = SharedPreferencesProviderImpl(appContext)

fun onActivityStarted(activity: Activity) {
if (!exemptOfPatternActivities.contains(activity.javaClass) && patternShouldBeRequested()) {

// Do not ask for pattern if biometric is enabled
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && BiometricManager.getBiometricManager(activity).isBiometricEnabled) {
visibleActivitiesCounter++
return
}

askUserForPattern(activity)
}

visibleActivitiesCounter++
}

fun onActivityStopped(activity: Activity) {
if (visibleActivitiesCounter > 0) visibleActivitiesCounter--

bayPassUnlockOnce()
val powerMgr = activity.getSystemService(Context.POWER_SERVICE) as PowerManager
if (isPatternEnabled() && !powerMgr.isScreenOn) {
activity.moveTaskToBack(true)
}
}

private fun patternShouldBeRequested(): Boolean {
val lastUnlockTimestamp = preferencesProvider.getLong(PREFERENCE_LAST_UNLOCK_TIMESTAMP, 0)
val timeout = LockTimeout.valueOf(preferencesProvider.getString(PREFERENCE_LOCK_TIMEOUT, LockTimeout.IMMEDIATELY.name)!!).toMilliseconds()
return if (System.currentTimeMillis() - lastUnlockTimestamp > timeout && visibleActivitiesCounter <= 0) isPatternEnabled()
else false
}

fun isPatternEnabled(): Boolean {
return preferencesProvider.getBoolean(PatternActivity.PREFERENCE_SET_PATTERN, false)
}

private fun askUserForPattern(activity: Activity) {
val i = Intent(appContext, PatternActivity::class.java).apply {
action = PatternActivity.ACTION_CHECK
flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
activity.startActivity(i)
}

fun onBiometricCancelled(activity: Activity) {
askUserForPattern(activity)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public void onAuthenticationFailed() {
private void authError() {
if (PassCodeManager.INSTANCE.isPassCodeEnabled()) {
PassCodeManager.INSTANCE.onBiometricCancelled(mActivity);
} else if (PatternManager.getPatternManager().isPatternEnabled()) {
PatternManager.getPatternManager().onBiometricCancelled(mActivity);
} else if (PatternManager.INSTANCE.isPatternEnabled()) {
PatternManager.INSTANCE.onBiometricCancelled(mActivity);
}

mActivity.finish();
Expand Down

0 comments on commit 928bec6

Please sign in to comment.