Working on TOTP

This commit is contained in:
Yogesh Choudhary Paliyal
2021-09-25 14:43:28 +05:30
parent e4ba06e6f7
commit b6bc82f6b1
10 changed files with 68 additions and 21 deletions

2
.idea/misc.xml generated
View File

@@ -8,7 +8,9 @@
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/activity_add_totpactivity.xml" value="0.16485507246376813" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/activity_dashboard.xml" value="0.2" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/fragment_detail.xml" value="0.1403985507246377" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/fragment_home.xml" value="0.16304347826086957" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/item_accounts.xml" value="0.1" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/layout/item_totp.xml" value="0.67" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/main/res/xml/backup_preferences.xml" value="0.4287690179806362" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/production/res/mipmap-anydpi-v26/ic_launcher.xml" value="0.21614583333333334" />
<entry key="..\:/Users/paliy/StudioProjects/KeyPass/app/src/staging/res/drawable/ic_twotone_content_copy_24.xml" value="0.21614583333333334" />

View File

@@ -3,6 +3,7 @@ package com.yogeshpaliyal.keypass.data
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.Gson
import com.google.gson.annotations.SerializedName
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.constants.AccountType
@@ -66,7 +67,17 @@ data class AccountModel(
return id
}
override fun getDiffBody(): Any? {
return if(type == AccountType.TOPT){
super.getDiffBody()
}else{
Gson().toJson(this)
}
}
fun getOtp() = TOTPHelper.generate(password?.toByteArray())
fun getTOtpProgress() = TOTPHelper.getProgress().toInt()
override fun getLayoutId(): Int = if(type == AccountType.TOPT) R.layout.item_totp else R.layout.item_accounts
}

View File

@@ -51,6 +51,12 @@ class AddTOTPActivity : AppCompatActivity() {
}
})
mViewModel.goBack.observe(this, Observer {
it.getContentIfNotHandled()?.let {
onBackPressed()
}
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

View File

@@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yogeshpaliyal.keypass.AppDatabase
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.constants.AccountType
import com.yogeshpaliyal.keypass.data.AccountModel
import com.yogeshpaliyal.keypass.utils.Event
import dagger.hilt.android.lifecycle.HiltViewModel
@@ -31,7 +32,7 @@ class AddTOTPViewModel @Inject constructor(private val appDatabase: AppDatabase)
val secretKey = secretKey.value
val accountName = accountName.value
if (accountName.isNullOrEmpty()){
if (secretKey.isNullOrEmpty()){
_error.postValue(Event(R.string.alert_black_secret_key))
return@launch
}
@@ -41,8 +42,9 @@ class AddTOTPViewModel @Inject constructor(private val appDatabase: AppDatabase)
return@launch
}
val accountModel = AccountModel(password = secretKey, title = accountName)
val accountModel = AccountModel(password = secretKey, title = accountName,type = AccountType.TOPT)
appDatabase.getDao().insertOrUpdateAccount(accountModel)
_goBack.postValue(Event(Unit))
}
}

View File

