Skip to content

Commit

Permalink
[Refactor] Attempt to fix displaying wrong images due to recycled view
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Sep 8, 2023
1 parent 8b3d6de commit 2e44f6b
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ private void getActivityView(@NonNull Context context, @NonNull ViewHolder holde
activityName.replaceFirst(mPackageName, "") : activityName);
}
// Icon
ImageLoader.getInstance().displayImage(mPackageName + "_" + activityName, activityInfo, holder.imageView);
String tag = mPackageName + "_" + activityName;
holder.imageView.setTag(tag);
ImageLoader.getInstance().displayImage(tag, activityInfo, holder.imageView);
// TaskAffinity
holder.textView1.setText(String.format(Locale.ROOT, "%s: %s",
getString(R.string.task_affinity), activityInfo.taskAffinity));
Expand Down Expand Up @@ -592,7 +594,9 @@ private void getServicesView(@NonNull Context context, @NonNull ViewHolder holde
serviceInfo.name.replaceFirst(mPackageName, "") : serviceInfo.name);
}
// Icon
ImageLoader.getInstance().displayImage(mPackageName + "_" + serviceInfo.name, serviceInfo, holder.imageView);
String tag = mPackageName + "_" + serviceInfo.name;
holder.imageView.setTag(tag);
ImageLoader.getInstance().displayImage(tag, serviceInfo, holder.imageView);
// Flags and Permission
StringBuilder flagsAndPermission = new StringBuilder(Utils.getServiceFlagsString(serviceInfo.flags));
if (flagsAndPermission.length() != 0) {
Expand Down Expand Up @@ -669,7 +673,9 @@ private void getReceiverView(@NonNull Context context, @NonNull ViewHolder holde
: activityInfo.name);
}
// Icon
ImageLoader.getInstance().displayImage(mPackageName + "_" + activityInfo.name, activityInfo, holder.imageView);
String tag = mPackageName + "_" + activityInfo.name;
holder.imageView.setTag(tag);
ImageLoader.getInstance().displayImage(tag, activityInfo, holder.imageView);
// TaskAffinity
holder.textView1.setText(String.format(Locale.ROOT, "%s: %s",
getString(R.string.task_affinity), activityInfo.taskAffinity));
Expand Down Expand Up @@ -720,7 +726,9 @@ private void getProviderView(@NonNull Context context, @NonNull ViewHolder holde
// Label
holder.labelView.setText(Utils.camelCaseToSpaceSeparatedString(Utils.getLastComponent(providerName)));
// Icon
ImageLoader.getInstance().displayImage(mPackageName + "_" + providerName, providerInfo, holder.imageView);
String tag = mPackageName + "_" + providerName;
holder.imageView.setTag(tag);
ImageLoader.getInstance().displayImage(tag, providerInfo, holder.imageView);
// Uri permission
holder.textView1.setText(String.format(Locale.ROOT, "%s: %s", getString(R.string.grant_uri_permission), providerInfo.grantUriPermissions));
// Path permissions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ private void getPermissionsView(@NonNull Context context, @NonNull ViewHolder ho
permissionInfo.name.replaceFirst(mPackageName, "") : permissionInfo.name);
}
// Icon
ImageLoader.getInstance().displayImage(mPackageName + "_" + permissionInfo.name, permissionInfo, holder.imageView);
String tag = mPackageName + "_" + permissionInfo.name;
holder.imageView.setTag(tag);
ImageLoader.getInstance().displayImage(tag, permissionInfo, holder.imageView);
// Description
CharSequence description = permissionInfo.loadDescription(packageManager);
if (description != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public View getView(int position, View convertView, ViewGroup parent) {
view = (ImageView) convertView;
}
IconItemInfo info = mIcons[position];

view.setTag(info.packageName);
ImageLoader.getInstance().displayImage(info.packageName, info, view);
return convertView;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,12 @@ public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
FmItem item = mAdapterList.get(position);
String tag = item.getTag();
holder.icon.setTag(tag);
holder.title.setText(item.getName());
String modificationDate = DateUtils.formatDateTime(mFmActivity, item.getLastModified());
// Set icon
// We're retrieving tag here which can be a bit expensive, but since we're using a recycler view, only a few
// operations will be done at a time which is acceptable compared to increasing loading time just to generate
// a tag.
ImageLoader.getInstance().displayImage(item.getTag(), holder.icon, new FmIconFetcher(item));
ImageLoader.getInstance().displayImage(tag, holder.icon, new FmIconFetcher(item));
// Set sub-icon
// TODO: 24/5/23 Set sub-icon if needed
if (item.type == FileType.DIRECTORY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,9 @@ public void onBindViewHolder(@NonNull MatchingActivitiesRecyclerViewAdapter.View
String activityName = resolvedInfo.name;
String summary = resolvedInfo.appLabel + "\n" + resolvedInfo.shortName;
holder.summary.setText(summary);
mImageLoader.displayImage(resolvedInfo.packageName + "_" + resolvedInfo.label, holder.icon,
new ResolveInfoImageFetcher(resolvedInfo.resolveInfo));
String tag = resolvedInfo.packageName + "_" + resolvedInfo.label;
holder.icon.setTag(tag);
mImageLoader.displayImage(tag, holder.icon, new ResolveInfoImageFetcher(resolvedInfo.resolveInfo));
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(mIntent);
intent.setClassName(resolvedInfo.packageName, activityName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,9 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String name = info.packageName + "\n" + activityName;
holder.subtitle.setText(name);
holder.subtitle.setTextIsSelectable(true);
ImageLoader.getInstance().displayImage(info.packageName + "_" + activityName, info, holder.icon);
String tag = info.packageName + "_" + activityName;
holder.icon.setTag(tag);
ImageLoader.getInstance().displayImage(tag, info, holder.icon);
holder.actionIcon.setOnClickListener(v -> {
Intent intent = new Intent(mActivity.mMutableIntent);
intent.setClassName(info.packageName, activityName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.sha.setVisibility(View.GONE);
}
// Load app icon
holder.icon.setTag(item.packageName);
ImageLoader.getInstance().displayImage(item.packageName, item, holder.icon);
// Set app label
if (!TextUtils.isEmpty(mSearchQuery) && item.label.toLowerCase(Locale.ROOT).contains(mSearchQuery)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
info = pm.getApplicationInfo(packageName, 0);
} catch (PackageManager.NameNotFoundException ignore) {
}
holder.icon.setTag(packageName);
ImageLoader.getInstance().displayImage(packageName, info, holder.icon);
String label;
if (info != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ public void onBindViewHolder(@NonNull BodyViewHolder holder, int position) {
} else applicationInfo = null;
String processName = processItem.name;
// Load icon
holder.icon.setTag(processName);
ImageLoader.getInstance().displayImage(processName, applicationInfo, holder.icon);
// Set process name
holder.processName.setText(UIUtils.getHighlightedText(processName, mModel.getQuery(), mQueryStringHighlightColor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ public void run() {
if (imageViewReusedOrClosed(mQueueItem)) return;
ImageView iv = mQueueItem.imageView.get();
if (iv != null) {
iv.setImageBitmap(mImage);
Object tag = iv.getTag();
if (tag == null || tag.equals(mQueueItem.tag)) {
iv.setImageBitmap(mImage);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.packageName.setVisibility(View.VISIBLE);
holder.packageName.setText(info.name);
// Load icon
holder.icon.setTag(applicationInfo.packageName);
ImageLoader.getInstance().displayImage(applicationInfo.packageName, applicationInfo, holder.icon);
} catch (PackageManager.NameNotFoundException e) {
holder.title.setText(info.name);
holder.packageName.setVisibility(View.GONE);
holder.icon.setTag(info.name);
ImageLoader.getInstance().displayImage(info.name, null, holder.icon);
}
holder.icon.setOnClickListener(v -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public void onBindViewHolder(@NonNull ListItemViewHolder holder, int position) {
// Set label (or package name on failure)
holder.appLabel.setText(usageInfo.appLabel);
// Set icon
holder.appIcon.setTag(usageInfo.packageName);
ImageLoader.getInstance().displayImage(usageInfo.packageName, usageInfo.applicationInfo, holder.appIcon);
// Set user ID
if (mActivity.viewModel.hasMultipleUsers()) {
Expand Down

0 comments on commit 2e44f6b

Please sign in to comment.