FileDocCategorySizeDatePackage
TimeZoneResultAdapter.javaAPI DocAndroid 5.1 API10865Thu Mar 12 22:22:54 GMT 2015com.android.timezonepicker

TimeZoneResultAdapter

public class TimeZoneResultAdapter extends android.widget.BaseAdapter implements com.android.timezonepicker.TimeZoneFilterTypeAdapter.OnSetFilterListener, android.widget.AdapterView.OnItemClickListener

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final int
VIEW_TAG_TIME_ZONE
private static final int
EMPTY_INDEX
private static final String
SHARED_PREFS_NAME
SharedPref name and key for recent time zones
private static final String
KEY_RECENT_TIMEZONES
private int
mLastFilterType
private String
mLastFilterString
private int
mLastFilterTime
private boolean
mHasResults
private static final String
RECENT_TIMEZONES_DELIMITER
The delimiter we use when serializing recent timezones to shared preferences
private static final int
MAX_RECENT_TIMEZONES
The maximum number of recent timezones to save
private android.content.Context
mContext
private android.view.LayoutInflater
mInflater
private com.android.timezonepicker.TimeZonePickerView.OnTimeZoneSetListener
mTimeZoneSetListener
private TimeZoneData
mTimeZoneData
private int[]
mFilteredTimeZoneIndices
private int
mFilteredTimeZoneLength
Constructors Summary
public TimeZoneResultAdapter(android.content.Context context, TimeZoneData tzd, com.android.timezonepicker.TimeZonePickerView.OnTimeZoneSetListener l)


        
              
        super();

        mContext = context;
        mTimeZoneData = tzd;
        mTimeZoneSetListener = l;

        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        mFilteredTimeZoneIndices = new int[mTimeZoneData.size()];

        onSetFilter(TimeZoneFilterTypeAdapter.FILTER_TYPE_NONE, null, 0);
    
Methods Summary
public booleanareAllItemsEnabled()

        return false;
    
public intgetCount()

        return mFilteredTimeZoneLength;
    
public java.lang.ObjectgetItem(int position)

        if (position < 0 || position >= mFilteredTimeZoneLength) {
            return null;
        }

        return mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
    
public longgetItemId(int position)

        return mFilteredTimeZoneIndices[position];
    
public java.lang.StringgetLastFilterString()

        return mLastFilterString;
    
public intgetLastFilterTime()

        return mLastFilterTime;
    
public intgetLastFilterType()

        return mLastFilterType;
    
public android.view.ViewgetView(int position, android.view.View convertView, android.view.ViewGroup parent)

        View v = convertView;

        if (mFilteredTimeZoneIndices[position] == EMPTY_INDEX) {
            v = mInflater.inflate(R.layout.empty_time_zone_item, null);
            return v;
        }

        // We'll need to re-inflate the view if it was null, or if it was used as an empty item.
        if (v == null || v.findViewById(R.id.empty_item) != null) {
            v = mInflater.inflate(R.layout.time_zone_item, null);
            ViewHolder.setupViewHolder(v);
        }

        ViewHolder vh = (ViewHolder) v.getTag();

        TimeZoneInfo tzi = mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
        v.setTag(VIEW_TAG_TIME_ZONE, tzi);

        vh.timeZone.setText(tzi.mDisplayName);

        vh.timeOffset.setText(tzi.getGmtDisplayName(mContext));

        String location = tzi.mCountry;
        if (location == null) {
            vh.location.setVisibility(View.INVISIBLE);
        } else {
            vh.location.setText(location);
            vh.location.setVisibility(View.VISIBLE);
        }

        return v;
    
public booleanhasResults()

        return mHasResults;
    
public booleanhasStableIds()

        return true;
    
public booleanisEnabled(int position)

        return mFilteredTimeZoneIndices[position] >= 0;
    
public voidonItemClick(android.widget.AdapterView parent, android.view.View v, int position, long id)

        if (mTimeZoneSetListener != null) {
            TimeZoneInfo tzi = (TimeZoneInfo) v.getTag(VIEW_TAG_TIME_ZONE);
            if (tzi != null) {
              mTimeZoneSetListener.onTimeZoneSet(tzi);
              saveRecentTimezone(tzi.mTzId);
            }
        }
    
