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

Commit cfa55535 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

AudioService: modify logic for independent a11y volume

Add support for enabling independent a11y volume based on
  a11y services state change.
Add compile time constant USE_FLAG_ENABLE_ACCESSIBILITY_VOLUME
  for testing a11y volume simply when TalkBack is enabled.

Test: change new USE_FLAG... constant to false and enable Talkback
Bug 30448020
Change-Id: I541481ed6ca24bc46872818bb5306c5662ab80e9
parent 70efb526
Loading
Loading
Loading
Loading
+39 −16
Original line number Diff line number Diff line
@@ -144,7 +144,8 @@ import java.util.Objects;
 */
public class AudioService extends IAudioService.Stub
        implements AccessibilityManager.TouchExplorationStateChangeListener,
            AccessibilityManager.AccessibilityStateChangeListener{
            AccessibilityManager.AccessibilityStateChangeListener,
            AccessibilityManager.AccessibilityServicesStateChangeListener {

    private static final String TAG = "AudioService";

@@ -780,7 +781,7 @@ public class AudioService extends IAudioService.Stub
                TAG,
                SAFE_VOLUME_CONFIGURE_TIMEOUT_MS);

        initA11yMonitoring(mContext);
        initA11yMonitoring();
        onIndicateSystemReady();
    }

@@ -5925,14 +5926,26 @@ public class AudioService extends IAudioService.Stub
    //==========================================================================================
    // Accessibility

    private void initA11yMonitoring(Context ctxt) {
        AccessibilityManager accessibilityManager =
                (AccessibilityManager) ctxt.getSystemService(Context.ACCESSIBILITY_SERVICE);
    /**
     * Compile-time constant to enable the use of an independent a11y volume:
     * - set to true to listen to a11y services state changes and read
     *   the whether any exposes the FLAG_ENABLE_ACCESSIBILITY_VOLUME flag
     * - set to false to listen to when accessibility services are started (e.g. "TalkBack started")
     */
    private static final boolean USE_FLAG_ENABLE_ACCESSIBILITY_VOLUME = true;

    private void initA11yMonitoring() {
        final AccessibilityManager accessibilityManager =
                (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
        updateDefaultStreamOverrideDelay(accessibilityManager.isTouchExplorationEnabled());
        updateA11yVolumeAlias(accessibilityManager.isEnabled());
        accessibilityManager.addTouchExplorationStateChangeListener(this);
        if (USE_FLAG_ENABLE_ACCESSIBILITY_VOLUME) {
            accessibilityManager.addAccessibilityServicesStateChangeListener(this);
        } else {
            accessibilityManager.addAccessibilityStateChangeListener(this);
        }
    }

    //---------------------------------------------------------------------------------
    // A11y: taking touch exploration into account for selecting the default
@@ -5969,15 +5982,24 @@ public class AudioService extends IAudioService.Stub

    private static boolean sIndependentA11yVolume = false;

    // implementation of AccessibilityStateChangeListener
    @Override
    public void onAccessibilityStateChanged(boolean enabled) {
        updateA11yVolumeAlias(enabled);
    }

    private void updateA11yVolumeAlias(boolean a11Enabled) {
        if (DEBUG_VOL) Log.d(TAG, "Accessibility mode changed to " + a11Enabled);
        // a11y has its own volume stream when a11y service is enabled
        sIndependentA11yVolume = a11Enabled;
    // implementation of AccessibilityServicesStateChangeListener
    @Override
    public void onAccessibilityServicesStateChanged() {
        final AccessibilityManager accessibilityManager =
                (AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE);
        updateA11yVolumeAlias(accessibilityManager.isAccessibilityVolumeStreamActive());
    }

    private void updateA11yVolumeAlias(boolean a11VolEnabled) {
        if (DEBUG_VOL) Log.d(TAG, "Accessibility volume enabled = " + a11VolEnabled);
        if (sIndependentA11yVolume != a11VolEnabled) {
            sIndependentA11yVolume = a11VolEnabled;
            // update the volume mapping scheme
            updateStreamVolumeAlias(true /*updateVolumes*/, TAG);
            // update the volume controller behavior
@@ -5985,6 +6007,7 @@ public class AudioService extends IAudioService.Stub
                    VolumePolicy.A11Y_MODE_INDEPENDENT_A11Y_VOLUME :
                        VolumePolicy.A11Y_MODE_MEDIA_A11Y_VOLUME);
        }
    }

    //==========================================================================================
    // Camera shutter sound policy.