From 9b85e50f7f770e7982de621bae4435f3729a611e Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 26 May 2020 21:39:58 +0200 Subject: [PATCH] use a separate DecompressItemsAdapter at decompression --- .../pro/activities/DecompressActivity.kt | 10 +- .../pro/adapters/DecompressItemsAdapter.kt | 137 ++++++++++++++++++ .../main/res/layout/activity_decompress.xml | 1 + .../item_decompression_list_file_dir.xml | 32 ++++ 4 files changed, 175 insertions(+), 5 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt create mode 100644 app/src/main/res/layout/item_decompression_list_file_dir.xml diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt index e542dfad..bc2a78a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/DecompressActivity.kt @@ -9,7 +9,7 @@ import com.simplemobiletools.commons.extensions.showErrorToast import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.isOreoPlus import com.simplemobiletools.filemanager.pro.R -import com.simplemobiletools.filemanager.pro.adapters.ItemsAdapter +import com.simplemobiletools.filemanager.pro.adapters.DecompressItemsAdapter import com.simplemobiletools.filemanager.pro.models.ListItem import kotlinx.android.synthetic.main.activity_decompress.* import java.io.BufferedInputStream @@ -27,13 +27,13 @@ class DecompressActivity : SimpleActivity() { return } - getRealPathFromURI(uri)?.apply { - title = getFilenameFromPath() - } + val realPath = getRealPathFromURI(uri) + title = realPath?.getFilenameFromPath() ?: uri.toString().getFilenameFromPath() try { val listItems = getListItems(uri) - ItemsAdapter(this, listItems, null, decompress_list, false, null) { + DecompressItemsAdapter(this, listItems, decompress_list) { + }.apply { decompress_list.adapter = this } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt new file mode 100644 index 00000000..843fc50e --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/adapters/DecompressItemsAdapter.kt @@ -0,0 +1,137 @@ +package com.simplemobiletools.filemanager.pro.adapters + +import android.content.pm.PackageManager +import android.graphics.drawable.Drawable +import android.util.TypedValue +import android.view.Menu +import android.view.View +import android.view.ViewGroup +import com.bumptech.glide.Glide +import com.bumptech.glide.load.engine.DiskCacheStrategy +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions +import com.bumptech.glide.request.RequestOptions +import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter +import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor +import com.simplemobiletools.commons.extensions.getFileSignature +import com.simplemobiletools.commons.extensions.getTextSize +import com.simplemobiletools.commons.extensions.getTimeFormat +import com.simplemobiletools.commons.helpers.getFilePlaceholderDrawables +import com.simplemobiletools.commons.views.MyRecyclerView +import com.simplemobiletools.filemanager.pro.R +import com.simplemobiletools.filemanager.pro.activities.SimpleActivity +import com.simplemobiletools.filemanager.pro.extensions.config +import com.simplemobiletools.filemanager.pro.models.ListItem +import kotlinx.android.synthetic.main.item_list_file_dir.view.* +import java.util.* + +class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableList, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : + MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { + + private lateinit var fileDrawable: Drawable + private lateinit var folderDrawable: Drawable + private var fileDrawables = HashMap() + private var fontSize = 0f + private var smallerFontSize = 0f + private var dateFormat = "" + private var timeFormat = "" + + init { + initDrawables() + fontSize = activity.getTextSize() + smallerFontSize = fontSize * 0.8f + dateFormat = activity.config.dateFormat + timeFormat = activity.getTimeFormat() + } + + override fun getActionMenuId() = 0 + + override fun prepareActionMode(menu: Menu) {} + + override fun actionItemPressed(id: Int) {} + + override fun getSelectableItemCount() = 0 + + override fun getIsItemSelectable(position: Int) = false + + override fun getItemSelectionKey(position: Int) = 0 + + override fun getItemKeyPosition(key: Int) = 0 + + override fun onActionModeCreated() {} + + override fun onActionModeDestroyed() {} + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_decompression_list_file_dir, parent) + + override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { + val fileDirItem = listItems[position] + holder.bindView(fileDirItem, false, false) { itemView, layoutPosition -> + setupView(itemView, fileDirItem) + } + bindViewHolder(holder) + } + + override fun getItemCount() = listItems.size + + override fun onViewRecycled(holder: ViewHolder) { + super.onViewRecycled(holder) + if (!activity.isDestroyed && !activity.isFinishing) { + val icon = holder.itemView.item_icon + if (icon != null) { + Glide.with(activity).clear(icon) + } + } + } + + private fun setupView(view: View, listItem: ListItem) { + view.apply { + val fileName = listItem.name + item_name.text = fileName + item_name.setTextColor(textColor) + item_name.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) + + if (listItem.isDirectory) { + item_icon.setImageDrawable(folderDrawable) + } else { + val drawable = fileDrawables.getOrElse(fileName.substringAfterLast(".").toLowerCase(), { fileDrawable }) + val options = RequestOptions() + .signature(listItem.mPath.getFileSignature()) + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) + .error(drawable) + .centerCrop() + + val itemToLoad = getImagePathToLoad(listItem.path) + if (!activity.isDestroyed) { + Glide.with(activity) + .load(itemToLoad) + .transition(DrawableTransitionOptions.withCrossFade()) + .apply(options) + .into(item_icon) + } + } + } + } + + private fun getImagePathToLoad(path: String): Any { + return if (path.endsWith(".apk", true)) { + val packageInfo = activity.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES) + if (packageInfo != null) { + val appInfo = packageInfo.applicationInfo + appInfo.sourceDir = path + appInfo.publicSourceDir = path + appInfo.loadIcon(activity.packageManager) + } else { + path + } + } else { + path + } + } + + private fun initDrawables() { + folderDrawable = resources.getColoredDrawableWithColor(R.drawable.ic_folder_vector, textColor) + folderDrawable.alpha = 180 + fileDrawable = resources.getDrawable(R.drawable.ic_file_generic) + fileDrawables = getFilePlaceholderDrawables(activity) + } +} diff --git a/app/src/main/res/layout/activity_decompress.xml b/app/src/main/res/layout/activity_decompress.xml index 46489bde..1b4ce42b 100644 --- a/app/src/main/res/layout/activity_decompress.xml +++ b/app/src/main/res/layout/activity_decompress.xml @@ -10,6 +10,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" + android:paddingTop="@dimen/small_margin" android:scrollbars="none" app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager" /> diff --git a/app/src/main/res/layout/item_decompression_list_file_dir.xml b/app/src/main/res/layout/item_decompression_list_file_dir.xml new file mode 100644 index 00000000..381d1dcb --- /dev/null +++ b/app/src/main/res/layout/item_decompression_list_file_dir.xml @@ -0,0 +1,32 @@ + + + + + + + +