Merge "Add ability to read flag values to Flag Library." into sc-v2-dev
This commit is contained in:
@@ -179,8 +179,7 @@ android_library {
|
|||||||
"src/**/*.kt",
|
"src/**/*.kt",
|
||||||
"src/**/*.java",
|
"src/**/*.java",
|
||||||
"src/**/I*.aidl",
|
"src/**/I*.aidl",
|
||||||
"src-release/**/*.kt",
|
":ReleaseJavaFiles",
|
||||||
"src-release/**/*.java",
|
|
||||||
],
|
],
|
||||||
static_libs: [
|
static_libs: [
|
||||||
"SystemUIAnimationLib",
|
"SystemUIAnimationLib",
|
||||||
|
|||||||
@@ -18,16 +18,22 @@ package com.android.systemui.flags
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.provider.Settings
|
||||||
import androidx.concurrent.futures.CallbackToFutureAdapter
|
import androidx.concurrent.futures.CallbackToFutureAdapter
|
||||||
import com.google.common.util.concurrent.ListenableFuture
|
import com.google.common.util.concurrent.ListenableFuture
|
||||||
|
import org.json.JSONException
|
||||||
|
import org.json.JSONObject
|
||||||
|
|
||||||
class FlagManager constructor(val context: Context) {
|
class FlagManager constructor(val context: Context) : FlagReader {
|
||||||
companion object {
|
companion object {
|
||||||
const val RECEIVING_PACKAGE = "com.android.systemui"
|
const val RECEIVING_PACKAGE = "com.android.systemui"
|
||||||
const val ACTION_SET_FLAG = "com.android.systemui.action.SET_FLAG"
|
const val ACTION_SET_FLAG = "com.android.systemui.action.SET_FLAG"
|
||||||
const val FLAGS_PERMISSION = "com.android.systemui.permission.FLAGS"
|
const val FLAGS_PERMISSION = "com.android.systemui.permission.FLAGS"
|
||||||
const val FIELD_ID = "id"
|
const val FIELD_ID = "id"
|
||||||
const val FIELD_VALUE = "value"
|
const val FIELD_VALUE = "value"
|
||||||
|
const val FIELD_TYPE = "type"
|
||||||
|
const val TYPE_BOOLEAN = "boolean"
|
||||||
|
private const val SETTINGS_PREFIX = "systemui/flags"
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFlagsFuture(): ListenableFuture<Collection<Flag<*>>> {
|
fun getFlagsFuture(): ListenableFuture<Collection<Flag<*>>> {
|
||||||
@@ -54,6 +60,28 @@ class FlagManager constructor(val context: Context) {
|
|||||||
context.sendBroadcast(intent)
|
context.sendBroadcast(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun isEnabled(id: Int, def: Boolean): Boolean {
|
||||||
|
return isEnabled(id) ?: def
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns the stored value or null if not set. */
|
||||||
|
fun isEnabled(id: Int): Boolean? {
|
||||||
|
val data: String = Settings.Secure.getString(
|
||||||
|
context.contentResolver, keyToSettingsPrefix(id))
|
||||||
|
if (data.isEmpty()) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
val json: JSONObject
|
||||||
|
try {
|
||||||
|
json = JSONObject(data)
|
||||||
|
return if (!assertType(json, TYPE_BOOLEAN)) {
|
||||||
|
null
|
||||||
|
} else json.getBoolean(FIELD_VALUE)
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
throw InvalidFlagStorageException()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun createIntent(id: Int): Intent {
|
private fun createIntent(id: Int): Intent {
|
||||||
val intent = Intent(ACTION_SET_FLAG)
|
val intent = Intent(ACTION_SET_FLAG)
|
||||||
intent.setPackage(RECEIVING_PACKAGE)
|
intent.setPackage(RECEIVING_PACKAGE)
|
||||||
@@ -61,4 +89,18 @@ class FlagManager constructor(val context: Context) {
|
|||||||
|
|
||||||
return intent
|
return intent
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
fun keyToSettingsPrefix(key: Int): String? {
|
||||||
|
return SETTINGS_PREFIX + "/" + key
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun assertType(json: JSONObject, type: String): Boolean {
|
||||||
|
return try {
|
||||||
|
json.getString(FIELD_TYPE) == TYPE_BOOLEAN
|
||||||
|
} catch (e: JSONException) {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InvalidFlagStorageException : Exception("Data found but is invalid")
|
||||||
@@ -13,28 +13,26 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
package com.android.systemui.flags
|
||||||
package com.android.systemui.flags;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin for loading flag values
|
* Plugin for loading flag values
|
||||||
*/
|
*/
|
||||||
public interface FlagReader {
|
interface FlagReader {
|
||||||
/** Returns a boolean value for the given flag. */
|
/** Returns a boolean value for the given flag. */
|
||||||
default boolean isEnabled(int id, boolean def) {
|
fun isEnabled(id: Int, def: Boolean): Boolean {
|
||||||
return def;
|
return def
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a listener to be alerted when any flag changes. */
|
/** Add a listener to be alerted when any flag changes. */
|
||||||
default void addListener(Listener listener) {}
|
fun addListener(listener: Listener) {}
|
||||||
|
|
||||||
/** Remove a listener to be alerted when any flag changes. */
|
/** Remove a listener to be alerted when any flag changes. */
|
||||||
default void removeListener(Listener listener) {}
|
fun removeListener(listener: Listener) {}
|
||||||
|
|
||||||
/** A simple listener to be alerted when a flag changes. */
|
/** A simple listener to be alerted when a flag changes. */
|
||||||
interface Listener {
|
interface Listener {
|
||||||
/** */
|
/** */
|
||||||
void onFlagChanged(int id);
|
fun onFlagChanged(id: Int)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -33,6 +33,7 @@ import androidx.annotation.NonNull;
|
|||||||
import com.android.systemui.Dumpable;
|
import com.android.systemui.Dumpable;
|
||||||
import com.android.systemui.dagger.SysUISingleton;
|
import com.android.systemui.dagger.SysUISingleton;
|
||||||
import com.android.systemui.dump.DumpManager;
|
import com.android.systemui.dump.DumpManager;
|
||||||
|
import com.android.systemui.util.settings.SecureSettings;
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
@@ -58,18 +59,16 @@ import javax.inject.Inject;
|
|||||||
public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
||||||
private static final String TAG = "SysUIFlags";
|
private static final String TAG = "SysUIFlags";
|
||||||
|
|
||||||
private static final String SYSPROP_PREFIX = "persist.systemui.flag_";
|
private final FlagManager mFlagManager;
|
||||||
private static final String FIELD_TYPE = "type";
|
private final SecureSettings mSecureSettings;
|
||||||
private static final String TYPE_BOOLEAN = "boolean";
|
|
||||||
private final SystemPropertiesHelper mSystemPropertiesHelper;
|
|
||||||
|
|
||||||
private final Map<Integer, Boolean> mBooleanFlagCache = new HashMap<>();
|
private final Map<Integer, Boolean> mBooleanFlagCache = new HashMap<>();
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper, Context context,
|
public FeatureFlagManager(FlagManager flagManager,
|
||||||
|
SecureSettings secureSettings, Context context,
|
||||||
DumpManager dumpManager) {
|
DumpManager dumpManager) {
|
||||||
mSystemPropertiesHelper = systemPropertiesHelper;
|
mFlagManager = flagManager;
|
||||||
|
mSecureSettings = secureSettings;
|
||||||
IntentFilter filter = new IntentFilter(ACTION_SET_FLAG);
|
IntentFilter filter = new IntentFilter(ACTION_SET_FLAG);
|
||||||
context.registerReceiver(mReceiver, filter, FLAGS_PERMISSION, null);
|
context.registerReceiver(mReceiver, filter, FLAGS_PERMISSION, null);
|
||||||
dumpManager.registerDumpable(TAG, this);
|
dumpManager.registerDumpable(TAG, this);
|
||||||
@@ -88,20 +87,10 @@ public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
|||||||
|
|
||||||
/** Returns the stored value or null if not set. */
|
/** Returns the stored value or null if not set. */
|
||||||
private Boolean isEnabledInternal(int id) {
|
private Boolean isEnabledInternal(int id) {
|
||||||
String data = mSystemPropertiesHelper.get(keyToSysPropKey(id));
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
JSONObject json;
|
|
||||||
try {
|
try {
|
||||||
json = new JSONObject(data);
|
return mFlagManager.isEnabled(id);
|
||||||
if (!assertType(json, TYPE_BOOLEAN)) {
|
} catch (Exception e) {
|
||||||
return null;
|
eraseInternal(id);
|
||||||
}
|
|
||||||
|
|
||||||
return json.getBoolean(FIELD_VALUE);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
eraseInternal(id); // Don't restart SystemUI in this case.
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -116,9 +105,9 @@ public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
|||||||
|
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
try {
|
try {
|
||||||
json.put(FIELD_TYPE, TYPE_BOOLEAN);
|
json.put(FlagManager.FIELD_TYPE, FlagManager.TYPE_BOOLEAN);
|
||||||
json.put(FIELD_VALUE, value);
|
json.put(FIELD_VALUE, value);
|
||||||
mSystemPropertiesHelper.set(keyToSysPropKey(id), json.toString());
|
mSecureSettings.putString(mFlagManager.keyToSettingsPrefix(id), json.toString());
|
||||||
Log.i(TAG, "Set id " + id + " to " + value);
|
Log.i(TAG, "Set id " + id + " to " + value);
|
||||||
restartSystemUI();
|
restartSystemUI();
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
@@ -135,7 +124,7 @@ public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
|||||||
/** Works just like {@link #eraseFlag(int)} except that it doesn't restart SystemUI. */
|
/** Works just like {@link #eraseFlag(int)} except that it doesn't restart SystemUI. */
|
||||||
private void eraseInternal(int id) {
|
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), "");
|
mSecureSettings.putString(mFlagManager.keyToSettingsPrefix(id), "");
|
||||||
Log.i(TAG, "Erase id " + id);
|
Log.i(TAG, "Erase id " + id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,18 +140,6 @@ public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
|||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String keyToSysPropKey(int key) {
|
|
||||||
return SYSPROP_PREFIX + key;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean assertType(JSONObject json, String type) {
|
|
||||||
try {
|
|
||||||
return json.getString(FIELD_TYPE).equals(TYPE_BOOLEAN);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.systemui.flags
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import dagger.Module
|
||||||
|
import dagger.Provides
|
||||||
|
|
||||||
|
@Module
|
||||||
|
object FlagsModule {
|
||||||
|
@JvmStatic
|
||||||
|
@Provides
|
||||||
|
fun provideFlagManager(context: Context): FlagManager {
|
||||||
|
return FlagManager(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import androidx.annotation.NonNull;
|
|||||||
import com.android.systemui.Dumpable;
|
import com.android.systemui.Dumpable;
|
||||||
import com.android.systemui.dagger.SysUISingleton;
|
import com.android.systemui.dagger.SysUISingleton;
|
||||||
import com.android.systemui.dump.DumpManager;
|
import com.android.systemui.dump.DumpManager;
|
||||||
|
import com.android.systemui.util.settings.SecureSettings;
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
@@ -40,10 +41,17 @@ import javax.inject.Inject;
|
|||||||
public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
public class FeatureFlagManager implements FlagReader, FlagWriter, Dumpable {
|
||||||
SparseBooleanArray mAccessedFlags = new SparseBooleanArray();
|
SparseBooleanArray mAccessedFlags = new SparseBooleanArray();
|
||||||
@Inject
|
@Inject
|
||||||
public FeatureFlagManager(SystemPropertiesHelper systemPropertiesHelper, Context context,
|
public FeatureFlagManager(
|
||||||
DumpManager dumpManager) {
|
SecureSettings secureSettings, Context context, DumpManager dumpManager) {
|
||||||
dumpManager.registerDumpable("SysUIFlags", this);
|
dumpManager.registerDumpable("SysUIFlags", this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(Listener run) {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeListener(Listener run) {}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isEnabled(int key, boolean defaultValue) {
|
public boolean isEnabled(int key, boolean defaultValue) {
|
||||||
mAccessedFlags.append(key, defaultValue);
|
mAccessedFlags.append(key, defaultValue);
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2021 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.systemui.flags
|
||||||
|
|
||||||
|
import dagger.Module
|
||||||
|
|
||||||
|
@Module
|
||||||
|
object FlagsModule
|
||||||
@@ -40,6 +40,7 @@ import com.android.systemui.flags.FeatureFlagManager;
|
|||||||
import com.android.systemui.flags.FeatureFlags;
|
import com.android.systemui.flags.FeatureFlags;
|
||||||
import com.android.systemui.flags.FlagReader;
|
import com.android.systemui.flags.FlagReader;
|
||||||
import com.android.systemui.flags.FlagWriter;
|
import com.android.systemui.flags.FlagWriter;
|
||||||
|
import com.android.systemui.flags.FlagsModule;
|
||||||
import com.android.systemui.fragments.FragmentService;
|
import com.android.systemui.fragments.FragmentService;
|
||||||
import com.android.systemui.log.dagger.LogModule;
|
import com.android.systemui.log.dagger.LogModule;
|
||||||
import com.android.systemui.model.SysUiState;
|
import com.android.systemui.model.SysUiState;
|
||||||
@@ -101,6 +102,7 @@ import dagger.Provides;
|
|||||||
ControlsModule.class,
|
ControlsModule.class,
|
||||||
DemoModeModule.class,
|
DemoModeModule.class,
|
||||||
FalsingModule.class,
|
FalsingModule.class,
|
||||||
|
FlagsModule.class,
|
||||||
LogModule.class,
|
LogModule.class,
|
||||||
PeopleHubModule.class,
|
PeopleHubModule.class,
|
||||||
PluginModule.class,
|
PluginModule.class,
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import androidx.test.filters.SmallTest;
|
|||||||
|
|
||||||
import com.android.systemui.SysuiTestCase;
|
import com.android.systemui.SysuiTestCase;
|
||||||
import com.android.systemui.dump.DumpManager;
|
import com.android.systemui.dump.DumpManager;
|
||||||
|
import com.android.systemui.util.settings.SecureSettings;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -52,7 +53,8 @@ import java.io.StringWriter;
|
|||||||
public class FeatureFlagManagerTest extends SysuiTestCase {
|
public class FeatureFlagManagerTest extends SysuiTestCase {
|
||||||
FeatureFlagManager mFeatureFlagManager;
|
FeatureFlagManager mFeatureFlagManager;
|
||||||
|
|
||||||
@Mock private SystemPropertiesHelper mProps;
|
@Mock private FlagManager mFlagManager;
|
||||||
|
@Mock private SecureSettings mSecureSettings;
|
||||||
@Mock private Context mContext;
|
@Mock private Context mContext;
|
||||||
@Mock private DumpManager mDumpManager;
|
@Mock private DumpManager mDumpManager;
|
||||||
|
|
||||||
@@ -60,14 +62,14 @@ public class FeatureFlagManagerTest extends SysuiTestCase {
|
|||||||
public void setup() {
|
public void setup() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
mFeatureFlagManager = new FeatureFlagManager(mProps, mContext, mDumpManager);
|
mFeatureFlagManager = new FeatureFlagManager(mSecureSettings, mContext, mDumpManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void onFinished() {
|
public void onFinished() {
|
||||||
// SystemPropertiesHelper and Context are provided for constructor consistency with the
|
// SecureSettings and Context are provided for constructor consistency with the
|
||||||
// debug version of the FeatureFlagManager, but should never be used.
|
// debug version of the FeatureFlagManager, but should never be used.
|
||||||
verifyZeroInteractions(mProps, mContext);
|
verifyZeroInteractions(mSecureSettings, mContext);
|
||||||
// The dump manager should be registered with even for the release version, but that's it.
|
// The dump manager should be registered with even for the release version, but that's it.
|
||||||
verify(mDumpManager).registerDumpable(anyString(), any());
|
verify(mDumpManager).registerDumpable(anyString(), any());
|
||||||
verifyNoMoreInteractions(mDumpManager);
|
verifyNoMoreInteractions(mDumpManager);
|
||||||
|
|||||||
Reference in New Issue
Block a user