1cc0e782871eb6b946ded880e391866f27953654bAlan Viverette/*
2cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Copyright (C) 2013 The Android Open Source Project
3cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
4cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Licensed under the Apache License, Version 2.0 (the "License");
5cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * you may not use this file except in compliance with the License.
6cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * You may obtain a copy of the License at
7cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
8cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *      http://www.apache.org/licenses/LICENSE-2.0
9cc0e782871eb6b946ded880e391866f27953654bAlan Viverette *
10cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Unless required by applicable law or agreed to in writing, software
11cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * distributed under the License is distributed on an "AS IS" BASIS,
12cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * See the License for the specific language governing permissions and
14cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * limitations under the License.
15cc0e782871eb6b946ded880e391866f27953654bAlan Viverette */
16cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
17341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverettepackage com.android.settings.accessibility;
18341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette
19341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport android.content.ComponentName;
20341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport android.content.Context;
21cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.res.Configuration;
22cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport android.content.res.Resources;
23341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport android.provider.Settings;
24341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport android.text.TextUtils.SimpleStringSplitter;
25341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette
26cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport java.util.Collections;
27341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport java.util.HashSet;
28cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteimport java.util.Locale;
29341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viveretteimport java.util.Set;
30341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette
31341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette/**
32cc0e782871eb6b946ded880e391866f27953654bAlan Viverette * Utility methods used within accessibility settings.
33341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette */
34cc0e782871eb6b946ded880e391866f27953654bAlan Viveretteclass AccessibilityUtils {
35cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    /**
363f257c4dfba2acc2d61b91baf4f69524a1dc02afSvetoslav     * @return the set of enabled accessibility services. If there are not services
373f257c4dfba2acc2d61b91baf4f69524a1dc02afSvetoslav     * it returned the unmodifiable {@link Collections#emptySet()}.
38cc0e782871eb6b946ded880e391866f27953654bAlan Viverette     */
39341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette    static Set<ComponentName> getEnabledServicesFromSettings(Context context) {
40cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final String enabledServicesSetting = Settings.Secure.getString(
41cc0e782871eb6b946ded880e391866f27953654bAlan Viverette                context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
42341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        if (enabledServicesSetting == null) {
43cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            return Collections.emptySet();
44341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        }
45cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
46cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Set<ComponentName> enabledServices = new HashSet<ComponentName>();
47cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final SimpleStringSplitter colonSplitter = AccessibilitySettings.sStringColonSplitter;
48341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        colonSplitter.setString(enabledServicesSetting);
49cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
50341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        while (colonSplitter.hasNext()) {
51cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final String componentNameString = colonSplitter.next();
52cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            final ComponentName enabledService = ComponentName.unflattenFromString(
53341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette                    componentNameString);
54341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette            if (enabledService != null) {
55341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette                enabledServices.add(enabledService);
56341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette            }
57341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        }
58cc0e782871eb6b946ded880e391866f27953654bAlan Viverette
59341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette        return enabledServices;
60341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette    }
61341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette
62cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    /**
63cc0e782871eb6b946ded880e391866f27953654bAlan Viverette     * @return a localized version of the text resource specified by resId
64cc0e782871eb6b946ded880e391866f27953654bAlan Viverette     */
65cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    static CharSequence getTextForLocale(Context context, Locale locale, int resId) {
66cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Resources res = context.getResources();
67cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Configuration config = res.getConfiguration();
68cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        final Locale prevLocale = config.locale;
69cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        try {
70cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            config.locale = locale;
71cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            res.updateConfiguration(config, null);
72cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            return res.getText(resId);
73cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        } finally {
74cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            config.locale = prevLocale;
75cc0e782871eb6b946ded880e391866f27953654bAlan Viverette            res.updateConfiguration(config, null);
76cc0e782871eb6b946ded880e391866f27953654bAlan Viverette        }
77cc0e782871eb6b946ded880e391866f27953654bAlan Viverette    }
78341ff66e0374a46f89c51bca2ef55da368b7e40fAlan Viverette}
79