Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 65f09f30 authored by Victor Chang's avatar Victor Chang
Browse files

Add a header view to show the country in RegionZonePicker

Extra fixes in this CL
- Minor string update in time zone picker.
  Use date_time_search_region string in search bar,
  and date_time_set_timezone_title string for lower case "zone".
- Fixed b/76893139. Remove the unnecessary top padding in RecyclerView.
  Create a new layout file time_zone_items_list.xml without the padding.
- Add missing return statement when region ISO code
  is invalid in RegionZonePicker#getAllTimeZoneInfos

Bug: 76209571
Bug: 76893139
Test: m RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.datetime.timezone
Test: Verified that the strings are updated in the UI
Change-Id: I1fb3e2abf80da3cb53cfbc3363bbe46e40a6ac22
parent 201c629f
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  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.
  -->
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"/>
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@
            android:summary="@string/summary_placeholder" />
        <com.android.settingslib.RestrictedPreference
            android:key="region_zone"
            android:title="@string/date_time_select_zone"
            android:title="@string/date_time_set_timezone_title"
            android:summary="@string/summary_placeholder" />
        <com.android.settingslib.widget.FooterPreference/>
    </PreferenceCategory>
+87 −14
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.datetime.timezone;

import android.icu.text.BreakIterator;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.annotation.WorkerThread;
import android.support.v7.widget.RecyclerView;
@@ -40,48 +41,98 @@ import java.util.Locale;
 * {@class AdapterItem} must be provided when an instance is created.
 */
