Merge changes Icde45251,Ic5356aa0 into sc-v2-dev
* changes: Cache flag values when they are read. Restart SystemUI when a flag changes.
This commit is contained in:
@@ -28,6 +28,7 @@ import com.android.systemui.dagger.SysUISingleton;
|
|||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -54,6 +55,8 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {
|
|||||||
private static final String FLAGS_PERMISSION = "com.android.systemui.permission.FLAGS";
|
private static final String FLAGS_PERMISSION = "com.android.systemui.permission.FLAGS";
|
||||||
private final SystemPropertiesHelper mSystemPropertiesHelper;
|
private final SystemPropertiesHelper mSystemPropertiesHelper;
|
||||||
|
|
||||||
|
private final Map<Integer, Boolean> mBooleanFlagCache = new HashMap<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper, Context context) {
|
public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper, Context context) {
|
||||||
mSystemPropertiesHelper = systemPropertiesHelper;
|
mSystemPropertiesHelper = systemPropertiesHelper;
|
||||||
@@ -64,31 +67,48 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {
|
|||||||
|
|
||||||
/** Return a {@link BooleanFlag}'s value. */
|
/** Return a {@link BooleanFlag}'s value. */
|
||||||
public boolean isEnabled(int id, boolean defaultValue) {
|
public boolean isEnabled(int id, boolean defaultValue) {
|
||||||
|
if (!mBooleanFlagCache.containsKey(id)) {
|
||||||
|
Boolean result = isEnabledInternal(id);
|
||||||
|
mBooleanFlagCache.put(id, result == null ? defaultValue : result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return mBooleanFlagCache.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the stored value or null if not set. */
|
||||||
|
private Boolean isEnabledInternal(int id) {
|
||||||
String data = mSystemPropertiesHelper.get(keyToSysPropKey(id));
|
String data = mSystemPropertiesHelper.get(keyToSysPropKey(id));
|
||||||
if (data.isEmpty()) {
|
if (data.isEmpty()) {
|
||||||
return defaultValue;
|
return null;
|
||||||
}
|
}
|
||||||
JSONObject json;
|
JSONObject json;
|
||||||
try {
|
try {
|
||||||
json = new JSONObject(data);
|
json = new JSONObject(data);
|
||||||
if (!assertType(json, TYPE_BOOLEAN)) {
|
if (!assertType(json, TYPE_BOOLEAN)) {
|
||||||
return defaultValue;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.getBoolean(FIELD_VALUE);
|
return json.getBoolean(FIELD_VALUE);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
eraseFlag(id);
|
eraseInternal(id); // Don't restart SystemUI in this case.
|
||||||
return defaultValue;
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set whether a given {@link BooleanFlag} is enabled or not. */
|
/** Set whether a given {@link BooleanFlag} is enabled or not. */
|
||||||
public void setEnabled(int id, boolean value) {
|
public void setEnabled(int id, boolean value) {
|
||||||
|
Boolean currentValue = isEnabledInternal(id);
|
||||||
|
if (currentValue != null && currentValue == value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
try {
|
try {
|
||||||
json.put(FIELD_TYPE, TYPE_BOOLEAN);
|
json.put(FIELD_TYPE, TYPE_BOOLEAN);
|
||||||
json.put(FIELD_VALUE, value);
|
json.put(FIELD_VALUE, value);
|
||||||
mSystemPropertiesHelper.set(keyToSysPropKey(id), json.toString());
|
mSystemPropertiesHelper.set(keyToSysPropKey(id), json.toString());
|
||||||
Log.i(TAG, "Set id " + id + " to " + value);
|
Log.i(TAG, "Set id " + id + " to " + value);
|
||||||
|
restartSystemUI();
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// no-op
|
// no-op
|
||||||
}
|
}
|
||||||
@@ -96,6 +116,12 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {
|
|||||||
|
|
||||||
/** Erase a flag's overridden value if there is one. */
|
/** Erase a flag's overridden value if there is one. */
|
||||||
public void eraseFlag(int id) {
|
public void eraseFlag(int id) {
|
||||||
|
eraseInternal(id);
|
||||||
|
restartSystemUI();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Works just like {@link #eraseFlag(int)} except that it doesn't restart SystemUI. */
|
||||||
|
private void eraseInternal(int id) {
|
||||||
// We can't actually "erase" things from sysprops, but we can set them to empty!
|
// We can't actually "erase" things from sysprops, but we can set them to empty!
|
||||||
mSystemPropertiesHelper.set(keyToSysPropKey(id), "");
|
mSystemPropertiesHelper.set(keyToSysPropKey(id), "");
|
||||||
Log.i(TAG, "Erase id " + id);
|
Log.i(TAG, "Erase id " + id);
|
||||||
@@ -105,6 +131,12 @@ public class FeatureFlagManager implements FlagReader, FlagWriter {
|
|||||||
|
|
||||||
public void removeListener(Listener run) {}
|
public void removeListener(Listener run) {}
|
||||||
|
|
||||||
|
private void restartSystemUI() {
|
||||||
|
Log.i(TAG, "Restarting SystemUI");
|
||||||
|
// SysUI starts back when up exited. Is there a better way to do this?
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
private static String keyToSysPropKey(int key) {
|
private static String keyToSysPropKey(int key) {
|
||||||
return SYSPROP_PREFIX + key;
|
return SYSPROP_PREFIX + key;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user