1/*
2 * Copyright (C) 2011 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
17package android.support.v4.accessibilityservice;
18
19import android.accessibilityservice.AccessibilityService;
20import android.accessibilityservice.AccessibilityServiceInfo;
21import android.content.pm.ResolveInfo;
22import android.os.Build;
23
24/**
25 * Helper for accessing features in {@link android.accessibilityservice.AccessibilityService}
26 * introduced after API level 4 in a backwards compatible fashion.
27 */
28public class AccessibilityServiceInfoCompat {
29
30    static interface AccessibilityServiceInfoVersionImpl {
31        public String getId(AccessibilityServiceInfo info);
32        public ResolveInfo getResolveInfo(AccessibilityServiceInfo info);
33        public boolean getCanRetrieveWindowContent(AccessibilityServiceInfo info);
34        public String getDescription(AccessibilityServiceInfo info);
35        public String getSettingsActivityName(AccessibilityServiceInfo info);
36    }
37
38    static class AccessibilityServiceInfoStubImpl implements AccessibilityServiceInfoVersionImpl {
39
40        public boolean getCanRetrieveWindowContent(AccessibilityServiceInfo info) {
41            return false;
42        }
43
44        public String getDescription(AccessibilityServiceInfo info) {
45            return null;
46        }
47
48        public String getId(AccessibilityServiceInfo info) {
49            return null;
50        }
51
52        public ResolveInfo getResolveInfo(AccessibilityServiceInfo info) {
53            return null;
54        }
55
56        public String getSettingsActivityName(AccessibilityServiceInfo info) {
57            return null;
58        }
59    }
60
61    static class AccessibilityServiceInfoIcsImpl extends AccessibilityServiceInfoStubImpl {
62
63        @Override
64        public boolean getCanRetrieveWindowContent(AccessibilityServiceInfo info) {
65            return AccessibilityServiceInfoCompatIcs.getCanRetrieveWindowContent(info);
66        }
67
68        @Override
69        public String getDescription(AccessibilityServiceInfo info) {
70            return AccessibilityServiceInfoCompatIcs.getDescription(info);
71        }
72
73        @Override
74        public String getId(AccessibilityServiceInfo info) {
75            return AccessibilityServiceInfoCompatIcs.getId(info);
76        }
77
78        @Override
79        public ResolveInfo getResolveInfo(AccessibilityServiceInfo info) {
80            return AccessibilityServiceInfoCompatIcs.getResolveInfo(info);
81        }
82
83        @Override
84        public String getSettingsActivityName(AccessibilityServiceInfo info) {
85            return AccessibilityServiceInfoCompatIcs.getSettingsActivityName(info);
86        }
87    }
88
89    static {
90        if (Build.VERSION.SDK_INT >= 14) { // ICS
91            IMPL = new AccessibilityServiceInfoIcsImpl();
92        } else {
93            IMPL = new AccessibilityServiceInfoStubImpl();
94        }
95    }
96
97    private static final AccessibilityServiceInfoVersionImpl IMPL;
98
99    /**
100     * Mask for all feedback types.
101     *
102     * @see AccessibilityServiceInfo#FEEDBACK_SPOKEN
103     * @see AccessibilityServiceInfo#FEEDBACK_HAPTIC
104     * @see AccessibilityServiceInfo#FEEDBACK_AUDIBLE
105     * @see AccessibilityServiceInfo#FEEDBACK_VISUAL
106     * @see AccessibilityServiceInfo#FEEDBACK_GENERIC
107     */
108    public static final int FEEDBACK_ALL_MASK = 0xFFFFFFFF;
109
110    /*
111     * Hide constructor
112     */
113    private AccessibilityServiceInfoCompat() {
114
115    }
116
117    /**
118     * The accessibility service id.
119     * <p>
120     * <strong>Generated by the system.</strong>
121     * </p>
122     *
123     * @return The id.
124     */
125    public static String getId(AccessibilityServiceInfo info) {
126        return IMPL.getId(info);
127    }
128
129    /**
130     * The service {@link ResolveInfo}.
131     * <p>
132     * <strong>Generated by the system.</strong>
133     * </p>
134     *
135     * @return The info.
136     */
137    public static ResolveInfo getResolveInfo(AccessibilityServiceInfo info) {
138        return IMPL.getResolveInfo(info);
139    }
140
141    /**
142     * The settings activity name.
143     * <p>
144     * <strong>Statically set from {@link AccessibilityService#SERVICE_META_DATA
145     * meta-data}.</strong>
146     * </p>
147     *
148     * @return The settings activity name.
149     */
150    public static String getSettingsActivityName(AccessibilityServiceInfo info) {
151        return IMPL.getSettingsActivityName(info);
152    }
153
154    /**
155     * Whether this service can retrieve the current window's content.
156     * <p>
157     * <strong>Statically set from {@link AccessibilityService#SERVICE_META_DATA
158     * meta-data}.</strong>
159     * </p>
160     *
161     * @return True window content can be retrieved.
162     */
163    public static boolean getCanRetrieveWindowContent(AccessibilityServiceInfo info) {
164        return IMPL.getCanRetrieveWindowContent(info);
165    }
166
167    /**
168     * Description of the accessibility service.
169     * <p>
170     * <strong>Statically set from {@link AccessibilityService#SERVICE_META_DATA
171     * meta-data}.</strong>
172     * </p>
173     *
174     * @return The description.
175     */
176    public static String getDescription(AccessibilityServiceInfo info) {
177        return IMPL.getDescription(info);
178    }
179
180    /**
181     * Returns the string representation of a feedback type. For example,
182     * {@link AccessibilityServiceInfo#FEEDBACK_SPOKEN} is represented by the
183     * string FEEDBACK_SPOKEN.
184     *
185     * @param feedbackType The feedback type.
186     * @return The string representation.
187     */
188    public static String feedbackTypeToString(int feedbackType) {
189        StringBuilder builder = new StringBuilder();
190        builder.append("[");
191        while (feedbackType > 0) {
192            final int feedbackTypeFlag = 1 << Integer.numberOfTrailingZeros(feedbackType);
193            feedbackType &= ~feedbackTypeFlag;
194            if (builder.length() > 1) {
195                builder.append(", ");
196            }
197            switch (feedbackTypeFlag) {
198                case AccessibilityServiceInfo.FEEDBACK_AUDIBLE:
199                    builder.append("FEEDBACK_AUDIBLE");
200                    break;
201                case AccessibilityServiceInfo.FEEDBACK_HAPTIC:
202                    builder.append("FEEDBACK_HAPTIC");
203                    break;
204                case AccessibilityServiceInfo.FEEDBACK_GENERIC:
205                    builder.append("FEEDBACK_GENERIC");
206                    break;
207                case AccessibilityServiceInfo.FEEDBACK_SPOKEN:
208                    builder.append("FEEDBACK_SPOKEN");
209                    break;
210                case AccessibilityServiceInfo.FEEDBACK_VISUAL:
211                    builder.append("FEEDBACK_VISUAL");
212                    break;
213            }
214        }
215        builder.append("]");
216        return builder.toString();
217    }
218
219    /**
220     * Returns the string representation of a flag. For example,
221     * {@link AccessibilityServiceInfo#DEFAULT} is represented by the
222     * string DEFAULT.
223     *
224     * @param flag The flag.
225     * @return The string representation.
226     */
227    public static String flagToString(int flag) {
228        switch (flag) {
229            case AccessibilityServiceInfo.DEFAULT:
230                return "DEFAULT";
231            default:
232                return null;
233        }
234    }
235}
236