Skip to content

Commit 2537fdf

Browse files
committed
QuickPass 2.4.0
- image adding (beta 3)
1 parent 5121897 commit 2537fdf

File tree

10 files changed

+348
-59
lines changed

10 files changed

+348
-59
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@
214214
<category android:name="android.intent.category.LAUNCHER" />
215215
</intent-filter>
216216
</activity-alias>
217+
<provider
218+
android:name="androidx.core.content.FileProvider"
219+
android:authorities="${applicationId}.provider"
220+
android:exported="false"
221+
android:grantUriPermissions="true">
222+
<meta-data
223+
android:name="android.support.FILE_PROVIDER_PATHS"
224+
android:resource="@xml/provider_paths" />
225+
</provider>
217226
</application>
218227

219228
</manifest>

app/src/main/java/com/mikhailgrigorev/quickpass/EditPassActivity.kt

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import androidx.appcompat.app.AppCompatActivity
2626
import androidx.appcompat.app.AppCompatDelegate
2727
import androidx.core.app.ActivityCompat
2828
import androidx.core.content.ContextCompat
29+
import androidx.core.content.FileProvider
2930
import com.google.android.material.chip.Chip
3031
import com.mikhailgrigorev.quickpass.dbhelpers.DataBaseHelper
3132
import com.mikhailgrigorev.quickpass.dbhelpers.PasswordsDataBaseHelper
@@ -41,6 +42,7 @@ import java.util.*
4142

