-
-
Notifications
You must be signed in to change notification settings - Fork 459
Avoid StrictMode warnings (followup) #4809
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,18 +227,24 @@ private void setDeviceIO(final @NotNull Device device, final boolean includeDyna | |
// this way of getting the size of storage might be problematic for storages bigger than 2GB | ||
// check the use of | ||
// https://developer.android.com/reference/java/io/File.html#getFreeSpace%28%29 | ||
final @Nullable File internalStorageFile = context.getExternalFilesDir(null); | ||
if (internalStorageFile != null) { | ||
StatFs internalStorageStat = new StatFs(internalStorageFile.getPath()); | ||
device.setStorageSize(getTotalInternalStorage(internalStorageStat)); | ||
device.setFreeStorage(getUnusedInternalStorage(internalStorageStat)); | ||
} | ||
|
||
final @Nullable StatFs externalStorageStat = getExternalStorageStat(internalStorageFile); | ||
if (externalStorageStat != null) { | ||
device.setExternalStorageSize(getTotalExternalStorage(externalStorageStat)); | ||
device.setExternalFreeStorage(getUnusedExternalStorage(externalStorageStat)); | ||
} | ||
options | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do this to have a base of 0 strictmode warnings, right? I'm just wondering if we should keep this as-is, as the collection of these attributes can be turned off via settings anyway. |
||
.getRuntimeManager() | ||
.runWithRelaxedPolicy( | ||
() -> { | ||
final @Nullable File internalStorageFile = context.getExternalFilesDir(null); | ||
if (internalStorageFile != null) { | ||
StatFs internalStorageStat = new StatFs(internalStorageFile.getPath()); | ||
device.setStorageSize(getTotalInternalStorage(internalStorageStat)); | ||
device.setFreeStorage(getUnusedInternalStorage(internalStorageStat)); | ||
} | ||
|
||
final @Nullable StatFs externalStorageStat = | ||
getExternalStorageStat(internalStorageFile); | ||
if (externalStorageStat != null) { | ||
device.setExternalStorageSize(getTotalExternalStorage(externalStorageStat)); | ||
device.setExternalFreeStorage(getUnusedExternalStorage(externalStorageStat)); | ||
} | ||
}); | ||
|
||
if (device.getConnectionType() == null) { | ||
// wifi, ethernet or cellular, null if none | ||
|
@@ -479,7 +485,7 @@ private Long getUnusedExternalStorage(final @NotNull StatFs stat) { | |
@Nullable | ||
private String getDeviceId() { | ||
try { | ||
return Installation.id(context); | ||
return options.getRuntimeManager().runWithRelaxedPolicy(() -> Installation.id(context)); | ||
} catch (Throwable e) { | ||
options.getLogger().log(SentryLevel.ERROR, "Error getting installationId.", e); | ||
} | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -37,7 +37,7 @@ class AndroidRuntimeManagerTest { | |||||||||
|
||||||||||
// Run the function and assert LAX policies | ||||||||||
called = | ||||||||||
sut.runWithRelaxedPolicy { | ||||||||||
sut.runWithRelaxedPolicy<Boolean> { | ||||||||||
assertEquals( | ||||||||||
StrictMode.ThreadPolicy.LAX.toString(), | ||||||||||
StrictMode.getThreadPolicy().toString(), | ||||||||||
|
@@ -64,6 +64,37 @@ class AndroidRuntimeManagerTest { | |||||||||
StrictMode.setThreadPolicy(threadPolicy) | ||||||||||
StrictMode.setVmPolicy(vmPolicy) | ||||||||||
|
||||||||||
// Run the function and assert LAX policies | ||||||||||
try { | ||||||||||
sut.runWithRelaxedPolicy<Unit> { | ||||||||||
assertEquals( | ||||||||||
StrictMode.ThreadPolicy.LAX.toString(), | ||||||||||
StrictMode.getThreadPolicy().toString(), | ||||||||||
) | ||||||||||
assertEquals(StrictMode.VmPolicy.LAX.toString(), StrictMode.getVmPolicy().toString()) | ||||||||||
called = true | ||||||||||
throw Exception("Test exception") | ||||||||||
} | ||||||||||
} catch (_: Exception) {} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe extend the test to assert that the exception is thrown, something like this:
Suggested change
|
||||||||||
|
||||||||||
// Policies should be reverted back | ||||||||||
assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) | ||||||||||
assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) | ||||||||||
|
||||||||||
// Ensure the code ran | ||||||||||
assertTrue(called) | ||||||||||
} | ||||||||||
|
||||||||||
@Test | ||||||||||
fun `runWithRelaxedPolicy with Runnable changes policy when running and restores it afterwards even if the code throws`() { | ||||||||||
var called = false | ||||||||||
val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build() | ||||||||||
val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build() | ||||||||||
|
||||||||||
// Set and assert the StrictMode policies | ||||||||||
StrictMode.setThreadPolicy(threadPolicy) | ||||||||||
StrictMode.setVmPolicy(vmPolicy) | ||||||||||
|
||||||||||
// Run the function and assert LAX policies | ||||||||||
try { | ||||||||||
sut.runWithRelaxedPolicy { | ||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test to ensure if an exception is thrown it's properly propagated and not silently swallowed? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it's worth mentioning that as a separate item, maybe combine it with the line below? Not sure if the danger check likes that though 😅