Skip to content

Commit

Permalink
Migrate admin prefs for show_map_sdk and show_map_basemap to a new ad…
Browse files Browse the repository at this point in the history
…min setting, "Maps", which shows or hides the entire Maps preference screen.
  • Loading branch information
zestyping committed Jul 23, 2019
1 parent 46ca56f commit a4163b3
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public void onCreate() {

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
FormMetadataMigrator.migrate(prefs);
PrefMigrator.migrate(prefs, PrefMigrator.MIGRATIONS);
PrefMigrator.migrateSharedPrefs();
AutoSendPreferenceMigrator.migrate();

initializeJavaRosa();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public final class AdminKeys {
private static final String KEY_NAVIGATION = "change_navigation";
static final String KEY_CONSTRAINT_BEHAVIOR = "change_constraint_behavior";

private static final String KEY_BASEMAP_SOURCE = "basemap_source";
static final String KEY_MAPS = "maps";

private static final String KEY_ANALYTICS = "analytics";
public static final String KEY_MOVING_BACKWARDS = "moving_backwards";
Expand Down Expand Up @@ -97,8 +97,6 @@ public final class AdminKeys {
ag(KEY_NAVIGATION, GeneralKeys.KEY_NAVIGATION),
ag(KEY_CONSTRAINT_BEHAVIOR, GeneralKeys.KEY_CONSTRAINT_BEHAVIOR),

ag(KEY_BASEMAP_SOURCE, GeneralKeys.KEY_BASEMAP_SOURCE),

ag(KEY_ANALYTICS, GeneralKeys.KEY_ANALYTICS)
};

Expand All @@ -117,7 +115,8 @@ public final class AdminKeys {
KEY_MARK_AS_FINALIZED,
KEY_CHANGE_ADMIN_PASSWORD,
KEY_MOVING_BACKWARDS,
ALLOW_OTHER_WAYS_OF_EDITING_FORM
ALLOW_OTHER_WAYS_OF_EDITING_FORM,
KEY_MAPS
);

static Collection<String> serverKeys = Collections.singletonList(
Expand Down Expand Up @@ -151,10 +150,6 @@ public final class AdminKeys {
KEY_SHOW_SPLASH_SCREEN
);

static Collection<String> mapsKeys = Arrays.asList(
KEY_BASEMAP_SOURCE
);

private static Collection<String> allKeys() {
Collection<String> keys = new ArrayList<>();
for (AdminAndGeneralKeys atg : adminToGeneral) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.odk.collect.android.preferences;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import org.odk.collect.android.application.Collect;
Expand Down Expand Up @@ -92,4 +93,8 @@ public void reloadPreferences() {
save(key, get(key));
}
}

public SharedPreferences getSharedPreferences() {
return sharedPreferences;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import androidx.annotation.Nullable;

import static org.odk.collect.android.preferences.AdminKeys.KEY_MAPS;
import static org.odk.collect.android.preferences.PreferencesActivity.INTENT_KEY_ADMIN_MODE;

public class GeneralPreferencesFragment extends BasePreferenceFragment implements Preference.OnPreferenceClickListener {
Expand Down Expand Up @@ -104,7 +105,8 @@ private void setPreferencesVisibility() {
preferenceScreen.removePreference(findPreference("user_interface"));
}

if (!hasAtleastOneSettingEnabled(AdminKeys.mapsKeys)) {
boolean mapsScreenEnabled = (boolean) AdminSharedPreferences.getInstance().get(KEY_MAPS);
if (!mapsScreenEnabled) {
preferenceScreen.removePreference(findPreference("maps"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ private PrefMigrator() { } // prevent instantiation
.withValues("osmdroid", "openmap_carto_positron")
.toPairs(KEY_BASEMAP_SOURCE, BASEMAP_SOURCE_CARTO, KEY_CARTO_MAP_STYLE, "positron")
.withValues("osmdroid", "openmap_carto_darkmatter")
.toPairs(KEY_BASEMAP_SOURCE, BASEMAP_SOURCE_CARTO, KEY_CARTO_MAP_STYLE, "dark_matter")
.toPairs(KEY_BASEMAP_SOURCE, BASEMAP_SOURCE_CARTO, KEY_CARTO_MAP_STYLE, "dark_matter"),
};

static final Migration[] ADMIN_MIGRATIONS = {
// When either the map SDK or the basemap selection were previously
// hidden, we want to hide the entire Maps preference screen.
translateKey("show_map_sdk").toKey("maps")
.fromValue(false).toValue(false),
translateKey("show_map_basemap").toKey("maps")
.fromValue(false).toValue(false)
};

public static void migrate(SharedPreferences prefs, Migration... migrations) {
Expand All @@ -81,6 +90,7 @@ public static void migrate(SharedPreferences prefs, Migration... migrations) {

public static void migrateSharedPrefs() {
migrate(PrefUtils.getSharedPrefs(), MIGRATIONS);
migrate(PrefUtils.getAdminSharedPrefs(), ADMIN_MIGRATIONS);
}

public interface Migration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static SharedPreferences getSharedPrefs() {
Collect.getInstance().getApplicationContext());
}

public static SharedPreferences getAdminSharedPrefs() {
return AdminSharedPreferences.getInstance().getSharedPreferences();
}

public static ListPreference createListPref(
Context context, String key, String title, int[] labelIds, String[] values) {
String[] labels = new String[labelIds.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
android:key="show_splash_screen"
android:title="@string/show_splash_title" />
<CheckBoxPreference
android:key="basemap_source"
android:title="@string/basemap_source" />
android:key="maps"
android:title="@string/maps" />
<CheckBoxPreference
android:key="periodic_form_updates_check"
android:title="@string/periodic_form_updates_check_title" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,28 @@ private void assertPrefs(Object... pairs) {
PrefMigrator.migrateSharedPrefs();
assertPrefs("basemap_source", "carto", "carto_map_style", "dark_matter");
}

@Test public void shouldMigrateAdminSettings() {
prefs = AdminSharedPreferences.getInstance().getSharedPreferences();

initPrefs("unrelated", "value");
PrefMigrator.migrateSharedPrefs();
assertPrefs("unrelated", "value");

initPrefs("show_map_sdk", true);
PrefMigrator.migrateSharedPrefs();
assertPrefs("show_map_sdk", true);

initPrefs("show_map_sdk", false);
PrefMigrator.migrateSharedPrefs();
assertPrefs("maps", false);

initPrefs("show_map_basemap", true);
PrefMigrator.migrateSharedPrefs();
assertPrefs("show_map_basemap", true);

initPrefs("show_map_basemap", false);
PrefMigrator.migrateSharedPrefs();
assertPrefs("maps", false);
}
}

0 comments on commit a4163b3

Please sign in to comment.