1 package com.android.ex.photo.util; 2 3 import android.content.Context; 4 import android.os.Build; 5 import androidx.core.view.accessibility.AccessibilityEventCompat; 6 import androidx.core.view.accessibility.AccessibilityRecordCompat; 7 import android.view.View; 8 import android.view.accessibility.AccessibilityEvent; 9 import android.view.accessibility.AccessibilityManager; 10 11 public class Util { isTouchExplorationEnabled(AccessibilityManager accessibilityManager)12 public static boolean isTouchExplorationEnabled(AccessibilityManager accessibilityManager) { 13 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 14 return accessibilityManager.isTouchExplorationEnabled(); 15 } else { 16 return false; 17 } 18 } 19 20 /** 21 * Make an announcement which is related to some sort of a context change. Also see 22 * {@link android.view.View#announceForAccessibility} 23 * @param view The view that triggered the announcement 24 * @param accessibilityManager AccessibilityManager instance. If it is null, the method can 25 * obtain an instance itself. 26 * @param text The announcement text 27 */ announceForAccessibility( final View view, AccessibilityManager accessibilityManager, final CharSequence text)28 public static void announceForAccessibility( 29 final View view, AccessibilityManager accessibilityManager, 30 final CharSequence text) { 31 // Jelly Bean added support for speaking text verbatim 32 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { 33 view.announceForAccessibility(text); 34 return; 35 } 36 37 final Context context = view.getContext().getApplicationContext(); 38 if (accessibilityManager == null) { 39 accessibilityManager = (AccessibilityManager) context.getSystemService( 40 Context.ACCESSIBILITY_SERVICE); 41 } 42 43 if (!accessibilityManager.isEnabled()) { 44 return; 45 } 46 47 final int eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED; 48 49 // Construct an accessibility event with the minimum recommended 50 // attributes. An event without a class name or package may be dropped. 51 final AccessibilityEvent event = AccessibilityEvent.obtain(eventType); 52 event.getText().add(text); 53 event.setEnabled(view.isEnabled()); 54 event.setClassName(view.getClass().getName()); 55 event.setPackageName(context.getPackageName()); 56 57 // JellyBean MR1 requires a source view to set the window ID. 58 final AccessibilityRecordCompat record = AccessibilityEventCompat.asRecord(event); 59 record.setSource(view); 60 61 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { 62 view.getParent().requestSendAccessibilityEvent(view, event); 63 } else { 64 // Sends the event directly through the accessibility manager. 65 accessibilityManager.sendAccessibilityEvent(event); 66 } 67 } 68 } 69