Time zone, Region, UTC picker
- Extract most common view related codes into BaseTimeZoneAdapter
and BaseTimeZonePicker. Subclass handles the text formatting and
order.
- Search view is added compared to previous version of time
zone picker
- SpannableUtil is added to preserve spannable when formatting
String resource.
- Fix the bug using GMT+<arabic> as time zone id. b/73132985
- Fix Talkback treating flags on screens as a separate element
Bug: 72146259
Bug: 73132985
Bug: 73952488
Test: mm RunSettingsRoboTests
Change-Id: I42c6ac369199c09d11e7f5cc4707358fa4780fed
(cherry picked from commit fbd30acef0
)
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.datetime.timezone;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
import android.icu.text.DateFormat;
|
||||
import android.icu.text.SimpleDateFormat;
|
||||
import android.icu.util.Calendar;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.datetime.timezone.model.TimeZoneData;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Render a list of {@class TimeZoneInfo} into the list view in {@class BaseTimeZonePicker}
|
||||
*/
|
||||
public abstract class BaseTimeZoneInfoPicker extends BaseTimeZonePicker {
|
||||
protected static final String TAG = "RegionZoneSearchPicker";
|
||||
protected ZoneAdapter mAdapter;
|
||||
|
||||
protected BaseTimeZoneInfoPicker(int titleResId, int searchHintResId,
|
||||
boolean searchEnabled, boolean defaultExpandSearch) {
|
||||
super(titleResId, searchHintResId, searchEnabled, defaultExpandSearch);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected BaseTimeZoneAdapter createAdapter(TimeZoneData timeZoneData) {
|
||||
mAdapter = new ZoneAdapter(getContext(), getAllTimeZoneInfos(timeZoneData),
|
||||
this::onListItemClick, getLocale());
|
||||
return mAdapter;
|
||||
}
|
||||
|
||||
private void onListItemClick(int position) {
|
||||
final TimeZoneInfo timeZoneInfo = mAdapter.getItem(position).mTimeZoneInfo;
|
||||
getActivity().setResult(Activity.RESULT_OK, prepareResultData(timeZoneInfo));
|
||||
getActivity().finish();
|
||||
}
|
||||
|
||||
protected Intent prepareResultData(TimeZoneInfo selectedTimeZoneInfo) {
|
||||
return new Intent().putExtra(EXTRA_RESULT_TIME_ZONE_ID, selectedTimeZoneInfo.getId());
|
||||
}
|
||||
|
||||
public abstract List<TimeZoneInfo> getAllTimeZoneInfos(TimeZoneData timeZoneData);
|
||||
|
||||
protected static class ZoneAdapter extends BaseTimeZoneAdapter<TimeZoneInfoItem> {
|
||||
|
||||
public ZoneAdapter(Context context, List<TimeZoneInfo> timeZones,
|
||||
OnListItemClickListener onListItemClickListener, Locale locale) {
|
||||
super(createTimeZoneInfoItems(context, timeZones, locale),
|
||||
onListItemClickListener, locale, true /* showItemSummary */);
|
||||
}
|
||||
|
||||
private static List<TimeZoneInfoItem> createTimeZoneInfoItems(Context context,
|
||||
List<TimeZoneInfo> timeZones, Locale locale) {
|
||||
final DateFormat currentTimeFormat = new SimpleDateFormat(
|
||||
android.text.format.DateFormat.getTimeFormatString(context), locale);
|
||||
final ArrayList<TimeZoneInfoItem> results = new ArrayList<>(timeZones.size());
|
||||
final Resources resources = context.getResources();
|
||||
long i = 0;
|
||||
for (TimeZoneInfo timeZone : timeZones) {
|
||||
results.add(new TimeZoneInfoItem(i++, timeZone, resources, currentTimeFormat));
|
||||
}
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
private static class TimeZoneInfoItem implements BaseTimeZoneAdapter.AdapterItem {
|
||||
private final long mItemId;
|
||||
private final TimeZoneInfo mTimeZoneInfo;
|
||||
private final Resources mResources;
|
||||
private final DateFormat mTimeFormat;
|
||||
private final String mTitle;
|
||||
private final String[] mSearchKeys;
|
||||
|
||||
private TimeZoneInfoItem(long itemId, TimeZoneInfo timeZoneInfo, Resources resources,
|
||||
DateFormat timeFormat) {
|
||||
mItemId = itemId;
|
||||
mTimeZoneInfo = timeZoneInfo;
|
||||
mResources = resources;
|
||||
mTimeFormat = timeFormat;
|
||||
mTitle = createTitle(timeZoneInfo);
|
||||
mSearchKeys = new String[] { mTitle };
|
||||
}
|
||||
|
||||
private static String createTitle(TimeZoneInfo timeZoneInfo) {
|
||||
String name = timeZoneInfo.getExemplarLocation();
|
||||
if (name == null) {
|
||||
name = timeZoneInfo.getGenericName();
|
||||
}
|
||||
if (name == null && timeZoneInfo.getTimeZone().inDaylightTime(new Date())) {
|
||||
name = timeZoneInfo.getDaylightName();
|
||||
}
|
||||
if (name == null) {
|
||||
name = timeZoneInfo.getStandardName();
|
||||
}
|
||||
if (name == null) {
|
||||
name = String.valueOf(timeZoneInfo.getGmtOffset());
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getTitle() {
|
||||
return mTitle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CharSequence getSummary() {
|
||||
String name = mTimeZoneInfo.getGenericName();
|
||||
if (name == null) {
|
||||
if (mTimeZoneInfo.getTimeZone().inDaylightTime(new Date())) {
|
||||
name = mTimeZoneInfo.getDaylightName();
|
||||
} else {
|
||||
name = mTimeZoneInfo.getStandardName();
|
||||
}
|
||||
}
|
||||
if (name == null) {
|
||||
return mTimeZoneInfo.getGmtOffset();
|
||||
} else {
|
||||
return SpannableUtil.getResourcesText(mResources,
|
||||
R.string.zone_info_offset_and_name, mTimeZoneInfo.getGmtOffset(), name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIconText() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCurrentTime() {
|
||||
return mTimeFormat.format(Calendar.getInstance(mTimeZoneInfo.getTimeZone()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId() {
|
||||
return mItemId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSearchKeys() {
|
||||
return mSearchKeys;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user