Bläddra i källkod

可用版本,扩展了fossify

sequoia00 1 vecka sedan
förälder
incheckning
7b914283dd

+ 82 - 48
File-Manager-1.6.4/app/src/main/kotlin/org/fossify/filemanager/adapters/ItemsAdapter.kt

@@ -98,6 +98,8 @@ import org.fossify.filemanager.databinding.ItemFileDirListBinding
 import org.fossify.filemanager.databinding.ItemFileGridBinding
 import org.fossify.filemanager.databinding.ItemSectionBinding
 import org.fossify.filemanager.dialogs.CompressAsDialog
+import org.fossify.filemanager.dialogs.FileOperationProgressDialog
+import org.fossify.filemanager.dialogs.PasswordConfirmationDialog
 import org.fossify.filemanager.extensions.config
 import org.fossify.filemanager.extensions.isPathOnRoot
 import org.fossify.filemanager.extensions.isZipFile
@@ -238,10 +240,12 @@ class ItemsAdapter(
     override fun onActionModeCreated() {
         swipeRefreshLayout?.isRefreshing = false
         swipeRefreshLayout?.isEnabled = false
+        notifyDataSetChanged()
     }
 
     override fun onActionModeDestroyed() {
         swipeRefreshLayout?.isEnabled = config.enablePullToRefresh
+        notifyDataSetChanged()
     }
 
     override fun getItemViewType(position: Int): Int {
@@ -692,10 +696,14 @@ class ItemsAdapter(
                     return@handleSAFDialog
                 }
 
-                EnterPasswordDialog(activity, { password ->
-                    activity.toast(R.string.encrypting)
+                PasswordConfirmationDialog(activity) { password ->
+                    val progressDialog = FileOperationProgressDialog(activity, R.string.encrypting)
+                    progressDialog.start()
                     ensureBackgroundThread {
-                        val counts = encryptItems(selected, password)
+                        val counts = encryptItems(selected, password) { fileName, fileIndex, fileCount, processed, total ->
+                            progressDialog.update(fileName, fileIndex, fileCount, processed, total)
+                        }
+                        progressDialog.finish()
                         activity.runOnUiThread {
                             when {
                                 counts.failed > 0 -> activity.toast(R.string.encryption_failed)
@@ -706,9 +714,7 @@ class ItemsAdapter(
                             finishActMode()
                         }
                     }
-                }, {
-                    // user canceled the password prompt
-                })
+                }
             }
         }
     }
@@ -730,9 +736,13 @@ class ItemsAdapter(
                 }
 
                 EnterPasswordDialog(activity, { password ->
-                    activity.toast(R.string.decrypting)
+                    val progressDialog = FileOperationProgressDialog(activity, R.string.decrypting)
+                    progressDialog.start()
                     ensureBackgroundThread {
-                        val counts = decryptItems(selected, password)
+                        val counts = decryptItems(selected, password) { fileName, fileIndex, fileCount, processed, total ->
+                            progressDialog.update(fileName, fileIndex, fileCount, processed, total)
+                        }
+                        progressDialog.finish()
                         activity.runOnUiThread {
                             when {
                                 counts.failed > 0 -> activity.toast(R.string.decryption_failed)
@@ -750,57 +760,68 @@ class ItemsAdapter(
         }
     }
 
-    private fun encryptItems(items: List<FileDirItem>, password: String): EncDecCounts {
-        var ok = 0
-        var skipped = 0
-        var failed = 0
-        items.forEach { item ->
-            val files = if (item.isDirectory) {
+    private fun encryptItems(
+        items: List<FileDirItem>,
+        password: String,
+        onProgress: (String, Int, Int, Long, Long) -> Unit = { _, _, _, _, _ -> }
+    ): EncDecCounts {
+        val allFiles = items.flatMap { item ->
+            if (item.isDirectory) {
                 File(item.path).walkTopDown().filter { it.isFile }.toList()
             } else {
                 listOf(File(item.path))
             }
-            files.forEach { f ->
-                try {
-                    if (f.exists() && f.length() > 0L) {
-                        CryptoHelper.encryptFile(f, password)
-                        ok++
-                    } else {
-                        skipped++
+        }
+        var ok = 0
+        var skipped = 0
+        var failed = 0
+        allFiles.forEachIndexed { index, f ->
+            try {
+                if (f.exists() && f.length() > 0L) {
+                    CryptoHelper.encryptFile(f, password) { processed, total ->
+                        onProgress(f.name, index + 1, allFiles.size, processed, total)
                     }
-                } catch (e: AlreadyEncryptedException) {
+                    ok++
+                } else {
                     skipped++
-                } catch (e: Exception) {
-                    failed++
                 }
+            } catch (e: AlreadyEncryptedException) {
+                skipped++
+            } catch (e: Exception) {
+                failed++
             }
         }
         return EncDecCounts(ok, skipped, failed)
     }
 
-    private fun decryptItems(items: List<FileDirItem>, password: String): EncDecCounts {
-        var ok = 0
-        var skipped = 0
-        var failed = 0
-        items.forEach { item ->
+    private fun decryptItems(
+        items: List<FileDirItem>,
+        password: String,
+        onProgress: (String, Int, Int, Long, Long) -> Unit = { _, _, _, _, _ -> }
+    ): EncDecCounts {
+        val allFiles = items.flatMap { item ->
             val files = if (item.isDirectory) {
-                File(item.path).walkTopDown()
-                    .filter { it.isFile && it.name.endsWith(CryptoHelper.LOCKED_SUFFIX) }
-                    .toList()
+                File(item.path).walkTopDown().filter { it.isFile }.toList()
             } else {
                 listOf(File(item.path))
             }
-            files.forEach { f ->
-                if (!CryptoHelper.isEncrypted(f)) {
-                    skipped++
-                    return@forEach
-                }
-                try {
-                    CryptoHelper.decryptFile(f, password)
-                    ok++
-                } catch (e: Exception) {
-                    failed++
+            files.filter { it.exists() && it.name.endsWith(CryptoHelper.LOCKED_SUFFIX) }
+        }
+        var ok = 0
+        var skipped = 0
+        var failed = 0
+        allFiles.forEachIndexed { index, f ->
+            if (!CryptoHelper.isEncrypted(f)) {
+                skipped++
+                return@forEachIndexed
+            }
+            try {
+                CryptoHelper.decryptFile(f, password) { processed, total ->
+                    onProgress(f.name, index + 1, allFiles.size, processed, total)
                 }
+                ok++
+            } catch (e: Exception) {
+                failed++
             }
         }
         return EncDecCounts(ok, skipped, failed)
@@ -1205,10 +1226,23 @@ class ItemsAdapter(
                 itemDate?.setTextColor(textColor)
                 itemDate?.setTextSize(TypedValue.COMPLEX_UNIT_PX, smallerFontSize)
 
-                itemCheck?.beVisibleIf(isSelected)
-                if (isSelected) {
-                    itemCheck?.background?.applyColorFilter(properPrimaryColor)
-                    itemCheck?.applyColorFilter(contrastColor)
+                val inSelectionMode = selectedKeys.isNotEmpty()
+                itemCheck?.apply {
+                    when {
+                        !inSelectionMode -> beGone()
+                        isSelected -> {
+                            beVisible()
+                            background?.applyColorFilter(properPrimaryColor)
+                            applyColorFilter(contrastColor)
+                            imageAlpha = 255
+                        }
+                        else -> {
+                            beVisible()
+                            background?.applyColorFilter(0x40FFFFFF)
+                            applyColorFilter(0x00FFFFFF)
+                            imageAlpha = 0
+                        }
+                    }
                 }
 
                 if (!isListViewType && !listItem.isDirectory) {
@@ -1446,7 +1480,7 @@ class ItemsAdapter(
         override val itemIcon: ImageView = binding.itemIcon
         override val itemDetails: TextView = binding.itemDetails
         override val itemDate: TextView = binding.itemDate
-        override val itemCheck: ImageView? = null
+        override val itemCheck: ImageView = binding.itemCheck
         override val itemSection: TextView? = null
 
         override fun getRoot(): View = binding.root
@@ -1470,7 +1504,7 @@ class ItemsAdapter(
         override val itemIcon: ImageView = binding.itemIcon
         override val itemDetails: TextView? = null
         override val itemDate: TextView? = null
-        override val itemCheck: ImageView? = null
+        override val itemCheck: ImageView = binding.itemCheck
         override val itemSection: TextView? = null
 
         override fun getRoot(): View = binding.root

+ 45 - 0
File-Manager-1.6.4/app/src/main/kotlin/org/fossify/filemanager/dialogs/FileOperationProgressDialog.kt

@@ -0,0 +1,45 @@
+package org.fossify.filemanager.dialogs
+
+import androidx.appcompat.app.AlertDialog
+import org.fossify.commons.activities.BaseSimpleActivity
+import org.fossify.commons.extensions.getAlertDialogBuilder
+import org.fossify.commons.extensions.setupDialogStuff
+import org.fossify.filemanager.databinding.DialogFileOperationProgressBinding
+
+class FileOperationProgressDialog(
+    private val activity: BaseSimpleActivity,
+    private val titleRes: Int
+) {
+    private var dialog: AlertDialog? = null
+    private val binding: DialogFileOperationProgressBinding =
+        DialogFileOperationProgressBinding.inflate(activity.layoutInflater, null, false)
+
+    fun start() {
+        activity.getAlertDialogBuilder().apply {
+            activity.setupDialogStuff(binding.root, this, titleRes) { alertDialog ->
+                dialog = alertDialog
+                alertDialog.setCancelable(false)
+            }
+        }
+    }
+
+    fun update(fileName: String, fileIndex: Int, fileCount: Int, processed: Long, total: Long) {
+        activity.runOnUiThread {
+            if (dialog == null) {
+                return@runOnUiThread
+            }
+            binding.progressFileName.text = fileName
+            binding.progressFilesText.text = if (fileCount > 1) "${fileIndex}/$fileCount" else ""
+            val percent = if (total > 0) (processed * 100 / total).toInt() else 0
+            binding.progressBar.progress = percent
+            binding.progressPercentText.text = "$percent%"
+        }
+    }
+
+    fun finish() {
+        activity.runOnUiThread {
+            dialog?.dismiss()
+            dialog = null
+        }
+    }
+}

+ 48 - 0
File-Manager-1.6.4/app/src/main/kotlin/org/fossify/filemanager/dialogs/PasswordConfirmationDialog.kt

@@ -0,0 +1,48 @@
+package org.fossify.filemanager.dialogs
+
+import androidx.appcompat.app.AlertDialog
+import org.fossify.commons.activities.BaseSimpleActivity
+import org.fossify.commons.extensions.getAlertDialogBuilder
+import org.fossify.commons.extensions.setupDialogStuff
+import org.fossify.commons.extensions.showKeyboard
+import org.fossify.commons.extensions.toast
+import org.fossify.filemanager.R
+import org.fossify.filemanager.databinding.DialogPasswordConfirmationBinding
+
+class PasswordConfirmationDialog(
+    activity: BaseSimpleActivity,
+    private val callback: (password: String) -> Unit
+) {
+    private var dialog: AlertDialog? = null
+    private val binding: DialogPasswordConfirmationBinding =
+        DialogPasswordConfirmationBinding.inflate(activity.layoutInflater, null, false)
+
+    init {
+        activity.getAlertDialogBuilder()
+            .setPositiveButton(R.string.ok, null)
+            .setNegativeButton(R.string.cancel, null)
+            .apply {
+                activity.setupDialogStuff(binding.root, this, R.string.password) { alertDialog ->
+                    dialog = alertDialog
+                    alertDialog.showKeyboard(binding.password)
+                    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
+                        val password = binding.password.text?.toString().orEmpty()
+                        val confirmation = binding.passwordConfirmation.text?.toString().orEmpty()
+                        when {
+                            password.isEmpty() -> {
+                                // keep the dialog open
+                            }
+                            password != confirmation -> {
+                                activity.toast(R.string.passwords_do_not_match)
+                                binding.passwordConfirmation.setText("")
+                            }
+                            else -> {
+                                alertDialog.dismiss()
+                                callback(password)
+                            }
+                        }
+                    }
+                }
+            }
+    }
+}

+ 13 - 2
File-Manager-1.6.4/app/src/main/kotlin/org/fossify/filemanager/helpers/CryptoHelper.kt

@@ -154,7 +154,12 @@ object CryptoHelper {
         return TrailerMeta(chunkSize, originalSize, salt, nonce, verifier)
     }
 
-    fun encryptFile(file: File, password: String, chunkMb: Int = DEFAULT_CHUNK_MB) {
+    fun encryptFile(
+        file: File,
+        password: String,
+        chunkMb: Int = DEFAULT_CHUNK_MB,
+        onProgress: ((Long, Long) -> Unit)? = null
+    ) {
         if (!file.exists() || !file.isFile) {
             throw NotAFileException()
         }
@@ -205,6 +210,7 @@ object CryptoHelper {
 
                 processed += bytesRead
                 blockOffset += (bytesRead + HMAC_BLOCK_SIZE - 1) / HMAC_BLOCK_SIZE
+                onProgress?.invoke(processed, encryptLength)
             }
 
             raf.seek(originalSize)
@@ -219,7 +225,11 @@ object CryptoHelper {
         }
     }
 
-    fun decryptFile(file: File, password: String) {
+    fun decryptFile(
+        file: File,
+        password: String,
+        onProgress: ((Long, Long) -> Unit)? = null
+    ) {
         if (!file.exists() || !file.isFile) {
             throw NotAFileException()
         }
@@ -267,6 +277,7 @@ object CryptoHelper {
 
                 processed += bytesRead
                 blockOffset += (bytesRead + HMAC_BLOCK_SIZE - 1) / HMAC_BLOCK_SIZE
+                onProgress?.invoke(processed, decryptLength)
             }
 
             raf.setLength(meta.originalSize)

+ 53 - 0
File-Manager-1.6.4/app/src/main/res/layout/dialog_file_operation_progress.xml

@@ -0,0 +1,53 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingStart="@dimen/bigger_margin"
+    android:paddingTop="@dimen/bigger_margin"
+    android:paddingEnd="@dimen/bigger_margin"
+    android:paddingBottom="@dimen/bigger_margin">
+
+    <TextView
+        android:id="@+id/progress_file_name"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:ellipsize="middle"
+        android:maxLines="1"
+        android:textSize="@dimen/normal_text_size"
+        android:textColor="?android:attr/textColorPrimary" />
+
+    <ProgressBar
+        android:id="@+id/progress_bar"
+        style="?android:attr/progressBarStyleHorizontal"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/medium_margin"
+        android:max="100"
+        android:progressTint="?attr/colorPrimary" />
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="@dimen/small_margin"
+        android:orientation="horizontal">
+
+        <TextView
+            android:id="@+id/progress_files_text"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="start"
+            android:textSize="@dimen/smaller_text_size"
+            android:textColor="?android:attr/textColorPrimary" />
+
+        <TextView
+            android:id="@+id/progress_percent_text"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_gravity="end"
+            android:layout_weight="1"
+            android:gravity="end"
+            android:textSize="@dimen/smaller_text_size"
+            android:textColor="?android:attr/textColorPrimary" />
+    </LinearLayout>
+</LinearLayout>

+ 41 - 0
File-Manager-1.6.4/app/src/main/res/layout/dialog_password_confirmation.xml

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:padding="@dimen/activity_margin">
+
+    <org.fossify.commons.views.MyTextInputLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:hint="@string/password"
+        app:passwordToggleEnabled="true">
+
+        <com.google.android.material.textfield.TextInputEditText
+            android:id="@+id/password"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginBottom="@dimen/activity_margin"
+            android:inputType="textPassword"
+            android:singleLine="true"
+            android:textCursorDrawable="@null"
+            android:textSize="@dimen/normal_text_size" />
+    </org.fossify.commons.views.MyTextInputLayout>
+
+    <org.fossify.commons.views.MyTextInputLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:hint="@string/confirm_password"
+        app:passwordToggleEnabled="true">
+
+        <com.google.android.material.textfield.TextInputEditText
+            android:id="@+id/password_confirmation"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:inputType="textPassword"
+            android:singleLine="true"
+            android:textCursorDrawable="@null"
+            android:textSize="@dimen/normal_text_size" />
+    </org.fossify.commons.views.MyTextInputLayout>
+</LinearLayout>

+ 15 - 1
File-Manager-1.6.4/app/src/main/res/layout/item_file_dir_list.xml

@@ -56,6 +56,20 @@
             app:layout_constraintVertical_bias="0.5"
             tools:text="1 KB" />
 
+        <ImageView
+            android:id="@+id/item_check"
+            android:layout_width="@dimen/selection_check_size"
+            android:layout_height="@dimen/selection_check_size"
+            android:layout_marginStart="@dimen/tiny_margin"
+            android:background="@drawable/circle_background"
+            android:contentDescription="@null"
+            android:padding="@dimen/tiny_margin"
+            android:src="@drawable/ic_check_vector"
+            android:visibility="gone"
+            app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintTop_toTopOf="parent" />
+
         <TextView
             android:id="@+id/item_date"
             android:layout_width="wrap_content"
@@ -64,7 +78,7 @@
             android:gravity="end"
             android:textSize="@dimen/smaller_text_size"
             app:layout_constraintBottom_toBottomOf="@+id/item_details"
-            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintEnd_toStartOf="@+id/item_check"
             app:layout_constraintTop_toTopOf="@+id/item_details"
             tools:text="1.1.1970" />
 

+ 2 - 0
File-Manager-1.6.4/app/src/main/res/values-zh/strings.xml

@@ -13,4 +13,6 @@
     <string name="target_file_exists">目标文件已存在</string>
     <string name="nothing_encrypted">没有可加密的文件</string>
     <string name="nothing_decrypted">没有可解密的文件</string>
+<string name="confirm_password">确认密码</string>
+<string name="passwords_do_not_match">两次输入的密码不一致</string>
 </resources>

+ 2 - 0
File-Manager-1.6.4/app/src/main/res/values/strings.xml

@@ -38,6 +38,8 @@
 <string name="target_file_exists">Target file already exists</string>
 <string name="nothing_encrypted">No files to encrypt</string>
 <string name="nothing_decrypted">No files to decrypt</string>
+<string name="confirm_password">Confirm password</string>
+<string name="passwords_do_not_match">Passwords do not match</string>
     <string name="compress_as">Compress as</string>
     <string name="compressing">Compressing…</string>
     <string name="decompressing">Decompressing…</string>