public class BaseTimeZoneAdapter<T extends BaseTimeZoneAdapter.AdapterItem>
        extends RecyclerView.Adapter<BaseTimeZoneAdapter.ItemViewHolder> {
        extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    @VisibleForTesting
    static final int TYPE_HEADER = 0;
    @VisibleForTesting
    static final int TYPE_ITEM = 1;

    private final List<T> mOriginalItems;
    private final OnListItemClickListener<T> mOnListItemClickListener;
    private final Locale mLocale;
    private final boolean mShowItemSummary;
    private final boolean mShowHeader;
    private final CharSequence mHeaderText;

    private List<T> mItems;
    private ArrayFilter mFilter;

    public BaseTimeZoneAdapter(List<T> items, OnListItemClickListener<T>
            onListItemClickListener, Locale locale, boolean showItemSummary) {
    /**
     * @param headerText the text shown in the header, or null to show no header.
     */
    public BaseTimeZoneAdapter(List<T> items, OnListItemClickListener<T> onListItemClickListener,
            Locale locale, boolean showItemSummary, @Nullable CharSequence headerText) {
        mOriginalItems = items;
        mItems = items;
        mOnListItemClickListener = onListItemClickListener;
        mLocale = locale;
        mShowItemSummary = showItemSummary;
        mShowHeader = headerText != null;
        mHeaderText = headerText;
        setHasStableIds(true);
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.time_zone_search_item, parent, false);
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch(viewType) {
            case TYPE_HEADER: {
                final View view = inflater.inflate(R.layout.preference_category_material_settings,
                        parent, false);
                return new HeaderViewHolder(view);
            }
            case TYPE_ITEM: {
                final View view = inflater.inflate(R.layout.time_zone_search_item, parent, false);
                return new ItemViewHolder(view, mOnListItemClickListener);
            }
            default:
                throw new IllegalArgumentException("Unexpected viewType: " + viewType);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        holder.setAdapterItem(mItems.get(position));
        holder.mSummaryFrame.setVisibility(mShowItemSummary ? View.VISIBLE : View.GONE);
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            ((HeaderViewHolder) holder).setText(mHeaderText);
        } else if (holder instanceof ItemViewHolder) {
            ItemViewHolder<T> itemViewHolder = (ItemViewHolder<T>) holder;
            itemViewHolder.setAdapterItem(getDataItem(position));
            itemViewHolder.mSummaryFrame.setVisibility(mShowItemSummary ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public long getItemId(int position) {
        return getItem(position).getItemId();
        // Data item can't have negative id
        return isPositionHeader(position) ? -1 : getDataItem(position).getItemId();
    }

    @Override
    public int getItemCount() {
        return mItems.size();
        return mItems.size() + getHeaderCount();
    }

    @Override
    public int getItemViewType(int position) {
        return isPositionHeader(position) ? TYPE_HEADER : TYPE_ITEM;
    }

    /*
     * Avoid being overridden by making the method final, since constructor shouldn't invoke
     * overridable method.
     */
    @Override
    public final void setHasStableIds(boolean hasStableIds) {
        super.setHasStableIds(hasStableIds);
    }

    private int getHeaderCount() {
        return mShowHeader ? 1 : 0;
    }

    private boolean isPositionHeader(int position) {
        return mShowHeader && position == 0;
    }

    public @NonNull ArrayFilter getFilter() {
@@ -91,8 +142,12 @@ public class BaseTimeZoneAdapter<T extends BaseTimeZoneAdapter.AdapterItem>
        return mFilter;
    }

    public T getItem(int position) {
        return mItems.get(position);
    /**
     * @throws IndexOutOfBoundsException if the view type at the position is a header
     */
    @VisibleForTesting
    public T getDataItem(int position) {
        return mItems.get(position - getHeaderCount());
    }

    public interface AdapterItem {
@@ -100,10 +155,28 @@ public class BaseTimeZoneAdapter<T extends BaseTimeZoneAdapter.AdapterItem>
        CharSequence getSummary();
        String getIconText();
        String getCurrentTime();

        /**
         * @return unique non-negative number
         */
        long getItemId();
        String[] getSearchKeys();
    }

    private static class HeaderViewHolder extends RecyclerView.ViewHolder {
        private final TextView mTextView;

        public HeaderViewHolder(View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(android.R.id.title);
        }

        public void setText(CharSequence text) {
            mTextView.setText(text);
        }
    }

    @VisibleForTesting
    public static class ItemViewHolder<T extends BaseTimeZoneAdapter.AdapterItem>
            extends RecyclerView.ViewHolder implements View.OnClickListener {

+13 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.content.res.Resources;
import android.icu.text.DateFormat;
import android.icu.text.SimpleDateFormat;
import android.icu.util.Calendar;
import android.support.annotation.Nullable;

import com.android.settings.R;
import com.android.settings.datetime.timezone.model.TimeZoneData;
@@ -47,10 +48,17 @@ public abstract class BaseTimeZoneInfoPicker extends BaseTimeZonePicker {
    @Override
    protected BaseTimeZoneAdapter createAdapter(TimeZoneData timeZoneData) {
        mAdapter = new ZoneAdapter(getContext(), getAllTimeZoneInfos(timeZoneData),
                this::onListItemClick, getLocale());
                this::onListItemClick, getLocale(), getHeaderText());
        return mAdapter;
    }

    /**
     * @return the text shown in the header, or null to show no header.
     */
    protected @Nullable CharSequence getHeaderText() {
        return null;
    }

    private void onListItemClick(TimeZoneInfoItem item) {
        final TimeZoneInfo timeZoneInfo = item.mTimeZoneInfo;
        getActivity().setResult(Activity.RESULT_OK, prepareResultData(timeZoneInfo));
@@ -66,9 +74,11 @@ public abstract class BaseTimeZoneInfoPicker extends BaseTimeZonePicker {
    protected static class ZoneAdapter extends BaseTimeZoneAdapter<TimeZoneInfoItem> {

        public ZoneAdapter(Context context, List<TimeZoneInfo> timeZones,
                OnListItemClickListener<TimeZoneInfoItem> onListItemClickListener, Locale locale) {
                OnListItemClickListener<TimeZoneInfoItem> onListItemClickListener, Locale locale,
                CharSequence headerText) {
            super(createTimeZoneInfoItems(context, timeZones, locale),
                    onListItemClickListener, locale,  true /* showItemSummary */);
                    onListItemClickListener, locale,  true /* showItemSummary */,
                    headerText /* headerText */);
        }

        private static List<TimeZoneInfoItem> createTimeZoneInfoItems(Context context,
+2 −2
Original line number Diff line number Diff line
@@ -84,7 +84,7 @@ public abstract class BaseTimeZonePicker extends InstrumentedFragment
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.recycler_view, container, false);
        final View view = inflater.inflate(R.layout.time_zone_items_list, container, false);
        mRecyclerView = view.findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, /* reverseLayout */ false));
Loading