Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc7bf047 authored by Paul Colta's avatar Paul Colta
Browse files

HDMI: Migrate settings from R/S to U+ using legacy keys

This patch improves the existing migration mechanism by using the legacy
keys and converting the int values of the settings to the new ones.

Bug: 429952373
Bug: 432362341
Test: 1. adb shell rm -f /data/system/shared_prefs/hdmi_migration_prefs.xml && adb shell rm -f /data/system/shared_prefs/cec_config.xml
      2. adb shell settings put global hdmi_control_auto_device_off_enabled 0 // This can be replaced with other values.
      3. adb reboot
      4. Check for migration logs and files (adb shell cat /data/system/shared_prefs/hdmi_migration_prefs.xml, adb shell cat /data/system/shared_prefs/cec_config.xml)
Flag: EXEMPT migration implementation
Change-Id: I91214df47efdb026aecd4c35a8295744c577b10e
parent cda0f42f
Loading
Loading
Loading
Loading
+85 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.os.Environment;
import android.os.SystemProperties;
import android.provider.Settings.Global;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Slog;

import com.android.internal.R;
@@ -66,6 +67,18 @@ public class HdmiCecConfig {
     */
    private static final String MIGRATION_GLOBAL_TO_SHARED_DONE_KEY = "global_to_shared_migrated";

    // Legacy Settings.Global keys from Android R/S
    private static final String LEGACY_GLOBAL_HDMI_CONTROL_ENABLED = "hdmi_control_enabled";
    private static final String LEGACY_GLOBAL_HDMI_CONTROL_AUTO_WAKEUP_ENABLED =
            "hdmi_control_auto_wakeup_enabled";
    private static final String LEGACY_GLOBAL_HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED =
            "hdmi_control_auto_device_off_enabled";
    private static final String LEGACY_GLOBAL_HDMI_CONTROL_VOLUME_CONTROL_ENABLED =
            "hdmi_control_volume_control_enabled";
    private static final String LEGACY_GLOBAL_HDMI_SYSTEM_AUDIO_CONTROL_ENABLED =
            "hdmi_system_audio_control_enabled";
    private static final String LEGACY_GLOBAL_HDMI_CEC_SWITCH_ENABLED =
            "hdmi_cec_switch_enabled";
    private static final int STORAGE_SYSPROPS = 0;
    private static final int STORAGE_GLOBAL_SETTINGS = 1;
    private static final int STORAGE_SHARED_PREFS = 2;
@@ -736,6 +749,43 @@ public class HdmiCecConfig {
        return deviceContext.getSharedPreferences(prefsFile, Context.MODE_PRIVATE);
    }

    /**
     * Helper method to migrate a single integer setting from Global.Settings to SharedPreferences,
     * handling potential key name changes and value mapping.
     */
    private void migrateIntSetting(ContentResolver resolver, @NonNull String oldKey,
            @NonNull @SettingName String newKey, int enabledValue, int disabledValue) {
        // Skip if already present in the new SharedPreferences storage.
        if (hasSettingValue(newKey)) {
            Slog.d(TAG, "Setting " + newKey + " already in SharedPreferences. Skipping "
                    + "migration.");
            return;
        }

        String globalValue = Global.getString(resolver, oldKey);
        if (globalValue != null) {
            Slog.i(TAG, "Migrating setting: " + oldKey + " -> " + newKey);
            try {
                int oldValue = Integer.parseInt(globalValue);
                // Legacy Global.Settings store 1 for enabled, 0 for disabled.
                int newValue = (oldValue == 1) ? enabledValue : disabledValue;
                setIntValue(newKey, newValue);
                Slog.i(TAG, "Migrated " + oldKey + " (" + globalValue + ") to " + newKey + " ( "
                        + newValue + ")");
            } catch (NumberFormatException e) {
                Slog.e(TAG, "Failed to parse Global setting " + oldKey + ": " + globalValue, e);
            } catch (IllegalArgumentException e) {
                Slog.e(TAG, "Failed to migrate setting " + newKey + ", value from " + oldKey
                        +" (" + globalValue + ")", e);
            } catch (Exception e) {
                Slog.e(TAG, "Unexpected error migrating setting " + oldKey + " to " + newKey,
                        e);
            }
        } else {
            Slog.d(TAG, "No value found for " + oldKey + " in Global.Settings.");
        }
    }

    /**
     * Migrates HDMI CEC settings from {@link android.provider.Settings.Global} to
     * {@link SharedPreferences}.
@@ -756,16 +806,48 @@ public class HdmiCecConfig {
        Slog.i(TAG, "Starting settings migration from Global.Settings to SharedPreferences.");
        ContentResolver resolver = mContext.getContentResolver();

        // Migrate settings with known key changes between R/S and U.
        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_CONTROL_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_HDMI_CEC_ENABLED,
                HdmiControlManager.HDMI_CEC_CONTROL_ENABLED,
                HdmiControlManager.HDMI_CEC_CONTROL_DISABLED);

        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_CONTROL_AUTO_WAKEUP_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_TV_WAKE_ON_ONE_TOUCH_PLAY,
                HdmiControlManager.TV_WAKE_ON_ONE_TOUCH_PLAY_ENABLED,
                HdmiControlManager.TV_WAKE_ON_ONE_TOUCH_PLAY_DISABLED);

        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_TV_SEND_STANDBY_ON_SLEEP,
                HdmiControlManager.TV_SEND_STANDBY_ON_SLEEP_ENABLED,
                HdmiControlManager.TV_SEND_STANDBY_ON_SLEEP_DISABLED);

        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_CONTROL_VOLUME_CONTROL_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_VOLUME_CONTROL_MODE,
                HdmiControlManager.VOLUME_CONTROL_ENABLED,
                HdmiControlManager.VOLUME_CONTROL_DISABLED);

        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_SYSTEM_AUDIO_CONTROL_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_CONTROL,
                HdmiControlManager.SYSTEM_AUDIO_CONTROL_ENABLED,
                HdmiControlManager.SYSTEM_AUDIO_CONTROL_DISABLED);

        migrateIntSetting(resolver, LEGACY_GLOBAL_HDMI_CEC_SWITCH_ENABLED,
                HdmiControlManager.CEC_SETTING_NAME_ROUTING_CONTROL,
                HdmiControlManager.ROUTING_CONTROL_ENABLED,
                HdmiControlManager.ROUTING_CONTROL_DISABLED);

        // Iterate through all other settings to migrate any that kept the same key name
        for (String settingName : getAllSettings()) {
            String key = settingName;
            // Skip if already present in the new SharedPreferences storage.
            if (hasSettingValue(settingName)) {
                continue;
            }
            // Try to read the old value from Global.Settings.
            // Try to read the old value from Global.Settings using the same key name.
            String globalValue = Global.getString(resolver, key);
            if (globalValue != null) {
                Slog.i(TAG, "Migrating setting: " + key);
                Slog.i(TAG, "Migrating setting (same key): " + key);
                try {
                    if (isIntValueType(key)) {
                        setIntValue(key, Integer.parseInt(globalValue));
@@ -783,6 +865,7 @@ public class HdmiCecConfig {
                }
            }
        }

        // Mark migration as completed.
        migrationPrefs.edit().putBoolean(MIGRATION_GLOBAL_TO_SHARED_DONE_KEY, true).apply();
        Slog.i(TAG, "Settings migration from Global.Settings to SharedPreferences complete.");