📌  相关文章
📜  带有 LolliPin 库的 Android 中的 Material Design Pincode

📅  最后修改于: 2022-05-13 01:54:31.104000             🧑  作者: Mango

带有 LolliPin 库的 Android 中的 Material Design Pincode

Lollipin 是一个棒棒糖材料设计风格的安卓 Pincode 库,它为开发人员提供了实现材料 Pincode 活动而无需任何忙乱。现在,问题是为什么要使用 Lollipin 库。

  • 密码保护:密码本身不保存,仅使用 SHA-1 算法进行哈希处理。然后将此哈希保存在 SharedPreferences 中,允许验证用户是否输入了正确的 PinCode,而无需检索它。
  • 指纹介绍:一旦用户启用了密码,他也可以使用他的指纹扫描仪来解锁他的设备。
  • 开发人员友好:这个很棒的库为您提供了一个令人惊叹的预构建用户界面,其中包含各种简化工作的方法。

下面给出了一个示例视频,以了解我们将在本文中做什么。请注意,我们将使用Kotlin语言来实现这个项目。

分步实施

第 1 步:创建一个新项目

要在 Android Studio 中创建新项目,请参阅如何在 Android Studio 中创建/启动新项目。请注意,选择 Kotlin 作为编程语言。

第二步:添加依赖

将以下这些依赖项添加到您的项目中,这会将 Lollipin 库导入您现有的项目中。

// Paste this in build.gradle app level
// This will import the Lollipin library into your existing project
implementation ('com.github.omadahealth:lollipin:2.1.0@aar'{
      transitive = true
   }
   
   // Paste this in your build.gradle project level
   // This is for custom ripple animation
   allprojects {
    repositories {
        maven{
            url "https://github.com/omadahealth/omada-nexus/raw/master/release"
        }
        jcenter()
    }
   }

第 3 步:

那么,让我们讨论一下 Lollipin 库的核心组件。所以基本上,我们需要创建一个专门的活动来实现我们的储物柜活动。在我们的例子中,我们为此目的创建了一个 PinCodeKotlin 活动。

PinCodeKotlin 活动

Kotlin
import android.widget.Toast
import com.github.omadahealth.lollipin.lib.managers.AppLockActivity
  
// Here we are extending our activity with 
// AppLockActivity so that we can use
// its core features
class PinCodeKotlin : AppLockActivity() {
  
    // For Implementing forgot pin logic
    override fun showForgotDialog() {
        Toast.makeText(this,"Implement your forgot password logic here.",Toast.LENGTH_LONG).show()
    }
  
    // For handling pin failure  events
    override fun onPinFailure(attempts: Int) {
        Toast.makeText(this,"Pin entered is Incorrect and attempts done are $attempts",Toast.LENGTH_LONG).show()
    }
  
    // For handling pin success events
    override fun onPinSuccess(attempts: Int) {
        Toast.makeText(this,"Correct Pin",Toast.LENGTH_LONG).show()
    }
  
    // For overriding default length option
    // We can override the size of
    // Pin required as we want
    override fun getPinLength(): Int {
        return 4
    }
}


XML


  
    
        
        
        
            
                
  
                
            
        
    
  


Kotlin
import android.app.Application
import com.github.omadahealth.lollipin.lib.managers.LockManager
  
class app: Application() {
    override fun onCreate() {
        super.onCreate()
          
        // Here we are enabling our 
        // custom Pin code activity
        LockManager.getInstance().enableAppLock(this,PinCodeKotlin::class.java)
          
        // We are setting custom logo 
        // for our Pin code activity
        LockManager.getInstance().appLock.logoId=R.drawable.ic_gfg
        
        // This is used to give locker a custom timeout setting
        // It is in milliseconds
        // LockManager.getInstance().appLock.timeout=4000
    }
}


Kotlin
import `in`.kay.gfglollipinapp.databinding.ActivityMainBinding
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.omadahealth.lollipin.lib.managers.AppLock
import com.github.omadahealth.lollipin.lib.managers.LockManager
  
class MainActivity : AppCompatActivity() {
      
    // This is view binding which is used to 
    // bind view without those long findViewbyIds line
    private lateinit var binding: ActivityMainBinding
    
    // We are creating a global locker instance for our work
    private var locker = LockManager.getInstance()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
          
        // locker.appLock.logoId=R.drawable.ic_gfg
        // Used to enable our locker first time
        binding.btnEnable.setOnClickListener {
            
            // We are fwding app to PinCodeKotlin 
            // class for enabling the PinCode
            val intent = Intent(this@MainActivity, PinCodeKotlin::class.java)
              
            // We add some extras which is provided by library
            intent.putExtra(AppLock.EXTRA_TYPE, AppLock.ENABLE_PINLOCK)
            startActivityForResult(intent, 101)
        }
        binding.btnChangePin.setOnClickListener {
            val intent = Intent(this@MainActivity, PinCodeKotlin::class.java)
  
            // We are checking that is our passcode is already set or not
            // In simple terms if user is new and he didn't set the passcode
            // then we will send it to choose new passcode
            if (locker.isAppLockEnabled && locker.appLock.isPasscodeSet) {
                intent.putExtra(AppLock.EXTRA_TYPE, AppLock.CHANGE_PIN)
                startActivity(intent)
            } else {
                intent.putExtra(AppLock.EXTRA_TYPE, AppLock.ENABLE_PINLOCK)
                startActivityForResult(intent, 101)
            }
        }
        binding.btnGoToLocked.setOnClickListener {
            // This is simple intent for LockedActivity
            val intent = Intent(this@MainActivity, LockedActivity::class.java)
            startActivity(intent)
        }
        // This is to disable the PinCode 
        binding.btnDisable.setOnClickListener {
            locker.disableAppLock()
            Toast.makeText(this,"Disabled pin code successfully",Toast.LENGTH_LONG).show()
        }
    }
  
    // This is used to get some external calls to our activity 
    // as we are passing some result code for some operations
    // which will send the result and the data by this method
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
  
        when (requestCode) {
            101 -> {
                Toast.makeText(this, "PinCode enabled", Toast.LENGTH_SHORT).show()
            }
        }
    }
}


