Merge "Revert "[CEC Configuration] Add listener support to the HdmiCecConfig""
diff --git a/services/core/java/com/android/server/hdmi/HdmiCecConfig.java b/services/core/java/com/android/server/hdmi/HdmiCecConfig.java
index 2374ece..a261fa1 100644
--- a/services/core/java/com/android/server/hdmi/HdmiCecConfig.java
+++ b/services/core/java/com/android/server/hdmi/HdmiCecConfig.java
@@ -22,19 +22,12 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.annotation.StringDef;
-import android.content.ContentResolver;
 import android.content.Context;
 import android.content.SharedPreferences;
-import android.database.ContentObserver;
 import android.hardware.hdmi.HdmiControlManager;
-import android.net.Uri;
 import android.os.Environment;
-import android.os.Handler;
-import android.os.Looper;
 import android.os.SystemProperties;
-import android.os.UserHandle;
 import android.provider.Settings.Global;
-import android.util.ArrayMap;
 import android.util.Slog;
 
 import com.android.internal.annotations.VisibleForTesting;
@@ -95,23 +88,6 @@
     @Nullable private final CecSettings mProductConfig;
     @Nullable private final CecSettings mVendorOverride;
 
-    private final ArrayMap<Setting, Set<SettingChangeListener>>
-            mSettingChangeListeners = new ArrayMap<>();
-
-    private SettingsObserver mSettingsObserver;
-
-    /**
-     * Listener used to get notifications when value of a setting changes.
-     */
-    public interface SettingChangeListener {
-        /**
-         * Called when value of a setting changes.
-         *
-         * @param setting name of a CEC setting that changed
-         */
-        void onChange(@NonNull @CecSettingName String setting);
-    }
-
     /**
      * Setting storage input/output helper class.
      */
@@ -183,18 +159,6 @@
         }
     }
 
-    private class SettingsObserver extends ContentObserver {
-        SettingsObserver(Handler handler) {
-            super(handler);
-        }
-
-        @Override
-        public void onChange(boolean selfChange, Uri uri) {
-            String setting = uri.getLastPathSegment();
-            HdmiCecConfig.this.notifyGlobalSettingChanged(setting);
-        }
-    }
-
     @VisibleForTesting
     HdmiCecConfig(@NonNull Context context,
                   @NonNull StorageAdapter storageAdapter,
@@ -347,7 +311,6 @@
         } else if (storage == STORAGE_SHARED_PREFS) {
             Slog.d(TAG, "Setting '" + storageKey + "' shared pref.");
             mStorageAdapter.storeSharedPref(storageKey, value);
-            notifySettingChanged(setting);
         }
     }
 
@@ -355,103 +318,6 @@
         return Integer.decode(value.getIntValue());
     }
 