4243
class EditPassActivity : AppCompatActivity() {
4344

45+
private var isImage = false
4446
private val _keyTheme = "themePreference"
4547
private val _keyUsername = "prefUserNameKey"
4648
private val _preferenceFile = "quickPassPreference"
@@ -623,26 +625,32 @@ class EditPassActivity : AppCompatActivity() {
623625
if (file.exists()){
624626
val uri = Uri.fromFile(file)
625627
attachedImage.setImageURI(uri)
628+
clearImage.visibility = View.VISIBLE
629+
630+
val width = attachedImage.drawable.minimumWidth/2
631+
val height = attachedImage.drawable.minimumHeight/2
632+
attachedImage.layoutParams.height = height
633+
attachedImage.layoutParams.width = width
626634

627635
attachedImage.setOnClickListener {
628-
if (imageHolder.scaleX == 0.5f) {
629-
imageHolder.animate()
630-
.scaleY(1f)
631-
.scaleX(1f)
632-
.setInterpolator(AccelerateDecelerateInterpolator()).duration = 400
633-
//imageHolder.scaleX = 1f
634-
//imageHolder.scaleY = 1f
635-
} else {
636-
imageHolder.animate()
637-
.scaleY(0.5f)
638-
.scaleX(0.5f)
639-
.setInterpolator(AccelerateDecelerateInterpolator()).duration = 400
640-
//imageHolder.scaleX = 0.5f
641-
//imageHolder.scaleY = 0.5f
642-
}
636+
val uriForOpen = FileProvider.getUriForFile(
637+
this,
638+
this.applicationContext.packageName.toString() + ".provider",
639+
file
640+
)
641+
val intent = Intent()
642+
intent.action = Intent.ACTION_VIEW
643+
intent.setDataAndType(uriForOpen, "image/*")
644+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
645+
startActivity(intent)
643646
}
644647
}
645648

649+
clearImage.setOnClickListener {
650+
file.delete()
651+
attachedImage.setImageURI(null)
652+
}
653+
646654
}
647655

648656

@@ -727,6 +735,10 @@ class EditPassActivity : AppCompatActivity() {
727735
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
728736
// I'M GETTING THE URI OF THE IMAGE AS DATA AND SETTING IT TO THE IMAGEVIEW
729737
attachedImage.setImageURI(data?.data)
738+
val width = attachedImage.drawable.minimumWidth/2
739+
val height = attachedImage.drawable.minimumHeight/2
740+
attachedImage.layoutParams.height = height
741+
attachedImage.layoutParams.width = width
730742
if (ContextCompat.checkSelfPermission(
731743
this,
732744
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
@@ -759,6 +771,7 @@ class EditPassActivity : AppCompatActivity() {
759771
val file = File(mediaStorageDir, "$passName.jpg")
760772

761773
copyFile(File(getRealPathFromURI(selectedImageURI)), file)
774+
isImage = true
762775

763776
}
764777
}

app/src/main/java/com/mikhailgrigorev/quickpass/NewPasswordActivity.kt

Lines changed: 199 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package com.mikhailgrigorev.quickpass
22

3+
import android.Manifest
34
import android.annotation.SuppressLint
5+
import android.app.Activity
46
import android.content.*
7+
import android.content.pm.PackageManager
58
import android.content.res.Configuration
69
import android.database.Cursor
10+
import android.net.Uri
11+
import android.os.Build
712
import android.os.Bundle
813
import android.os.Handler
14+
import android.provider.MediaStore
915
import android.text.Editable
1016
import android.text.TextWatcher
17+
import android.util.Log
1118
import android.util.TypedValue
1219
import android.view.KeyEvent
1320
import android.view.MotionEvent
@@ -17,18 +24,55 @@ import android.widget.SeekBar
1724
import android.widget.Toast
1825
import androidx.appcompat.app.AppCompatActivity
1926
import androidx.appcompat.app.AppCompatDelegate
27+
import androidx.core.app.ActivityCompat
2028
import androidx.core.content.ContextCompat
29+
import androidx.core.content.FileProvider
2130
import com.google.android.material.chip.Chip
2231
import com.mikhailgrigorev.quickpass.dbhelpers.DataBaseHelper
2332
import com.mikhailgrigorev.quickpass.dbhelpers.PasswordsDataBaseHelper
33+
import kotlinx.android.synthetic.main.activity_edit_pass.*
2434
import kotlinx.android.synthetic.main.activity_new_password.*
35+
import kotlinx.android.synthetic.main.activity_new_password.accountAvatar
36+
import kotlinx.android.synthetic.main.activity_new_password.accountAvatarText
37+
import kotlinx.android.synthetic.main.activity_new_password.attachedImage
38+
import kotlinx.android.synthetic.main.activity_new_password.authToggle
39+
import kotlinx.android.synthetic.main.activity_new_password.back
40+
import kotlinx.android.synthetic.main.activity_new_password.cardPass
41+
import kotlinx.android.synthetic.main.activity_new_password.cryptToggle
42+
import kotlinx.android.synthetic.main.activity_new_password.email
43+
import kotlinx.android.synthetic.main.activity_new_password.emailField
44+
import kotlinx.android.synthetic.main.activity_new_password.emailSwitch
45+
import kotlinx.android.synthetic.main.activity_new_password.genPasswordId
46+
import kotlinx.android.synthetic.main.activity_new_password.genPasswordIdField
47+
import kotlinx.android.synthetic.main.activity_new_password.generatePassword
48+
import kotlinx.android.synthetic.main.activity_new_password.keyWordsField
49+
import kotlinx.android.synthetic.main.activity_new_password.lengthToggle
50+
import kotlinx.android.synthetic.main.activity_new_password.lettersToggle
51+
import kotlinx.android.synthetic.main.activity_new_password.newName
52+
import kotlinx.android.synthetic.main.activity_new_password.newNameField
53+
import kotlinx.android.synthetic.main.activity_new_password.noteField
54+
import kotlinx.android.synthetic.main.activity_new_password.numbersToggle
55+
import kotlinx.android.synthetic.main.activity_new_password.passQuality
56+
import kotlinx.android.synthetic.main.activity_new_password.passSettings
57+
import kotlinx.android.synthetic.main.activity_new_password.savePass
58+
import kotlinx.android.synthetic.main.activity_new_password.seekBar
59+
import kotlinx.android.synthetic.main.activity_new_password.symToggles
60+
import kotlinx.android.synthetic.main.activity_new_password.timeLimit
61+
import kotlinx.android.synthetic.main.activity_new_password.upload
62+
import kotlinx.android.synthetic.main.activity_new_password.upperCaseToggle
63+
import java.io.File
64+
import java.io.FileInputStream
65+
import java.io.FileOutputStream
66+
import java.io.IOException
67+
import java.nio.channels.FileChannel
2568
import java.text.SimpleDateFormat
2669
import java.util.*
2770
import kotlin.random.Random
2871

2972

3073
class NewPasswordActivity : AppCompatActivity() {
3174

75+
private var isImage = false
3276
private val _keyTheme = "themePreference"
3377
private val _preferenceFile = "quickPassPreference"
3478
private var length = 20
@@ -393,10 +437,6 @@ class NewPasswordActivity : AppCompatActivity() {
393437
}
394438

395439
back.setOnClickListener {
396-
//logo.visibility = View.VISIBLE
397-
//val rotation = AnimationUtils.loadAnimation(this, R.anim.rotate_splash)
398-
//rotation.fillAfter = true
399-
//logo.startAnimation(rotation)
400440
val intent = Intent()
401441
intent.putExtra("login", login)
402442
setResult(1, intent)
@@ -462,9 +502,114 @@ class NewPasswordActivity : AppCompatActivity() {
462502
}
463503
}
464504
}
505+
506+
upload.setOnClickListener{
507+
checkPermissionForImage()
508+
}
509+
510+
511+
512+
val mediaStorageDir = File(
513+
applicationContext.getExternalFilesDir("QuickPassPhotos")!!.absolutePath
514+
)
515+
if (!mediaStorageDir.exists()) {
516+
mediaStorageDir.mkdirs()
517+
Toast.makeText(applicationContext, "Directory Created", Toast.LENGTH_LONG).show()
518+
}
519+
520+
if (!mediaStorageDir.exists()) {
521+
if (!mediaStorageDir.mkdirs()) {
522+
Log.d("App", "failed to create directory")
523+
}
524+
}
525+
526+
val file = File(mediaStorageDir, "${newNameField.text}.jpg")
527+
if (file.exists()){
528+
val uri = Uri.fromFile(file)
529+
attachedImage.setImageURI(uri)
530+
clearImage.visibility = View.VISIBLE
531+
532+
val width = attachedImage.drawable.minimumWidth/2
533+
val height = attachedImage.drawable.minimumHeight/2
534+
attachedImage.layoutParams.height = height
535+
attachedImage.layoutParams.width = width
536+
537+
attachedImage.setOnClickListener {
538+
val uriForOpen = FileProvider.getUriForFile(
539+
this,
540+
this.applicationContext.packageName.toString() + ".provider",
541+
file
542+
)
543+
val intent = Intent()
544+
intent.action = Intent.ACTION_VIEW
545+
intent.setDataAndType(uriForOpen, "image/*")
546+
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
547+
startActivity(intent)
548+
}
549+
}
550+
551+
552+
}
553+
554+
555+
val PERMISSION_CODE_READ = 1001
556+
val PERMISSION_CODE_WRITE = 1002
557+
val IMAGE_PICK_CODE = 1000
558+
559+
private fun checkPermissionForImage() {
560+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
561+
if ((checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
562+
&& (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
563+
) {
564+
val permission = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)
565+
val permissionCoarse = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)
566+
567+
requestPermissions(permission, PERMISSION_CODE_READ) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_READ LIKE 1001
568+
requestPermissions(permissionCoarse, PERMISSION_CODE_WRITE) // GIVE AN INTEGER VALUE FOR PERMISSION_CODE_WRITE LIKE 1002
569+
} else {
570+
pickImageFromGallery()
571+
}
572+
}
465573
}
466574

467575

576+
private fun pickImageFromGallery() {
577+
val intent = Intent(Intent.ACTION_PICK)
578+
intent.type = "image/*"
579+
startActivityForResult(intent, IMAGE_PICK_CODE) // GIVE AN INTEGER VALUE FOR IMAGE_PICK_CODE LIKE 1000
580+
}
581+
582+
583+
@Throws(IOException::class)
584+
private fun copyFile(sourceFile: File, destFile: File) {
585+
if (!sourceFile.exists()) {
586+
return
587+
}
588+
var source: FileChannel? = null
589+
var destination: FileChannel? = null
590+
source = FileInputStream(sourceFile).channel
591+
destination = FileOutputStream(destFile).channel
592+
if (destination != null && source != null) {
593+
destination.transferFrom(source, 0, source.size())
594+
}
595+
source?.close()
596+
destination?.close()
597+
}
598+
599+
private fun getRealPathFromURI(contentURI: Uri): String? {
600+
val result: String?
601+
val cursor = contentResolver.query(contentURI, null, null, null, null)
602+
if (cursor == null) { // Source is Dropbox or other similar local file path
603+
result = contentURI.path
604+
} else {
605+
cursor.moveToFirst()
606+
val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
607+
result = cursor.getString(idx)
608+
cursor.close()
609+
}
610+
return result
611+
}
612+
468613
override fun onKeyUp(keyCode: Int, msg: KeyEvent?): Boolean {
469614
when (keyCode) {
470615
KeyEvent.KEYCODE_BACK -> {
@@ -492,5 +637,55 @@ class NewPasswordActivity : AppCompatActivity() {
492637
private fun Context.toast(message:String)=
493638
Toast.makeText(this,message, Toast.LENGTH_SHORT).show()
494639

640+
@SuppressLint("SdCardPath")
641+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
642+
super.onActivityResult(requestCode, resultCode, data)
643+
if (requestCode == 1) {
644+
if (resultCode == 1) {
645+
recreate()
646+
}
647+
}
648+
if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE) {
649+
// I'M GETTING THE URI OF THE IMAGE AS DATA AND SETTING IT TO THE IMAGEVIEW
650+
attachedImage.setImageURI(data?.data)
651+
val width = attachedImage.drawable.minimumWidth/2
652+
val height = attachedImage.drawable.minimumHeight/2
653+
attachedImage.layoutParams.height = height
654+
attachedImage.layoutParams.width = width
655+
if (ContextCompat.checkSelfPermission(
656+
this,
657+
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
658+
)
659+
!= PackageManager.PERMISSION_GRANTED
660+
) {
661+
ActivityCompat.requestPermissions(
662+
this,
663+
arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE),
664+
PackageManager.PERMISSION_GRANTED
665+
)
666+
}
667+
668+
val selectedImageURI: Uri = data?.data!!
669+
670+
val mediaStorageDir = File(
671+
applicationContext.getExternalFilesDir("QuickPassPhotos")!!.absolutePath
672+
)
673+
if (!mediaStorageDir.exists()) {
674+
mediaStorageDir.mkdirs()
675+
Toast.makeText(applicationContext, "Directory Created", Toast.LENGTH_LONG).show()
676+
}
677+
678+
if (!mediaStorageDir.exists()) {
679+
if (!mediaStorageDir.mkdirs()) {
680+
Log.d("App", "failed to create directory")
681+
}
682+
}
683+
684+
val file = File(mediaStorageDir, "${newNameField.text}.jpg")
685+
686+
copyFile(File(getRealPathFromURI(selectedImageURI)), file)
687+
isImage = true
688+
}
689+
}
495690

496691
}

0 commit comments

Comments
 (0)