| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.ApplicationInfo; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.content.res.XmlResourceParser; |
| 27 |
import android.os.LocaleList; |
| 28 |
import android.util.AttributeSet; |
| 29 |
import android.util.Slog; |
| 30 |
import android.util.Xml; |
| 31 |
|
| 32 |
import com.android.internal.util.XmlUtils; |
| 33 |
|
| 34 |
import org.xmlpull.v1.XmlPullParserException; |
| 35 |
|
| 36 |
import java.io.IOException; |
| 37 |
import java.lang.annotation.Retention; |
| 38 |
import java.lang.annotation.RetentionPolicy; |
| 39 |
import java.util.HashSet; |
| 40 |
import java.util.Set; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class LocaleConfig { |
| 56 |
|
| 57 |
private static final String TAG = "LocaleConfig"; |
| 58 |
public static final String TAG_LOCALE_CONFIG = "locale-config"; |
| 59 |
public static final String TAG_LOCALE = "locale"; |
| 60 |
private LocaleList mLocales; |
| 61 |
private int mStatus; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public static final int STATUS_SUCCESS = 0; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public static final int STATUS_NOT_SPECIFIED = 1; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public static final int STATUS_PARSING_FAILED = 2; |
| 75 |
|
| 76 |
|
| 77 |
@IntDef(prefix = { "STATUS_" }, value = { |
| 78 |
STATUS_SUCCESS, |
| 79 |
STATUS_NOT_SPECIFIED, |
| 80 |
STATUS_PARSING_FAILED |
| 81 |
}) |
| 82 |
@Retention(RetentionPolicy.SOURCE) |
| 83 |
public @interface Status{} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public LocaleConfig(@NonNull Context context) { |
| 93 |
int resId = 0; |
| 94 |
Resources res = context.getResources(); |
| 95 |
try { |
| 96 |
|
| 97 |
resId = new ApplicationInfo(context.getApplicationInfo()).getLocaleConfigRes(); |
| 98 |
|
| 99 |
XmlResourceParser parser = res.getXml(resId); |
| 100 |
parseLocaleConfig(parser, res); |
| 101 |
} catch (Resources.NotFoundException e) { |
| 102 |
Slog.w(TAG, "The resource file pointed to by the given resource ID isn't found."); |
| 103 |
mStatus = STATUS_NOT_SPECIFIED; |
| 104 |
} catch (XmlPullParserException | IOException e) { |
| 105 |
Slog.w(TAG, "Failed to parse XML configuration from " |
| 106 |
+ res.getResourceEntryName(resId), e); |
| 107 |
mStatus = STATUS_PARSING_FAILED; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private void parseLocaleConfig(XmlResourceParser parser, Resources res) |
| 115 |
throws IOException, XmlPullParserException { |
| 116 |
XmlUtils.beginDocument(parser, TAG_LOCALE_CONFIG); |
| 117 |
int outerDepth = parser.getDepth(); |
| 118 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 119 |
Set<String> localeNames = new HashSet<String>(); |
| 120 |
while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 121 |
if (TAG_LOCALE.equals(parser.getName())) { |
| 122 |
final TypedArray attributes = res.obtainAttributes( |
| 123 |
attrs, com.android.internal.R.styleable.LocaleConfig_Locale); |
| 124 |
String nameAttr = attributes.getString( |
| 125 |
com.android.internal.R.styleable.LocaleConfig_Locale_name); |
| 126 |
localeNames.add(nameAttr); |
| 127 |
attributes.recycle(); |
| 128 |
} else { |
| 129 |
XmlUtils.skipCurrentTag(parser); |
| 130 |
} |
| 131 |
} |
| 132 |
mStatus = STATUS_SUCCESS; |
| 133 |
mLocales = LocaleList.forLanguageTags(String.join(",", localeNames)); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public @Nullable LocaleList getSupportedLocales() { |
| 145 |
return mLocales; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public @Status int getStatus() { |
| 166 |
return mStatus; |
| 167 |
} |
| 168 |
} |