-    private void notifyGlobalSettingChanged(String setting) {
-        switch (setting) {
-            case Global.HDMI_CONTROL_ENABLED:
-                notifySettingChanged(HdmiControlManager.CEC_SETTING_NAME_HDMI_CEC_ENABLED);
-                break;
-            case Global.HDMI_CEC_VERSION:
-                notifySettingChanged(HdmiControlManager.CEC_SETTING_NAME_HDMI_CEC_VERSION);
-                break;
-            case Global.HDMI_CONTROL_SEND_STANDBY_ON_SLEEP:
-                notifySettingChanged(HdmiControlManager.CEC_SETTING_NAME_SEND_STANDBY_ON_SLEEP);
-                break;
-        }
-    }
-
-    private void notifySettingChanged(@NonNull @CecSettingName String name) {
-        Setting setting = getSetting(name);
-        if (setting == null) {
-            throw new IllegalArgumentException("Setting '" + name + "' does not exist.");
-        }
-        notifySettingChanged(setting);
-    }
-
-    private void notifySettingChanged(@NonNull Setting setting) {
-        Set<SettingChangeListener> listeners = mSettingChangeListeners.get(setting);
-        if (listeners == null) {
-            return;  // No listeners registered, do nothing.
-        }
-        for (SettingChangeListener listener: listeners) {
-            listener.onChange(setting.getName());
-        }
-    }
-
-    /**
-     * This method registers Global Setting change observer.
-     * Needs to be called once after initialization of HdmiCecConfig.
-     */
-    public void registerGlobalSettingsObserver(Looper looper) {
-        Handler handler = new Handler(looper);
-        mSettingsObserver = new SettingsObserver(handler);
-        ContentResolver resolver = mContext.getContentResolver();
-        String[] settings = new String[] {
-                Global.HDMI_CONTROL_ENABLED,
-                Global.HDMI_CEC_VERSION,
-                Global.HDMI_CONTROL_SEND_STANDBY_ON_SLEEP,
-        };
-        for (String setting: settings) {
-            resolver.registerContentObserver(Global.getUriFor(setting), false,
-                                             mSettingsObserver, UserHandle.USER_ALL);
-        }
-    }
-
-    /**
-     * This method unregisters Global Setting change observer.
-     */
-    public void unregisterGlobalSettingsObserver() {
-        ContentResolver resolver = mContext.getContentResolver();
-        resolver.unregisterContentObserver(mSettingsObserver);
-    }
-
-    /**
-     * Register change listener for a given setting name.
-     */
-    public void registerChangeListener(@NonNull @CecSettingName String name,
-                                       SettingChangeListener listener) {
-        Setting setting = getSetting(name);
-        if (setting == null) {
-            throw new IllegalArgumentException("Setting '" + name + "' does not exist.");
-        }
-        @Storage int storage = getStorage(setting);
-        if (storage != STORAGE_GLOBAL_SETTINGS && storage != STORAGE_SHARED_PREFS) {
-            throw new IllegalArgumentException("Change listeners for setting '" + name
-                    + "' not supported.");
-        }
-        if (!mSettingChangeListeners.containsKey(setting)) {
-            mSettingChangeListeners.put(setting, new HashSet<>());
-        }
-        mSettingChangeListeners.get(setting).add(listener);
-    }
-
-    /**
-     * Remove change listener for a given setting name.
-     */
-    public void removeChangeListener(@NonNull @CecSettingName String name,
-                                     SettingChangeListener listener) {
-        Setting setting = getSetting(name);
-        if (setting == null) {
-            throw new IllegalArgumentException("Setting '" + name + "' does not exist.");
-        }
-        if (mSettingChangeListeners.containsKey(setting)) {
-            Set<SettingChangeListener> listeners = mSettingChangeListeners.get(setting);
-            listeners.remove(listener);
-            if (listeners.isEmpty()) {
-                mSettingChangeListeners.remove(setting);
-            }
-        }
-    }
-
     /**
      * Returns a list of all settings based on the XML metadata.
      */
diff --git a/services/core/java/com/android/server/hdmi/HdmiControlService.java b/services/core/java/com/android/server/hdmi/HdmiControlService.java
index f3d0623..56b73ba 100644
--- a/services/core/java/com/android/server/hdmi/HdmiControlService.java
+++ b/services/core/java/com/android/server/hdmi/HdmiControlService.java
@@ -496,7 +496,6 @@
         if (mMessageValidator == null) {
             mMessageValidator = new HdmiCecMessageValidator(this);
         }
