That fuck shit the fascists are using
1package org.tm.archive.util;
2
3import android.content.Context;
4import android.util.DisplayMetrics;
5
6import androidx.annotation.NonNull;
7
8import org.tm.archive.dependencies.ApplicationDependencies;
9
10import java.util.LinkedHashMap;
11import java.util.Map;
12
13/**
14 * Helper class to get density information about a device's display
15 */
16public final class ScreenDensity {
17
18 private static final String UNKNOWN = "unknown";
19
20 private static final float XHDPI_TO_LDPI = 0.25f;
21 private static final float XHDPI_TO_MDPI = 0.5f;
22 private static final float XHDPI_TO_HDPI = 0.75f;
23
24 private static final LinkedHashMap<Integer, String> LEVELS = new LinkedHashMap<Integer, String>() {{
25 put(DisplayMetrics.DENSITY_LOW, "ldpi");
26 put(DisplayMetrics.DENSITY_MEDIUM, "mdpi");
27 put(DisplayMetrics.DENSITY_HIGH, "hdpi");
28 put(DisplayMetrics.DENSITY_XHIGH, "xhdpi");
29 put(DisplayMetrics.DENSITY_XXHIGH, "xxhdpi");
30 put(DisplayMetrics.DENSITY_XXXHIGH, "xxxhdpi");
31 }};
32
33 private final String bucket;
34 private final int density;
35
36 public ScreenDensity(String bucket, int density) {
37 this.bucket = bucket;
38 this.density = density;
39 }
40
41 public static @NonNull ScreenDensity get(@NonNull Context context) {
42 int density = context.getResources().getDisplayMetrics().densityDpi;
43
44 String bucket = UNKNOWN;
45
46 for (Map.Entry<Integer, String> entry : LEVELS.entrySet()) {
47 bucket = entry.getValue();
48 if (entry.getKey() > density) {
49 break;
50 }
51 }
52
53 return new ScreenDensity(bucket, density);
54 }
55
56 public static @NonNull String getBestDensityBucketForDevice() {
57 ScreenDensity density = get(ApplicationDependencies.getApplication());
58
59 if (density.isKnownDensity()) {
60 return density.bucket;
61 } else {
62 return "xhdpi";
63 }
64 }
65
66 public String getBucket() {
67 return bucket;
68 }
69
70 public boolean isKnownDensity() {
71 return !bucket.equals(UNKNOWN);
72 }
73
74 @Override
75 public @NonNull String toString() {
76 return bucket + " (" + density + ")";
77 }
78
79 public static float xhdpiRelativeDensityScaleFactor(@NonNull String density) {
80 switch (density) {
81 case "ldpi":
82 return XHDPI_TO_LDPI;
83 case "mdpi":
84 return XHDPI_TO_MDPI;
85 case "hdpi":
86 return XHDPI_TO_HDPI;
87 case "xhdpi":
88 return 1f;
89 default:
90 throw new IllegalStateException("Unsupported density: " + density);
91 }
92
93 }
94}