|
| 1 | +package com.simplemobiletools.filemanager.pro.adapters |
| 2 | + |
| 3 | +import android.content.pm.PackageManager |
| 4 | +import android.graphics.drawable.Drawable |
| 5 | +import android.util.TypedValue |
| 6 | +import android.view.Menu |
| 7 | +import android.view.View |
| 8 | +import android.view.ViewGroup |
| 9 | +import com.bumptech.glide.Glide |
| 10 | +import com.bumptech.glide.load.engine.DiskCacheStrategy |
| 11 | +import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions |
| 12 | +import com.bumptech.glide.request.RequestOptions |
| 13 | +import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter |
| 14 | +import com.simplemobiletools.commons.extensions.getColoredDrawableWithColor |
| 15 | +import com.simplemobiletools.commons.extensions.getFileSignature |
| 16 | +import com.simplemobiletools.commons.extensions.getTextSize |
| 17 | +import com.simplemobiletools.commons.extensions.getTimeFormat |
| 18 | +import com.simplemobiletools.commons.helpers.getFilePlaceholderDrawables |
| 19 | +import com.simplemobiletools.commons.views.MyRecyclerView |
| 20 | +import com.simplemobiletools.filemanager.pro.R |
| 21 | +import com.simplemobiletools.filemanager.pro.activities.SimpleActivity |
| 22 | +import com.simplemobiletools.filemanager.pro.extensions.config |
| 23 | +import com.simplemobiletools.filemanager.pro.models.ListItem |
| 24 | +import kotlinx.android.synthetic.main.item_list_file_dir.view.* |
| 25 | +import java.util.* |
| 26 | + |
| 27 | +class DecompressItemsAdapter(activity: SimpleActivity, var listItems: MutableList<ListItem>, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : |
| 28 | + MyRecyclerViewAdapter(activity, recyclerView, null, itemClick) { |
| 29 | + |
| 30 | + private lateinit var fileDrawable: Drawable |
| 31 | + private lateinit var folderDrawable: Drawable |
| 32 | + private var fileDrawables = HashMap<String, Drawable>() |
| 33 | + private var fontSize = 0f |
| 34 | + private var smallerFontSize = 0f |
| 35 | + private var dateFormat = "" |
| 36 | + private var timeFormat = "" |
| 37 | + |
| 38 | + init { |
| 39 | + initDrawables() |
| 40 | + fontSize = activity.getTextSize() |
| 41 | + smallerFontSize = fontSize * 0.8f |
| 42 | + dateFormat = activity.config.dateFormat |
| 43 | + timeFormat = activity.getTimeFormat() |
| 44 | + } |
| 45 | + |
| 46 | + override fun getActionMenuId() = 0 |
| 47 | + |
| 48 | + override fun prepareActionMode(menu: Menu) {} |
| 49 | + |
| 50 | + override fun actionItemPressed(id: Int) {} |
| 51 | + |
| 52 | + override fun getSelectableItemCount() = 0 |
| 53 | + |
| 54 | + override fun getIsItemSelectable(position: Int) = false |
| 55 | + |
| 56 | + override fun getItemSelectionKey(position: Int) = 0 |
| 57 | + |
| 58 | + override fun getItemKeyPosition(key: Int) = 0 |
| 59 | + |
| 60 | + override fun onActionModeCreated() {} |
| 61 | + |
| 62 | + override fun onActionModeDestroyed() {} |
| 63 | + |
| 64 | + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = createViewHolder(R.layout.item_decompression_list_file_dir, parent) |
| 65 | + |
| 66 | + override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { |
| 67 | + val fileDirItem = listItems[position] |
| 68 | + holder.bindView(fileDirItem, false, false) { itemView, layoutPosition -> |
| 69 | + setupView(itemView, fileDirItem) |
| 70 | + } |
| 71 | + bindViewHolder(holder) |
| 72 | + } |
| 73 | + |
| 74 | + override fun getItemCount() = listItems.size |
| 75 | + |
| 76 | + override fun onViewRecycled(holder: ViewHolder) { |
| 77 | + super.onViewRecycled(holder) |
| 78 | + if (!activity.isDestroyed && !activity.isFinishing) { |
| 79 | + val icon = holder.itemView.item_icon |
| 80 | + if (icon != null) { |
| 81 | + Glide.with(activity).clear(icon) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private fun setupView(view: View, listItem: ListItem) { |
| 87 | + view.apply { |
| 88 | + val fileName = listItem.name |
| 89 | + item_name.text = fileName |
| 90 | + item_name.setTextColor(textColor) |
| 91 | + item_name.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize) |
| 92 | + |
| 93 | + if (listItem.isDirectory) { |
| 94 | + item_icon.setImageDrawable(folderDrawable) |
| 95 | + } else { |
| 96 | + val drawable = fileDrawables.getOrElse(fileName.substringAfterLast(".").toLowerCase(), { fileDrawable }) |
| 97 | + val options = RequestOptions() |
| 98 | + .signature(listItem.mPath.getFileSignature()) |
| 99 | + .diskCacheStrategy(DiskCacheStrategy.RESOURCE) |
| 100 | + .error(drawable) |
| 101 | + .centerCrop() |
| 102 | + |
| 103 | + val itemToLoad = getImagePathToLoad(listItem.path) |
| 104 | + if (!activity.isDestroyed) { |
| 105 | + Glide.with(activity) |
| 106 | + .load(itemToLoad) |
| 107 | + .transition(DrawableTransitionOptions.withCrossFade()) |
| 108 | + .apply(options) |
| 109 | + .into(item_icon) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private fun getImagePathToLoad(path: String): Any { |
| 116 | + return if (path.endsWith(".apk", true)) { |
| 117 | + val packageInfo = activity.packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES) |
| 118 | + if (packageInfo != null) { |
| 119 | + val appInfo = packageInfo.applicationInfo |
| 120 | + appInfo.sourceDir = path |
| 121 | + appInfo.publicSourceDir = path |
| 122 | + appInfo.loadIcon(activity.packageManager) |
| 123 | + } else { |
| 124 | + path |
| 125 | + } |
| 126 | + } else { |
| 127 | + path |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private fun initDrawables() { |
| 132 | + folderDrawable = resources.getColoredDrawableWithColor(R.drawable.ic_folder_vector, textColor) |
| 133 | + folderDrawable.alpha = 180 |
| 134 | + fileDrawable = resources.getDrawable(R.drawable.ic_file_generic) |
| 135 | + fileDrawables = getFilePlaceholderDrawables(activity) |
| 136 | + } |
| 137 | +} |
0 commit comments