[CEC Configuration] Move context to constructor

Simplifies the interface

Bug: 168020131
Test: atest HdmiCecConfigTest
Change-Id: I5751c861b9752b13d5ddc7697fabc8a970091153
This commit is contained in:
Michal Olech
2020-10-23 13:35:38 +02:00
parent 896b383329
commit 9677b5aefe
3 changed files with 37 additions and 36 deletions

View File

@@ -58,10 +58,6 @@ public class HdmiCecConfig {
private static final String ETC_DIR = "etc";
private static final String CONFIG_FILE = "cec_config.xml";
@Nullable private CecSettings mProductConfig = null;
@Nullable private CecSettings mVendorOverride = null;
@Nullable private StorageAdapter mStorageAdapter = null;
@IntDef({
STORAGE_SYSPROPS,
STORAGE_GLOBAL_SETTINGS,
@@ -83,6 +79,11 @@ public class HdmiCecConfig {
public static final String SYSPROP_SYSTEM_AUDIO_MODE_MUTING =
"ro.hdmi.cec.audio.system_audio_mode_muting.enabled";
@NonNull private final Context mContext;
@NonNull private final StorageAdapter mStorageAdapter;
@Nullable private final CecSettings mProductConfig;
@Nullable private final CecSettings mVendorOverride;
/**
* Setting storage input/output helper class.
*/
@@ -126,20 +127,22 @@ public class HdmiCecConfig {
}
@VisibleForTesting
HdmiCecConfig(@Nullable CecSettings productConfig,
@Nullable CecSettings vendorOverride,
@Nullable StorageAdapter storageAdapter) {
HdmiCecConfig(@NonNull Context context,
@NonNull StorageAdapter storageAdapter,
@Nullable CecSettings productConfig,
@Nullable CecSettings vendorOverride) {
mContext = context;
mStorageAdapter = storageAdapter;
mProductConfig = productConfig;
mVendorOverride = vendorOverride;
mStorageAdapter = storageAdapter;
}
HdmiCecConfig() {
this(readSettingsFromFile(Environment.buildPath(Environment.getProductDirectory(),
HdmiCecConfig(@NonNull Context context) {
this(context, new StorageAdapter(),
readSettingsFromFile(Environment.buildPath(Environment.getProductDirectory(),
ETC_DIR, CONFIG_FILE)),
readSettingsFromFile(Environment.buildPath(Environment.getVendorDirectory(),
ETC_DIR, CONFIG_FILE)),
new StorageAdapter());
ETC_DIR, CONFIG_FILE)));
}
@Nullable
@@ -211,7 +214,7 @@ public class HdmiCecConfig {
}
}
private String retrieveValue(@NonNull Context context, @NonNull Setting setting) {
private String retrieveValue(@NonNull Setting setting) {
@Storage int storage = getStorage(setting);
String storageKey = getStorageKey(setting);
if (storage == STORAGE_SYSPROPS) {
@@ -220,14 +223,13 @@ public class HdmiCecConfig {
setting.getDefaultValue().getStringValue());
} else if (storage == STORAGE_GLOBAL_SETTINGS) {
Slog.d(TAG, "Reading '" + storageKey + "' global setting.");
return mStorageAdapter.retrieveGlobalSetting(context, storageKey,
return mStorageAdapter.retrieveGlobalSetting(mContext, storageKey,
setting.getDefaultValue().getStringValue());
}
return null;
}
private void storeValue(@NonNull Context context, @NonNull Setting setting,
@NonNull String value) {
private void storeValue(@NonNull Setting setting, @NonNull String value) {
@Storage int storage = getStorage(setting);
String storageKey = getStorageKey(setting);
if (storage == STORAGE_SYSPROPS) {
@@ -235,7 +237,7 @@ public class HdmiCecConfig {
mStorageAdapter.storeSystemProperty(storageKey, value);
} else if (storage == STORAGE_GLOBAL_SETTINGS) {
Slog.d(TAG, "Setting '" + storageKey + "' global setting.");
mStorageAdapter.storeGlobalSetting(context, storageKey, value);
mStorageAdapter.storeGlobalSetting(mContext, storageKey, value);
}
}
@@ -303,20 +305,19 @@ public class HdmiCecConfig {
/**
* For a given setting name returns the current value of that setting.
*/
public String getValue(@NonNull Context context, @NonNull @CecSettingName String name) {
public String getValue(@NonNull @CecSettingName String name) {
Setting setting = getSetting(name);
if (setting == null) {
throw new IllegalArgumentException("Setting '" + name + "' does not exist.");
}
Slog.d(TAG, "Getting CEC setting value '" + name + "'.");
return retrieveValue(context, setting);
return retrieveValue(setting);
}
/**
* For a given setting name and value sets the current value of that setting.
*/
public void setValue(@NonNull Context context, @NonNull @CecSettingName String name,
@NonNull String value) {
public void setValue(@NonNull @CecSettingName String name, @NonNull String value) {
Setting setting = getSetting(name);
if (setting == null) {
throw new IllegalArgumentException("Setting '" + name + "' does not exist.");
@@ -329,6 +330,6 @@ public class HdmiCecConfig {
+ "' value: '" + value + "'.");
}
Slog.d(TAG, "Updating CEC setting '" + name + "' to '" + value + "'.");
storeValue(context, setting, value);
storeValue(setting, value);
}
}

View File

@@ -190,7 +190,7 @@ public class HdmiControlService extends SystemService {
private boolean mHdmiCecVolumeControlEnabled;
// Make sure HdmiCecConfig is instantiated and the XMLs are read.
private final HdmiCecConfig mHdmiCecConfig = new HdmiCecConfig();
private final HdmiCecConfig mHdmiCecConfig;
/**
* Interface to report send result.
@@ -488,6 +488,7 @@ public class HdmiControlService extends SystemService {
}
mLocalDevices = deviceTypes;
mSettingsObserver = new SettingsObserver(mHandler);
mHdmiCecConfig = new HdmiCecConfig(context);
}
protected static List<Integer> getIntList(String string) {
@@ -2328,7 +2329,7 @@ public class HdmiControlService extends SystemService {
List<String> allSettings = hdmiCecConfig.getAllSettings();
Set<String> userSettings = new HashSet<>(hdmiCecConfig.getUserSettings());
for (String setting : allSettings) {
pw.println(setting + ": " + hdmiCecConfig.getValue(getContext(), setting)
pw.println(setting + ": " + hdmiCecConfig.getValue(setting)
+ " (default: " + hdmiCecConfig.getDefaultValue(setting) + ")"
+ (userSettings.contains(setting) ? " [modifiable]" : ""));
}
@@ -2375,7 +2376,7 @@ public class HdmiControlService extends SystemService {
enforceAccessPermission();
long token = Binder.clearCallingIdentity();
try {
return HdmiControlService.this.getHdmiCecConfig().getValue(getContext(), name);
return HdmiControlService.this.getHdmiCecConfig().getValue(name);
} finally {
Binder.restoreCallingIdentity(token);
}
@@ -2386,7 +2387,7 @@ public class HdmiControlService extends SystemService {
enforceAccessPermission();
long token = Binder.clearCallingIdentity();
try {
HdmiControlService.this.getHdmiCecConfig().setValue(getContext(), name, value);
HdmiControlService.this.getHdmiCecConfig().setValue(name, value);
} finally {
Binder.restoreCallingIdentity(token);
}

View File

@@ -245,7 +245,7 @@ public final class HdmiCecConfigTest {
+ "<cec-settings>"
+ "</cec-settings>", null);
assertThrows(IllegalArgumentException.class,
() -> hdmiCecConfig.getValue(mContext, "foo"));
() -> hdmiCecConfig.getValue("foo"));
}
@Test
@@ -267,7 +267,7 @@ public final class HdmiCecConfigTest {
+ " <default-value string-value=\"to_tv\" />"
+ " </setting>"
+ "</cec-settings>", null);
assertThat(hdmiCecConfig.getValue(mContext,
assertThat(hdmiCecConfig.getValue(
HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP))
.isEqualTo(HdmiControlManager.SEND_STANDBY_ON_SLEEP_BROADCAST);
}
@@ -292,7 +292,7 @@ public final class HdmiCecConfigTest {
+ " <default-value string-value=\"none\" />"
+ " </setting>"
+ "</cec-settings>", null);
assertThat(hdmiCecConfig.getValue(mContext,
assertThat(hdmiCecConfig.getValue(
HdmiControlManager.CEC_SETTING_NAME_POWER_STATE_CHANGE_ON_ACTIVE_SOURCE_LOST))
.isEqualTo(HdmiProperties.power_state_change_on_active_source_lost_values
.STANDBY_NOW.name().toLowerCase());
@@ -305,7 +305,7 @@ public final class HdmiCecConfigTest {
+ "<cec-settings>"
+ "</cec-settings>", null);
assertThrows(IllegalArgumentException.class,
() -> hdmiCecConfig.setValue(mContext, "foo", "bar"));
() -> hdmiCecConfig.setValue("foo", "bar"));
}
@Test
@@ -324,7 +324,7 @@ public final class HdmiCecConfigTest {
+ " </setting>"
+ "</cec-settings>", null);
assertThrows(IllegalArgumentException.class,
() -> hdmiCecConfig.setValue(mContext,
() -> hdmiCecConfig.setValue(
HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP,
HdmiControlManager.SEND_STANDBY_ON_SLEEP_BROADCAST));
}
@@ -345,7 +345,7 @@ public final class HdmiCecConfigTest {
+ " </setting>"
+ "</cec-settings>", null);
assertThrows(IllegalArgumentException.class,
() -> hdmiCecConfig.setValue(mContext,
() -> hdmiCecConfig.setValue(
HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP,
"bar"));
}
@@ -365,8 +365,7 @@ public final class HdmiCecConfigTest {
+ " <default-value string-value=\"to_tv\" />"
+ " </setting>"
+ "</cec-settings>", null);
hdmiCecConfig.setValue(mContext,
HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP,
hdmiCecConfig.setValue(HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP,
HdmiControlManager.SEND_STANDBY_ON_SLEEP_BROADCAST);
verify(mStorageAdapter).storeGlobalSetting(mContext,
Global.HDMI_CONTROL_SEND_STANDBY_ON_SLEEP,
@@ -387,7 +386,7 @@ public final class HdmiCecConfigTest {
+ " <default-value string-value=\"none\" />"
+ " </setting>"
+ "</cec-settings>", null);
hdmiCecConfig.setValue(mContext,
hdmiCecConfig.setValue(
HdmiControlManager.CEC_SETTING_NAME_POWER_STATE_CHANGE_ON_ACTIVE_SOURCE_LOST,
HdmiProperties.power_state_change_on_active_source_lost_values
.STANDBY_NOW.name().toLowerCase());
@@ -409,6 +408,6 @@ public final class HdmiCecConfigTest {
} catch (IOException | DatatypeConfigurationException | XmlPullParserException e) {
Slog.e(TAG, "Encountered an error while reading/parsing CEC config strings", e);
}
return new HdmiCecConfig(productConfig, vendorOverride, mStorageAdapter);
return new HdmiCecConfig(mContext, mStorageAdapter, productConfig, vendorOverride);
}
}