1/*
2 * Copyright (C) 2013 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 com.android.settings.accessibility;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.res.Configuration;
22import android.content.res.Resources;
23import android.provider.Settings;
24import android.text.TextUtils.SimpleStringSplitter;
25
26import java.util.Collections;
27import java.util.HashSet;
28import java.util.Locale;
29import java.util.Set;
30
31/**
32 * Utility methods used within accessibility settings.
33 */
34class AccessibilityUtils {
35    /**
36     * @return the set of enabled accessibility services. If there are not services
37     * it returned the unmodifiable {@link Collections#emptySet()}.
38     */
39    static Set<ComponentName> getEnabledServicesFromSettings(Context context) {
40        final String enabledServicesSetting = Settings.Secure.getString(
41                context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
42        if (enabledServicesSetting == null) {
43            return Collections.emptySet();
44        }
45
46        final Set<ComponentName> enabledServices = new HashSet<ComponentName>();
47        final SimpleStringSplitter colonSplitter = AccessibilitySettings.sStringColonSplitter;
48        colonSplitter.setString(enabledServicesSetting);
49
50        while (colonSplitter.hasNext()) {
51            final String componentNameString = colonSplitter.next();
52            final ComponentName enabledService = ComponentName.unflattenFromString(
53                    componentNameString);
54            if (enabledService != null) {
55                enabledServices.add(enabledService);
56            }
57        }
58
59        return enabledServices;
60    }
61
62    /**
63     * @return a localized version of the text resource specified by resId
64     */
65    static CharSequence getTextForLocale(Context context, Locale locale, int resId) {
66        final Resources res = context.getResources();
67        final Configuration config = res.getConfiguration();
68        final Locale prevLocale = config.locale;
69        try {
70            config.locale = locale;
71            res.updateConfiguration(config, null);
72            return res.getText(resId);
73        } finally {
74            config.locale = prevLocale;
75            res.updateConfiguration(config, null);
76        }
77    }
78}
79