Openable barcode content

This commit is contained in:
M66B 2024-02-08 21:31:56 +01:00
parent 9988d9bb52
commit cbf8afa636
2 changed files with 15 additions and 8 deletions

View File

@ -81,6 +81,8 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
private final Context context; private final Context context;
private final LayoutInflater inflater; private final LayoutInflater inflater;
private final LifecycleOwner owner; private final LifecycleOwner owner;
private final int textColorTertiary;
private final int textColorLink;
private List<EntityAttachment> items = new ArrayList<>(); private List<EntityAttachment> items = new ArrayList<>();
@ -318,26 +320,27 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
String barcode = args.getString("barcode"); String barcode = args.getString("barcode");
if (!TextUtils.isEmpty(barcode)) { if (!TextUtils.isEmpty(barcode)) {
boolean link;
Uri uri; Uri uri;
try { try {
uri = UriHelper.guessScheme(Uri.parse(barcode)); uri = UriHelper.guessScheme(Uri.parse(barcode));
link = UriHelper.isHyperLink(uri) || UriHelper.isMail(uri);
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(ex); Log.w(ex);
uri = null; uri = null;
link = false;
} }
tvContent.setTypeface(null, link ? Typeface.NORMAL : Typeface.BOLD); boolean openable = (uri != null &&
!TextUtils.isEmpty(uri.getScheme()) &&
!"tel".equals(uri.getScheme()));
tvContent.setTypeface(null, openable ? Typeface.NORMAL : Typeface.BOLD);
int flags = tvContent.getPaintFlags(); int flags = tvContent.getPaintFlags();
if (link) if (openable)
flags |= Paint.UNDERLINE_TEXT_FLAG; flags |= Paint.UNDERLINE_TEXT_FLAG;
else else
flags &= ~Paint.UNDERLINE_TEXT_FLAG; flags &= ~Paint.UNDERLINE_TEXT_FLAG;
tvContent.setPaintFlags(flags); tvContent.setPaintFlags(flags);
tvContent.setTextColor(openable ? textColorLink : textColorTertiary);
tvContent.setTag(link ? uri.toString() : null); tvContent.setTag(openable ? uri.toString() : null);
tvContent.setText(barcode); tvContent.setText(barcode);
tvContent.setVisibility(View.VISIBLE); tvContent.setVisibility(View.VISIBLE);
} }
@ -500,6 +503,9 @@ public class AdapterMedia extends RecyclerView.Adapter<AdapterMedia.ViewHolder>
this.owner = parentFragment.getViewLifecycleOwner(); this.owner = parentFragment.getViewLifecycleOwner();
this.inflater = LayoutInflater.from(context); this.inflater = LayoutInflater.from(context);
this.textColorTertiary = Helper.resolveColor(context, android.R.attr.textColorTertiary);
this.textColorLink = Helper.resolveColor(context, android.R.attr.textColorLink);
setHasStableIds(true); setHasStableIds(true);
owner.getLifecycle().addObserver(new LifecycleObserver() { owner.getLifecycle().addObserver(new LifecycleObserver() {

View File

@ -54,6 +54,7 @@
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:text="1234567890" android:text="1234567890"
android:textAppearance="@style/TextAppearance.AppCompat.Small" android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textIsSelectable="true"
android:textSize="12sp" android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"