Skip to content

Commit 94d28a6

Browse files
authored
Merge branch 'master' into mila/multiple-inequality-support
2 parents 7b43dde + dfa22c7 commit 94d28a6

18 files changed

Lines changed: 1633 additions & 151 deletions

File tree

firebase-components/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
# Unreleased
2+
* [changed] Internal changes to ensure only one interface is provided for
3+
kotlinx.coroutines.CoroutineDispatcher interfaces when both firebase-common and
4+
firebase-common-ktx provide them.
5+
26

37

firebase-components/src/main/java/com/google/firebase/components/ComponentRuntime.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class ComponentRuntime implements ComponentContainer, ComponentLoader {
4949
private final Map<Qualified<?>, Provider<?>> lazyInstanceMap = new HashMap<>();
5050
private final Map<Qualified<?>, LazySet<?>> lazySetMap = new HashMap<>();
5151
private final List<Provider<ComponentRegistrar>> unprocessedRegistrarProviders;
52+
private Set<String> processedCoroutineDispatcherInterfaces = new HashSet<>();
5253
private final EventBus eventBus;
5354
private final AtomicReference<Boolean> eagerComponentsInitializedWith = new AtomicReference<>();
5455
private final ComponentRegistrarProcessor componentRegistrarProcessor;
@@ -123,6 +124,25 @@ private void discoverComponents(List<Component<?>> componentsToAdd) {
123124
}
124125
}
125126