XML


  
    
    
  
        
        
    
  
    
    


Kotlin
import `in`.kay.gfglollipinapp.databinding.ActivityLockedBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.github.omadahealth.lollipin.lib.PinActivity
import com.github.omadahealth.lollipin.lib.PinCompatActivity
  
class LockedActivity :  PinCompatActivity() {
    private lateinit var binding: ActivityLockedBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityLockedBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}


XML


  
    
    


在这一步之后,我们需要告诉 Lollipin 库使用这个库。所以我们可以通过两种方式做到这一点。

  1. 通过在每个活动中告诉图书馆
  2. 通过在一个应用程序级类中告诉库

我们将使用第二种方法,因为它主要由库本身建议。

应用类

我们还需要使用 name 属性将此类附加到我们的 Android 清单文件中。我们的 AndroidManifest.xml 在下面

XML



  
    
        
        
        
            
                
  
                
            
        
    
  

科特林

import android.app.Application
import com.github.omadahealth.lollipin.lib.managers.LockManager
  
class app: Application() {
    override fun onCreate() {
        super.onCreate()
          
        // Here we are enabling our 
        // custom Pin code activity
        LockManager.getInstance().enableAppLock(this,PinCodeKotlin::class.java)
          
        // We are setting custom logo 
        // for our Pin code activity
        LockManager.getInstance().appLock.logoId=R.drawable.ic_gfg
        
        // This is used to give locker a custom timeout setting
        // It is in milliseconds
        // LockManager.getInstance().appLock.timeout=4000
    }
}

主要活动

现在,我们需要从我们的主 Activity 中调用库的不同核心功能。

科特林

import `in`.kay.gfglollipinapp.databinding.ActivityMainBinding
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.github.omadahealth.lollipin.lib.managers.AppLock
import com.github.omadahealth.lollipin.lib.managers.LockManager
  
class MainActivity : AppCompatActivity() {
      
    // This is view binding which is used to 
    // bind view without those long findViewbyIds line
    private lateinit var binding: ActivityMainBinding
    
    // We are creating a global locker instance for our work
    private var locker = LockManager.getInstance()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
          
        // locker.appLock.logoId=R.drawable.ic_gfg
        // Used to enable our locker first time
        binding.btnEnable.setOnClickListener {
            
            // We are fwding app to PinCodeKotlin 
            // class for enabling the PinCode
            val intent = Intent(this@MainActivity, PinCodeKotlin::class.java)
              
            // We add some extras which is provided by library
            intent.putExtra(AppLock.EXTRA_TYPE, AppLock.ENABLE_PINLOCK)
            startActivityForResult(intent, 101)
        }
        binding.btnChangePin.setOnClickListener {
            val intent = Intent(this@MainActivity, PinCodeKotlin::class.java)
  
            // We are checking that is our passcode is already set or not
            // In simple terms if user is new and he didn't set the passcode
            // then we will send it to choose new passcode
            if (locker.isAppLockEnabled && locker.appLock.isPasscodeSet) {
                intent.putExtra(AppLock.EXTRA_TYPE, AppLock.CHANGE_PIN)
                startActivity(intent)
            } else {
                intent.putExtra(AppLock.EXTRA_TYPE, AppLock.ENABLE_PINLOCK)
                startActivityForResult(intent, 101)
            }
        }
        binding.btnGoToLocked.setOnClickListener {
            // This is simple intent for LockedActivity
            val intent = Intent(this@MainActivity, LockedActivity::class.java)
            startActivity(intent)
        }
        // This is to disable the PinCode 
        binding.btnDisable.setOnClickListener {
            locker.disableAppLock()
            Toast.makeText(this,"Disabled pin code successfully",Toast.LENGTH_LONG).show()
        }
    }
  
    // This is used to get some external calls to our activity 
    // as we are passing some result code for some operations
    // which will send the result and the data by this method
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
  
        when (requestCode) {
            101 -> {
                Toast.makeText(this, "PinCode enabled", Toast.LENGTH_SHORT).show()
            }
        }
    }
}

所以让我们为此目的设计我们的布局文件

activity_main.xml

XML



  
    
    
  
        
        
    
  
    
    

最后,我们将设计我们的锁定活动,该活动将在 X 秒(即超时时间)后锁定。

锁定活动

科特林

import `in`.kay.gfglollipinapp.databinding.ActivityLockedBinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.github.omadahealth.lollipin.lib.PinActivity
import com.github.omadahealth.lollipin.lib.PinCompatActivity
  
class LockedActivity :  PinCompatActivity() {
    private lateinit var binding: ActivityLockedBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding= ActivityLockedBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }
}

以及此活动的布局

ActivityLocked.xml

XML



  
    
    

所以,我们准备好了。立即运行应用程序。

输出: