add the functionality to the PIN fragment

This commit is contained in:
tibbi 2017-08-02 00:00:32 +02:00
parent 805633dfb6
commit f0ace2f57d
22 changed files with 105 additions and 39 deletions

View file

@ -7,15 +7,14 @@ import android.view.View
import android.view.ViewGroup
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.interfaces.HashListener
import com.simplemobiletools.gallery.views.PatternTab
import com.simplemobiletools.gallery.interfaces.SecurityTab
class PasswordTypesAdapter(val context: Context, val requiredHash: String, val hashListener: HashListener) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any {
val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false)
container.addView(view)
if (position == 0)
(view as PatternTab).initTab(requiredHash, hashListener)
(view as SecurityTab).initTab(requiredHash, hashListener)
return view
}

View file

@ -0,0 +1,5 @@
package com.simplemobiletools.gallery.interfaces
interface SecurityTab {
fun initTab(requiredHash: String, listener: HashListener)
}

View file

@ -12,11 +12,12 @@ import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.interfaces.HashListener
import com.simplemobiletools.gallery.interfaces.SecurityTab
import kotlinx.android.synthetic.main.tab_pattern.view.*
class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
var hash = ""
var requiredHash = ""
class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), SecurityTab {
private var hash = ""
private var requiredHash = ""
lateinit var hashListener: HashListener
override fun onFinishInflate() {
@ -41,7 +42,7 @@ class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context
})
}
fun initTab(requiredHash: String, listener: HashListener) {
override fun initTab(requiredHash: String, listener: HashListener) {
this.requiredHash = requiredHash
hash = requiredHash
hashListener = listener
@ -52,23 +53,21 @@ class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context
hash = newHash
pattern_lock_view.clearPattern()
pattern_lock_title.setText(R.string.repeat_pattern)
} else if (hash == newHash) {
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT)
Handler().postDelayed({
hashListener.receivedHash(hash)
}, 300)
} else {
if (hash == newHash) {
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT)
Handler().postDelayed({
hashListener.receivedHash(hash)
}, 300)
} else {
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG)
context.toast(R.string.wrong_pattern)
Handler().postDelayed({
pattern_lock_view.clearPattern()
if (requiredHash.isEmpty()) {
hash = ""
pattern_lock_title.setText(R.string.insert_pattern)
}
}, 1000)
}
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG)
context.toast(R.string.wrong_pattern)
Handler().postDelayed({
pattern_lock_view.clearPattern()
if (requiredHash.isEmpty()) {
hash = ""
pattern_lock_title.setText(R.string.insert_pattern)
}
}, 1000)
}
}
}

View file

@ -3,11 +3,21 @@ package com.simplemobiletools.gallery.views
import android.content.Context
import android.util.AttributeSet
import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.interfaces.HashListener
import com.simplemobiletools.gallery.interfaces.SecurityTab
import kotlinx.android.synthetic.main.tab_pin.view.*
import java.math.BigInteger
import java.security.MessageDigest
import java.util.*
class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) {
var pin = ""
class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), SecurityTab {
private var hash = ""
private var requiredHash = ""
private var pin = ""
lateinit var hashListener: HashListener
override fun onFinishInflate() {
super.onFinishInflate()
@ -27,6 +37,12 @@ class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, at
pin_ok.setOnClickListener { confirmPIN() }
}
override fun initTab(requiredHash: String, listener: HashListener) {
this.requiredHash = requiredHash
hash = requiredHash
hashListener = listener
}
private fun addNumber(number: String) {
if (pin.length < 10) {
pin += number
@ -42,10 +58,39 @@ class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, at
}
private fun confirmPIN() {
val newHash = getHashedPin()
if (pin.isEmpty()) {
context.toast(R.string.please_enter_pin)
} else if (hash.isEmpty()) {
hash = newHash
resetPin()
pin_lock_title.setText(R.string.repeat_pin)
} else if (hash == newHash) {
hashListener.receivedHash(hash)
} else {
resetPin()
context.toast(R.string.wrong_pin)
if (requiredHash.isEmpty()) {
hash = ""
pin_lock_title.setText(R.string.enter_pin)
}
}
}
private fun resetPin() {
pin = ""
pin_lock_current_pin.text = ""
}
private fun updatePinCode() {
pin_lock_current_pin.text = "*".repeat(pin.length)
}
private fun getHashedPin(): String {
val messageDigest = MessageDigest.getInstance("SHA-1")
messageDigest.update(pin.toByteArray(charset("UTF-8")))
val digest = messageDigest.digest()
val bigInteger = BigInteger(1, digest)
return String.format(Locale.getDefault(), "%0${digest.size * 2}x", bigInteger).toLowerCase()
}
}

View file

@ -45,7 +45,7 @@
android:id="@+id/pin_1"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"/>
@ -53,7 +53,7 @@
android:id="@+id/pin_2"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"/>
@ -61,7 +61,7 @@
android:id="@+id/pin_3"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"/>
</LinearLayout>
@ -76,7 +76,7 @@
android:id="@+id/pin_4"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"/>
@ -84,7 +84,7 @@
android:id="@+id/pin_5"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"/>
@ -92,7 +92,7 @@
android:id="@+id/pin_6"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"/>
</LinearLayout>
@ -107,7 +107,7 @@
android:id="@+id/pin_7"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"/>
@ -115,7 +115,7 @@
android:id="@+id/pin_8"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"/>
@ -123,7 +123,7 @@
android:id="@+id/pin_9"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"/>
</LinearLayout>
@ -138,7 +138,7 @@
android:id="@+id/pin_c"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="C"/>
@ -146,7 +146,7 @@
android:id="@+id/pin_0"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="0"/>
@ -154,8 +154,9 @@
android:id="@+id/pin_ok"
style="@style/PinNumberStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="@dimen/activity_margin"
android:src="@drawable/ic_check"/>
</LinearLayout>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -76,12 +76,13 @@
<string name="no_capable_app_found">Aplicativo não encontrado</string>
<string name="setting_wallpaper">Definindo como papel de parede;</string>
<string name="wallpaper_set_successfully">Papel de parede com sucesso</string>
<string name="portrait_aspect_ratio">Retrato</string>
<string name="landscape_aspect_ratio">Paisagem</string>
<string name="portrait_aspect_ratio">Retrato</string>
<string name="landscape_aspect_ratio">Paisagem</string>
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Zadajte PIN</string>
<string name="please_enter_pin">Prosím zadajte PIN</string>
<string name="wrong_pin">Nesprávny PIN</string>
<string name="repeat_pin">Zopakujte PIN</string>
<string name="pattern">Vzor</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection -->
<string name="pin">PIN</string>
<string name="enter_pin">Enter PIN</string>
<string name="please_enter_pin">Please enter a PIN</string>
<string name="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string>