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

Commit 59c7b4b0 authored by Craig Stout's avatar Craig Stout
Browse files

TimePickerDialog does not force new ui on TV.

Add support for done button in LegacyTimePickerDelegate.
Provide leanback themed legacy time picker layout with buttons
on the side.

b/15194230

Change-Id: Ie05c1cf45b334e9642095d63c682aba314ff3c6b
parent 8e5e11b9
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -16,9 +16,11 @@

package android.app;

import android.app.UiModeManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
@@ -110,11 +112,14 @@ public class TimePickerDialog extends AlertDialog
                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker_dialog, null);
        setView(view);
        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);
        mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);

        // Initialize state
        UiModeManager uiModeManager =
                (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
        if (uiModeManager.getCurrentModeType() != Configuration.UI_MODE_TYPE_TELEVISION) {
            mTimePicker.setLegacyMode(false /* will show new UI */);
        }
        mTimePicker.setShowDoneButton(true);
        mTimePicker.setDismissCallback(new TimePicker.TimePickerDismissCallback() {
            @Override
+47 −4
Original line number Diff line number Diff line
@@ -75,6 +75,11 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
    // accommodates these two cases to be backwards compatible.
    private final Button mAmPmButton;

    // May be null if layout has no done button
    private final View mDoneButton;
    private boolean mShowDoneButton;
    private TimePicker.TimePickerDismissCallback mDismissCallback;

    private final String[] mAmPmStrings;

    private boolean mIsEnabled = DEFAULT_ENABLED_STATE;
@@ -218,6 +223,19 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
            }
        }

        mDoneButton = delegator.findViewById(R.id.done_button);
        if (mDoneButton != null) {
            mDoneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mDismissCallback != null) {
                        mDismissCallback.dismiss(mDelegator, false, getCurrentHour(),
                                                 getCurrentMinute());
                    }
                }
            });
        }

        getHourFormatData();

        // update controls to initial state
@@ -242,6 +260,9 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
        if (mDelegator.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            mDelegator.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
        }

        mShowDoneButton = false;
        updateDoneButton();
    }

    private void getHourFormatData() {
@@ -408,12 +429,23 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {

    @Override
    public void setShowDoneButton(boolean showDoneButton) {
        // Nothing to do
        mShowDoneButton = showDoneButton;
        updateDoneButton();
    }

    private boolean isShowDoneButton() {
        return mShowDoneButton;
    }

    private void updateDoneButton() {
        if (mDoneButton != null) {
            mDoneButton.setVisibility(mShowDoneButton ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void setDismissCallback(TimePicker.TimePickerDismissCallback callback) {
        // Nothing to do
        mDismissCallback = callback;
    }

    @Override
@@ -428,7 +460,8 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {

    @Override
    public Parcelable onSaveInstanceState(Parcelable superState) {
        return new SavedState(superState, getCurrentHour(), getCurrentMinute());
        return new SavedState(superState, getCurrentHour(), getCurrentMinute(),
                isShowDoneButton());
    }

    @Override
@@ -436,6 +469,7 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
        SavedState ss = (SavedState) state;
        setCurrentHour(ss.getHour());
        setCurrentMinute(ss.getMinute());
        setShowDoneButton(ss.isShowDoneButton());
    }

    @Override
@@ -596,16 +630,20 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {

        private final int mMinute;

        private SavedState(Parcelable superState, int hour, int minute) {
        private final boolean mShowDoneButton;

        private SavedState(Parcelable superState, int hour, int minute, boolean showDoneButton) {
            super(superState);
            mHour = hour;
            mMinute = minute;
            mShowDoneButton = showDoneButton;
        }

        private SavedState(Parcel in) {
            super(in);
            mHour = in.readInt();
            mMinute = in.readInt();
            mShowDoneButton = (in.readInt() == 1);
        }

        public int getHour() {
@@ -616,11 +654,16 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
            return mMinute;
        }

        public boolean isShowDoneButton() {
            return mShowDoneButton;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            dest.writeInt(mHour);
            dest.writeInt(mMinute);
            dest.writeInt(mShowDoneButton ? 1 : 0);
        }

        @SuppressWarnings({"unused", "hiding"})
+92 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2014 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.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/timePickerLayout"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingStart="8dip"
    android:paddingEnd="8dip">

    <LinearLayout android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingStart="8dip"
        android:paddingEnd="8dip"
        android:layoutDirection="ltr">

        <!-- hour -->
        <NumberPicker
            android:id="@+id/hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dip"
            android:layout_marginBottom="16dip"
            android:focusable="true"
            android:focusableInTouchMode="true"
            />

        <!-- divider -->
        <TextView
            android:id="@+id/divider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="6dip"
            android:layout_marginEnd="6dip"
            android:layout_gravity="center_vertical"
            android:importantForAccessibility="no"
            />

        <!-- minute -->
        <NumberPicker
            android:id="@+id/minute"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dip"
            android:layout_marginBottom="16dip"
            android:focusable="true"
            android:focusableInTouchMode="true"
            />

    </LinearLayout>

    <!-- AM / PM -->
    <NumberPicker
        android:id="@+id/amPm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dip"
        android:layout_marginBottom="16dip"
        android:layout_marginStart="8dip"
        android:layout_marginEnd="8dip"
        android:focusable="true"
        android:focusableInTouchMode="true"
        />

    <!-- Width fixed here because TextView doesn't set MEASURED_STATE_TOO_SMALL -->
    <Button
        android:id="@+id/done_button"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="@string/done_label"
        android:textSize="@dimen/timepicker_done_label_size"
        style="?android:attr/buttonBarButtonStyle" />

</LinearLayout>
+5 −0
Original line number Diff line number Diff line
@@ -22,4 +22,9 @@
    <style name="AlertDialog.Leanback.Light">
    </style>

    <style name="Widget.Leanback.TimePicker" parent="Widget.Material.TimePicker">
        <item name="legacyLayout">@layout/time_picker_legacy_leanback</item>
    </style>


</resources>
+2 −0
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@
      <item name="textColorPrimary">@color/primary_text_leanback_dark</item>
      <item name="textColorSecondary">@color/secondary_text_leanback_dark</item>
      <item name="alertDialogStyle">@style/AlertDialog.Leanback</item>
      <item name="timePickerStyle">@style/Widget.Leanback.TimePicker</item>
    </style>

    <style name="Theme.Leanback.Light.Dialog.TimePicker" parent="Theme.Material.Light.Dialog.BaseTimePicker">
@@ -40,6 +41,7 @@
      <item name="textColorPrimary">@color/primary_text_leanback_light</item>
      <item name="textColorSecondary">@color/secondary_text_leanback_light</item>
      <item name="alertDialogStyle">@style/AlertDialog.Leanback.Light</item>
      <item name="timePickerStyle">@style/Widget.Leanback.TimePicker</item>
    </style>

    <style name="Theme.Leanback.Light.Dialog" parent="Theme.Material.Light.Dialog">