-
-
Notifications
You must be signed in to change notification settings - Fork 459
Avoid StrictMode warnings #4724
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
Changes from all commits
95282db
683bb3b
3f3eb00
ad0dd38
a7e737e
c1b6bc6
b2e03d4
57f6ea3
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.sentry.android.core.internal.util; | ||
|
||
import android.os.StrictMode; | ||
import io.sentry.util.runtime.IRuntimeManager; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
@ApiStatus.Internal | ||
public final class AndroidRuntimeManager implements IRuntimeManager { | ||
@Override | ||
public <T> T runWithRelaxedPolicy(final @NotNull IRuntimeManagerCallback<T> toRun) { | ||
final @NotNull StrictMode.ThreadPolicy oldPolicy = StrictMode.getThreadPolicy(); | ||
final @NotNull StrictMode.VmPolicy oldVmPolicy = StrictMode.getVmPolicy(); | ||
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX); | ||
StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX); | ||
try { | ||
return toRun.run(); | ||
} finally { | ||
StrictMode.setThreadPolicy(oldPolicy); | ||
StrictMode.setVmPolicy(oldVmPolicy); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.sentry.android.core.internal.util | ||
|
||
import android.os.StrictMode | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlin.test.AfterTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertTrue | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AndroidRuntimeManagerTest { | ||
|
||
val sut = AndroidRuntimeManager() | ||
|
||
@AfterTest | ||
fun `clean up`() { | ||
// Revert StrictMode policies to avoid issues with other tests | ||
StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX) | ||
StrictMode.setVmPolicy(StrictMode.VmPolicy.LAX) | ||
} | ||
|
||
@Test | ||
fun `runWithRelaxedPolicy changes policy when running and restores it afterwards`() { | ||
var called = false | ||
val threadPolicy = StrictMode.ThreadPolicy.Builder().detectAll().penaltyDeath().build() | ||
val vmPolicy = StrictMode.VmPolicy.Builder().detectAll().penaltyDeath().build() | ||
assertNotEquals(StrictMode.ThreadPolicy.LAX, threadPolicy) | ||
assertNotEquals(StrictMode.VmPolicy.LAX, vmPolicy) | ||
|
||
// Set and assert the StrictMode policies | ||
StrictMode.setThreadPolicy(threadPolicy) | ||
StrictMode.setVmPolicy(vmPolicy) | ||
assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) | ||
assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) | ||
|
||
// Run the function and assert LAX policies | ||
called = | ||
sut.runWithRelaxedPolicy { | ||
assertEquals( | ||
StrictMode.ThreadPolicy.LAX.toString(), | ||
StrictMode.getThreadPolicy().toString(), | ||
) | ||
assertEquals(StrictMode.VmPolicy.LAX.toString(), StrictMode.getVmPolicy().toString()) | ||
true | ||
} | ||
|
||
// 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 changes policy 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 { | ||
assertEquals( | ||
StrictMode.ThreadPolicy.LAX.toString(), | ||
StrictMode.getThreadPolicy().toString(), | ||
) | ||
assertEquals(StrictMode.VmPolicy.LAX.toString(), StrictMode.getVmPolicy().toString()) | ||
called = true | ||
throw Exception("Test exception") | ||
} | ||
} catch (_: Exception) {} | ||
|
||
// Policies should be reverted back | ||
assertEquals(threadPolicy.toString(), StrictMode.getThreadPolicy().toString()) | ||
assertEquals(vmPolicy.toString(), StrictMode.getVmPolicy().toString()) | ||
|
||
// Ensure the code ran | ||
assertTrue(called) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,34 +40,22 @@ public void processDirectory(final @NotNull File directory) { | |
try { | ||
logger.log(SentryLevel.DEBUG, "Processing dir. %s", directory.getAbsolutePath()); | ||
|
||
if (!directory.exists()) { | ||
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name)); | ||
if (filteredListFiles == null) { | ||
logger.log( | ||
SentryLevel.WARNING, | ||
"Directory '%s' doesn't exist. No cached events to send.", | ||
SentryLevel.ERROR, | ||
"Cache dir %s is null or is not a directory.", | ||
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. so does 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. yep. From the javadoc |
||
directory.getAbsolutePath()); | ||
return; | ||
} | ||
if (!directory.isDirectory()) { | ||
logger.log( | ||
SentryLevel.ERROR, "Cache dir %s is not a directory.", directory.getAbsolutePath()); | ||
return; | ||
} | ||
|
||
final File[] listFiles = directory.listFiles(); | ||
if (listFiles == null) { | ||
logger.log(SentryLevel.ERROR, "Cache dir %s is null.", directory.getAbsolutePath()); | ||
return; | ||
} | ||
|
||
final File[] filteredListFiles = directory.listFiles((d, name) -> isRelevantFileName(name)); | ||
|
||
logger.log( | ||
SentryLevel.DEBUG, | ||
"Processing %d items from cache dir %s", | ||
filteredListFiles != null ? filteredListFiles.length : 0, | ||
filteredListFiles.length, | ||
directory.getAbsolutePath()); | ||
|
||
for (File file : listFiles) { | ||
for (File file : filteredListFiles) { | ||
stefanosiano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// it ignores .sentry-native database folder and new ones that might come up | ||
if (!file.isFile()) { | ||
logger.log(SentryLevel.DEBUG, "File %s is not a File.", file.getAbsolutePath()); | ||
|
Uh oh!
There was an error while loading. Please reload this page.