Skip to content

Commit

Permalink
add password visibility option
Browse files Browse the repository at this point in the history
  • Loading branch information
WirelessAlien committed Feb 27, 2024
1 parent 4b15fa8 commit 50a0454
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 17 deletions.
96 changes: 87 additions & 9 deletions app/src/main/java/com/wirelessalien/zipxtract/ExtractFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageButton
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.documentfile.provider.DocumentFile
Expand Down Expand Up @@ -453,6 +454,22 @@ class ExtractFragment : Fragment() {
val inflater = layoutInflater
val dialogView = inflater.inflate(R.layout.password_input_dialog, null)
val passwordEditText = dialogView.findViewById<EditText>(R.id.passwordInput)
val showPasswordButton = dialogView.findViewById<ImageButton>(R.id.showPasswordButton)

var isPasswordVisible = false

showPasswordButton.setOnClickListener {
isPasswordVisible = !isPasswordVisible
if (isPasswordVisible) {
// Show password
passwordEditText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_on)
} else {
// Hide password
passwordEditText.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_off)
}
}

val passwordDialog = MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
Expand All @@ -464,6 +481,7 @@ class ExtractFragment : Fragment() {
extractSplitZipFile(null, outputDirectory)
}
passwordDialog.show()

}
}

Expand Down Expand Up @@ -685,22 +703,42 @@ class ExtractFragment : Fragment() {
val tempFile = createTempFileFromInputStreamAsync(bufferedInputStream)

if (isZipFileEncrypted(tempFile)) {
val inflater = layoutInflater
val dialogView = inflater.inflate(R.layout.password_input_dialog, null)

// Find the password EditText and show password button in the custom layout
val passwordEditText = dialogView.findViewById<EditText>(R.id.passwordInput)
val showPasswordButton = dialogView.findViewById<ImageButton>(R.id.showPasswordButton)

var isPasswordVisible = false

showPasswordButton.setOnClickListener {
isPasswordVisible = !isPasswordVisible
if (isPasswordVisible) {
// Show password
passwordEditText.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_on)
} else {
// Hide password
passwordEditText.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_off)
}
}

// Ask for password
val passwordEditText = EditText(requireContext())
passwordEditText.inputType = InputType.TYPE_TEXT_VARIATION_PASSWORD

val passwordDialog = MaterialAlertDialogBuilder(requireContext())
// Create the password input dialog
val passwordDialog = MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
.setView(passwordEditText)
.setView(dialogView) // Set the custom layout as the dialog view
.setPositiveButton(getString(R.string.extract)) { _, _ ->
val password = passwordEditText.text.toString()

zip4jExtractZipFile(tempFile, password, outputDirectory)
}
.setNegativeButton(getString(R.string.cancel), null)
.create()

// Show the password input dialog
passwordDialog.show()
} else {

Expand Down Expand Up @@ -1037,18 +1075,39 @@ class ExtractFragment : Fragment() {
val inflater = layoutInflater
val dialogView = inflater.inflate(R.layout.password_input_dialog, null)
val passwordInputView = dialogView.findViewById<EditText>(R.id.passwordInput)
val showPasswordButton = dialogView.findViewById<ImageButton>(R.id.showPasswordButton)

var isPasswordVisible = false

showPasswordButton.setOnClickListener {
isPasswordVisible = !isPasswordVisible
if (isPasswordVisible) {
// Show password
passwordInputView.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_on)
} else {
// Hide password
passwordInputView.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_off)
}
}

MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
.setView(dialogView)
.setPositiveButton(getString(R.string.extract)) { _, _ ->
val password = passwordInputView.text.toString()
callback(password.takeIf { it.isNotEmpty() })
}.setNegativeButton(getString(R.string.no_password)) { _, _ ->
}
.setNegativeButton(getString(R.string.no_password)) { _, _ ->
callback(null)
}.show()
}
.show()
}


