2020-08-05 14:06:54 +02:00
|
|
|
package sushi.hardcore.droidfs
|
|
|
|
|
2020-08-09 15:27:31 +02:00
|
|
|
import android.graphics.Color
|
2020-08-05 14:06:54 +02:00
|
|
|
import android.os.Bundle
|
2020-08-09 15:27:31 +02:00
|
|
|
import android.text.InputType
|
2020-08-05 14:06:54 +02:00
|
|
|
import android.view.View
|
2020-08-09 15:27:31 +02:00
|
|
|
import android.view.WindowManager
|
|
|
|
import android.widget.EditText
|
2020-08-05 14:06:54 +02:00
|
|
|
import android.widget.Toast
|
|
|
|
import com.otaliastudios.cameraview.CameraListener
|
|
|
|
import com.otaliastudios.cameraview.PictureResult
|
|
|
|
import com.otaliastudios.cameraview.controls.Facing
|
|
|
|
import com.otaliastudios.cameraview.controls.Flash
|
|
|
|
import kotlinx.android.synthetic.main.activity_camera.*
|
2020-08-09 17:01:47 +02:00
|
|
|
import sushi.hardcore.droidfs.provider.RestrictedFileProvider
|
2020-08-05 14:06:54 +02:00
|
|
|
import sushi.hardcore.droidfs.util.GocryptfsVolume
|
|
|
|
import sushi.hardcore.droidfs.util.MiscUtils
|
|
|
|
import sushi.hardcore.droidfs.util.PathUtils
|
|
|
|
import sushi.hardcore.droidfs.widgets.ColoredAlertDialogBuilder
|
|
|
|
import java.io.ByteArrayInputStream
|
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
import java.util.*
|
|
|
|
|
2020-08-09 17:01:47 +02:00
|
|
|
class CameraActivity : BaseActivity() {
|
2020-08-05 14:06:54 +02:00
|
|
|
companion object {
|
|
|
|
private val flashModes = listOf(Flash.AUTO, Flash.ON, Flash.OFF)
|
|
|
|
private const val fileNameRandomMin = 100000
|
|
|
|
private const val fileNameRandomMax = 999999
|
|
|
|
private val dateFormat = SimpleDateFormat("yyyyMMdd_HHmmss")
|
|
|
|
private val random = Random()
|
|
|
|
}
|
|
|
|
private var currentFlashModeIndex = 0
|
2020-08-09 15:27:31 +02:00
|
|
|
private var timerDuration = 0
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
if (value > 0){
|
|
|
|
image_timer.setColorFilter(Color.GREEN)
|
|
|
|
} else {
|
|
|
|
image_timer.clearColorFilter()
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 14:06:54 +02:00
|
|
|
private lateinit var gocryptfsVolume: GocryptfsVolume
|
|
|
|
private lateinit var outputDirectory: String
|
|
|
|
private lateinit var fileName: String
|
2020-08-09 17:01:47 +02:00
|
|
|
private var isFinishingIntentionally = false
|
2020-08-05 14:06:54 +02:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_camera)
|
|
|
|
gocryptfsVolume = GocryptfsVolume(intent.getIntExtra("sessionID", -1))
|
|
|
|
outputDirectory = intent.getStringExtra("path")!!
|
|
|
|
camera.setLifecycleOwner(this)
|
|
|
|
camera.addCameraListener(object: CameraListener(){
|
|
|
|
override fun onPictureTaken(result: PictureResult) {
|
|
|
|
take_photo_button.onPhotoTaken()
|
|
|
|
val inputStream = ByteArrayInputStream(result.data)
|
|
|
|
if (gocryptfsVolume.importFile(inputStream, PathUtils.path_join(outputDirectory, fileName))){
|
|
|
|
Toast.makeText(applicationContext, getString(R.string.picture_save_success, fileName), Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
|
|
|
ColoredAlertDialogBuilder(applicationContext)
|
|
|
|
.setTitle(R.string.error)
|
|
|
|
.setMessage(R.string.picture_save_failed)
|
|
|
|
.setCancelable(false)
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ -> finish() }
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
take_photo_button.onClick = ::onClickTakePhoto
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onClickTakePhoto() {
|
|
|
|
val baseName = "IMG_"+dateFormat.format(Date())+"_"
|
|
|
|
do {
|
|
|
|
fileName = baseName+(random.nextInt(fileNameRandomMax-fileNameRandomMin)+fileNameRandomMin)+".jpg"
|
|
|
|
} while (gocryptfsVolume.pathExists(fileName))
|
2020-08-09 15:27:31 +02:00
|
|
|
if (timerDuration > 0){
|
|
|
|
text_timer.visibility = View.VISIBLE
|
|
|
|
Thread{
|
|
|
|
for (i in timerDuration downTo 1){
|
|
|
|
runOnUiThread { text_timer.text = i.toString() }
|
|
|
|
Thread.sleep(1000)
|
|
|
|
}
|
|
|
|
runOnUiThread {
|
|
|
|
camera.takePicture()
|
|
|
|
text_timer.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}.start()
|
|
|
|
} else {
|
|
|
|
camera.takePicture()
|
|
|
|
}
|
2020-08-05 14:06:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fun onClickFlash(view: View) {
|
|
|
|
currentFlashModeIndex = MiscUtils.incrementIndex(currentFlashModeIndex, flashModes)
|
|
|
|
camera.flash = flashModes[currentFlashModeIndex]
|
|
|
|
image_flash.setImageResource(when (camera.flash) {
|
|
|
|
Flash.AUTO -> R.drawable.icon_flash_auto
|
|
|
|
Flash.ON -> R.drawable.icon_flash_on
|
|
|
|
else -> R.drawable.icon_flash_off
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fun onClickCameraSwitch(view: View) {
|
|
|
|
camera.toggleFacing()
|
|
|
|
if (camera.facing == Facing.FRONT){
|
|
|
|
image_camera_switch.setImageResource(R.drawable.icon_camera_back)
|
|
|
|
} else {
|
|
|
|
image_camera_switch.setImageResource(R.drawable.icon_camera_front)
|
|
|
|
Thread {
|
|
|
|
Thread.sleep(25)
|
|
|
|
camera.flash = flashModes[currentFlashModeIndex] //refresh flash mode after switching camera
|
|
|
|
}.start()
|
|
|
|
}
|
|
|
|
}
|
2020-08-09 15:27:31 +02:00
|
|
|
|
|
|
|
fun onClickTimer(view: View) {
|
|
|
|
val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null)
|
|
|
|
val dialogEditText = dialogEditTextView.findViewById<EditText>(R.id.dialog_edit_text)
|
|
|
|
dialogEditText.inputType = InputType.TYPE_CLASS_NUMBER
|
|
|
|
val dialog = ColoredAlertDialogBuilder(this)
|
|
|
|
.setView(dialogEditTextView)
|
|
|
|
.setTitle("Enter the timer duration (in s)")
|
|
|
|
.setPositiveButton(R.string.ok) { _, _ ->
|
|
|
|
timerDuration = dialogEditText.text.toString().toInt()
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.create()
|
|
|
|
dialogEditText.setOnEditorActionListener { _, _, _ ->
|
|
|
|
timerDuration = dialogEditText.text.toString().toInt()
|
|
|
|
dialog.dismiss()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
|
|
|
dialog.show()
|
|
|
|
}
|
2020-08-09 17:01:47 +02:00
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
if (!isFinishingIntentionally) {
|
|
|
|
gocryptfsVolume.close()
|
|
|
|
RestrictedFileProvider.wipeAll(this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onBackPressed() {
|
|
|
|
super.onBackPressed()
|
|
|
|
isFinishingIntentionally = true
|
|
|
|
}
|
2020-08-05 14:06:54 +02:00
|
|
|
}
|