Skip to content

Commit 0771151

Browse files
committed
Moving files to utils
1 parent be80d66 commit 0771151

File tree

3 files changed

+159
-140
lines changed

3 files changed

+159
-140
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.facebook.react.views.imagehelper;
2+
3+
import android.util.DisplayMetrics;
4+
5+
import androidx.annotation.NonNull;
6+
7+
enum PhoneDensity {
8+
Medium(DisplayMetrics.DENSITY_MEDIUM, "mdpi"),
9+
High(DisplayMetrics.DENSITY_HIGH, "hdpi"),
10+
XHigh(DisplayMetrics.DENSITY_XHIGH, "xhdpi"),
11+
XXHigh(DisplayMetrics.DENSITY_XXHIGH, "xxhdpi"),
12+
XXXHigh(DisplayMetrics.DENSITY_XXXHIGH, "xxxhdpi");
13+
14+
int density;
15+
16+
@NonNull
17+
String fileParentSuffix;
18+
19+
PhoneDensity(
20+
int density,
21+
@NonNull String fileParentSuffix
22+
) {
23+
this.density = density;
24+
this.fileParentSuffix = fileParentSuffix;
25+
}
26+
}

ReactAndroid/src/main/java/com/facebook/react/views/imagehelper/ResourceDrawableIdHelper.java

Lines changed: 2 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@
1010
import android.content.Context;
1111
import android.graphics.drawable.Drawable;
1212
import android.net.Uri;
13-
import android.util.DisplayMetrics;
14-
import android.view.WindowManager;
1513

1614
import androidx.annotation.NonNull;
1715
import androidx.annotation.Nullable;
1816

1917
import java.io.File;
2018
import java.util.HashMap;
2119
import java.util.Map;
22-
import java.util.concurrent.ConcurrentHashMap;
2320

2421
import javax.annotation.concurrent.ThreadSafe;
2522

