2020-07-17 16:35:39 +02:00
|
|
|
package sushi.hardcore.droidfs
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Build
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.widget.AdapterView.OnItemClickListener
|
2020-07-27 16:20:52 +02:00
|
|
|
import android.widget.TextView
|
2020-07-17 16:35:39 +02:00
|
|
|
import kotlinx.android.synthetic.main.activity_open.checkbox_remember_path
|
|
|
|
import kotlinx.android.synthetic.main.activity_open.checkbox_save_password
|
|
|
|
import kotlinx.android.synthetic.main.activity_open.edit_password
|
|
|
|
import kotlinx.android.synthetic.main.activity_open.edit_volume_path
|
|
|
|
import kotlinx.android.synthetic.main.activity_open.saved_path_listview
|
|
|
|
import kotlinx.android.synthetic.main.toolbar.*
|
|
|
|
import sushi.hardcore.droidfs.adapters.SavedVolumesAdapter
|
|
|
|
import sushi.hardcore.droidfs.explorers.ExplorerActivity
|
|
|
|
import sushi.hardcore.droidfs.explorers.ExplorerActivityDrop
|
|
|
|
import sushi.hardcore.droidfs.explorers.ExplorerActivityPick
|
|
|
|
import sushi.hardcore.droidfs.fingerprint_stuff.FingerprintPasswordHashSaver
|
2020-07-26 21:16:06 +02:00
|
|
|
import sushi.hardcore.droidfs.util.PathUtils
|
2020-07-17 16:35:39 +02:00
|
|
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
|
|
|
import sushi.hardcore.droidfs.util.WidgetUtil
|
|
|
|
import sushi.hardcore.droidfs.util.Wiper
|
2020-07-27 16:20:52 +02:00
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
2020-07-17 16:35:39 +02:00
|
|
|
import java.io.File
|
|
|
|
import java.util.*
|
|
|
|
|
2020-07-21 15:05:05 +02:00
|
|
|
class OpenActivity : BaseActivity() {
|
2020-07-17 16:35:39 +02:00
|
|
|
companion object {
|
|
|
|
private const val PICK_DIRECTORY_REQUEST_CODE = 1
|
|
|
|
}
|
|
|
|
private lateinit var savedVolumesAdapter: SavedVolumesAdapter
|
|
|
|
private lateinit var fingerprintPasswordHashSaver: FingerprintPasswordHashSaver
|
2020-07-27 16:20:52 +02:00
|
|
|
private lateinit var rootCipherDir: String
|
2020-07-17 16:35:39 +02:00
|
|
|
private var sessionID = -1
|
|
|
|
private var usf_fingerprint = false
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_open)
|
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
usf_fingerprint = sharedPrefs.getBoolean("usf_fingerprint", false)
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && usf_fingerprint) {
|
|
|
|
fingerprintPasswordHashSaver = FingerprintPasswordHashSaver(this, sharedPrefs)
|
|
|
|
} else {
|
|
|
|
WidgetUtil.hide(checkbox_save_password)
|
|
|
|
}
|
|
|
|
savedVolumesAdapter = SavedVolumesAdapter(this, sharedPrefs)
|
|
|
|
if (savedVolumesAdapter.count > 0){
|
|
|
|
saved_path_listview.adapter = savedVolumesAdapter
|
|
|
|
saved_path_listview.onItemClickListener = OnItemClickListener { _, _, position, _ ->
|
2020-07-27 16:20:52 +02:00
|
|
|
rootCipherDir = savedVolumesAdapter.getItem(position)
|
|
|
|
edit_volume_path.setText(rootCipherDir)
|
|
|
|
val cipherText = sharedPrefs.getString(rootCipherDir, null)
|
2020-07-17 16:35:39 +02:00
|
|
|
if (cipherText != null){ //password hash saved
|
2020-07-27 16:20:52 +02:00
|
|
|
fingerprintPasswordHashSaver.decrypt(cipherText, rootCipherDir, ::openUsingPasswordHash)
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WidgetUtil.hide(saved_path_listview)
|
|
|
|
}
|
|
|
|
edit_password.setOnEditorActionListener { v, _, _ ->
|
|
|
|
onClickOpen(v)
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:05:05 +02:00
|
|
|
fun pickDirectory(view: View?) {
|
2020-07-17 16:35:39 +02:00
|
|
|
val i = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
|
|
|
startActivityForResult(i, PICK_DIRECTORY_REQUEST_CODE)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
if (resultCode == Activity.RESULT_OK) {
|
|
|
|
if (requestCode == PICK_DIRECTORY_REQUEST_CODE) {
|
|
|
|
if (data != null) {
|
2020-07-26 21:16:06 +02:00
|
|
|
val path = PathUtils.getFullPathFromTreeUri(data.data, this)
|
2020-07-17 16:35:39 +02:00
|
|
|
edit_volume_path.setText(path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onClickOpen(view: View?) {
|
2020-07-27 16:20:52 +02:00
|
|
|
val dialogLoadingView = layoutInflater.inflate(R.layout.dialog_loading, null)
|
|
|
|
val dialogTextMessage = dialogLoadingView.findViewById<TextView>(R.id.text_message)
|
|
|
|
dialogTextMessage.text = getString(R.string.loading_msg_open)
|
|
|
|
val dialogLoading = ColoredAlertDialogBuilder(this)
|
|
|
|
.setView(dialogLoadingView)
|
|
|
|
.setTitle(R.string.loading)
|
|
|
|
.setCancelable(false)
|
|
|
|
.create()
|
|
|
|
dialogLoading.show()
|
|
|
|
Thread {
|
|
|
|
rootCipherDir = edit_volume_path.text.toString() //fresh get in case of manual rewrite
|
|
|
|
if (rootCipherDir.isEmpty()) {
|
|
|
|
dialogLoading.dismiss()
|
|
|
|
toastFromThread(R.string.enter_volume_path)
|
|
|
|
} else {
|
|
|
|
val password = edit_password.text.toString().toCharArray()
|
|
|
|
var returnedHash: ByteArray? = null
|
|
|
|
if (usf_fingerprint && checkbox_save_password.isChecked){
|
|
|
|
returnedHash = ByteArray(GocryptfsVolume.KeyLen)
|
|
|
|
}
|
|
|
|
sessionID = GocryptfsVolume.init(rootCipherDir, password, null, returnedHash)
|
|
|
|
if (sessionID != -1) {
|
|
|
|
var startExplorerImmediately = true
|
|
|
|
if (checkbox_remember_path.isChecked) {
|
|
|
|
savedVolumesAdapter.addVolumePath(rootCipherDir)
|
|
|
|
if (checkbox_save_password.isChecked && returnedHash != null){
|
|
|
|
fingerprintPasswordHashSaver.encryptAndSave(returnedHash, rootCipherDir) { success ->
|
|
|
|
dialogLoading.dismiss()
|
|
|
|
if (success){
|
|
|
|
startExplorer()
|
|
|
|
}
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
startExplorerImmediately = false
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
}
|
|
|
|
if (startExplorerImmediately){
|
|
|
|
dialogLoading.dismiss()
|
|
|
|
startExplorer()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dialogLoading.dismiss()
|
|
|
|
runOnUiThread {
|
|
|
|
ColoredAlertDialogBuilder(this)
|
|
|
|
.setTitle(R.string.open_volume_failed)
|
|
|
|
.setMessage(R.string.open_volume_failed_msg)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
Arrays.fill(password, 0.toChar())
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
}.start()
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun openUsingPasswordHash(passwordHash: ByteArray){
|
2020-07-27 16:20:52 +02:00
|
|
|
sessionID = GocryptfsVolume.init(rootCipherDir, null, passwordHash, null)
|
2020-07-17 16:35:39 +02:00
|
|
|
if (sessionID != -1){
|
|
|
|
startExplorer()
|
|
|
|
} else {
|
2020-07-27 16:20:52 +02:00
|
|
|
ColoredAlertDialogBuilder(this)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setTitle(R.string.open_volume_failed)
|
2020-07-26 21:16:06 +02:00
|
|
|
.setMessage(R.string.open_failed_hash_msg)
|
2020-07-17 16:35:39 +02:00
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
Arrays.fill(passwordHash, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun startExplorer() {
|
2020-07-27 16:20:52 +02:00
|
|
|
var explorerIntent: Intent? = null
|
|
|
|
val currentIntentAction = intent.action
|
|
|
|
if (currentIntentAction != null) {
|
|
|
|
if ((currentIntentAction == Intent.ACTION_SEND || currentIntentAction == Intent.ACTION_SEND_MULTIPLE) && intent.extras != null) { //import via android share menu
|
|
|
|
explorerIntent = Intent(this, ExplorerActivityDrop::class.java)
|
|
|
|
explorerIntent.action = currentIntentAction //forward action
|
|
|
|
explorerIntent.putExtras(intent.extras!!) //forward extras
|
|
|
|
} else if (currentIntentAction == "pick") { //pick items to import
|
|
|
|
explorerIntent = Intent(this, ExplorerActivityPick::class.java)
|
|
|
|
explorerIntent.flags = Intent.FLAG_ACTIVITY_FORWARD_RESULT
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
if (explorerIntent == null) {
|
|
|
|
explorerIntent = Intent(this, ExplorerActivity::class.java) //default opening
|
2020-07-17 16:35:39 +02:00
|
|
|
}
|
2020-07-27 16:20:52 +02:00
|
|
|
explorerIntent.putExtra("sessionID", sessionID)
|
|
|
|
explorerIntent.putExtra("volume_name", File(rootCipherDir).name)
|
|
|
|
startActivity(explorerIntent)
|
2020-07-17 16:35:39 +02:00
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onClickSavePasswordHash(view: View) {
|
|
|
|
if (checkbox_save_password.isChecked){
|
|
|
|
if (!fingerprintPasswordHashSaver.canAuthenticate()){
|
|
|
|
checkbox_save_password.isChecked = false
|
|
|
|
} else {
|
|
|
|
checkbox_remember_path.isChecked = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onClickRememberPath(view: View) {
|
|
|
|
if (!checkbox_remember_path.isChecked){
|
|
|
|
checkbox_save_password.isChecked = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
if (::fingerprintPasswordHashSaver.isInitialized && fingerprintPasswordHashSaver.isListening){
|
|
|
|
fingerprintPasswordHashSaver.stopListening()
|
|
|
|
if (fingerprintPasswordHashSaver.fingerprintFragment.isAdded){
|
|
|
|
fingerprintPasswordHashSaver.fingerprintFragment.dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
Wiper.wipeEditText(edit_password)
|
|
|
|
}
|
|
|
|
}
|