@@ -1,13 +1,13 @@
package com.yogeshpaliyal.keypass.ui.home
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.*
import com.yogeshpaliyal.keypass.AppDatabase
import com.yogeshpaliyal.keypass.data.AccountModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject
/*
@@ -27,16 +27,31 @@ class DashboardViewModel @Inject constructor(application: Application, val appDb
MutableLiveData<String>()
}
private val appDao = appDb.getDao()
val mediator = MediatorLiveData<LiveData<List<AccountModel>>>()
init {
mediator.addSource(keyword) {
mediator.postValue(appDb.getDao().getAllAccounts(keyword.value, tag.value))
mediator.postValue(appDao.getAllAccounts(keyword.value, tag.value))
}
mediator.addSource(tag) {
mediator.postValue(appDb.getDao().getAllAccounts(keyword.value, tag.value))
mediator.postValue(appDao.getAllAccounts(keyword.value, tag.value))
}
mediator.postValue(appDb.getDao().getAllAccounts(keyword.value, tag.value))
mediator.postValue(appDao.getAllAccounts(keyword.value, tag.value))
reloadData()
}
private fun reloadData(){
viewModelScope.launch(Dispatchers.IO) {
while (true){
delay(1000)
mediator.postValue(appDao.getAllAccounts(keyword.value, tag.value))
}
}
}
}

View File

@@ -72,8 +72,11 @@ class HomeFragment : Fragment() {
return binding.root
}
val observer = Observer<List<AccountModel>> {
mAdapter.updateData(Resource.success(it))
private val observer = Observer<List<AccountModel>> {
val newList = it.map {
it.copy(id = it.id, title = it.title, uniqueId = it.uniqueId, username = it.username, it.password, it.site, it.notes, it.tags, it.type)
}
mAdapter.updateData(Resource.success(ArrayList(newList)))
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

View File

@@ -19,6 +19,7 @@ import androidx.navigation.findNavController
import com.google.android.material.transition.MaterialElevationScale
import com.yogeshpaliyal.keypass.R
import com.yogeshpaliyal.keypass.databinding.ActivityDashboardBinding
import com.yogeshpaliyal.keypass.ui.addTOTP.AddTOTPActivity
import com.yogeshpaliyal.keypass.ui.detail.DetailActivity
import com.yogeshpaliyal.keypass.ui.generate.GeneratePasswordActivity
import com.yogeshpaliyal.keypass.ui.home.DashboardViewModel
@@ -86,7 +87,8 @@ class DashboardActivity :
}
}
DetailActivity.start(this)
// DetailActivity.start(this)
AddTOTPActivity.start(this)
}
bottomNavDrawer.apply {

View File

@@ -12,6 +12,10 @@ public class TOTPHelper {
return String.format(Locale.getDefault(),"%06d", generate(secret, System.currentTimeMillis() / 1000, 6));
}
public static long getProgress(){
return 30 - ((System.currentTimeMillis() / 1000)%30);
}
public static int generate(byte[] key, long t, int digits)
{
int r = 0;
@@ -44,6 +48,7 @@ public class TOTPHelper {
}
catch(Exception e){
e.printStackTrace();
}
return r;

View File

@@ -41,7 +41,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Secret Key"
android:text="@{mViewModel.secretKey}"
android:text="@={mViewModel.secretKey}"
android:id="@+id/etSecretKey"/>
</com.google.android.material.textfield.TextInputLayout>
@@ -59,7 +59,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/account_name"
android:text="@{mViewModel.accountName}"
android:text="@={mViewModel.accountName}"
android:id="@+id/etAccount"/>
</com.google.android.material.textfield.TextInputLayout>

View File

@@ -25,16 +25,15 @@
app:layout_constraintEnd_toEndOf="parent"
app:cardUseCompatPadding="true"
android:elevation="@dimen/plane_00"
android:clickable="true"
android:focusable="true"
app:cardElevation="0dp"
android:onClick="@{(view)->listener.onItemClick(view, model)}"
android:layout_marginVertical="@dimen/grid_0_25"
android:layout_marginHorizontal="@dimen/grid_0_5">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp">
android:paddingStart="8dp"
android:paddingVertical="12dp"
android:paddingEnd="12dp">
<com.google.android.material.textview.MaterialTextView
@@ -43,6 +42,7 @@
android:layout_height="60dp"
android:text="@{model.initials}"
android:gravity="center"
android:layout_marginStart="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
@@ -55,12 +55,13 @@
app:layout_constraintBottom_toBottomOf="@id/startIcon"
app:layout_constraintEnd_toEndOf="@id/startIcon"
app:layout_constraintStart_toStartOf="@id/startIcon"
android:progress="30"
android:progress="@{model.getTOtpProgress()}"
app:indicatorSize="65dp"
android:padding="0dp"
android:minHeight="0dp"
android:maxHeight="0dp"
android:max="60"
tools:progress="100"
android:max="30"
android:min="0"/>
<TextView