2525import android .view .View ;
2626import android .view .ViewConfiguration ;
2727import android .view .ViewGroup ;
28+ import android .view .accessibility .AccessibilityManager ;
2829import android .view .accessibility .AccessibilityNodeInfo ;
2930import android .widget .GridLayout ;
3031import android .widget .PopupWindow ;
@@ -209,6 +210,7 @@ public interface IExtraKeysView {
209210 protected SpecialButtonsLongHoldRunnable mSpecialButtonsLongHoldRunnable ;
210211 protected int mLongPressCount ;
211212
213+ protected boolean mAccessibilityEnabled ;
212214
213215 public ExtraKeysView (Context context , AttributeSet attrs ) {
214216 super (context , attrs );
@@ -224,6 +226,9 @@ public ExtraKeysView(Context context, AttributeSet attrs) {
224226
225227 setLongPressTimeout (ViewConfiguration .getLongPressTimeout ());
226228 setLongPressRepeatDelay (DEFAULT_LONG_PRESS_REPEAT_DELAY );
229+
230+ AccessibilityManager am = (AccessibilityManager ) context .getSystemService (Context .ACCESSIBILITY_SERVICE );
231+ mAccessibilityEnabled = am .isEnabled ();
227232 }
228233
229234
@@ -540,11 +545,32 @@ public void onAnyExtraKeyButtonClick(View view, @NonNull ExtraKeyButton buttonIn
540545 state .setIsActive (!state .isActive );
541546 if (!state .isActive )
542547 state .setIsLocked (false );
548+
549+ announceSpecialKeyStateChangeForAccessibility (buttonInfo .getKey (), state );
543550 } else {
544551 onExtraKeyButtonClick (view , buttonInfo , button );
545552 }
546553 }
547554
555+ private void announceSpecialKeyStateChangeForAccessibility (CharSequence buttonName , SpecialButtonState state ) {
556+ if (mAccessibilityEnabled ) {
557+ CharSequence stateText ;
558+ if (!state .isActive ) {
559+ stateText = getResources ().getText (R .string .a11y_special_key_off );
560+ } else if (state .isLocked ) {
561+ stateText = getResources ().getText (R .string .a11y_special_key_latched_on );
562+ } else {
563+ stateText = getResources ().getText (R .string .a11y_special_key_on );
564+
565+ }
566+ String announcementText = buttonName
567+ + " "
568+ + stateText
569+ ;
570+ announceForAccessibility (announcementText );
571+ }
572+ }
573+
548574
549575 public void startScheduledExecutors (View view , ExtraKeyButton buttonInfo , MaterialButton button ) {
550576 stopScheduledExecutors ();
@@ -566,7 +592,7 @@ public void startScheduledExecutors(View view, ExtraKeyButton buttonInfo, Materi
566592 if (state == null ) return ;
567593 if (mHandler == null )
568594 mHandler = new Handler (Looper .getMainLooper ());
569- mSpecialButtonsLongHoldRunnable = new SpecialButtonsLongHoldRunnable (state );
595+ mSpecialButtonsLongHoldRunnable = new SpecialButtonsLongHoldRunnable (state , buttonInfo );
570596 mHandler .postDelayed (mSpecialButtonsLongHoldRunnable , mLongPressTimeout );
571597 }
572598 }
@@ -585,16 +611,20 @@ public void stopScheduledExecutors() {
585611
586612 public class SpecialButtonsLongHoldRunnable implements Runnable {
587613 public final SpecialButtonState mState ;
614+ public final ExtraKeyButton mButtonInfo ;
588615
589- public SpecialButtonsLongHoldRunnable (SpecialButtonState state ) {
616+ public SpecialButtonsLongHoldRunnable (SpecialButtonState state , ExtraKeyButton buttonInfo ) {
590617 mState = state ;
618+ mButtonInfo = buttonInfo ;
591619 }
592620
593621 public void run () {
594622 // Toggle active and lock state
595623 mState .setIsLocked (!mState .isActive );
596624 mState .setIsActive (!mState .isActive );
597625 mLongPressCount ++;
626+
627+ announceSpecialKeyStateChangeForAccessibility (mButtonInfo .getKey (), mState );
598628 }
599629 }
600630
@@ -662,8 +692,10 @@ public Boolean readSpecialButton(SpecialButton specialButton, boolean autoSetInA
662692 return false ;
663693
664694 // Disable active state only if not locked
665- if (autoSetInActive && !state .isLocked )
695+ if (autoSetInActive && !state .isLocked ) {
666696 state .setIsActive (false );
697+ announceSpecialKeyStateChangeForAccessibility (specialButton .getKey (), state );
698+ }
667699
668700 return true ;
669701 }
0 commit comments