Skip to content

Commit

Permalink
Merge pull request #6296 from seadowg/project-delete-mem-leak
Browse files Browse the repository at this point in the history
Fix memory leak when deleting project
  • Loading branch information
grzesiek2010 authored Aug 29, 2024
2 parents 98e673d + 9866b5b commit 2dff662
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import org.odk.collect.android.application.initialization.ExistingProjectMigrator;
import org.odk.collect.android.audio.AudioRecordingControllerFragment;
import org.odk.collect.android.audio.AudioRecordingErrorDialogFragment;
import org.odk.collect.android.backgroundwork.SendFormsTaskSpec;
import org.odk.collect.android.backgroundwork.AutoUpdateTaskSpec;
import org.odk.collect.android.backgroundwork.SendFormsTaskSpec;
import org.odk.collect.android.backgroundwork.SyncFormsTaskSpec;
import org.odk.collect.android.configure.qr.QRCodeScannerFragment;
import org.odk.collect.android.configure.qr.QRCodeTabsActivity;
Expand Down Expand Up @@ -47,7 +47,6 @@
import org.odk.collect.android.preferences.dialogs.ChangeAdminPasswordDialog;
import org.odk.collect.android.preferences.dialogs.ResetDialogPreferenceFragmentCompat;
import org.odk.collect.android.preferences.dialogs.ServerAuthDialogFragment;
import org.odk.collect.android.preferences.screens.BaseAdminPreferencesFragment;
import org.odk.collect.android.preferences.screens.BasePreferencesFragment;
import org.odk.collect.android.preferences.screens.BaseProjectPreferencesFragment;
import org.odk.collect.android.preferences.screens.ExperimentalPreferencesFragment;
Expand Down Expand Up @@ -77,8 +76,8 @@
import org.odk.collect.android.utilities.ThemeUtils;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.items.SelectOneFromMapDialogFragment;
import org.odk.collect.async.network.NetworkStateProvider;
import org.odk.collect.async.Scheduler;
import org.odk.collect.async.network.NetworkStateProvider;
import org.odk.collect.draw.DrawActivity;
import org.odk.collect.googlemaps.GoogleMapFragment;
import org.odk.collect.location.LocationClient;
Expand Down Expand Up @@ -230,8 +229,6 @@ interface Builder {

void inject(BaseProjectPreferencesFragment baseProjectPreferencesFragment);

void inject(BaseAdminPreferencesFragment baseAdminPreferencesFragment);

void inject(AndroidShortcutsActivity androidShortcutsActivity);

void inject(ProjectSettingsDialog projectSettingsDialog);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import org.odk.collect.android.preferences.Defaults;
import org.odk.collect.android.preferences.PreferenceVisibilityHandler;
import org.odk.collect.android.preferences.ProjectPreferencesViewModel;
import org.odk.collect.android.preferences.source.SettingsStore;
import org.odk.collect.android.preferences.source.SharedPreferencesSettingsProvider;
import org.odk.collect.android.projects.ProjectCreator;
import org.odk.collect.android.projects.ProjectDeleter;
Expand Down Expand Up @@ -394,18 +393,6 @@ public FormSessionRepository providesFormSessionStore(Application application) {
return new AppStateFormSessionRepository(application);
}

@Provides
@Named("GENERAL_SETTINGS_STORE")
public SettingsStore providesGeneralSettingsStore(SettingsProvider settingsProvider) {
return new SettingsStore(settingsProvider.getUnprotectedSettings());
}

@Provides
@Named("ADMIN_SETTINGS_STORE")
public SettingsStore providesAdminSettingsStore(SettingsProvider settingsProvider) {
return new SettingsStore(settingsProvider.getProtectedSettings());
}

@Provides
public ExternalWebPageHelper providesExternalWebPageHelper() {
return new ExternalWebPageHelper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,40 @@
import androidx.annotation.NonNull;

import org.jetbrains.annotations.NotNull;
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.preferences.source.SettingsStore;

import javax.inject.Inject;
import javax.inject.Named;
import org.odk.collect.shared.settings.Settings;

public abstract class BaseAdminPreferencesFragment extends BasePreferencesFragment {

@Inject
@Named("ADMIN_SETTINGS_STORE")
SettingsStore adminSettingsStore;
private String projectId;
private Settings adminSettings;

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
DaggerUtils.getComponent(context).inject(this);
projectId = projectsDataService.getCurrentProject().getUuid();
adminSettings = settingsProvider.getProtectedSettings(projectId);
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
getPreferenceManager().setPreferenceDataStore(adminSettingsStore);
getPreferenceManager().setPreferenceDataStore(new SettingsStore(adminSettings));
}

@Override
public void onResume() {
super.onResume();
settingsProvider.getProtectedSettings().registerOnSettingChangeListener(this);
adminSettings.registerOnSettingChangeListener(this);
}

@Override
public void onPause() {
super.onPause();
settingsProvider.getProtectedSettings().unregisterOnSettingChangeListener(this);
adminSettings.unregisterOnSettingChangeListener(this);
}

@Override
public void onSettingChanged(@NotNull String key) {
settingsChangeHandler.onSettingChanged(projectsDataService.getCurrentProject().getUuid(), settingsProvider.getProtectedSettings().getAll().get(key), key);
settingsChangeHandler.onSettingChanged(projectId, adminSettings.getAll().get(key), key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,12 @@
import org.odk.collect.android.preferences.ProjectPreferencesViewModel;
import org.odk.collect.android.preferences.source.SettingsStore;
import org.odk.collect.android.utilities.AdminPasswordProvider;
import org.odk.collect.shared.settings.Settings;

import javax.inject.Inject;
import javax.inject.Named;

public abstract class BaseProjectPreferencesFragment extends BasePreferencesFragment {

@Inject
@Named("GENERAL_SETTINGS_STORE")
SettingsStore generalSettingsStore;

@Inject
AdminPasswordProvider adminPasswordProvider;

Expand All @@ -34,17 +30,22 @@ public abstract class BaseProjectPreferencesFragment extends BasePreferencesFrag
PreferenceVisibilityHandler preferenceVisibilityHandler;

protected ProjectPreferencesViewModel projectPreferencesViewModel;
private Settings settings;
private String projectId;

@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
DaggerUtils.getComponent(context).inject(this);
projectPreferencesViewModel = new ViewModelProvider(requireActivity(), factory).get(ProjectPreferencesViewModel.class);

projectId = projectsDataService.getCurrentProject().getUuid();
settings = settingsProvider.getUnprotectedSettings(projectId);
}

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
getPreferenceManager().setPreferenceDataStore(generalSettingsStore);
getPreferenceManager().setPreferenceDataStore(new SettingsStore(settings));
}

@Override
Expand All @@ -56,17 +57,17 @@ public void onViewCreated(@NotNull View view, @Nullable Bundle savedInstanceStat
@Override
public void onResume() {
super.onResume();
settingsProvider.getUnprotectedSettings().registerOnSettingChangeListener(this);
settings.registerOnSettingChangeListener(this);
}

@Override
public void onPause() {
super.onPause();
settingsProvider.getUnprotectedSettings().unregisterOnSettingChangeListener(this);
settings.unregisterOnSettingChangeListener(this);
}

@Override
public void onSettingChanged(@NotNull String key) {
settingsChangeHandler.onSettingChanged(projectsDataService.getCurrentProject().getUuid(), settingsProvider.getUnprotectedSettings().getAll().get(key), key);
settingsChangeHandler.onSettingChanged(projectId, settings.getAll().get(key), key);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.odk.collect.android.preferences.screens

import android.content.Context
import androidx.preference.Preference
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.equalTo
Expand All @@ -11,19 +11,19 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.odk.collect.android.R
import org.odk.collect.android.application.Collect
import org.odk.collect.android.injection.config.AppDependencyModule
import org.odk.collect.android.support.CollectHelpers
import org.odk.collect.fragmentstest.FragmentScenarioLauncherRule
import org.odk.collect.metadata.InstallIDProvider
import org.odk.collect.settings.InMemSettingsProvider
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.ProjectKeys

@RunWith(AndroidJUnit4::class)
class FormMetadataPreferencesFragmentTest {
private val installIDProvider = mock<InstallIDProvider>()
private val settingsProvider = InMemSettingsProvider()
private val settingsProvider =
ApplicationProvider.getApplicationContext<Collect>().component.settingsProvider()

@get:Rule
var launcherRule = FragmentScenarioLauncherRule()
Expand All @@ -34,11 +34,9 @@ class FormMetadataPreferencesFragmentTest {
override fun providesInstallIDProvider(settingsProvider: SettingsProvider): InstallIDProvider {
return installIDProvider
}

override fun providesSettingsProvider(context: Context): SettingsProvider {
return settingsProvider
}
})

CollectHelpers.setupDemoProject()
}

@Test
Expand Down Expand Up @@ -72,7 +70,8 @@ class FormMetadataPreferencesFragmentTest {
whenever(installIDProvider.installID).thenReturn("123456789")
settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_USERNAME, "John")
settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_PHONENUMBER, "789")
settingsProvider.getUnprotectedSettings().save(ProjectKeys.KEY_METADATA_EMAIL, "john@gmail.com")
settingsProvider.getUnprotectedSettings()
.save(ProjectKeys.KEY_METADATA_EMAIL, "john@gmail.com")

launcherRule
.launch(FormMetadataPreferencesFragment::class.java)
Expand Down

0 comments on commit 2dff662

Please sign in to comment.