ZoneGetter.java
3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.sw.laryngoscope.utils;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.icu.text.TimeZoneFormat;
import android.icu.text.TimeZoneNames;
import android.os.Build;
import androidx.annotation.RequiresApi;
import com.sw.laryngoscope.R;
import com.sw.laryngoscope.common.InitParam;
import org.xmlpull.v1.XmlPullParserException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import static com.sw.laryngoscope.common.InitParam.zoneList;
public class ZoneGetter {
//private final List<HashMap<String, Object>> mZones = new ArrayList<HashMap<String, Object>>();
private final HashSet<String> mLocalZones = new HashSet<String>();
@RequiresApi(api = Build.VERSION_CODES.N)
public List<HashMap<String, Object>> getZones(Context context) {
/*for (String olsonId : libcore.icu.TimeZoneNames.forLocale(Locale
.getDefault())) {
mLocalZones.add(olsonId);
}*/
try {
XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
while (xrp.next() != XmlResourceParser.START_TAG) {
continue;
}
xrp.next();
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
while (xrp.getEventType() != XmlResourceParser.START_TAG) {
if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
return zoneList;
}
xrp.next();
}
if (xrp.getName().equals("timezone")) {
String olsonId = xrp.getAttributeValue(0);
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(InitParam.ZONE_ID, olsonId);
String countryname = xrp.nextText();
map.put(InitParam.ZONE_COUNTRYNAME, countryname);
zoneList.add(map);
//Logger.d("timezone1"," addTimeZone--location: "+olsonId);
//Logger.d("timezone2"," addTimeZone--location: "+countryname);
//addTimeZone(olsonId, context);
}
while (xrp.getEventType() != XmlResourceParser.END_TAG) {
xrp.next();
}
xrp.next();
}
xrp.close();
} catch (XmlPullParserException xppe) {
} catch (java.io.IOException ioe) {
}
return zoneList;
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void addTimeZone(String olsonId, Context context) {
// We always need the "GMT-07:00" string.
final TimeZone timezone = TimeZone.getTimeZone(olsonId);
String displayName="";
String location = "";
int sep = olsonId.lastIndexOf('/');
if (sep > 0 && sep + 1 < olsonId.length()) {
location = olsonId.substring(sep + 1);
}
//Logger.d("timezone"," addTimeZone--location: "+location);
/*int tznameID = context.getApplicationContext().getResources().getIdentifier(location, "string", context.getApplicationContext().getPackageName());
displayName = context.getApplicationContext().getResources().getString(tznameID);
Logger.d("timezone"," addTimeZone--displayName: "+displayName);*/
if(displayName.equals("") || (displayName == null)) {
final TimeZoneFormat mTimeZoneFormat = TimeZoneFormat.getInstance(Locale.getDefault());
final TimeZoneNames timeZoneNames = mTimeZoneFormat.getTimeZoneNames();
displayName = timeZoneNames.getExemplarLocationName(olsonId);
}
Logger.d("timezone"," addTimeZone--displayName2: "+displayName);
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put("id", olsonId);
map.put("countryname", displayName);
//map.put("timezone", getTimeZoneText(timezone));
//mZones.add(map);
}
}