Skip to content

Commit 26ac686

Browse files
authored
Merge branch 'master' into robolectric-4.10
2 parents 4c75e74 + b7a34fb commit 26ac686

File tree

10 files changed

+51
-134
lines changed

10 files changed

+51
-134
lines changed

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/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/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/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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private CrashlyticsReport.Session.Device populateSessionDeviceData() {
209209
final StatFs statFs = new StatFs(Environment.getDataDirectory().getPath());
210210
final int arch = getDeviceArchitecture();
211211
final int availableProcessors = Runtime.getRuntime().availableProcessors();
212-
final long totalRam = CommonUtils.getTotalRamInBytes();
212+
final long totalRam = CommonUtils.calculateTotalRamInBytes(context);
213213
final long diskSpace = (long) statFs.getBlockCount() * (long) statFs.getBlockSize();
214214
final boolean isEmulator = CommonUtils.isEmulator();
215215
final int state = CommonUtils.getDeviceState();
@@ -278,7 +278,9 @@ private Event.Device populateEventDeviceData(int orientation) {
278278
final int batteryVelocity = battery.getBatteryVelocity();
279279
final boolean proximityEnabled = CommonUtils.getProximitySensorEnabled(context);
280280
final long usedRamBytes =
281-
CommonUtils.getTotalRamInBytes() - CommonUtils.calculateFreeRamInBytes(context);
281+
ensureNonNegative(
282+
CommonUtils.calculateTotalRamInBytes(context)
283+
- CommonUtils.calculateFreeRamInBytes(context));
282284
final long diskUsedBytes =
283285
CommonUtils.calculateUsedDiskSpaceInBytes(Environment.getDataDirectory().getPath());
284286

@@ -466,4 +468,9 @@ private static int getDeviceArchitecture() {
466468

467469
return arch;
468470
}
471+
472+
/** Returns the given value, or zero is the value is negative. */
473+
private static long ensureNonNegative(long value) {
474+
return value > 0 ? value : 0;
475+
}
469476
}

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased
2-
* [feature] Add option to allow SDK create cache indexes automatically to improve query execution locally. [`db.getPersistentCacheIndexManager().enableIndexAutoCreation()`](//github.com/firebase/firebase-android-sdk/pull/4987)
2+
* [feature] Add the option to allow the SDK to create cache indexes automatically to
3+
improve query execution locally. [`db.getPersistentCacheIndexManager().enableIndexAutoCreation()`](//github.com/firebase/firebase-android-sdk/pull/4987)
34

45
# 24.7.1
56
* [fixed] Implement equals method on Filter class. [#5210](//github.com/firebase/firebase-android-sdk/issues/5210)

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ public FirebaseApp getApp() {
356356
* @return A task that resolves once all indices are successfully configured.
357357
* @throws IllegalArgumentException if the JSON format is invalid
358358
* @deprecated Instead of creating cache indexes manually, consider using {@link
359-
* PersistentCacheIndexManager#enableIndexAutoCreation()} to let SDK decide whether to create
360-
* cache indexes for queries running locally.
359+
* PersistentCacheIndexManager#enableIndexAutoCreation()} to let the SDK decide whether to
360+
* create cache indexes for queries running locally.
361361
*/
362362
@Deprecated
363363
@PreviewApi

firebase-firestore/src/main/java/com/google/firebase/firestore/PersistentCacheIndexManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void disableIndexAutoCreation() {
5252
}
5353

5454
/**
55-
* Removes all persistent cache indexes. Please note this function will also deletes indexes
56-
* generated by {@link FirebaseFirestore#setIndexConfiguration(String)}, which is deprecated.
55+
* Removes all persistent cache indexes. Please note this function also deletes indexes generated
56+
* by {@link FirebaseFirestore#setIndexConfiguration(String)}, which is deprecated.
5757
*/
5858
public void deleteAllIndexes() {
5959
client.deleteAllFieldIndexes();

0 commit comments

Comments
 (0)