@@ -29,23 +26,11 @@ public class ResourceDrawableIdHelper {
2926

3027
private final Map<String, Integer> mResourceDrawableIdMap;
3128

32-
private final ConcurrentHashMap<String, String> mResourceCacheMap;
33-
34-
private static final String CACHE_DRAWABLE_DIRECTORY_SCHEME = "otas/app/src/main/res";
35-
private static final String[] RESOURCE_EXTENSIONS = {
36-
"xml",
37-
"png",
38-
"svg",
39-
"jpg"
40-
};
4129
private static final String LOCAL_RESOURCE_SCHEME = "res";
4230
private static volatile ResourceDrawableIdHelper sResourceDrawableIdHelper;
4331

44-
private int densityDpi;
45-
4632
private ResourceDrawableIdHelper() {
4733
mResourceDrawableIdMap = new HashMap<>();
48-
mResourceCacheMap = new ConcurrentHashMap<>();
4934
}
5035

5136
public static ResourceDrawableIdHelper getInstance() {
@@ -98,7 +83,7 @@ private String sanitizeResourceDrawableId(@NonNull String name) {
9883
name = sanitizeResourceDrawableId(name);
9984

10085
// Checks to see if we have an ota version of the file, otherwise default to normal behavior.
101-
File otaFile = getDrawableFileFromCache(context, name);
86+
File otaFile = ResourceDrawableUtils.getDrawableFileFromCache(context, name);
10287
if (otaFile != null) {
10388
return Drawable.createFromPath(otaFile.getAbsolutePath());
10489
}
@@ -115,7 +100,7 @@ public Uri getResourceDrawableUri(Context context, @Nullable String name) {
115100
name = sanitizeResourceDrawableId(name);
116101

117102
// Checks to see if we have an ota version of the file, otherwise default to normal behavior.
118-
File otaFile = getDrawableFileFromCache(context, name);
103+
File otaFile = ResourceDrawableUtils.getDrawableFileFromCache(context, name);
119104
if (otaFile != null) {
120105
return Uri.fromFile(otaFile);
121106
}
@@ -125,127 +110,4 @@ public Uri getResourceDrawableUri(Context context, @Nullable String name) {
125110
? new Uri.Builder().scheme(LOCAL_RESOURCE_SCHEME).path(String.valueOf(resId)).build()
126111
: Uri.EMPTY;
127112
}
128-
129-
/**
130-
* Checks the cache to see if there is a drawable file downloaded via OTA.
131-
*/
132-
@Nullable
133-
private File getDrawableFileFromCache(Context context, @Nullable String fileName) {
134-
if (fileName == null || fileName.isEmpty()) {
135-
return null;
136-
}
137-
138-
String cacheMapFileName = mResourceCacheMap.get(fileName);
139-
140-
// Check the cache to see if we've already looked up the file before.
141-
if (cacheMapFileName != null) {
142-
return new File(cacheMapFileName);
143-
}
144-
145-
File file = null;
146-
int densityDpi = getDensityDpi(context);
147-
PhoneDensity[] phoneDensities = PhoneDensity.values();
148-
149-
// We start from the medium dpi and go up.
150-
for (PhoneDensity phoneDensity : phoneDensities) {
151-
String drawableFileParent = String.format("drawable-%s", phoneDensity.fileParentSuffix);
152-
String mipMapFileParent = String.format("mipmap-%s", phoneDensity.fileParentSuffix);
153-
154-
String[] parentFileNames = { drawableFileParent, mipMapFileParent };
155-
156-
File resourceFile = checkFiles(context, parentFileNames, fileName);
157-
158-
if (resourceFile != null) {
159-
file = resourceFile;
160-
}
161-
162-
// If we've found a file at our current dpi level, return it.
163-
// Otherwise continue looking up the chain.
164-
if (densityDpi <= phoneDensity.density) {
165-
if (file != null) {
166-
mResourceCacheMap.put(fileName, file.getAbsolutePath());
167-
return file;
168-
}
169-
}
170-
}
171-
172-
// As a last resort, check the drawable/raw folders.
173-
String[] parentFileNames = { "drawable", "raw" };
174-
file = checkFiles(context, parentFileNames, fileName);
175-
176-
if (file != null) {
177-
mResourceCacheMap.put(fileName, file.getAbsolutePath());
178-
}
179-
180-
return file;
181-
}
182-
183-
/**
184-
* Given a list of files, check if any of them exist.
185-
* Checks multiple extension types.
186-
*/
187-
private File checkFiles(Context context, String[] parentFileNames, String fileName) {
188-
for(String parentFileName : parentFileNames) {
189-
for (String extension : RESOURCE_EXTENSIONS) {
190-
File file = getFile(context, parentFileName, fileName, extension);
191-
if (file.exists()) {
192-
return file;
193-
}
194-
}
195-
}
196-
197-
return null;
198-
}
199-
200-
/**
201-
* Returns a file object with the correct directory extensions.
202-
*/
203-
private File getFile(Context context, String parentFileName, String fileName, String extension) {
204-
String fullDrawableFileName = String.format(
205-
"%s/%s/%s.%s",
206-
CACHE_DRAWABLE_DIRECTORY_SCHEME,
207-
parentFileName,
208-
fileName,
209-
extension
210-
);
211-
212-
return new File(context.getCacheDir(), fullDrawableFileName);
213-
}
214-
215-
/**
216-
* Returns the density dpi for the device.
217-
*/
218-
private int getDensityDpi(Context context) {
219-
if (densityDpi == 0) {
220-
DisplayMetrics metrics = new DisplayMetrics();
221-
222-
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
223-
windowManager.getDefaultDisplay().getMetrics(metrics);
224-
225-
densityDpi = metrics.densityDpi;
226-
}
227-
228-
return densityDpi;
229-
}
230-
231-
enum PhoneDensity {
232-
Medium(DisplayMetrics.DENSITY_MEDIUM, "mdpi"),
233-
High(DisplayMetrics.DENSITY_HIGH, "hdpi"),
234-
XHigh(DisplayMetrics.DENSITY_XHIGH, "xhdpi"),
235-
XXHigh(DisplayMetrics.DENSITY_XXHIGH, "xxhdpi"),
236-
XXXHigh(DisplayMetrics.DENSITY_XXXHIGH, "xxxhdpi");
237-
238-
int density;
239-
240-
@NonNull
241-
String fileParentSuffix;
242-
243-
PhoneDensity(
244-
int density,
245-
@NonNull String fileParentSuffix
246-
) {
247-
this.density = density;
248-
this.fileParentSuffix = fileParentSuffix;
249-
}
250-
}
251113
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package com.facebook.react.views.imagehelper;
2+
3+
import android.content.Context;
4+
import android.util.DisplayMetrics;
5+
import android.view.WindowManager;
6+
7+
import androidx.annotation.Nullable;
8+
9+
import java.io.File;
10+
import java.util.concurrent.ConcurrentHashMap;
11+
12+
public class ResourceDrawableUtils {
13+
private static final ConcurrentHashMap<String, String> mResourceCacheMap = new ConcurrentHashMap<>();
14+
private static final String CACHE_DRAWABLE_DIRECTORY_SCHEME = "otas/app/src/main/res";
15+
private static final String[] RESOURCE_EXTENSIONS = {
16+
"xml",
17+
"png",
18+
"svg",
19+
"jpg"
20+
};
21+
22+
private static int densityDpi;
23+
24+
/**
25+
* Checks the cache to see if there is a drawable file downloaded via OTA.
26+
*/
27+
@Nullable
28+
static File getDrawableFileFromCache(Context context, @Nullable String fileName) {
29+
if (fileName == null || fileName.isEmpty()) {
30+
return null;
31+
}
32+
33+
String cacheMapFileName = mResourceCacheMap.get(fileName);
34+
35+
// Check the cache to see if we've already looked up the file before.
36+
if (cacheMapFileName != null) {
37+
return new File(cacheMapFileName);
38+
}
39+
40+
File file = null;
41+
int densityDpi = getDensityDpi(context);
42+
PhoneDensity[] phoneDensities = PhoneDensity.values();
43+
44+
// We start from the medium dpi and go up.
45+
for (PhoneDensity phoneDensity : phoneDensities) {
46+
String drawableFileParent = String.format("drawable-%s", phoneDensity.fileParentSuffix);
47+
String mipMapFileParent = String.format("mipmap-%s", phoneDensity.fileParentSuffix);
48+
49+
String[] parentFileNames = { drawableFileParent, mipMapFileParent };
50+
51+
File resourceFile = checkFiles(context, parentFileNames, fileName);
52+
53+
if (resourceFile != null) {
54+
file = resourceFile;
55+
}
56+
57+
// If we've found a file at our current dpi level, return it.
58+
// Otherwise continue looking up the chain.
59+
if (densityDpi <= phoneDensity.density) {
60+
if (file != null) {
61+
mResourceCacheMap.put(fileName, file.getAbsolutePath());
62+
return file;
63+
}
64+
}
65+
}
66+
67+
// As a last resort, check the drawable/raw folders.
68+
String[] parentFileNames = { "drawable", "raw" };
69+
file = checkFiles(context, parentFileNames, fileName);
70+
71+
if (file != null) {
72+
mResourceCacheMap.put(fileName, file.getAbsolutePath());
73+
}
74+
75+
return file;
76+
}
77+
78+
/**
79+
* Given a list of files, check if any of them exist.
80+
* Checks multiple extension types.
81+
*/
82+
private static File checkFiles(Context context, String[] parentFileNames, String fileName) {
83+
for(String parentFileName : parentFileNames) {
84+
for (String extension : RESOURCE_EXTENSIONS) {
85+
File file = getFile(context, parentFileName, fileName, extension);
86+
if (file.exists()) {
87+
return file;
88+
}
89+
}
90+
}
91+
92+
return null;
93+
}
94+
95+
/**
96+
* Returns a file object with the correct directory extensions.
97+
*/
98+
private static File getFile(
99+
Context context,
100+
String parentFileName,
101+
String fileName,
102+
String extension
103+
) {
104+
String fullDrawableFileName = String.format(
105+
"%s/%s/%s.%s",
106+
CACHE_DRAWABLE_DIRECTORY_SCHEME,
107+
parentFileName,
108+
fileName,
109+
extension
110+
);
111+
112+
return new File(context.getCacheDir(), fullDrawableFileName);
113+
}
114+
115+
/**
116+
* Returns the density dpi for the device.
117+
*/
118+
private static int getDensityDpi(Context context) {
119+
// Cache this so we only have to do this once.
120+
if (densityDpi == 0) {
121+
DisplayMetrics metrics = new DisplayMetrics();
122+
123+
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
124+
windowManager.getDefaultDisplay().getMetrics(metrics);
125+
126+
densityDpi = metrics.densityDpi;
127+
}
128+
129+
return densityDpi;
130+
}
131+
}

0 commit comments

Comments
 (0)