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_NAMESharedPref 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_DELIMITERThe delimiter we use when serializing recent timezones to shared
preferences |
private static final int | MAX_RECENT_TIMEZONESThe 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 |
Methods Summary |
---|
public boolean | areAllItemsEnabled()
return false;
|
public int | getCount()
return mFilteredTimeZoneLength;
|
public java.lang.Object | getItem(int position)
if (position < 0 || position >= mFilteredTimeZoneLength) {
return null;
}
return mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
|
public long | getItemId(int position)
return mFilteredTimeZoneIndices[position];
|
public java.lang.String | getLastFilterString()
return mLastFilterString;
|
public int | getLastFilterTime()
return mLastFilterTime;
|
public int | getLastFilterType()
return mLastFilterType;
|
public android.view.View | getView(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 boolean | hasResults()
return mHasResults;
|
public boolean | hasStableIds()
return true;
|
public boolean | isEnabled(int position)
return mFilteredTimeZoneIndices[position] >= 0;
|
public void | onItemClick(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 void | onSetFilter(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 void | saveRecentTimezone(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.
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();
|