public voidonSetFilter(int filterType, java.lang.String str, int time)

        if (DEBUG) {
            Log.d(TAG, "onSetFilter: " + filterType + " [" + str + "] " + time);
        }

        mLastFilterType = filterType;
        mLastFilterString = str;
        mLastFilterTime = time;

        mFilteredTimeZoneLength = 0;
        int idx = 0;

        switch (filterType) {
            case TimeZoneFilterTypeAdapter.FILTER_TYPE_EMPTY:
                mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = EMPTY_INDEX;
                break;
            case TimeZoneFilterTypeAdapter.FILTER_TYPE_NONE:
                // Show the default/current value first
                int defaultTzIndex = mTimeZoneData.getDefaultTimeZoneIndex();
                if (defaultTzIndex != -1) {
                    mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = defaultTzIndex;
                }

                // Show the recent selections
                SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFS_NAME,
                        Context.MODE_PRIVATE);
                String recentsString = prefs.getString(KEY_RECENT_TIMEZONES, null);
                if (!TextUtils.isEmpty(recentsString)) {
                    String[] recents = recentsString.split(RECENT_TIMEZONES_DELIMITER);
                    for (int i = recents.length - 1; i >= 0; i--) {
                        if (!TextUtils.isEmpty(recents[i])
                                && !recents[i].equals(mTimeZoneData.mDefaultTimeZoneId)) {
                            int index = mTimeZoneData.findIndexByTimeZoneIdSlow(recents[i]);
                            if (index != -1) {
                                mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = index;
                            }
                        }
                    }
                }

                break;
            case TimeZoneFilterTypeAdapter.FILTER_TYPE_GMT:
                ArrayList<Integer> indices = mTimeZoneData.getTimeZonesByOffset(time);
                if (indices != null) {
                    for (Integer i : indices) {
                        mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = i;
                    }
                }
                break;
            case TimeZoneFilterTypeAdapter.FILTER_TYPE_COUNTRY:
                ArrayList<Integer> tzIds = mTimeZoneData.mTimeZonesByCountry.get(str);
                if (tzIds != null) {
                    for (Integer tzi : tzIds) {
                        mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = tzi;
                    }
                }
                break;
            case TimeZoneFilterTypeAdapter.FILTER_TYPE_STATE:
                // TODO Filter by state
                break;
            default:
                throw new IllegalArgumentException();
        }
        mHasResults = mFilteredTimeZoneLength > 0;

        notifyDataSetChanged();
    
public voidsaveRecentTimezone(java.lang.String id)
Saves the given timezone ID as a recent timezone under shared preferences. If there are already the maximum number of recent timezones saved, it will remove the oldest and append this one.

param
id the ID of the timezone to save
see
{@link #MAX_RECENT_TIMEZONES}

        SharedPreferences prefs = mContext.getSharedPreferences(SHARED_PREFS_NAME,
                Context.MODE_PRIVATE);
        String recentsString = prefs.getString(KEY_RECENT_TIMEZONES, null);
        if (recentsString == null) {
            recentsString = id;
        } else {
            // De-dup
            LinkedHashSet<String> recents = new LinkedHashSet<String>();
            for(String tzId : recentsString.split(RECENT_TIMEZONES_DELIMITER)) {
                if (!recents.contains(tzId) && !id.equals(tzId)) {
                    recents.add(tzId);
                }
            }

            Iterator<String> it = recents.iterator();
            while (recents.size() >= MAX_RECENT_TIMEZONES) {
                if (!it.hasNext()) {
                    break;
                }
                it.next();
                it.remove();
            }
            recents.add(id);

            StringBuilder builder = new StringBuilder();
            boolean first = true;
            for (String recent : recents) {
                if (first) {
                    first = false;
                } else {
                    builder.append(RECENT_TIMEZONES_DELIMITER);
                }
                builder.append(recent);
            }
            recentsString = builder.toString();
        }

        prefs.edit().putString(KEY_RECENT_TIMEZONES, recentsString).apply();