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 /**
25  * Periodically sends accessibility events to announce ongoing state changed. Based on the
26  * implementation in ProgressBar.
27  */
28 public class DragViewStateAnnouncer implements Runnable {
29 
30     private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200;
31 
32     private final View mTargetView;
33 
DragViewStateAnnouncer(View view)34     private DragViewStateAnnouncer(View view) {
35         mTargetView = view;
36     }
37 
announce(CharSequence msg)38     public void announce(CharSequence msg) {
39         mTargetView.setContentDescription(msg);
40         mTargetView.removeCallbacks(this);
41         mTargetView.postDelayed(this, TIMEOUT_SEND_ACCESSIBILITY_EVENT);
42     }
43 
cancel()44     public void cancel() {
45         mTargetView.removeCallbacks(this);
46     }
47 
48     @Override
run()49     public void run() {
50         mTargetView.sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
51     }
52 
createFor(View v)53     public static DragViewStateAnnouncer createFor(View v) {
54         if (((AccessibilityManager) v.getContext().getSystemService(Context.ACCESSIBILITY_SERVICE))
55                 .isEnabled()) {
56             return new DragViewStateAnnouncer(v);
57         } else {
58             return null;
59         }
60     }
61 }
62