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 static com.android.launcher3.compat.AccessibilityManagerCompat.isAccessibilityEnabled;
20 
21 import android.view.View;
22 import android.view.accessibility.AccessibilityEvent;
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 
DragViewStateAnnouncer(View view)36     private DragViewStateAnnouncer(View view) {
37         mTargetView = view;
38     }
39 
announce(CharSequence msg)40     public void announce(CharSequence msg) {
41         mTargetView.setContentDescription(msg);
42         mTargetView.removeCallbacks(this);
43         mTargetView.postDelayed(this, TIMEOUT_SEND_ACCESSIBILITY_EVENT);
44     }
45 
cancel()46     public void cancel() {
47         mTargetView.removeCallbacks(this);
48     }
49 
50     @Override
run()51     public void run() {
52         mTargetView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
53     }
54 
completeAction(int announceResId)55     public void completeAction(int announceResId) {
56         cancel();
57         Launcher launcher = Launcher.getLauncher(mTargetView.getContext());
58         launcher.getDragLayer().announceForAccessibility(launcher.getText(announceResId));
59     }
60 
createFor(View v)61     public static DragViewStateAnnouncer createFor(View v) {
62         return isAccessibilityEnabled(v.getContext()) ? new DragViewStateAnnouncer(v) : null;
63     }
64 }
65