3333import android .text .TextUtils ;
3434import androidx .annotation .Nullable ;
3535import com .google .firebase .crashlytics .internal .Logger ;
36- import java .io .BufferedReader ;
3736import java .io .Closeable ;
3837import java .io .File ;
39- import java .io .FileReader ;
4038import java .io .IOException ;
4139import java .io .InputStream ;
4240import java .security .MessageDigest ;
4745import java .util .List ;
4846import java .util .Locale ;
4947import java .util .Map ;
50- import java .util .regex .Pattern ;
5148
5249public class CommonUtils {
5350
@@ -73,15 +70,6 @@ public class CommonUtils {
7370 static final String BUILD_IDS_BUILD_ID_RESOURCE_NAME =
7471 "com.google.firebase.crashlytics.build_ids_build_id" ;
7572
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-
8573 // TODO: Maybe move this method into a more appropriate class.
8674 public static SharedPreferences getSharedPrefs (Context context ) {
8775 return context .getSharedPreferences (SHARED_PREFS_NAME , Context .MODE_PRIVATE );
@@ -92,37 +80,6 @@ public static SharedPreferences getLegacySharedPrefs(Context context) {
9280 return context .getSharedPreferences (LEGACY_SHARED_PREFS_NAME , Context .MODE_PRIVATE );
9381 }
9482
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-
12683 /**
12784 * Get Architecture based on Integer order in Protobuf enum
12885 *
@@ -175,49 +132,6 @@ static Architecture getValue() {
175132 }
176133 }
177134
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-
221135 /**
222136 * Returns the RunningAppProcessInfo object for the given package, or null if it cannot be found.
223137 */
@@ -311,6 +225,13 @@ public static String createInstanceIdFrom(String... sliceIds) {
311225 return (concatValue .length () > 0 ) ? sha1 (concatValue ) : null ;
312226 }
313227
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+
314235 /**
315236 * Calculates and returns the amount of free RAM in bytes.
316237 *
@@ -492,25 +413,6 @@ public static boolean isAppDebuggable(Context context) {
492413 return (context .getApplicationInfo ().flags & ApplicationInfo .FLAG_DEBUGGABLE ) != 0 ;
493414 }
494415
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-
514416 /**
515417 * Closes a {@link Closeable}, ignoring any {@link IOException}s raised in the process. Does
516418 * nothing if the {@link Closeable} is <code>null</code>.
0 commit comments