33
33
import android .text .TextUtils ;
34
34
import androidx .annotation .Nullable ;
35
35
import com .google .firebase .crashlytics .internal .Logger ;
36
- import java .io .BufferedReader ;
37
36
import java .io .Closeable ;
38
37
import java .io .File ;
39
- import java .io .FileReader ;
40
38
import java .io .IOException ;
41
39
import java .io .InputStream ;
42
40
import java .security .MessageDigest ;
47
45
import java .util .List ;
48
46
import java .util .Locale ;
49
47
import java .util .Map ;
50
- import java .util .regex .Pattern ;
51
48
52
49
public class CommonUtils {
53
50
@@ -73,15 +70,6 @@ public class CommonUtils {
73
70
static final String BUILD_IDS_BUILD_ID_RESOURCE_NAME =
74
71
"com.google.firebase.crashlytics.build_ids_build_id" ;
75
72
76
- private static final long UNCALCULATED_TOTAL_RAM = -1 ;
77
- static final int BYTES_IN_A_GIGABYTE = 1073741824 ;
78
- static final int BYTES_IN_A_MEGABYTE = 1048576 ;
79
- static final int BYTES_IN_A_KILOBYTE = 1024 ;
80
-
81
- // Caches the result of the total ram calculation, which is expensive, so we only want to
82
- // perform it once. The value won't change over time, so it's safe to cache.
83
- private static long totalRamInBytes = UNCALCULATED_TOTAL_RAM ;
84
-
85
73
// TODO: Maybe move this method into a more appropriate class.
86
74
public static SharedPreferences getSharedPrefs (Context context ) {
87
75
return context .getSharedPreferences (SHARED_PREFS_NAME , Context .MODE_PRIVATE );
@@ -92,37 +80,6 @@ public static SharedPreferences getLegacySharedPrefs(Context context) {
92
80
return context .getSharedPreferences (LEGACY_SHARED_PREFS_NAME , Context .MODE_PRIVATE );
93
81
}
94
82
95
- /**
96
- * Utility method to open the given file and extract the field matching fieldname. Assumes each
97
- * line of the file is formatted "fieldID:value" with an arbitrary amount of whitespace on both
98
- * sides of the colon. Will return null if the file can't be opened or the field was not found.
99
- */
100
- public static String extractFieldFromSystemFile (File file , String fieldname ) {
101
- String toReturn = null ;
102
- if (file .exists ()) {
103
-
104
- BufferedReader br = null ;
105
- try {
106
- br = new BufferedReader (new FileReader (file ), 1024 );
107
- String line ;
108
- while ((line = br .readLine ()) != null ) {
109
- final Pattern pattern = Pattern .compile ("\\ s*:\\ s*" );
110
- final String [] pieces = pattern .split (line , 2 );
111
- if (pieces .length > 1 && pieces [0 ].equals (fieldname )) {
112
- toReturn = pieces [1 ];
113
-
114
- break ;
115
- }
116
- }
117
- } catch (Exception e ) {
118
- Logger .getLogger ().e ("Error parsing " + file , e );
119
- } finally {
120
- CommonUtils .closeOrLog (br , "Failed to close system file reader." );
121
- }
122
- }
123
- return toReturn ;
124
- }
125
-
126
83
/**
127
84
* Get Architecture based on Integer order in Protobuf enum
128
85
*
@@ -175,49 +132,6 @@ static Architecture getValue() {
175
132
}
176
133
}
177
134
178
- /**
179
- * Returns the total ram of the device, in bytes, as read from the /proc/meminfo file. No API call
180
- * exists to get this value in API level 7.
181
- */
182
- public static synchronized long getTotalRamInBytes () {
183
- if (totalRamInBytes == UNCALCULATED_TOTAL_RAM ) {
184
- long bytes = 0 ;
185
- String result = extractFieldFromSystemFile (new File ("/proc/meminfo" ), "MemTotal" );
186
-
187
- if (!TextUtils .isEmpty (result )) {
188
- result = result .toUpperCase (Locale .US );
189
-
190
- try {
191
- if (result .endsWith ("KB" )) {
192
- bytes = convertMemInfoToBytes (result , "KB" , BYTES_IN_A_KILOBYTE );
193
- } else if (result .endsWith ("MB" )) {
194
- // It is uncertain that this notation would ever be returned, but we'll
195
- // leave in this handling since we don't know for certain it isn't.
196
- bytes = convertMemInfoToBytes (result , "MB" , BYTES_IN_A_MEGABYTE );
197
- } else if (result .endsWith ("GB" )) {
198
- // It is uncertain that this notation would ever be returned, but we'll
199
- // leave in this handling since we don't know for certain it isn't.
200
- bytes = convertMemInfoToBytes (result , "GB" , BYTES_IN_A_GIGABYTE );
201
- } else {
202
- Logger .getLogger ().w ("Unexpected meminfo format while computing RAM: " + result );
203
- }
204
- } catch (NumberFormatException e ) {
205
- Logger .getLogger ().e ("Unexpected meminfo format while computing RAM: " + result , e );
206
- }
207
- }
208
- totalRamInBytes = bytes ;
209
- }
210
- return totalRamInBytes ;
211
- }
212
-
213
- /**
214
- * Converts a meminfo value String with associated size notation into bytes by stripping the
215
- * provided unit notation and applying the provided multiplier.
216
- */
217
- static long convertMemInfoToBytes (String memInfo , String notation , int notationMultiplier ) {
218
- return Long .parseLong (memInfo .split (notation )[0 ].trim ()) * notationMultiplier ;
219
- }
220
-
221
135
/**
222
136
* Returns the RunningAppProcessInfo object for the given package, or null if it cannot be found.
223
137
*/
@@ -311,6 +225,13 @@ public static String createInstanceIdFrom(String... sliceIds) {
311
225
return (concatValue .length () > 0 ) ? sha1 (concatValue ) : null ;
312
226
}
313
227
228
+ /** Calculates the total ram of the device, in bytes. */
229
+ public static synchronized long calculateTotalRamInBytes (Context context ) {
230
+ MemoryInfo mi = new MemoryInfo ();
231
+ ((ActivityManager ) context .getSystemService (Context .ACTIVITY_SERVICE )).getMemoryInfo (mi );
232
+ return mi .totalMem ;
233
+ }
234
+
314
235
/**
315
236
* Calculates and returns the amount of free RAM in bytes.
316
237
*
@@ -492,25 +413,6 @@ public static boolean isAppDebuggable(Context context) {
492
413
return (context .getApplicationInfo ().flags & ApplicationInfo .FLAG_DEBUGGABLE ) != 0 ;
493
414
}
494
415
495
- /**
496
- * Gets values for string properties in the strings.xml file by its name. If a key is not present,
497
- * an empty String is returned.
498
- *
499
- * @param context {@link Context} to use when accessing resources
500
- * @param key {@link String} name of the string value to look up
501
- * @return {@link String} value of the specified property, or an empty string if it could not be
502
- * found.
503
- */
504
- public static String getStringsFileValue (Context context , String key ) {
505
- final int id = getResourcesIdentifier (context , key , "string" );
506
-
507
- if (id > 0 ) {
508
- return context .getString (id );
509
- }
510
-
511
- return "" ;
512
- }
513
-
514
416
/**
515
417
* Closes a {@link Closeable}, ignoring any {@link IOException}s raised in the process. Does
516
418
* nothing if the {@link Closeable} is <code>null</code>.
0 commit comments