127+
// kotlinx.coroutines.CoroutineDispatcher interfaces could be provided by both new version of
128+
// firebase-common and old version of firebase-common-ktx. In this scenario take the first
129+
// interface which was provided.
130+
131+
Iterator<Component<?>> it = componentsToAdd.iterator();
132+
while (it.hasNext()) {
133+
Component component = it.next();
134+
for (Object anInterface : component.getProvidedInterfaces().toArray()) {
135+
if (anInterface.toString().contains("kotlinx.coroutines.CoroutineDispatcher")) {
136+
if (processedCoroutineDispatcherInterfaces.contains(anInterface.toString())) {
137+
it.remove();
138+
break;
139+
} else {
140+
processedCoroutineDispatcherInterfaces.add(anInterface.toString());
141+
}
142+
}
143+
}
144+
}
145+
126146
if (components.isEmpty()) {
127147
CycleDetector.detect(componentsToAdd);
128148
} else {

firebase-crashlytics-ndk/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Unreleased
2+
* [changed] Updated `firebase-crashlytics` dependency to v18.4.2
23

34
# 18.4.1
45
* [changed] Updated `firebase-crashlytics` dependency to v18.4.1

firebase-crashlytics/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
22
* [feature] Include Firebase sessions with NDK crashes and ANRs.
3+
* [changed] Improved reliability when reporting memory usage.
34

45
# 18.4.1
56
* [changed] Updated `firebase-sessions` dependency to v1.0.2

firebase-crashlytics/firebase-crashlytics.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ dependencies {
7272
runtimeOnly 'com.google.firebase:firebase-installations:17.1.3'
7373
implementation 'com.google.firebase:firebase-measurement-connector:18.0.2'
7474
implementation "com.google.android.gms:play-services-tasks:18.0.1"
75-
implementation project(':firebase-sessions')
75+
implementation 'com.google.firebase:firebase-sessions:1.0.2'
7676

7777
javadocClasspath 'com.google.code.findbugs:jsr305:3.0.2'
7878

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/CommonUtilsTest.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,6 @@ protected void tearDown() throws Exception {
4949

5050
private static final String ABC_EXPECTED_HASH = "a9993e364706816aba3e25717850c26c9cd0d89d";
5151

52-
public void testConvertMemInfoToBytesFromKb() {
53-
assertEquals(
54-
1055760384,
55-
CommonUtils.convertMemInfoToBytes("1031016 KB", "KB", CommonUtils.BYTES_IN_A_KILOBYTE));
56-
}
57-
58-
public void testConvertMemInfoToBytesFromMb() {
59-
assertEquals(
60-
1081081856,
61-
CommonUtils.convertMemInfoToBytes("1031 MB", "MB", CommonUtils.BYTES_IN_A_MEGABYTE));
62-
}
63-
64-
public void testConvertMemInfoToBytesFromGb() {
65-
assertEquals(
66-
10737418240L,
67-
CommonUtils.convertMemInfoToBytes("10 GB", "GB", CommonUtils.BYTES_IN_A_GIGABYTE));
68-
}
69-
7052
public void testCreateInstanceIdFromNullInput() {
7153
assertNull(CommonUtils.createInstanceIdFrom(((String[]) null)));
7254
}
@@ -151,7 +133,7 @@ public void testGetCpuArchitecture() {
151133
}
152134

153135
public void testGetTotalRamInBytes() {
154-
final long bytes = CommonUtils.getTotalRamInBytes();
136+
final long bytes = CommonUtils.calculateTotalRamInBytes(getContext());
155137
// can't check complete string because emulators & devices may be different.
156138
assertTrue(bytes > 0);
157139
Log.d(Logger.TAG, "testGetTotalRam: " + bytes);

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/stacktrace/TrimmedThrowableDataTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.crashlytics.internal.stacktrace;
1616

17+
import static com.google.firebase.crashlytics.internal.stacktrace.TrimmedThrowableData.makeTrimmedThrowableData;
1718
import static org.mockito.Mockito.doReturn;
1819
import static org.mockito.Mockito.mock;
1920

@@ -53,7 +54,7 @@ public void testStackTraceIsTrimmed() {
5354
doReturn(mockStackTrace(3)).when(mockException).getStackTrace();
5455

5556
final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(1);
56-
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
57+
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);
5758

5859
assertEquals(1, t.stacktrace.length);
5960
assertEquals(mockException.getStackTrace()[0], t.stacktrace[0]);
@@ -65,7 +66,7 @@ public void testCauseStackTraceIsTrimmed() {
6566
doReturn(mockCause).when(mockException).getCause();
6667

6768
final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(1);
68-
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
69+
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);
6970

7071
assertEquals(1, t.stacktrace.length);
7172
assertEquals(mockException.getStackTrace()[0], t.stacktrace[0]);
@@ -78,7 +79,7 @@ public void testStackTraceIsNotModifiedIfSmallEnough() {
7879
doReturn(mockStackTrace(3)).when(mockException).getStackTrace();
7980

8081
final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(5);
81-
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
82+
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);
8283

8384
assertEquals(3, t.stacktrace.length);
8485
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));
@@ -90,7 +91,7 @@ public void testCauseStackTraceIsNotModifiedIfSmallEnough() {
9091
doReturn(mockCause).when(mockException).getCause();
9192

9293
final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(5);
93-
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
94+
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);
9495

9596
assertEquals(3, t.stacktrace.length);
9697
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));
@@ -105,7 +106,7 @@ public void testOnlyCauseStackTraceIsTrimmed() {
105106
doReturn(mockCause).when(mockException).getCause();
106107

107108
final StackTraceTrimmingStrategy trimmingStrategy = new TruncateStrategy(4);
108-
final TrimmedThrowableData t = new TrimmedThrowableData(mockException, trimmingStrategy);
109+
final TrimmedThrowableData t = makeTrimmedThrowableData(mockException, trimmingStrategy);
109110

110111
assertEquals(3, t.stacktrace.length);
111112
assertTrue(Arrays.equals(mockException.getStackTrace(), t.stacktrace));

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CommonUtils.java

Lines changed: 7 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333
import android.text.TextUtils;
3434
import androidx.annotation.Nullable;
3535
import com.google.firebase.crashlytics.internal.Logger;
36-
import java.io.BufferedReader;
3736
import java.io.Closeable;
3837
import java.io.File;
39-
import java.io.FileReader;
4038
import java.io.IOException;
4139
import java.io.InputStream;
4240
import java.security.MessageDigest;
@@ -47,7 +45,6 @@
4745
import java.util.List;
4846
import java.util.Locale;
4947
import java.util.Map;
50-
import java.util.regex.Pattern;
5148

5249
public 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>.

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ private void doOpenSession(String sessionIdentifier) {
560560

561561
StaticSessionData.AppData appData = createAppData(idManager, this.appData);
562562
StaticSessionData.OsData osData = createOsData();
563-
StaticSessionData.DeviceData deviceData = createDeviceData();
563+
StaticSessionData.DeviceData deviceData = createDeviceData(context);
564564

565565
nativeComponent.prepareNativeSession(
566566
sessionIdentifier,
@@ -770,15 +770,15 @@ private static StaticSessionData.OsData createOsData() {
770770
VERSION.RELEASE, VERSION.CODENAME, CommonUtils.isRooted());
771771
}
772772

773-
private static StaticSessionData.DeviceData createDeviceData() {
773+
private static StaticSessionData.DeviceData createDeviceData(Context context) {
774774
final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
775775
final long diskSpace = (long) statFs.getBlockCount() * (long) statFs.getBlockSize();
776776

777777
return StaticSessionData.DeviceData.create(
778778
CommonUtils.getCpuArchitectureInt(),
779779
Build.MODEL,
780780
Runtime.getRuntime().availableProcessors(),
781-
CommonUtils.getTotalRamInBytes(),
781+
CommonUtils.calculateTotalRamInBytes(context),
782782
diskSpace,
783783
CommonUtils.isEmulator(),
784784
CommonUtils.getDeviceState(),

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/common/CrashlyticsReportDataCapture.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package com.google.firebase.crashlytics.internal.common;
1616

17+
import static com.google.firebase.crashlytics.internal.stacktrace.TrimmedThrowableData.makeTrimmedThrowableData;
18+
1719
import android.app.ActivityManager;
1820
import android.app.ActivityManager.RunningAppProcessInfo;
1921
import android.content.Context;
@@ -97,7 +99,7 @@ public Event captureEventData(
9799
boolean includeAllThreads) {
98100
final int orientation = context.getResources().getConfiguration().orientation;
99101
final TrimmedThrowableData trimmedEvent =
100-
new TrimmedThrowableData(event, stackTraceTrimmingStrategy);
102+
makeTrimmedThrowableData(event, stackTraceTrimmingStrategy);
101103

102104
return Event.builder()
103105
.setType(type)
@@ -209,7 +211,7 @@ private CrashlyticsReport.Session.Device populateSessionDeviceData() {
209211
final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
210212
final int arch = getDeviceArchitecture();
211213
final int availableProcessors = Runtime.getRuntime().availableProcessors();
212-
final long totalRam = CommonUtils.getTotalRamInBytes();
214+
final long totalRam = CommonUtils.calculateTotalRamInBytes(context);
213215
final long diskSpace = (long) statFs.getBlockCount() * (long) statFs.getBlockSize();
214216
final boolean isEmulator = CommonUtils.isEmulator();
215217
final int state = CommonUtils.getDeviceState();
@@ -278,7 +280,9 @@ private Event.Device populateEventDeviceData(int orientation) {
278280
final int batteryVelocity = battery.getBatteryVelocity();
279281
final boolean proximityEnabled = CommonUtils.getProximitySensorEnabled(context);
280282
final long usedRamBytes =
281-
CommonUtils.getTotalRamInBytes() - CommonUtils.calculateFreeRamInBytes(context);
283+
ensureNonNegative(
284+
CommonUtils.calculateTotalRamInBytes(context)
285+
- CommonUtils.calculateFreeRamInBytes(context));
282286
final long diskUsedBytes =
283287
CommonUtils.calculateUsedDiskSpaceInBytes(Environment.getDataDirectory().getPath());
284288

@@ -466,4 +470,9 @@ private static int getDeviceArchitecture() {
466470

467471
return arch;
468472
}
473+
474+
/** Returns the given value, or zero is the value is negative. */
475+
private static long ensureNonNegative(long value) {
476+
return value > 0 ? value : 0;
477+
}
469478
}

0 commit comments

Comments
 (0)