OSDN Git Service

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