ZoneGetter.java 3.99 KB
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);
    }
 
}