2019-09-06 14:09:34 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2019-12-19 22:41:21 +00:00
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
2020-01-24 20:29:03 +00:00
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
|
|
|
|
|
|
import com.annimon.stream.Stream;
|
2019-12-19 22:41:21 +00:00
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2020-08-25 19:35:06 +00:00
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
2019-12-19 22:41:21 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2020-10-22 17:52:05 +00:00
|
|
|
import org.thoughtcrime.securesms.groups.SelectionLimits;
|
2020-10-15 19:49:09 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.RefreshAttributesJob;
|
2019-12-19 22:41:21 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.RemoteConfigRefreshJob;
|
|
|
|
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
2020-01-24 20:29:03 +00:00
|
|
|
import java.util.HashSet;
|
2019-12-19 22:41:21 +00:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
2020-11-16 16:27:58 +00:00
|
|
|
import java.util.Objects;
|
2020-01-24 20:29:03 +00:00
|
|
|
import java.util.Set;
|
2019-12-19 22:41:21 +00:00
|
|
|
import java.util.TreeMap;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2019-09-06 14:09:34 +00:00
|
|
|
/**
|
2019-12-19 22:41:21 +00:00
|
|
|
* A location for flags that can be set locally and remotely. These flags can guard features that
|
|
|
|
* are not yet ready to be activated.
|
|
|
|
*
|
|
|
|
* When creating a new flag:
|
2020-02-21 18:52:27 +00:00
|
|
|
* - Create a new string constant. This should almost certainly be prefixed with "android."
|
2020-04-27 21:17:34 +00:00
|
|
|
* - Add a method to retrieve the value using {@link #getBoolean(String, boolean)}. You can also add
|
2019-12-19 22:41:21 +00:00
|
|
|
* other checks here, like requiring other flags.
|
2020-02-06 02:53:04 +00:00
|
|
|
* - If you want to be able to change a flag remotely, place it in {@link #REMOTE_CAPABLE}.
|
|
|
|
* - If you would like to force a value for testing, place an entry in {@link #FORCED_VALUES}.
|
|
|
|
* Do not commit changes to this map!
|
2020-01-24 20:29:03 +00:00
|
|
|
*
|
|
|
|
* Other interesting things you can do:
|
|
|
|
* - Make a flag {@link #HOT_SWAPPABLE}
|
2020-04-27 21:17:34 +00:00
|
|
|
* - Make a flag {@link #STICKY} -- booleans only!
|
2020-02-21 18:52:27 +00:00
|
|
|
* - Register a listener for flag changes in {@link #FLAG_CHANGE_LISTENERS}
|
2019-09-06 14:09:34 +00:00
|
|
|
*/
|
2019-12-19 22:41:21 +00:00
|
|
|
public final class FeatureFlags {
|
|
|
|
|
|
|
|
private static final String TAG = Log.tag(FeatureFlags.class);
|
|
|
|
|
2020-07-27 19:25:01 +00:00
|
|
|
private static final long FETCH_INTERVAL = TimeUnit.HOURS.toMillis(2);
|
2019-12-19 22:41:21 +00:00
|
|
|
|
2020-09-03 21:35:17 +00:00
|
|
|
private static final String USERNAMES = "android.usernames";
|
|
|
|
private static final String GROUPS_V2_JOIN_VERSION = "android.groupsv2.joinVersion";
|
|
|
|
private static final String GROUPS_V2_LINKS_VERSION = "android.groupsv2.manageGroupLinksVersion";
|
2020-10-22 17:52:05 +00:00
|
|
|
private static final String GROUPS_V2_RECOMMENDED_LIMIT = "global.groupsv2.maxGroupSize";
|
|
|
|
private static final String GROUPS_V2_HARD_LIMIT = "global.groupsv2.groupSizeHardLimit";
|
2020-09-03 21:35:17 +00:00
|
|
|
private static final String INTERNAL_USER = "android.internalUser";
|
|
|
|
private static final String VERIFY_V2 = "android.verifyV2";
|
|
|
|
private static final String PHONE_NUMBER_PRIVACY_VERSION = "android.phoneNumberPrivacyVersion";
|
2020-09-08 22:03:56 +00:00
|
|
|
private static final String CLIENT_EXPIRATION = "android.clientExpiration";
|
2020-09-18 21:32:56 +00:00
|
|
|
public static final String RESEARCH_MEGAPHONE_1 = "research.megaphone.1";
|
2020-11-18 15:33:46 +00:00
|
|
|
public static final String DONATE_MEGAPHONE = "android.donate";
|
2020-10-22 19:22:21 +00:00
|
|
|
private static final String VIEWED_RECEIPTS = "android.viewed.receipts";
|
2020-10-22 20:01:52 +00:00
|
|
|
private static final String MAX_ENVELOPE_SIZE = "android.maxEnvelopeSize";
|
2020-10-15 19:49:09 +00:00
|
|
|
private static final String GV1_AUTO_MIGRATE_VERSION = "android.groupsv2.autoMigrateVersion";
|
|
|
|
private static final String GV1_MANUAL_MIGRATE_VERSION = "android.groupsv2.manualMigrateVersion";
|
2020-11-12 14:52:21 +00:00
|
|
|
private static final String GV1_FORCED_MIGRATE_VERSION = "android.groupsv2.forcedMigrateVersion";
|
2020-11-11 20:11:03 +00:00
|
|
|
private static final String GROUP_CALLING_VERSION = "android.groupsv2.callingVersion";
|
2019-12-19 22:41:21 +00:00
|
|
|
|
|
|
|
/**
|
2020-02-06 02:53:04 +00:00
|
|
|
* We will only store remote values for flags in this set. If you want a flag to be controllable
|
|
|
|
* remotely, place it in here.
|
2019-12-19 22:41:21 +00:00
|
|
|
*/
|
2020-11-17 13:58:14 +00:00
|
|
|
private static final Set<String> REMOTE_CAPABLE = SetUtil.newHashSet(
|
2020-10-22 17:52:05 +00:00
|
|
|
GROUPS_V2_RECOMMENDED_LIMIT,
|
|
|
|
GROUPS_V2_HARD_LIMIT,
|
2020-08-26 21:02:42 +00:00
|
|
|
GROUPS_V2_JOIN_VERSION,
|
|
|
|
GROUPS_V2_LINKS_VERSION,
|
2020-07-27 13:58:58 +00:00
|
|
|
INTERNAL_USER,
|
2020-08-27 13:49:45 +00:00
|
|
|
USERNAMES,
|
2020-09-08 22:03:56 +00:00
|
|
|
VERIFY_V2,
|
2020-09-18 21:32:56 +00:00
|
|
|
CLIENT_EXPIRATION,
|
2020-10-12 15:45:03 +00:00
|
|
|
RESEARCH_MEGAPHONE_1,
|
2020-11-18 15:33:46 +00:00
|
|
|
DONATE_MEGAPHONE,
|
2020-10-22 20:01:52 +00:00
|
|
|
VIEWED_RECEIPTS,
|
2020-10-15 19:49:09 +00:00
|
|
|
MAX_ENVELOPE_SIZE,
|
|
|
|
GV1_AUTO_MIGRATE_VERSION,
|
2020-11-11 20:11:03 +00:00
|
|
|
GV1_MANUAL_MIGRATE_VERSION,
|
2020-11-20 16:49:18 +00:00
|
|
|
GV1_FORCED_MIGRATE_VERSION,
|
2020-11-11 20:11:03 +00:00
|
|
|
GROUP_CALLING_VERSION
|
2020-02-06 02:53:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Values in this map will take precedence over any value. This should only be used for local
|
|
|
|
* development. Given that you specify a default when retrieving a value, and that we only store
|
|
|
|
* remote values for things in {@link #REMOTE_CAPABLE}, there should be no need to ever *commit*
|
|
|
|
* an addition to this map.
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
|
2020-04-27 21:17:34 +00:00
|
|
|
private static final Map<String, Object> FORCED_VALUES = new HashMap<String, Object>() {{
|
2019-12-19 22:41:21 +00:00
|
|
|
}};
|
|
|
|
|
2020-01-24 20:29:03 +00:00
|
|
|
/**
|
|
|
|
* By default, flags are only updated once at app start. This is to ensure that values don't
|
|
|
|
* change within an app session, simplifying logic. However, given that this can delay how often
|
|
|
|
* a flag is updated, you can put a flag in here to mark it as 'hot swappable'. Flags in this set
|
|
|
|
* will be updated arbitrarily at runtime. This will make values more responsive, but also places
|
|
|
|
* more burden on the reader to ensure that the app experience remains consistent.
|
|
|
|
*/
|
2020-11-17 13:58:14 +00:00
|
|
|
private static final Set<String> HOT_SWAPPABLE = SetUtil.newHashSet(
|
2020-08-25 19:35:06 +00:00
|
|
|
GROUPS_V2_JOIN_VERSION,
|
2020-08-24 13:43:41 +00:00
|
|
|
VERIFY_V2,
|
2020-11-06 20:58:10 +00:00
|
|
|
CLIENT_EXPIRATION,
|
|
|
|
MAX_ENVELOPE_SIZE
|
2020-01-24 23:38:48 +00:00
|
|
|
);
|
2020-01-24 20:29:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flags in this set will stay true forever once they receive a true value from a remote config.
|
|
|
|
*/
|
2020-11-17 13:58:14 +00:00
|
|
|
private static final Set<String> STICKY = SetUtil.newHashSet(
|
2020-08-07 19:38:31 +00:00
|
|
|
VERIFY_V2
|
2020-08-06 16:29:51 +00:00
|
|
|
);
|
2020-01-24 20:29:03 +00:00
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
/**
|
|
|
|
* Listeners that are called when the value in {@link #REMOTE_VALUES} changes. That means that
|
|
|
|
* hot-swappable flags will have this invoked as soon as we know about that change, but otherwise
|
|
|
|
* these will only run during initialization.
|
|
|
|
*
|
|
|
|
* These can be called on any thread, including the main thread, so be careful!
|
|
|
|
*
|
|
|
|
* Also note that this doesn't play well with {@link #FORCED_VALUES} -- changes there will not
|
|
|
|
* trigger changes in this map, so you'll have to do some manually hacking to get yourself in the
|
|
|
|
* desired test state.
|
|
|
|
*/
|
|
|
|
private static final Map<String, OnFlagChange> FLAG_CHANGE_LISTENERS = new HashMap<String, OnFlagChange>() {{
|
2020-10-15 19:49:09 +00:00
|
|
|
put(GV1_AUTO_MIGRATE_VERSION, change -> ApplicationDependencies.getJobManager().add(new RefreshAttributesJob()));
|
2020-02-21 18:52:27 +00:00
|
|
|
}};
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
private static final Map<String, Object> REMOTE_VALUES = new TreeMap<>();
|
2019-12-19 22:41:21 +00:00
|
|
|
|
|
|
|
private FeatureFlags() {}
|
|
|
|
|
2020-01-24 20:29:03 +00:00
|
|
|
public static synchronized void init() {
|
2020-04-27 21:17:34 +00:00
|
|
|
Map<String, Object> current = parseStoredConfig(SignalStore.remoteConfigValues().getCurrentConfig());
|
|
|
|
Map<String, Object> pending = parseStoredConfig(SignalStore.remoteConfigValues().getPendingConfig());
|
|
|
|
Map<String, Change> changes = computeChanges(current, pending);
|
2020-02-21 18:52:27 +00:00
|
|
|
|
|
|
|
SignalStore.remoteConfigValues().setCurrentConfig(mapToJson(pending));
|
|
|
|
REMOTE_VALUES.putAll(pending);
|
|
|
|
triggerFlagChangeListeners(changes);
|
|
|
|
|
2020-01-24 20:29:03 +00:00
|
|
|
Log.i(TAG, "init() " + REMOTE_VALUES.toString());
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
public static synchronized void refreshIfNecessary() {
|
|
|
|
long timeSinceLastFetch = System.currentTimeMillis() - SignalStore.remoteConfigValues().getLastFetchTime();
|
2019-12-19 22:41:21 +00:00
|
|
|
|
2020-09-29 14:10:25 +00:00
|
|
|
if (timeSinceLastFetch < 0 || timeSinceLastFetch > FETCH_INTERVAL) {
|
2020-01-24 20:29:03 +00:00
|
|
|
Log.i(TAG, "Scheduling remote config refresh.");
|
|
|
|
ApplicationDependencies.getJobManager().add(new RemoteConfigRefreshJob());
|
|
|
|
} else {
|
|
|
|
Log.i(TAG, "Skipping remote config refresh. Refreshed " + timeSinceLastFetch + " ms ago.");
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
public static synchronized void update(@NonNull Map<String, Object> config) {
|
|
|
|
Map<String, Object> memory = REMOTE_VALUES;
|
|
|
|
Map<String, Object> disk = parseStoredConfig(SignalStore.remoteConfigValues().getPendingConfig());
|
|
|
|
UpdateResult result = updateInternal(config, memory, disk, REMOTE_CAPABLE, HOT_SWAPPABLE, STICKY);
|
2020-01-24 20:29:03 +00:00
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
SignalStore.remoteConfigValues().setPendingConfig(mapToJson(result.getDisk()));
|
2020-01-24 20:29:03 +00:00
|
|
|
REMOTE_VALUES.clear();
|
|
|
|
REMOTE_VALUES.putAll(result.getMemory());
|
2020-02-28 22:33:19 +00:00
|
|
|
triggerFlagChangeListeners(result.getMemoryChanges());
|
2020-02-21 18:52:27 +00:00
|
|
|
|
|
|
|
SignalStore.remoteConfigValues().setLastFetchTime(System.currentTimeMillis());
|
2020-01-24 20:29:03 +00:00
|
|
|
|
|
|
|
Log.i(TAG, "[Memory] Before: " + memory.toString());
|
|
|
|
Log.i(TAG, "[Memory] After : " + result.getMemory().toString());
|
|
|
|
Log.i(TAG, "[Disk] Before: " + disk.toString());
|
|
|
|
Log.i(TAG, "[Disk] After : " + result.getDisk().toString());
|
|
|
|
}
|
|
|
|
|
2020-08-06 14:50:20 +00:00
|
|
|
/** Creating usernames, sending messages by username. */
|
2020-01-24 20:29:03 +00:00
|
|
|
public static synchronized boolean usernames() {
|
2020-07-21 18:26:40 +00:00
|
|
|
return getBoolean(USERNAMES, false);
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
2019-12-03 17:31:23 +00:00
|
|
|
|
2020-08-26 18:59:34 +00:00
|
|
|
/** Allow creation and managing of group links. */
|
|
|
|
public static boolean groupsV2manageGroupLinks() {
|
2020-09-22 20:31:07 +00:00
|
|
|
return getVersionFlag(GROUPS_V2_LINKS_VERSION) == VersionFlag.ON;
|
2020-08-06 16:29:51 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 15:20:07 +00:00
|
|
|
/**
|
|
|
|
* Maximum number of members allowed in a group.
|
|
|
|
*/
|
2020-10-22 17:52:05 +00:00
|
|
|
public static SelectionLimits groupLimits() {
|
|
|
|
return new SelectionLimits(getInteger(GROUPS_V2_RECOMMENDED_LIMIT, 151),
|
2020-11-12 14:52:21 +00:00
|
|
|
getInteger(GROUPS_V2_HARD_LIMIT, 1001));
|
2020-05-29 15:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 19:35:06 +00:00
|
|
|
/**
|
|
|
|
* Ability of local client to join a GV2 group.
|
|
|
|
* <p>
|
|
|
|
* You must still check GV2 capabilities to respect linked devices.
|
|
|
|
*/
|
|
|
|
public static GroupJoinStatus clientLocalGroupJoinStatus() {
|
2020-08-26 18:59:34 +00:00
|
|
|
switch (getVersionFlag(GROUPS_V2_JOIN_VERSION)) {
|
|
|
|
case ON_IN_FUTURE_VERSION: return GroupJoinStatus.UPDATE_TO_JOIN;
|
|
|
|
case ON : return GroupJoinStatus.LOCAL_CAN_JOIN;
|
|
|
|
case OFF :
|
|
|
|
default : return GroupJoinStatus.COMING_SOON;
|
|
|
|
}
|
2020-08-25 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public enum GroupJoinStatus {
|
|
|
|
/** No version of the client that can join V2 groups by link is in production. */
|
|
|
|
COMING_SOON,
|
|
|
|
|
|
|
|
/** A newer version of the client is in production that will allow joining via GV2 group links. */
|
|
|
|
UPDATE_TO_JOIN,
|
|
|
|
|
|
|
|
/** This version of the client allows joining via GV2 group links. */
|
|
|
|
LOCAL_CAN_JOIN
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:52:29 +00:00
|
|
|
/** Internal testing extensions. */
|
|
|
|
public static boolean internalUser() {
|
|
|
|
return getBoolean(INTERNAL_USER, false);
|
2020-05-29 18:19:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-07 19:38:31 +00:00
|
|
|
/** Whether or not to use the UUID in verification codes. */
|
|
|
|
public static boolean verifyV2() {
|
|
|
|
return getBoolean(VERIFY_V2, false);
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:03:56 +00:00
|
|
|
/** The raw client expiration JSON string. */
|
|
|
|
public static String clientExpiration() {
|
|
|
|
return getString(CLIENT_EXPIRATION, null);
|
|
|
|
}
|
|
|
|
|
2020-09-18 21:32:56 +00:00
|
|
|
/** The raw research megaphone CSV string */
|
|
|
|
public static String researchMegaphone() {
|
|
|
|
return getString(RESEARCH_MEGAPHONE_1, "");
|
|
|
|
}
|
|
|
|
|
2020-11-18 15:33:46 +00:00
|
|
|
/** The raw donate megaphone CSV string */
|
|
|
|
public static String donateMegaphone() {
|
|
|
|
return getString(DONATE_MEGAPHONE, "");
|
|
|
|
}
|
|
|
|
|
2020-09-03 21:35:17 +00:00
|
|
|
/**
|
|
|
|
* Whether the user can choose phone number privacy settings, and;
|
|
|
|
* Whether to fetch and store the secondary certificate
|
|
|
|
*/
|
|
|
|
public static boolean phoneNumberPrivacy() {
|
|
|
|
return getVersionFlag(PHONE_NUMBER_PRIVACY_VERSION) == VersionFlag.ON;
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:01:52 +00:00
|
|
|
/** Whether the user should display the content revealed dot in voice notes. */
|
2020-10-22 19:22:21 +00:00
|
|
|
public static boolean viewedReceipts() {
|
|
|
|
return getBoolean(VIEWED_RECEIPTS, false);
|
|
|
|
}
|
|
|
|
|
2020-10-22 20:01:52 +00:00
|
|
|
/** The max size envelope that is allowed to be sent. */
|
|
|
|
public static int maxEnvelopeSize() {
|
|
|
|
return getInteger(MAX_ENVELOPE_SIZE, 0);
|
|
|
|
}
|
|
|
|
|
2020-10-15 19:49:09 +00:00
|
|
|
/** Whether or not auto-migration from GV1->GV2 is enabled. */
|
|
|
|
public static boolean groupsV1AutoMigration() {
|
|
|
|
return getVersionFlag(GV1_AUTO_MIGRATE_VERSION) == VersionFlag.ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Whether or not manual migration from GV1->GV2 is enabled. */
|
|
|
|
public static boolean groupsV1ManualMigration() {
|
|
|
|
return groupsV1AutoMigration() && getVersionFlag(GV1_MANUAL_MIGRATE_VERSION) == VersionFlag.ON;
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:52:21 +00:00
|
|
|
/** Whether or not group calling is enabled. */
|
2020-11-11 20:11:03 +00:00
|
|
|
public static boolean groupCalling() {
|
|
|
|
return getVersionFlag(GROUP_CALLING_VERSION) == VersionFlag.ON;
|
|
|
|
}
|
|
|
|
|
2020-11-12 14:52:21 +00:00
|
|
|
/** Whether or not forced migration from GV1->GV2 is enabled. */
|
|
|
|
public static boolean groupsV1ForcedMigration() {
|
|
|
|
return groupsV1AutoMigration() && getVersionFlag(GV1_FORCED_MIGRATE_VERSION) == VersionFlag.ON;
|
|
|
|
}
|
|
|
|
|
2019-12-19 22:41:21 +00:00
|
|
|
/** Only for rendering debug info. */
|
2020-04-27 21:17:34 +00:00
|
|
|
public static synchronized @NonNull Map<String, Object> getMemoryValues() {
|
2019-12-19 22:41:21 +00:00
|
|
|
return new TreeMap<>(REMOTE_VALUES);
|
|
|
|
}
|
|
|
|
|
2020-02-06 02:53:04 +00:00
|
|
|
/** Only for rendering debug info. */
|
2020-04-27 21:17:34 +00:00
|
|
|
public static synchronized @NonNull Map<String, Object> getDiskValues() {
|
2020-02-21 18:52:27 +00:00
|
|
|
return new TreeMap<>(parseStoredConfig(SignalStore.remoteConfigValues().getCurrentConfig()));
|
2020-02-06 02:53:04 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 22:41:21 +00:00
|
|
|
/** Only for rendering debug info. */
|
2020-04-27 21:17:34 +00:00
|
|
|
public static synchronized @NonNull Map<String, Object> getPendingDiskValues() {
|
|
|
|
return new TreeMap<>(parseStoredConfig(SignalStore.remoteConfigValues().getPendingConfig()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Only for rendering debug info. */
|
|
|
|
public static synchronized @NonNull Map<String, Object> getForcedValues() {
|
2019-12-19 22:41:21 +00:00
|
|
|
return new TreeMap<>(FORCED_VALUES);
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:29:03 +00:00
|
|
|
@VisibleForTesting
|
2020-04-27 21:17:34 +00:00
|
|
|
static @NonNull UpdateResult updateInternal(@NonNull Map<String, Object> remote,
|
|
|
|
@NonNull Map<String, Object> localMemory,
|
|
|
|
@NonNull Map<String, Object> localDisk,
|
|
|
|
@NonNull Set<String> remoteCapable,
|
|
|
|
@NonNull Set<String> hotSwap,
|
|
|
|
@NonNull Set<String> sticky)
|
2020-01-24 20:29:03 +00:00
|
|
|
{
|
2020-04-27 21:17:34 +00:00
|
|
|
Map<String, Object> newMemory = new TreeMap<>(localMemory);
|
|
|
|
Map<String, Object> newDisk = new TreeMap<>(localDisk);
|
2020-01-24 20:29:03 +00:00
|
|
|
|
|
|
|
Set<String> allKeys = new HashSet<>();
|
|
|
|
allKeys.addAll(remote.keySet());
|
|
|
|
allKeys.addAll(localDisk.keySet());
|
|
|
|
allKeys.addAll(localMemory.keySet());
|
|
|
|
|
|
|
|
Stream.of(allKeys)
|
2020-02-06 02:53:04 +00:00
|
|
|
.filter(remoteCapable::contains)
|
2020-01-24 20:29:03 +00:00
|
|
|
.forEach(key -> {
|
2020-04-27 21:17:34 +00:00
|
|
|
Object remoteValue = remote.get(key);
|
|
|
|
Object diskValue = localDisk.get(key);
|
|
|
|
Object newValue = remoteValue;
|
|
|
|
|
|
|
|
if (newValue != null && diskValue != null && newValue.getClass() != diskValue.getClass()) {
|
|
|
|
Log.w(TAG, "Type mismatch! key: " + key);
|
|
|
|
|
|
|
|
newDisk.remove(key);
|
|
|
|
|
|
|
|
if (hotSwap.contains(key)) {
|
|
|
|
newMemory.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-01-24 20:29:03 +00:00
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
if (sticky.contains(key) && (newValue instanceof Boolean || diskValue instanceof Boolean)) {
|
2020-01-24 20:29:03 +00:00
|
|
|
newValue = diskValue == Boolean.TRUE ? Boolean.TRUE : newValue;
|
2020-04-27 21:17:34 +00:00
|
|
|
} else if (sticky.contains(key)) {
|
|
|
|
Log.w(TAG, "Tried to make a non-boolean sticky! Ignoring. (key: " + key + ")");
|
2020-01-24 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (newValue != null) {
|
|
|
|
newDisk.put(key, newValue);
|
|
|
|
} else {
|
|
|
|
newDisk.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hotSwap.contains(key)) {
|
|
|
|
if (newValue != null) {
|
|
|
|
newMemory.put(key, newValue);
|
|
|
|
} else {
|
|
|
|
newMemory.remove(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-28 22:33:19 +00:00
|
|
|
Stream.of(allKeys)
|
|
|
|
.filterNot(remoteCapable::contains)
|
|
|
|
.filterNot(key -> sticky.contains(key) && localDisk.get(key) == Boolean.TRUE)
|
|
|
|
.forEach(key -> {
|
|
|
|
newDisk.remove(key);
|
|
|
|
|
|
|
|
if (hotSwap.contains(key)) {
|
|
|
|
newMemory.remove(key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
return new UpdateResult(newMemory, newDisk, computeChanges(localMemory, newMemory));
|
2020-01-24 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
@VisibleForTesting
|
2020-04-27 21:17:34 +00:00
|
|
|
static @NonNull Map<String, Change> computeChanges(@NonNull Map<String, Object> oldMap, @NonNull Map<String, Object> newMap) {
|
2020-02-21 18:52:27 +00:00
|
|
|
Map<String, Change> changes = new HashMap<>();
|
|
|
|
Set<String> allKeys = new HashSet<>();
|
|
|
|
|
|
|
|
allKeys.addAll(oldMap.keySet());
|
|
|
|
allKeys.addAll(newMap.keySet());
|
|
|
|
|
|
|
|
for (String key : allKeys) {
|
2020-04-27 21:17:34 +00:00
|
|
|
Object oldValue = oldMap.get(key);
|
|
|
|
Object newValue = newMap.get(key);
|
2020-02-21 18:52:27 +00:00
|
|
|
|
|
|
|
if (oldValue == null && newValue == null) {
|
|
|
|
throw new AssertionError("Should not be possible.");
|
|
|
|
} else if (oldValue != null && newValue == null) {
|
|
|
|
changes.put(key, Change.REMOVED);
|
2020-04-27 21:17:34 +00:00
|
|
|
} else if (newValue != oldValue && newValue instanceof Boolean) {
|
|
|
|
changes.put(key, (boolean) newValue ? Change.ENABLED : Change.DISABLED);
|
2020-11-16 16:27:58 +00:00
|
|
|
} else if (!Objects.equals(oldValue, newValue)) {
|
2020-04-27 21:17:34 +00:00
|
|
|
changes.put(key, Change.CHANGED);
|
2020-02-21 18:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changes;
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 18:59:34 +00:00
|
|
|
private static @NonNull VersionFlag getVersionFlag(@NonNull String key) {
|
|
|
|
int versionFromKey = getInteger(key, 0);
|
|
|
|
|
|
|
|
if (versionFromKey == 0) {
|
|
|
|
return VersionFlag.OFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BuildConfig.CANONICAL_VERSION_CODE >= versionFromKey) {
|
|
|
|
return VersionFlag.ON;
|
|
|
|
} else {
|
|
|
|
return VersionFlag.ON_IN_FUTURE_VERSION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private enum VersionFlag {
|
|
|
|
/** The flag is no set */
|
|
|
|
OFF,
|
|
|
|
|
|
|
|
/** The flag is set on for a version higher than the current client version */
|
|
|
|
ON_IN_FUTURE_VERSION,
|
|
|
|
|
|
|
|
/** The flag is set on for this version or earlier */
|
|
|
|
ON
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
private static boolean getBoolean(@NonNull String key, boolean defaultValue) {
|
|
|
|
Boolean forced = (Boolean) FORCED_VALUES.get(key);
|
2019-12-19 22:41:21 +00:00
|
|
|
if (forced != null) {
|
|
|
|
return forced;
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
Object remote = REMOTE_VALUES.get(key);
|
|
|
|
if (remote instanceof Boolean) {
|
|
|
|
return (boolean) remote;
|
|
|
|
} else if (remote != null) {
|
|
|
|
Log.w(TAG, "Expected a boolean for key '" + key + "', but got something else! Falling back to the default.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int getInteger(@NonNull String key, int defaultValue) {
|
|
|
|
Integer forced = (Integer) FORCED_VALUES.get(key);
|
|
|
|
if (forced != null) {
|
|
|
|
return forced;
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:08:20 +00:00
|
|
|
Object remote = REMOTE_VALUES.get(key);
|
|
|
|
if (remote instanceof String) {
|
2020-04-27 21:17:34 +00:00
|
|
|
try {
|
2020-09-02 15:08:20 +00:00
|
|
|
return Integer.parseInt((String) remote);
|
2020-04-27 21:17:34 +00:00
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
Log.w(TAG, "Expected an int for key '" + key + "', but got something else! Falling back to the default.");
|
|
|
|
}
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2020-09-08 22:03:56 +00:00
|
|
|
private static String getString(@NonNull String key, String defaultValue) {
|
|
|
|
String forced = (String) FORCED_VALUES.get(key);
|
|
|
|
if (forced != null) {
|
|
|
|
return forced;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object remote = REMOTE_VALUES.get(key);
|
|
|
|
if (remote instanceof String) {
|
|
|
|
return (String) remote;
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
private static Map<String, Object> parseStoredConfig(String stored) {
|
|
|
|
Map<String, Object> parsed = new HashMap<>();
|
2019-12-19 22:41:21 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(stored)) {
|
|
|
|
Log.i(TAG, "No remote config stored. Skipping.");
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject root = new JSONObject(stored);
|
|
|
|
Iterator<String> iter = root.keys();
|
|
|
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
String key = iter.next();
|
2020-04-27 21:17:34 +00:00
|
|
|
parsed.put(key, root.get(key));
|
2019-12-19 22:41:21 +00:00
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new AssertionError("Failed to parse! Cleared storage.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
private static @NonNull String mapToJson(@NonNull Map<String, Object> map) {
|
2020-01-24 20:29:03 +00:00
|
|
|
try {
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
for (Map.Entry<String, Object> entry : map.entrySet()) {
|
|
|
|
json.put(entry.getKey(), entry.getValue());
|
2020-01-24 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
return json.toString();
|
2020-01-24 20:29:03 +00:00
|
|
|
} catch (JSONException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-21 18:52:27 +00:00
|
|
|
private static void triggerFlagChangeListeners(Map<String, Change> changes) {
|
|
|
|
for (Map.Entry<String, Change> change : changes.entrySet()) {
|
|
|
|
OnFlagChange listener = FLAG_CHANGE_LISTENERS.get(change.getKey());
|
|
|
|
|
|
|
|
if (listener != null) {
|
|
|
|
Log.i(TAG, "Triggering change listener for: " + change.getKey());
|
|
|
|
listener.onFlagChange(change.getValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 20:29:03 +00:00
|
|
|
@VisibleForTesting
|
|
|
|
static final class UpdateResult {
|
2020-04-27 21:17:34 +00:00
|
|
|
private final Map<String, Object> memory;
|
|
|
|
private final Map<String, Object> disk;
|
2020-09-08 22:03:56 +00:00
|
|
|
private final Map<String, Change> memoryChanges;
|
2020-01-24 20:29:03 +00:00
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
UpdateResult(@NonNull Map<String, Object> memory, @NonNull Map<String, Object> disk, @NonNull Map<String, Change> memoryChanges) {
|
2020-02-28 22:33:19 +00:00
|
|
|
this.memory = memory;
|
|
|
|
this.disk = disk;
|
|
|
|
this.memoryChanges = memoryChanges;
|
2020-01-24 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
public @NonNull Map<String, Object> getMemory() {
|
2020-01-24 20:29:03 +00:00
|
|
|
return memory;
|
|
|
|
}
|
|
|
|
|
2020-04-27 21:17:34 +00:00
|
|
|
public @NonNull Map<String, Object> getDisk() {
|
2020-01-24 20:29:03 +00:00
|
|
|
return disk;
|
|
|
|
}
|
2020-02-21 18:52:27 +00:00
|
|
|
|
2020-02-28 22:33:19 +00:00
|
|
|
public @NonNull Map<String, Change> getMemoryChanges() {
|
|
|
|
return memoryChanges;
|
2020-02-21 18:52:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@VisibleForTesting
|
|
|
|
interface OnFlagChange {
|
|
|
|
void onFlagChange(@NonNull Change change);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Change {
|
2020-04-27 21:17:34 +00:00
|
|
|
ENABLED, DISABLED, CHANGED, REMOVED
|
2020-01-24 20:29:03 +00:00
|
|
|
}
|
2019-09-26 14:12:51 +00:00
|
|
|
}
|