private fun extractXz(bufferedInputStream: BufferedInputStream, outputDirectory: DocumentFile?) {
toggleExtractButtonEnabled(false)

Expand Down Expand Up @@ -1178,6 +1237,24 @@ class ExtractFragment : Fragment() {
val inflater = layoutInflater
val dialogView = inflater.inflate(R.layout.password_input_dialog, null)
val passwordInputView = dialogView.findViewById<EditText>(R.id.passwordInput)
val showPasswordButton = dialogView.findViewById<ImageButton>(R.id.showPasswordButton)

var isPasswordVisible = false

showPasswordButton.setOnClickListener {
isPasswordVisible = !isPasswordVisible
if (isPasswordVisible) {
// Show password
passwordInputView.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_on)
} else {
// Hide password
passwordInputView.inputType =
InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
showPasswordButton.setImageResource(R.drawable.ic_visibility_off)
}
}

MaterialAlertDialogBuilder(requireContext(), R.style.MaterialDialog)
.setTitle(getString(R.string.enter_password))
Expand All @@ -1192,6 +1269,7 @@ class ExtractFragment : Fragment() {
.show()
}


//Thanks for the sample code
//https://github.com/omicronapps/7-Zip-JBinding-4Android/issues/5#issuecomment-744128480
private fun extractRar(archiveFormat: ArchiveFormat?, archiveFileUri: Uri) {
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/ic_visibility_off.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#FF000000"
android:pathData="m644,532 l-58,-58q11,-53 -27.5,-90.5T466,354l-58,-58q17,-8 35,-12t37,-4q75,0 127.5,52.5T660,460q0,19 -4,37t-12,35ZM772,658 L714,602q38,-29 68,-64.5t50,-77.5q-50,-101 -144.5,-160.5T480,240q-29,0 -56.5,4T368,256l-62,-62q42,-17 85.5,-25.5T480,160q137,0 250.5,72T904,426q4,8 6,16.5t2,17.5q0,9 -1.5,17.5T905,494q-22,49 -56,90.5T772,658ZM480,760q-134,0 -245,-72.5T60,497q-5,-8 -7.5,-17.5T50,460q0,-10 2,-19t7,-18q20,-40 46.5,-76.5T166,280l-83,-84q-11,-12 -10.5,-28.5T84,140q11,-11 28,-11t28,11l680,680q11,11 11.5,27.5T820,876q-11,11 -28,11t-28,-11L624,738q-35,11 -71,16.5t-73,5.5ZM222,336q-29,26 -53,57t-41,67q50,101 144.5,160.5T480,680q20,0 39,-2.5t39,-5.5l-36,-38q-11,3 -21,4.5t-21,1.5q-75,0 -127.5,-52.5T300,460q0,-11 1.5,-21t4.5,-21l-84,-82ZM541,429ZM390,504Z"/>
</vector>
27 changes: 27 additions & 0 deletions app/src/main/res/drawable/ic_visibility_on.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!--
~ Copyright (C) 2023 WirelessAlien <https://github.com/WirelessAlien>
~
~ This program is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ any later version.
~
~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="#FF000000"
android:pathData="M480,640q75,0 127.5,-52.5T660,460q0,-75 -52.5,-127.5T480,280q-75,0 -127.5,52.5T300,460q0,75 52.5,127.5T480,640ZM480,568q-45,0 -76.5,-31.5T372,460q0,-45 31.5,-76.5T480,352q45,0 76.5,31.5T588,460q0,45 -31.5,76.5T480,568ZM480,760q-134,0 -244.5,-72T61,498q-5,-9 -7.5,-18.5T51,460q0,-10 2.5,-19.5T61,422q64,-118 174.5,-190T480,160q134,0 244.5,72T899,422q5,9 7.5,18.5T909,460q0,10 -2.5,19.5T899,498q-64,118 -174.5,190T480,760ZM480,460ZM480,680q113,0 207.5,-59.5T832,460q-50,-101 -144.5,-160.5T480,240q-113,0 -207.5,59.5T128,460q50,101 144.5,160.5T480,680Z"/>
</vector>
33 changes: 25 additions & 8 deletions app/src/main/res/layout/password_input_dialog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,30 @@
android:layout_height="wrap_content"
style="@style/Widget.Material3.CardView.Outlined">

<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:hint="@string/enter_password"
android:autofillHints="password" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/passwordInput"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textPassword"
style="@style/Widget.Material3.TextInputEditText.OutlinedBox"
android:hint="@string/enter_password"
android:autofillHints="password" />

<ImageButton
android:id="@+id/showPasswordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="@style/Widget.Material3.Button.IconButton"
android:src="@drawable/ic_visibility_off"
android:contentDescription="Show password" />

</LinearLayout>
</com.google.android.material.card.MaterialCardView>
</LinearLayout>

0 comments on commit 50a0454

Please sign in to comment.