1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.accessibility; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.view.accessibility.AccessibilityEvent; 22 import android.view.accessibility.AccessibilityManager; 23 24 import com.android.launcher3.Launcher; 25 26 /** 27 * Periodically sends accessibility events to announce ongoing state changed. Based on the 28 * implementation in ProgressBar. 29 */ 30 public class DragViewStateAnnouncer implements Runnable { 31 32 private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200; 33 34 private final View mTargetView; 35 36 private DragViewStateAnnouncer(View view) { 37 mTargetView = view; 38 } 39 40 public void announce(CharSequence msg) { 41 mTargetView.setContentDescription(msg); 42 mTargetView.removeCallbacks(this); 43 mTargetView.postDelayed(this, TIMEOUT_SEND_ACCESSIBILITY_EVENT); 44 } 45 46 public void cancel() { 47 mTargetView.removeCallbacks(this); 48 } 49 50 @Override 51 public void run() { 52 mTargetView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED); 53 } 54 55 public void completeAction(int announceResId) { 56 cancel(); 57 Launcher launcher = Launcher.getLauncher(mTargetView.getContext()); 58 launcher.getDragLayer().announceForAccessibility(launcher.getText(announceResId)); 59 } 60 61 public static DragViewStateAnnouncer createFor(View v) { 62 if (((AccessibilityManager) v.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)) 63 .isEnabled()) { 64 return new DragViewStateAnnouncer(v); 65 } else { 66 return null; 67 } 68 } 69 } 70