Skip to content
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

Disable StrictMode for FormHiearchyActivity #6456

Merged
merged 4 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.os.StrictMode;

import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;
import org.odk.collect.android.BuildConfig;
import org.odk.collect.android.dynamicpreload.ExternalDataManager;
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.injection.config.AppDependencyComponent;
Expand All @@ -36,13 +33,14 @@
import org.odk.collect.android.injection.config.CollectProjectsDependencyModule;
import org.odk.collect.android.injection.config.CollectSelfieCameraDependencyModule;
import org.odk.collect.android.injection.config.DaggerAppDependencyComponent;
import org.odk.collect.android.utilities.CollectStrictMode;
import org.odk.collect.android.utilities.FormsRepositoryProvider;
import org.odk.collect.android.utilities.LocaleHelper;
import org.odk.collect.androidshared.data.AppState;
import org.odk.collect.androidshared.data.StateStore;
import org.odk.collect.async.network.NetworkStateProvider;
import org.odk.collect.androidshared.system.ExternalFilesUtils;
import org.odk.collect.async.Scheduler;
import org.odk.collect.async.network.NetworkStateProvider;
import org.odk.collect.audiorecorder.AudioRecorderDependencyComponent;
import org.odk.collect.audiorecorder.AudioRecorderDependencyComponentProvider;
import org.odk.collect.audiorecorder.DaggerAudioRecorderDependencyComponent;
Expand Down Expand Up @@ -149,33 +147,11 @@ public void onCreate() {

applicationComponent.applicationInitializer().initialize();
fixGoogleBug154855417();
setupStrictMode();
CollectStrictMode.enable();
}
);
}

/**
* Enable StrictMode and log violations to the system log.
* This catches disk and network access on the main thread, as well as leaked SQLite
* cursors and unclosed resources.
*/
private void setupStrictMode() {
if (BuildConfig.DEBUG) {
StrictMode.ThreadPolicy.Builder policyBuilder = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.permitDiskReads() // shared preferences are being read on main thread (`GetAndSubmitFormTest`)
.permitDiskWrites() // files are being created on the fly (`GetAndSubmitFormTest`)
.penaltyDeath();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
policyBuilder.permitUnbufferedIo(); // `ObjectInputStream#readObject` calls
}

StrictMode.setThreadPolicy(policyBuilder
.build());
}
}

private void setupDagger() {
applicationComponent = DaggerAppDependencyComponent.builder()
.application(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.odk.collect.android.javarosawrapper.JavaRosaFormController;
import org.odk.collect.android.projects.ProjectsDataService;
import org.odk.collect.android.utilities.ApplicationConstants;
import org.odk.collect.android.utilities.CollectStrictMode;
import org.odk.collect.android.utilities.FormEntryPromptUtils;
import org.odk.collect.android.utilities.FormsRepositoryProvider;
import org.odk.collect.android.utilities.HtmlUtils;
Expand Down Expand Up @@ -212,6 +213,8 @@ public void handleOnBackPressed() {

@Override
public void onCreate(Bundle savedInstanceState) {
CollectStrictMode.disable();

DaggerUtils.getComponent(this).inject(this);

String sessionId = getIntent().getStringExtra(EXTRA_SESSION_ID);
Expand Down Expand Up @@ -924,4 +927,10 @@ public void deleteGroup() {
goUpLevel();
}
}

@Override
protected void onDestroy() {
CollectStrictMode.enable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a temporary solution? If so should we file an issue to fix it in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added an issue: #6470

super.onDestroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.odk.collect.android.utilities

import android.os.Build
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import org.odk.collect.android.BuildConfig

object CollectStrictMode {

@JvmStatic
fun enable() {
if (BuildConfig.DEBUG) {
val policyBuilder = ThreadPolicy.Builder()
.detectAll()
.permitDiskReads() // shared preferences are being read on main thread (`GetAndSubmitFormTest`)
.permitDiskWrites() // files are being created on the fly (`GetAndSubmitFormTest`)
.penaltyDeath()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
policyBuilder.permitUnbufferedIo() // `ObjectInputStream#readObject` calls
}

StrictMode.setThreadPolicy(policyBuilder.build())
}
}

@JvmStatic
fun disable() {
StrictMode.setThreadPolicy(ThreadPolicy.Builder().permitCustomSlowCalls().build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class SynchronizedDatabaseConnection(
path,
name,
migrator,
databaseVersion
databaseVersion,
true
)

fun <T> withConnection(block: DatabaseConnection.() -> T): T {
Expand Down