11
11
import android .content .Context ;
12
12
import android .content .pm .PackageInfo ;
13
13
import android .content .pm .PackageManager ;
14
+ import android .text .TextUtils ;
14
15
15
16
import java .util .Arrays ;
16
17
import java .util .Collections ;
17
18
import java .util .List ;
19
+ import java .util .Locale ;
18
20
import java .util .TimeZone ;
19
21
20
22
import bolts .Continuation ;
@@ -36,12 +38,13 @@ public class ParseInstallation extends ParseObject {
36
38
private static final String KEY_DEVICE_TOKEN = "deviceToken" ;
37
39
private static final String KEY_PUSH_TYPE = "pushType" ;
38
40
private static final String KEY_TIME_ZONE = "timeZone" ;
41
+ private static final String KEY_LOCALE = "localeIdentifier" ;
39
42
private static final String KEY_APP_VERSION = "appVersion" ;
40
43
/* package */ static final String KEY_CHANNELS = "channels" ;
41
44
42
45
private static final List <String > READ_ONLY_FIELDS = Collections .unmodifiableList (
43
46
Arrays .asList (KEY_DEVICE_TYPE , KEY_INSTALLATION_ID , KEY_DEVICE_TOKEN , KEY_PUSH_TYPE ,
44
- KEY_TIME_ZONE , KEY_APP_VERSION , KEY_APP_NAME , KEY_PARSE_VERSION ,
47
+ KEY_TIME_ZONE , KEY_LOCALE , KEY_APP_VERSION , KEY_APP_NAME , KEY_PARSE_VERSION ,
45
48
KEY_APP_IDENTIFIER ));
46
49
47
50
// TODO(mengyan): Inject into ParseInstallationInstanceController
@@ -108,6 +111,7 @@ public String getInstallationId() {
108
111
updateTimezone ();
109
112
updateVersionInfo ();
110
113
updateDeviceInfo ();
114
+ updateLocaleIdentifier ();
111
115
}
112
116
}
113
117
@@ -165,8 +169,8 @@ public Task<Void> then(Task<Void> task) throws Exception {
165
169
// time zones from devices reporting other formats.
166
170
private void updateTimezone () {
167
171
String zone = TimeZone .getDefault ().getID ();
168
- if ((zone .indexOf ('/' ) > 0 || zone .equals ("GMT" )) && !zone .equals (get ("timeZone" ))) {
169
- performPut ("timeZone" , zone );
172
+ if ((zone .indexOf ('/' ) > 0 || zone .equals ("GMT" )) && !zone .equals (get (KEY_TIME_ZONE ))) {
173
+ performPut (KEY_TIME_ZONE , zone );
170
174
}
171
175
}
172
176
@@ -180,25 +184,65 @@ private void updateVersionInfo() {
180
184
String appVersion = pkgInfo .versionName ;
181
185
String appName = pm .getApplicationLabel (pm .getApplicationInfo (packageName , 0 )).toString ();
182
186
183
- if (packageName != null && !packageName .equals (get ("appIdentifier" ))) {
187
+ if (packageName != null && !packageName .equals (get (KEY_APP_IDENTIFIER ))) {
184
188
performPut (KEY_APP_IDENTIFIER , packageName );
185
189
}
186
- if (appName != null && !appName .equals (get ("appName" ))) {
190
+ if (appName != null && !appName .equals (get (KEY_APP_NAME ))) {
187
191
performPut (KEY_APP_NAME , appName );
188
192
}
189
- if (appVersion != null && !appVersion .equals (get ("appVersion" ))) {
193
+ if (appVersion != null && !appVersion .equals (get (KEY_APP_VERSION ))) {
190
194
performPut (KEY_APP_VERSION , appVersion );
191
195
}
192
196
} catch (PackageManager .NameNotFoundException e ) {
193
197
PLog .w (TAG , "Cannot load package info; will not be saved to installation" );
194
198
}
195
199
196
- if (!VERSION_NAME .equals (get ("parseVersion" ))) {
200
+ if (!VERSION_NAME .equals (get (KEY_PARSE_VERSION ))) {
197
201
performPut (KEY_PARSE_VERSION , VERSION_NAME );
198
202
}
199
203
}
200
204
}
201
205
206
+ /*
207
+ * Save locale in the following format:
208
+ * [language code]-[country code]
209
+ *
210
+ * The language codes are two-letter lowercase ISO language codes (such as "en") as defined by
211
+ * <a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1</a>.
212
+ * The country codes are two-letter uppercase ISO country codes (such as "US") as defined by
213
+ * <a href="http://en.wikipedia.org/wiki/ISO_3166-1_alpha-3">ISO 3166-1</a>.
214
+ *
215
+ * Note that Java uses several deprecated two-letter codes. The Hebrew ("he") language
216
+ * code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji". This
217
+ * rewriting happens even if you construct your own {@code Locale} object, not just for
218
+ * instances returned by the various lookup methods.
219
+ */
220
+ private void updateLocaleIdentifier () {
221
+ final Locale locale = Locale .getDefault ();
222
+
223
+ String language = locale .getLanguage ();
224
+ String country = locale .getCountry ();
225
+
226
+ if (TextUtils .isEmpty (language )) {
227
+ return ;
228
+ }
229
+
230
+ // rewrite depreciated two-letter codes
231
+ if (language .equals ("iw" )) language = "he" ; // Hebrew
232
+ if (language .equals ("in" )) language = "id" ; // Indonesian
233
+ if (language .equals ("ji" )) language = "yi" ; // Yiddish
234
+
235
+ String localeString = language ;
236
+
237
+ if (!TextUtils .isEmpty (country )) {
238
+ localeString = String .format (Locale .US , "%s-%s" , language , country );
239
+ }
240
+
241
+ if (!localeString .equals (get (KEY_LOCALE ))) {
242
+ performPut (KEY_LOCALE , localeString );
243
+ }
244
+ }
245
+
202
246
// TODO(mengyan): Move to ParseInstallationInstanceController
203
247
/* package */ void updateDeviceInfo () {
204
248
updateDeviceInfo (ParsePlugins .get ().installationId ());
0 commit comments