-        mHdmiCecConfig.registerGlobalSettingsObserver(mIoLooper);
     }
 
     private void bootCompleted() {
diff --git a/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecConfigTest.java b/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecConfigTest.java
index de9a5e4..d10e075 100644
--- a/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecConfigTest.java
+++ b/services/tests/servicestests/src/com/android/server/hdmi/HdmiCecConfigTest.java
@@ -18,17 +18,13 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import static junit.framework.Assert.assertTrue;
-import static junit.framework.Assert.fail;
 
-import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertThrows;
 
-import android.annotation.NonNull;
 import android.content.Context;
 import android.hardware.hdmi.HdmiControlManager;
-import android.os.Looper;
 import android.platform.test.annotations.Presubmit;
 import android.provider.Settings.Global;
 
@@ -42,21 +38,15 @@
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
 
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
 @SmallTest
 @Presubmit
 @RunWith(JUnit4.class)
 public final class HdmiCecConfigTest {
     private static final String TAG = "HdmiCecConfigTest";
 
-    private static final int TIMEOUT_CONTENT_CHANGE_SEC = 4;
-
     private Context mContext;
 
     @Mock private HdmiCecConfig.StorageAdapter mStorageAdapter;
-    @Mock private HdmiCecConfig.SettingChangeListener mSettingChangeListener;
 
     @Before
     public void setUp() throws Exception {
@@ -1029,105 +1019,4 @@
                 HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
                 Integer.toString(HdmiControlManager.SYSTEM_AUDIO_MODE_MUTING_DISABLED));
     }
-
-    @Test
-    public void registerChangeListener_SharedPref_BasicSanity() {
-        HdmiCecConfig hdmiCecConfig = HdmiCecConfig.createFromStrings(
-                mContext, mStorageAdapter,
-                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
-                        + "<cec-settings>"
-                        + "  <setting name=\"system_audio_mode_muting\""
-                        + "           value-type=\"int\""
-                        + "           user-configurable=\"true\">"
-                        + "    <allowed-values>"
-                        + "      <value int-value=\"0\" />"
-                        + "      <value int-value=\"1\" />"
-                        + "    </allowed-values>"
-                        + "    <default-value int-value=\"1\" />"
-                        + "  </setting>"
-                        + "</cec-settings>", null);
-        hdmiCecConfig.registerChangeListener(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
-                mSettingChangeListener);
-        hdmiCecConfig.setIntValue(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
-                HdmiControlManager.SYSTEM_AUDIO_MODE_MUTING_DISABLED);
-        verify(mSettingChangeListener).onChange(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING);
-    }
-
-    @Test
-    public void removeChangeListener_SharedPref_BasicSanity() {
-        HdmiCecConfig hdmiCecConfig = HdmiCecConfig.createFromStrings(
-                mContext, mStorageAdapter,
-                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
-                        + "<cec-settings>"
-                        + "  <setting name=\"system_audio_mode_muting\""
-                        + "           value-type=\"int\""
-                        + "           user-configurable=\"true\">"
-                        + "    <allowed-values>"
-                        + "      <value int-value=\"0\" />"
-                        + "      <value int-value=\"1\" />"
-                        + "    </allowed-values>"
-                        + "    <default-value int-value=\"1\" />"
-                        + "  </setting>"
-                        + "</cec-settings>", null);
-        hdmiCecConfig.registerChangeListener(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
-                mSettingChangeListener);
-        hdmiCecConfig.removeChangeListener(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
-                mSettingChangeListener);
-        hdmiCecConfig.setIntValue(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING,
-                HdmiControlManager.SYSTEM_AUDIO_MODE_MUTING_DISABLED);
-        verify(mSettingChangeListener, never()).onChange(
-                HdmiControlManager.CEC_SETTING_NAME_SYSTEM_AUDIO_MODE_MUTING);
-    }
-
-    /**
-     * Externally modified Global Settings still need to be supported. This test verifies that
-     * setting change notification is being forwarded to listeners registered via HdmiCecConfig.
-     */
-    @Test
-    public void globalSettingObserver_BasicSanity() throws Exception {
-        CountDownLatch notifyLatch = new CountDownLatch(1);
-        // Get current value of the setting in the system.
-        String val = Global.getString(mContext.getContentResolver(), Global.HDMI_CONTROL_ENABLED);
-        HdmiCecConfig hdmiCecConfig = HdmiCecConfig.createFromStrings(
-                mContext, mStorageAdapter,
-                "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
-                + "<cec-settings>"
-                + "  <setting name=\"hdmi_cec_enabled\""
-                + "           value-type=\"int\""
-                + "           user-configurable=\"true\">"
-                + "    <allowed-values>"
-                + "      <value int-value=\"0\" />"
-                + "      <value int-value=\"1\" />"
-                + "    </allowed-values>"
-                + "    <default-value int-value=\"1\" />"
-                + "  </setting>"
-                + "</cec-settings>", null);
-        hdmiCecConfig.registerGlobalSettingsObserver(Looper.getMainLooper());
-        HdmiCecConfig.SettingChangeListener latchUpdateListener =
-                new HdmiCecConfig.SettingChangeListener() {
-            @Override
-            public void onChange(@NonNull @HdmiControlManager.CecSettingName String setting) {
-                notifyLatch.countDown();
-                assertThat(setting).isEqualTo(HdmiControlManager.CEC_SETTING_NAME_HDMI_CEC_ENABLED);
-            }
-        };
-        hdmiCecConfig.registerChangeListener(
-                HdmiControlManager.CEC_SETTING_NAME_HDMI_CEC_ENABLED,
-                latchUpdateListener);
-        // Flip the value of the setting.
-        Global.putString(mContext.getContentResolver(), Global.HDMI_CONTROL_ENABLED,
-                         ((val == null || val.equals("1")) ? "0" : "1"));
-        if (!notifyLatch.await(TIMEOUT_CONTENT_CHANGE_SEC, TimeUnit.SECONDS)) {
-            fail("Timed out waiting for the notify callback");
-        }
-        hdmiCecConfig.unregisterGlobalSettingsObserver();
-        // Restore the previous value of the setting in the system.
-        Global.putString(mContext.getContentResolver(), Global.HDMI_CONTROL_ENABLED, val);
-    }
 }