1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.example.android.apis.accessibility;
18 
19 import com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.util.AttributeSet;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.view.accessibility.AccessibilityEvent;
27 import android.widget.BaseAdapter;
28 import android.widget.CheckBox;
29 import android.widget.ListView;
30 import android.widget.TextView;
31 
32 
33 /** Acts as a go-between for all AccessibilityEvents sent from items in the ListView, providing the
34  *  option of sending more context to an AccessibilityService by adding more AccessiblityRecords to
35  *  an event.
36  */
37 public class TaskListView extends ListView {
38 
TaskListView(Context context, AttributeSet attributeSet)39     public TaskListView(Context context, AttributeSet attributeSet) {
40         super(context, attributeSet);
41     }
42 
43     /**
44      * This method will fire whenever a child event wants to send an AccessibilityEvent.  As a
45      * result, it's a great place to add more AccessibilityRecords, if you want.  In this case,
46      * the code is grabbing the position of the item in the list, and assuming that to be the
47      * priority for the task.
48      */
49     @Override
onRequestSendAccessibilityEvent(View child, AccessibilityEvent event)50     public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
51         // Add a record for ourselves as well.
52         AccessibilityEvent record = AccessibilityEvent.obtain();
53         super.onInitializeAccessibilityEvent(record);
54 
55         int priority = (Integer) child.getTag();
56         String priorityStr = "Priority: " + priority;
57         record.setContentDescription(priorityStr);
58 
59         event.appendRecord(record);
60         return true;
61     }
62 }
63 
64 /** Adds Accessibility information to individual child views of rows in the list. */
65 final class TaskAdapter extends BaseAdapter{
66 
67     private String[] mLabels = null;
68     private boolean[] mCheckboxes = null;
69     private Context mContext = null;
70 
TaskAdapter(Context context, String[] labels, boolean[] checkboxes)71     public TaskAdapter(Context context, String[] labels, boolean[] checkboxes) {
72         super();
73         mContext = context;
74         mLabels = labels;
75         mCheckboxes = checkboxes;
76     }
77 
78     @Override
getCount()79     public int getCount() {
80         return mLabels.length;
81     }
82 
83     /** Expands the views for individual list entries, and sets content descriptions for use by the
84      *  TaskBackAccessibilityService.
85      */
86     @Override
getView(int position, View convertView, ViewGroup parent)87     public View getView(int position, View convertView, ViewGroup parent) {
88         if(convertView == null) {
89             LayoutInflater inflater = LayoutInflater.from(mContext);
90             convertView = inflater.inflate(R.layout.tasklist_row, parent, false);
91         }
92 
93         CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.tasklist_finished);
94         checkbox.setChecked(mCheckboxes[position]);
95 
96         TextView label = (TextView)(convertView.findViewById(R.id.tasklist_label));
97         label.setText(mLabels[position]);
98 
99         String contentDescription = new StringBuilder()
100                 .append(mContext.getString(R.string.task_name))
101                 .append(' ')
102                 .append(mLabels[position]).toString();
103         label.setContentDescription(contentDescription);
104 
105         convertView.setTag(position);
106 
107         return convertView;
108     }
109 
110     @Override
getItem(int position)111     public Object getItem(int position) {
112         return mLabels[position];
113     }
114 
115     @Override
getItemId(int position)116     public long getItemId(int position) {
117         return position;
118     }
119 }
120