From 6d4547ad69e6716c51fe6c572ea7ed8a93d3ba90 Mon Sep 17 00:00:00 2001 From: FongMi Date: Sat, 5 Apr 2025 23:47:08 +0800 Subject: [PATCH] Clean code --- .../android/tv/utils/ColorGenerator.java | 48 +++++++++---------- .../com/fongmi/android/tv/utils/ImgUtil.java | 8 ++-- .../com/fongmi/android/tv/utils/QRCode.java | 31 ++++++------ 3 files changed, 42 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/com/fongmi/android/tv/utils/ColorGenerator.java b/app/src/main/java/com/fongmi/android/tv/utils/ColorGenerator.java index 2da5bc701..5b2d7f1cb 100644 --- a/app/src/main/java/com/fongmi/android/tv/utils/ColorGenerator.java +++ b/app/src/main/java/com/fongmi/android/tv/utils/ColorGenerator.java @@ -7,30 +7,30 @@ import java.util.List; public class ColorGenerator { - private static final List PALETTE_500; + private static final List PALETTE_400; private static final List PALETTE_700; static { - PALETTE_500 = new ArrayList<>(); - PALETTE_500.add(MDColor.RED_500); - PALETTE_500.add(MDColor.PINK_500); - PALETTE_500.add(MDColor.PURPLE_500); - PALETTE_500.add(MDColor.DEEP_PURPLE_500); - PALETTE_500.add(MDColor.INDIGO_500); - PALETTE_500.add(MDColor.BLUE_500); - PALETTE_500.add(MDColor.LIGHT_BLUE_500); - PALETTE_500.add(MDColor.CYAN_500); - PALETTE_500.add(MDColor.TEAL_500); - PALETTE_500.add(MDColor.GREEN_500); - PALETTE_500.add(MDColor.LIGHT_GREEN_500); - PALETTE_500.add(MDColor.LIME_500); - PALETTE_500.add(MDColor.YELLOW_500); - PALETTE_500.add(MDColor.AMBER_500); - PALETTE_500.add(MDColor.ORANGE_500); - PALETTE_500.add(MDColor.DEEP_ORANGE_500); - PALETTE_500.add(MDColor.BROWN_500); - PALETTE_500.add(MDColor.GREY_500); - PALETTE_500.add(MDColor.BLUE_GREY_500); + PALETTE_400 = new ArrayList<>(); + PALETTE_400.add(MDColor.RED_400); + PALETTE_400.add(MDColor.PINK_400); + PALETTE_400.add(MDColor.PURPLE_400); + PALETTE_400.add(MDColor.DEEP_PURPLE_400); + PALETTE_400.add(MDColor.INDIGO_400); + PALETTE_400.add(MDColor.BLUE_400); + PALETTE_400.add(MDColor.LIGHT_BLUE_400); + PALETTE_400.add(MDColor.CYAN_400); + PALETTE_400.add(MDColor.TEAL_400); + PALETTE_400.add(MDColor.GREEN_400); + PALETTE_400.add(MDColor.LIGHT_GREEN_400); + PALETTE_400.add(MDColor.LIME_400); + PALETTE_400.add(MDColor.YELLOW_400); + PALETTE_400.add(MDColor.AMBER_400); + PALETTE_400.add(MDColor.ORANGE_400); + PALETTE_400.add(MDColor.DEEP_ORANGE_400); + PALETTE_400.add(MDColor.BROWN_400); + PALETTE_400.add(MDColor.GREY_400); + PALETTE_400.add(MDColor.BLUE_GREY_400); PALETTE_700 = new ArrayList<>(); PALETTE_700.add(MDColor.RED_700); @@ -54,11 +54,11 @@ public class ColorGenerator { PALETTE_700.add(MDColor.BLUE_GREY_700); } - public static int get500(String key) { - return PALETTE_500.get(Math.abs(key.hashCode()) % PALETTE_500.size()); + public static int get400(String key) { + return PALETTE_400.get((key.hashCode() & Integer.MAX_VALUE) % PALETTE_400.size()); } public static int get700(String key) { - return PALETTE_700.get(Math.abs(key.hashCode()) % PALETTE_700.size()); + return PALETTE_700.get((key.hashCode() & Integer.MAX_VALUE) % PALETTE_700.size()); } } diff --git a/app/src/main/java/com/fongmi/android/tv/utils/ImgUtil.java b/app/src/main/java/com/fongmi/android/tv/utils/ImgUtil.java index 72b17a8c7..1053328c7 100644 --- a/app/src/main/java/com/fongmi/android/tv/utils/ImgUtil.java +++ b/app/src/main/java/com/fongmi/android/tv/utils/ImgUtil.java @@ -50,14 +50,14 @@ public class ImgUtil { public static void load(String text, String url, ImageView view, ImageView.ScaleType scaleType, boolean rect) { view.setScaleType(scaleType); if (!TextUtils.isEmpty(url)) Glide.with(App.get()).asBitmap().load(getUrl(url)).placeholder(R.drawable.ic_img_loading).skipMemoryCache(true).dontAnimate().sizeMultiplier(Setting.getThumbnail()).signature(getSignature(url)).listener(getListener(view, scaleType)).into(view); - else if (text.length() > 0) view.setImageDrawable(getTextDrawable(text.substring(0, 1), rect)); + else if (!text.isEmpty()) view.setImageDrawable(getTextDrawable(text.substring(0, 1), rect)); else view.setImageResource(R.drawable.ic_img_error); } public static void loadVod(String text, String url, ImageView view) { view.setScaleType(ImageView.ScaleType.CENTER); if (!TextUtils.isEmpty(url)) Glide.with(App.get()).asBitmap().load(getUrl(url)).placeholder(R.drawable.ic_img_loading).listener(getListener(view)).into(view); - else if (text.length() > 0) view.setImageDrawable(getTextDrawable(text.substring(0, 1), true)); + else if (!text.isEmpty()) view.setImageDrawable(getTextDrawable(text.substring(0, 1), true)); else view.setImageResource(R.drawable.ic_img_error); } @@ -69,8 +69,8 @@ public class ImgUtil { private static Drawable getTextDrawable(String text, boolean rect) { TextDrawable.Builder builder = new TextDrawable.Builder().withBorder(ResUtil.dp2px(2), ColorGenerator.get700(text)); - if (rect) return builder.buildRoundRect(text, ColorGenerator.get500(text), ResUtil.dp2px(8)); - return builder.buildRound(text, ColorGenerator.get500(text)); + if (rect) return builder.buildRoundRect(text, ColorGenerator.get400(text), ResUtil.dp2px(8)); + return builder.buildRound(text, ColorGenerator.get400(text)); } public static Object getUrl(String url) { diff --git a/app/src/main/java/com/fongmi/android/tv/utils/QRCode.java b/app/src/main/java/com/fongmi/android/tv/utils/QRCode.java index ad92e82d8..331c87450 100644 --- a/app/src/main/java/com/fongmi/android/tv/utils/QRCode.java +++ b/app/src/main/java/com/fongmi/android/tv/utils/QRCode.java @@ -13,27 +13,24 @@ import java.util.Map; public class QRCode { - private static Bitmap createBitmap(BitMatrix matrix) { - int width = matrix.getWidth(); - int height = matrix.getHeight(); - int[] pixels = new int[width * height]; - for (int y = 0; y < height; y++) { - int offset = y * width; - for (int x = 0; x < width; x++) { - pixels[offset + x] = matrix.get(x, y) ? Color.BLACK : Color.WHITE; - } - } - Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - bitmap.setPixels(pixels, 0, width, 0, 0, width, height); - return bitmap; - } - - public static Bitmap getBitmap(String contents, int size, int margin) { + public static Bitmap getBitmap(String content, int size, int margin) { try { Map hints = new EnumMap<>(EncodeHintType.class); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); hints.put(EncodeHintType.MARGIN, margin); - return createBitmap(new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, ResUtil.dp2px(size), ResUtil.dp2px(size), hints)); + BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, ResUtil.dp2px(size), ResUtil.dp2px(size), hints); + int width = matrix.getWidth(); + int height = matrix.getHeight(); + int[] pixels = new int[width * height]; + for (int y = 0; y < height; y++) { + int offset = y * width; + for (int x = 0; x < width; x++) { + pixels[offset + x] = matrix.get(x, y) ? Color.BLACK : Color.WHITE; + } + } + Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + bitmap.setPixels(pixels, 0, width, 0, 0, width, height); + return bitmap; } catch (Exception e) { return null; }