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 android.view.ViewGroup
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.interfaces.HashListener 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() { class PasswordTypesAdapter(val context: Context, val requiredHash: String, val hashListener: HashListener) : PagerAdapter() {
override fun instantiateItem(container: ViewGroup, position: Int): Any { override fun instantiateItem(container: ViewGroup, position: Int): Any {
val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false) val view = LayoutInflater.from(context).inflate(layoutSelection(position), container, false)
container.addView(view) container.addView(view)
if (position == 0) (view as SecurityTab).initTab(requiredHash, hashListener)
(view as PatternTab).initTab(requiredHash, hashListener)
return view 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.R
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.interfaces.HashListener import com.simplemobiletools.gallery.interfaces.HashListener
import com.simplemobiletools.gallery.interfaces.SecurityTab
import kotlinx.android.synthetic.main.tab_pattern.view.* import kotlinx.android.synthetic.main.tab_pattern.view.*
class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs) { class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), SecurityTab {
var hash = "" private var hash = ""
var requiredHash = "" private var requiredHash = ""
lateinit var hashListener: HashListener lateinit var hashListener: HashListener
override fun onFinishInflate() { 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 this.requiredHash = requiredHash
hash = requiredHash hash = requiredHash
hashListener = listener hashListener = listener
@ -52,23 +53,21 @@ class PatternTab(context: Context, attrs: AttributeSet) : RelativeLayout(context
hash = newHash hash = newHash
pattern_lock_view.clearPattern() pattern_lock_view.clearPattern()
pattern_lock_title.setText(R.string.repeat_pattern) 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 { } else {
if (hash == newHash) { pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG)
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.CORRECT) context.toast(R.string.wrong_pattern)
Handler().postDelayed({ Handler().postDelayed({
hashListener.receivedHash(hash) pattern_lock_view.clearPattern()
}, 300) if (requiredHash.isEmpty()) {
} else { hash = ""
pattern_lock_view.setViewMode(PatternLockView.PatternViewMode.WRONG) pattern_lock_title.setText(R.string.insert_pattern)
context.toast(R.string.wrong_pattern) }
Handler().postDelayed({ }, 1000)
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.content.Context
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.updateTextColors 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 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) { class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, attrs), SecurityTab {
var pin = "" private var hash = ""
private var requiredHash = ""
private var pin = ""
lateinit var hashListener: HashListener
override fun onFinishInflate() { override fun onFinishInflate() {
super.onFinishInflate() super.onFinishInflate()
@ -27,6 +37,12 @@ class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, at
pin_ok.setOnClickListener { confirmPIN() } pin_ok.setOnClickListener { confirmPIN() }
} }
override fun initTab(requiredHash: String, listener: HashListener) {
this.requiredHash = requiredHash
hash = requiredHash
hashListener = listener
}
private fun addNumber(number: String) { private fun addNumber(number: String) {
if (pin.length < 10) { if (pin.length < 10) {
pin += number pin += number
@ -42,10 +58,39 @@ class PinTab(context: Context, attrs: AttributeSet) : RelativeLayout(context, at
} }
private fun confirmPIN() { 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() { private fun updatePinCode() {
pin_lock_current_pin.text = "*".repeat(pin.length) 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" android:id="@+id/pin_1"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="1"/> android:text="1"/>
@ -53,7 +53,7 @@
android:id="@+id/pin_2" android:id="@+id/pin_2"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="2"/> android:text="2"/>
@ -61,7 +61,7 @@
android:id="@+id/pin_3" android:id="@+id/pin_3"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="3"/> android:text="3"/>
</LinearLayout> </LinearLayout>
@ -76,7 +76,7 @@
android:id="@+id/pin_4" android:id="@+id/pin_4"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="4"/> android:text="4"/>
@ -84,7 +84,7 @@
android:id="@+id/pin_5" android:id="@+id/pin_5"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="5"/> android:text="5"/>
@ -92,7 +92,7 @@
android:id="@+id/pin_6" android:id="@+id/pin_6"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="6"/> android:text="6"/>
</LinearLayout> </LinearLayout>
@ -107,7 +107,7 @@
android:id="@+id/pin_7" android:id="@+id/pin_7"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="7"/> android:text="7"/>
@ -115,7 +115,7 @@
android:id="@+id/pin_8" android:id="@+id/pin_8"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="8"/> android:text="8"/>
@ -123,7 +123,7 @@
android:id="@+id/pin_9" android:id="@+id/pin_9"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="9"/> android:text="9"/>
</LinearLayout> </LinearLayout>
@ -138,7 +138,7 @@
android:id="@+id/pin_c" android:id="@+id/pin_c"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="C"/> android:text="C"/>
@ -146,7 +146,7 @@
android:id="@+id/pin_0" android:id="@+id/pin_0"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:text="0"/> android:text="0"/>
@ -154,8 +154,9 @@
android:id="@+id/pin_ok" android:id="@+id/pin_ok"
style="@style/PinNumberStyle" style="@style/PinNumberStyle"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="1" android:layout_weight="1"
android:padding="@dimen/activity_margin"
android:src="@drawable/ic_check"/> android:src="@drawable/ic_check"/>
</LinearLayout> </LinearLayout>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</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="no_capable_app_found">Aplicativo não encontrado</string>
<string name="setting_wallpaper">Definindo como papel de parede;</string> <string name="setting_wallpaper">Definindo como papel de parede;</string>
<string name="wallpaper_set_successfully">Papel de parede com sucesso</string> <string name="wallpaper_set_successfully">Papel de parede com sucesso</string>
<string name="portrait_aspect_ratio">Retrato</string> <string name="portrait_aspect_ratio">Retrato</string>
<string name="landscape_aspect_ratio">Paisagem</string> <string name="landscape_aspect_ratio">Paisagem</string>
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Zadajte 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="wrong_pin">Nesprávny PIN</string>
<string name="repeat_pin">Zopakujte PIN</string> <string name="repeat_pin">Zopakujte PIN</string>
<string name="pattern">Vzor</string> <string name="pattern">Vzor</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>

View file

@ -82,6 +82,7 @@
<!-- Password protection --> <!-- Password protection -->
<string name="pin">PIN</string> <string name="pin">PIN</string>
<string name="enter_pin">Enter 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="wrong_pin">Wrong PIN</string>
<string name="repeat_pin">Repeat PIN</string> <string name="repeat_pin">Repeat PIN</string>
<string name="pattern">Pattern</string> <string name="pattern">Pattern</string>