1
1
package com.mikhailgrigorev.quickpass
2
2
3
+ import android.Manifest
3
4
import android.annotation.SuppressLint
5
+ import android.app.Activity
4
6
import android.content.*
7
+ import android.content.pm.PackageManager
5
8
import android.content.res.Configuration
6
9
import android.database.Cursor
10
+ import android.net.Uri
11
+ import android.os.Build
7
12
import android.os.Bundle
8
13
import android.os.Handler
14
+ import android.provider.MediaStore
9
15
import android.text.Editable
10
16
import android.text.TextWatcher
17
+ import android.util.Log
11
18
import android.util.TypedValue
12
19
import android.view.KeyEvent
13
20
import android.view.MotionEvent
@@ -17,18 +24,55 @@ import android.widget.SeekBar
17
24
import android.widget.Toast
18
25
import androidx.appcompat.app.AppCompatActivity
19
26
import androidx.appcompat.app.AppCompatDelegate
27
+ import androidx.core.app.ActivityCompat
20
28
import androidx.core.content.ContextCompat
29
+ import androidx.core.content.FileProvider
21
30
import com.google.android.material.chip.Chip
22
31
import com.mikhailgrigorev.quickpass.dbhelpers.DataBaseHelper
23
32
import com.mikhailgrigorev.quickpass.dbhelpers.PasswordsDataBaseHelper
33
+ import kotlinx.android.synthetic.main.activity_edit_pass.*
24
34
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
25
68
import java.text.SimpleDateFormat
26
69
import java.util.*
27
70
import kotlin.random.Random
28
71
29
72
30
73
class NewPasswordActivity : AppCompatActivity () {
31
74
75
+ private var isImage = false
32
76
private val _keyTheme = " themePreference"
33
77
private val _preferenceFile = " quickPassPreference"
34
78
private var length = 20
@@ -393,10 +437,6 @@ class NewPasswordActivity : AppCompatActivity() {
393
437
}
394
438
395
439
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)
400
440
val intent = Intent ()
401
441
intent.putExtra(" login" , login)
402
442
setResult(1 , intent)
@@ -462,9 +502,114 @@ class NewPasswordActivity : AppCompatActivity() {
462
502
}
463
503
}
464
504
}
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
+ }
465
573
}
466
574
467
575
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
+
468
613
override fun onKeyUp (keyCode : Int , msg : KeyEvent ? ): Boolean {
469
614
when (keyCode) {
470
615
KeyEvent .KEYCODE_BACK -> {
@@ -492,5 +637,55 @@ class NewPasswordActivity : AppCompatActivity() {
492
637
private fun Context.toast (message : String )=
493
638
Toast .makeText(this ,message, Toast .LENGTH_SHORT ).show()
494
639
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
+ }
495
690
496
691
}
0 commit comments