diff --git a/demo/src/main/java/com/smashingboxes/surelockdemo/LoginActivity.kt b/demo/src/main/java/com/smashingboxes/surelockdemo/LoginActivity.kt index 561a5dc..73925db 100644 --- a/demo/src/main/java/com/smashingboxes/surelockdemo/LoginActivity.kt +++ b/demo/src/main/java/com/smashingboxes/surelockdemo/LoginActivity.kt @@ -36,7 +36,7 @@ class LoginActivity : AppCompatActivity(), SurelockFingerprintListener { Surelock.Builder(this) .withDefaultDialog(R.style.SurelockDemoDialog) .withKeystoreAlias(KEYSTORE_KEY_ALIAS) - .withFragmentManager(fragmentManager) + .withFragmentManager(supportFragmentManager) .withSurelockFragmentTag(FINGERPRINT_DIALOG_FRAGMENT_TAG) .withSurelockStorage(surelockStorage) .build() @@ -288,10 +288,10 @@ class LoginActivity : AppCompatActivity(), SurelockFingerprintListener { companion object { private val TAG = LoginActivity::class.java.simpleName - private val FINGERPRINT_DIALOG_FRAGMENT_TAG = "com.smashingboxes.surelockdemo.FINGERPRINT_DIALOG_FRAGMENT_TAG" - private val KEYSTORE_KEY_ALIAS = "com.smashingboxes.surelockdemo.KEYSTORE_KEY_ALIAS" - private val KEY_CRE_DEN_TIALS = "com.smashingboxes.surelockdemo.KEY_CRE_DEN_TIALS" - private val SHARED_PREFS_FILE_NAME = "surelock_demo_prefs" - private val DELIMITER = "%s]%s" + private const val FINGERPRINT_DIALOG_FRAGMENT_TAG = "com.smashingboxes.surelockdemo.FINGERPRINT_DIALOG_FRAGMENT_TAG" + private const val KEYSTORE_KEY_ALIAS = "com.smashingboxes.surelockdemo.KEYSTORE_KEY_ALIAS" + private const val KEY_CRE_DEN_TIALS = "com.smashingboxes.surelockdemo.KEY_CRE_DEN_TIALS" + private const val SHARED_PREFS_FILE_NAME = "surelock_demo_prefs" + private const val DELIMITER = "%s]%s" } } \ No newline at end of file diff --git a/surelock/src/main/java/com/smashingboxes/surelock/Surelock.kt b/surelock/src/main/java/com/smashingboxes/surelock/Surelock.kt index 405376b..6f1d8a8 100644 --- a/surelock/src/main/java/com/smashingboxes/surelock/Surelock.kt +++ b/surelock/src/main/java/com/smashingboxes/surelock/Surelock.kt @@ -1,7 +1,6 @@ package com.smashingboxes.surelock import android.annotation.TargetApi -import android.app.FragmentManager import android.app.KeyguardManager import android.content.Context import android.content.Intent @@ -11,6 +10,7 @@ import android.security.keystore.KeyPermanentlyInvalidatedException import android.security.keystore.KeyProperties import android.support.annotation.IntDef import android.support.annotation.StyleRes +import android.support.v4.app.FragmentManager import android.support.v4.hardware.fingerprint.FingerprintManagerCompat import android.util.Log import android.widget.Toast diff --git a/surelock/src/main/java/com/smashingboxes/surelock/SurelockDefaultDialog.kt b/surelock/src/main/java/com/smashingboxes/surelock/SurelockDefaultDialog.kt index ed12a28..598c4e0 100644 --- a/surelock/src/main/java/com/smashingboxes/surelock/SurelockDefaultDialog.kt +++ b/surelock/src/main/java/com/smashingboxes/surelock/SurelockDefaultDialog.kt @@ -1,12 +1,12 @@ package com.smashingboxes.surelock import android.app.Dialog -import android.app.DialogFragment -import android.app.FragmentManager import android.content.Context import android.content.res.TypedArray import android.os.Bundle import android.support.annotation.StyleRes +import android.support.v4.app.DialogFragment +import android.support.v4.app.FragmentManager import android.support.v4.content.ContextCompat import android.support.v4.hardware.fingerprint.FingerprintManagerCompat import android.view.LayoutInflater @@ -45,16 +45,16 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { private var styleId: Int = 0 // TODO clean up and genericize default dialog - add custom attribute set which can be overridden - private var iconView: SwirlView? = null - private var statusTextView: TextView? = null + private lateinit var iconView: SwirlView + private lateinit var statusTextView: TextView private val resetErrorTextRunnable = Runnable { if (isAdded) { - statusTextView?.apply { - setTextColor(ContextCompat.getColor(activity, R.color.hint_grey)) - text = resources.getString(R.string.fingerprint_hint) + statusTextView.apply { + setTextColor(ContextCompat.getColor(context, R.color.hint_grey)) + text = getString(R.string.fingerprint_hint) } - iconView?.setState(SwirlView.State.ON) + iconView.setState(SwirlView.State.ON) } } @@ -80,11 +80,11 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { cipherOperationMode = savedInstanceState.getInt(KEY_CIPHER_OP_MODE) styleId = savedInstanceState.getInt(KEY_STYLE_ID) } else { - cipherOperationMode = arguments.getInt(KEY_CIPHER_OP_MODE) - styleId = arguments.getInt(KEY_STYLE_ID) + cipherOperationMode = arguments!!.getInt(KEY_CIPHER_OP_MODE) + styleId = arguments!!.getInt(KEY_STYLE_ID) } - val attrs = activity.obtainStyledAttributes(styleId, R.styleable + val attrs = context!!.obtainStyledAttributes(styleId, R.styleable .SurelockDefaultDialog) val dialogTheme = attrs.getResourceId(R.styleable.SurelockDefaultDialog_sl_dialog_theme, 0) attrs.recycle() @@ -96,7 +96,7 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val view = inflater.inflate(R.layout.fingerprint_dialog_container, container, false) - val attrs = activity.obtainStyledAttributes(styleId, R.styleable.SurelockDefaultDialog) + val attrs = activity!!.obtainStyledAttributes(styleId, R.styleable.SurelockDefaultDialog) setUpViews(view, attrs) @@ -160,7 +160,7 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { override fun onResume() { super.onResume() uiHelper?.startListening(cryptoObject!!) - iconView?.setState(SwirlView.State.ON) + iconView.setState(SwirlView.State.ON) } override fun show(fragmentManager: FragmentManager, fingerprintDialogFragmentTag: String) { @@ -186,35 +186,35 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { } override fun onAuthenticationSucceeded(result: FingerprintManagerCompat.AuthenticationResult?) { - iconView?.postDelayed({ + iconView.postDelayed({ //TODO figure out a way to not make user have to run encryption/decryption themselves here if (Cipher.ENCRYPT_MODE == cipherOperationMode) { try { val encryptedValue = cryptoObject?.cipher?.doFinal(valueToEncrypt) keyForDecryption?.let { - if (encryptedValue != null) { - storage?.createOrUpdate(it, encryptedValue) - listener?.onFingerprintEnrolled() - } + storage?.createOrUpdate(it, encryptedValue!!) + listener?.onFingerprintEnrolled() } } catch (e: IllegalBlockSizeException) { listener?.onFingerprintError(e.message) } catch (e: BadPaddingException) { listener?.onFingerprintError(e.message) + } catch (e: NullPointerException) { + listener?.onFingerprintError(e.message) } - } else if (Cipher.DECRYPT_MODE == cipherOperationMode) { val encryptedValue = storage?.get(keyForDecryption!!) - val decryptedValue: ByteArray + val decryptedValue: ByteArray? try { - decryptedValue = cryptoObject?.cipher?.doFinal(encryptedValue) ?: ByteArray(0) - listener?.onFingerprintAuthenticated(decryptedValue) + decryptedValue = cryptoObject?.cipher?.doFinal(encryptedValue) + listener?.onFingerprintAuthenticated(decryptedValue!!) } catch (e: BadPaddingException) { listener?.onFingerprintError(e.message) } catch (e: IllegalBlockSizeException) { listener?.onFingerprintError(e.message) + } catch (e: NullPointerException) { + listener?.onFingerprintError(e.message) } - } dismiss() }, SUCCESS_DELAY_MILLIS) @@ -232,15 +232,15 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { } override fun onAuthenticationFailed() { - showError(statusTextView!!.resources.getString(R.string.fingerprint_not_recognized)) + showError(getString(R.string.fingerprint_not_recognized)) listener?.onFingerprintError(null) } private fun showError(error: CharSequence?) { - iconView?.setState(SwirlView.State.ERROR) - statusTextView?.apply { + iconView.setState(SwirlView.State.ERROR) + statusTextView.apply { text = error - setTextColor(ContextCompat.getColor(activity, R.color.error_red)) + setTextColor(ContextCompat.getColor(context, R.color.error_red)) removeCallbacks(resetErrorTextRunnable) postDelayed(resetErrorTextRunnable, ERROR_TIMEOUT_MILLIS) } @@ -248,11 +248,11 @@ class SurelockDefaultDialog : DialogFragment(), SurelockFragment { companion object { - private val KEY_CIPHER_OP_MODE = "com.smashingboxes.surelock.SurelockDefaultDialog.KEY_CIPHER_OP_MODE" - private val KEY_STYLE_ID = "com.smashingboxes.surelock.KEY_STYLE_ID" + private const val KEY_CIPHER_OP_MODE = "com.smashingboxes.surelock.SurelockDefaultDialog.KEY_CIPHER_OP_MODE" + private const val KEY_STYLE_ID = "com.smashingboxes.surelock.KEY_STYLE_ID" - private val ERROR_TIMEOUT_MILLIS: Long = 1600 - private val SUCCESS_DELAY_MILLIS: Long = 1300 //TODO make these configurable via attrs + private const val ERROR_TIMEOUT_MILLIS: Long = 1600 + private const val SUCCESS_DELAY_MILLIS: Long = 1300 //TODO make these configurable via attrs internal fun newInstance(cipherOperationMode: Int, @StyleRes styleId: Int): SurelockDefaultDialog { diff --git a/surelock/src/main/java/com/smashingboxes/surelock/SurelockFragment.kt b/surelock/src/main/java/com/smashingboxes/surelock/SurelockFragment.kt index 778c419..01aec96 100644 --- a/surelock/src/main/java/com/smashingboxes/surelock/SurelockFragment.kt +++ b/surelock/src/main/java/com/smashingboxes/surelock/SurelockFragment.kt @@ -1,6 +1,6 @@ package com.smashingboxes.surelock -import android.app.FragmentManager +import android.support.v4.app.FragmentManager import android.support.v4.hardware.fingerprint.FingerprintManagerCompat /** diff --git a/surelock/src/main/java/com/smashingboxes/surelock/SurelockMaterialDialog.kt b/surelock/src/main/java/com/smashingboxes/surelock/SurelockMaterialDialog.kt index 0eb3b51..29f88fb 100644 --- a/surelock/src/main/java/com/smashingboxes/surelock/SurelockMaterialDialog.kt +++ b/surelock/src/main/java/com/smashingboxes/surelock/SurelockMaterialDialog.kt @@ -1,9 +1,9 @@ package com.smashingboxes.surelock -import android.app.DialogFragment -import android.app.FragmentManager import android.content.Context import android.os.Bundle +import android.support.v4.app.DialogFragment +import android.support.v4.app.FragmentManager import android.support.v4.content.ContextCompat import android.support.v4.hardware.fingerprint.FingerprintManagerCompat import android.view.LayoutInflater @@ -30,8 +30,8 @@ import javax.crypto.IllegalBlockSizeException class SurelockMaterialDialog : DialogFragment(), SurelockFragment { - private var swirlView: SwirlView? = null - private var messageView: TextView? = null + private lateinit var swirlView: SwirlView + private lateinit var messageView: TextView private var fingerprintManager: FingerprintManagerCompat? = null private var cryptoObject: FingerprintManagerCompat.CryptoObject? = null @@ -44,11 +44,11 @@ class SurelockMaterialDialog : DialogFragment(), SurelockFragment { private val resetErrorTextRunnable = Runnable { if (isAdded) { - messageView?.apply { - setTextColor(ContextCompat.getColor(activity, R.color.hint_grey)) + messageView.apply { + setTextColor(ContextCompat.getColor(context, R.color.hint_grey)) text = resources.getString(R.string.fingerprint_hint) } - swirlView?.apply { + swirlView.apply { setState(SwirlView.State.ON) } } @@ -72,8 +72,8 @@ class SurelockMaterialDialog : DialogFragment(), SurelockFragment { // Do not create a new Fragment when the Activity is re-created such as orientation changes. retainInstance = true - cipherOperationMode = savedInstanceState?.getInt(KEY_CIPHER_OP_MODE) ?: arguments.getInt( - KEY_CIPHER_OP_MODE) + cipherOperationMode = savedInstanceState?.getInt(KEY_CIPHER_OP_MODE) ?: arguments?.getInt( + KEY_CIPHER_OP_MODE) ?: 0 setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog) } @@ -94,7 +94,7 @@ class SurelockMaterialDialog : DialogFragment(), SurelockFragment { cryptoObject?.let { uiHelper?.startListening(it) } - swirlView?.setState(SwirlView.State.ON) + swirlView.setState(SwirlView.State.ON) } override fun show(fragmentManager: FragmentManager, fingerprintDialogFragmentTag: String) { @@ -140,51 +140,51 @@ class SurelockMaterialDialog : DialogFragment(), SurelockFragment { } override fun onAuthenticationSucceeded(result: FingerprintManagerCompat.AuthenticationResult?) { - swirlView?.postDelayed({ + swirlView.postDelayed({ //TODO figure out a way to not make user have to run encryption/decryption // themselves here if (Cipher.ENCRYPT_MODE == cipherOperationMode) { try { val encryptedValue = cryptoObject?.cipher?.doFinal(valueToEncrypt) keyForDecryption?.let { - if (encryptedValue != null) { - storage?.createOrUpdate(it, encryptedValue) - listener?.onFingerprintEnrolled() - } + storage?.createOrUpdate(it, encryptedValue!!) + listener?.onFingerprintEnrolled() } } catch (e: IllegalBlockSizeException) { listener?.onFingerprintError(e.message) } catch (e: BadPaddingException) { listener?.onFingerprintError(e.message) + } catch (e: NullPointerException) { + listener?.onFingerprintError(e.message) } - } else if (Cipher.DECRYPT_MODE == cipherOperationMode) { val encryptedValue = storage?.get(keyForDecryption!!) - val decryptedValue: ByteArray + val decryptedValue: ByteArray? try { - decryptedValue = cryptoObject?.cipher?.doFinal(encryptedValue) ?: ByteArray(0) - listener?.onFingerprintAuthenticated(decryptedValue) + decryptedValue = cryptoObject?.cipher?.doFinal(encryptedValue) + listener?.onFingerprintAuthenticated(decryptedValue!!) } catch (e: BadPaddingException) { listener?.onFingerprintError(e.message) } catch (e: IllegalBlockSizeException) { listener?.onFingerprintError(e.message) + } catch (e: NullPointerException) { + listener?.onFingerprintError(e.message) } - } dismiss() }, SUCCESS_DELAY_MILLIS) } override fun onAuthenticationFailed() { - showError(messageView?.resources?.getString(R.string.fingerprint_not_recognized) ?: "") + showError(messageView.resources.getString(R.string.fingerprint_not_recognized)) listener?.onFingerprintError(null) } private fun showError(error: CharSequence?) { - swirlView?.setState(SwirlView.State.ERROR) - messageView?.apply { + swirlView.setState(SwirlView.State.ERROR) + messageView.apply { text = error - setTextColor(ContextCompat.getColor(activity, R.color.error_red)) + setTextColor(ContextCompat.getColor(context, R.color.error_red)) removeCallbacks(resetErrorTextRunnable) postDelayed(resetErrorTextRunnable, ERROR_TIMEOUT_MILLIS) } @@ -192,10 +192,10 @@ class SurelockMaterialDialog : DialogFragment(), SurelockFragment { companion object { - private val KEY_CIPHER_OP_MODE = "com.smashingboxes.surelock" + ".SurelockMaterialDialog.KEY_CIPHER_OP_MODE" + private const val KEY_CIPHER_OP_MODE = "com.smashingboxes.surelock" + ".SurelockMaterialDialog.KEY_CIPHER_OP_MODE" - private val ERROR_TIMEOUT_MILLIS: Long = 1600 - private val SUCCESS_DELAY_MILLIS: Long = 1300 + private const val ERROR_TIMEOUT_MILLIS: Long = 1600 + private const val SUCCESS_DELAY_MILLIS: Long = 1300 internal fun newInstance(cipherOperationMode: Int): SurelockMaterialDialog {