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

Commit dfaa6c74 authored by Fabrice Di Meglio's avatar Fabrice Di Meglio
Browse files

Fix TimePicker behavior

- use Alert Theme and make the TimePickerDialog show its done button
in the legacy mode
- if targetSdkVersion < L or if TV UI show the legacy TimePicker

Change-Id: I19ca6dbbcb50599c7e230262a23956c185f53a5c
parent 82fa0e7f
Loading
Loading
Loading
Loading
+8 −8
Original line number Diff line number Diff line
@@ -16,11 +16,9 @@

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;
@@ -30,6 +28,7 @@ import android.widget.TimePicker.OnTimeChangedListener;

import com.android.internal.R;

import static android.os.Build.VERSION_CODES.L;

/**
 * A dialog that prompts the user for the time of day using a {@link TimePicker}.
@@ -108,18 +107,19 @@ public class TimePickerDialog extends AlertDialog

        Context themeContext = getContext();

        final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
        if (targetSdkVersion < L) {
            setIcon(0);
            setTitle(R.string.time_picker_dialog_title);
            setButton(BUTTON_POSITIVE, themeContext.getText(R.string.date_time_done), this);
        }

        LayoutInflater inflater =
                (LayoutInflater) themeContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.time_picker_dialog, null);
        setView(view);
        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
+2 −2
Original line number Diff line number Diff line
@@ -224,7 +224,8 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
        }

        mDoneButton = delegator.findViewById(R.id.done_button);
        if (mDoneButton != null) {
        mShowDoneButton = (mDoneButton != null);
        if (mShowDoneButton) {
            mDoneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
@@ -261,7 +262,6 @@ class LegacyTimePickerDelegate extends TimePicker.AbstractTimePickerDelegate {
            mDelegator.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
        }

        mShowDoneButton = false;
        updateDoneButton();
    }

+17 −10
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.widget;

import android.annotation.Widget;
import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
@@ -29,7 +30,7 @@ import com.android.internal.R;

import java.util.Locale;

import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.L;

/**
 * A view for selecting the time of day, in either 24 hour or AM/PM mode. The
@@ -91,15 +92,24 @@ public class TimePicker extends FrameLayout {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TimePicker,
                mDefStyleAttr, mDefStyleRes);

        // Create the correct UI delegate. Default is the legacy one.
        final boolean isLegacyMode = shouldForceLegacyMode() ?
                true : a.getBoolean(R.styleable.TimePicker_legacyMode, true);
        // Create the correct UI delegate. Legacy mode is used when API Levels is below L
        // release or when it is a TV UI
        final boolean isLegacyMode =  a.getBoolean(
                R.styleable.TimePicker_legacyMode, isLegacyMode());

        a.recycle();

        setLegacyMode(isLegacyMode);
    }

    private boolean shouldForceLegacyMode() {
    private boolean isLegacyMode() {
        UiModeManager uiModeManager =
                (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE);
        if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
            return true;
        }
        final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
        return targetSdkVersion < KITKAT;
        return targetSdkVersion < L;
    }

    private TimePickerDelegate createLegacyUIDelegate(Context context, AttributeSet attrs,
@@ -113,10 +123,7 @@ public class TimePicker extends FrameLayout {
                defStyleRes);
    }

    /**
     * @hide
     */
    public void setLegacyMode(boolean isLegacyMode) {
    private void setLegacyMode(boolean isLegacyMode) {
        removeAllViewsInLayout();
        mDelegate = isLegacyMode ?
                createLegacyUIDelegate(mContext, mAttrs, mDefStyleAttr, mDefStyleRes) :
+2 −10
Original line number Diff line number Diff line
@@ -942,11 +942,7 @@ please see themes_device_defaults.xml.

    <!-- Holo theme for the TimePicker dialog windows, which is used by the
         {@link android.app.TimePickerDialog} class. -->
    <style name="Theme.Holo.Dialog.TimePicker">
        <item name="windowBackground">@color/transparent</item>
        <item name="windowTitleStyle">@style/DialogWindowTitle.Holo</item>
        <item name="windowContentOverlay">@null</item>
    </style>
    <style name="Theme.Holo.Dialog.TimePicker" parent="Theme.Holo.Dialog.Alert" />

    <!-- Theme for a window that will be displayed either full-screen on
         smaller screens (small, normal) or as a dialog on larger screens
@@ -1061,11 +1057,7 @@ please see themes_device_defaults.xml.

    <!-- Holo Light theme for the TimePicker dialog windows, which is used by the
         {@link android.app.TimePickerDialog} class. -->
    <style name="Theme.Holo.Light.Dialog.TimePicker">
        <item name="windowBackground">@color/transparent</item>
        <item name="windowTitleStyle">@style/DialogWindowTitle.Holo.Light</item>
        <item name="windowContentOverlay">@null</item>
    </style>
    <style name="Theme.Holo.Light.Dialog.TimePicker" parent="Theme.Holo.Light.Dialog.Alert" />

    <!-- Theme for a presentation window on a secondary display. -->
    <style name="Theme.Holo.Light.Dialog.Presentation" parent="Theme.Holo.Light.NoActionBar.Fullscreen" />