From 1cc0a2b4e4a66829eeffd7e8cc5dfca7590c0a73 Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Tue, 5 Mar 2019 15:15:47 -0500 Subject: [PATCH] Automated g4 rollback of changelist 236879782 PiperOrigin-RevId: 236896313 --- .../picker/PickerMainDemoFragment.java | 44 +-- .../catalog/picker/res/values/strings.xml | 13 +- .../catalog/tableofcontents/TocModule.java | 2 - .../material/picker/DaysHeaderAdapter.java | 93 ------ .../material/picker/MaterialCalendarView.java | 101 ------- .../picker/MaterialDatePickerDialog.java | 96 ++++-- .../picker/MaterialDatePickerView.java | 87 ------ .../picker/MaterialDateRangePickerDialog.java | 67 ----- .../picker/MaterialDateRangePickerView.java | 123 -------- .../material/picker/MaterialPickerDialog.java | 133 -------- .../MaterialStyledDatePickerDialog.java | 114 ------- .../android/material/picker/MonthInYear.java | 100 ------- .../material/picker/MonthInYearAdapter.java | 149 --------- .../picker/res/layout/date_picker_actions.xml | 40 --- .../res/layout/date_picker_calendar_days.xml | 26 -- .../date_picker_calendar_days_header.xml | 26 -- .../picker/res/layout/date_picker_dialog.xml | 40 --- .../picker/res/layout/date_picker_header.xml | 31 -- .../material/picker/res/values-v21/styles.xml | 1 + .../material/picker/res/values/attrs.xml | 23 -- .../material/picker/res/values/strings.xml | 24 -- .../material/picker/res/values/styles.xml | 4 - .../material/picker/res/values/themes.xml | 4 - .../material/theme/res/values/attrs.xml | 5 - .../material/theme/res/values/themes_base.xml | 12 - .../material/picker/AndroidManifest.xml | 25 -- .../picker/MonthInYearAdapterTest.java | 283 ------------------ .../material/picker/MonthInYearTest.java | 136 --------- 28 files changed, 89 insertions(+), 1713 deletions(-) delete mode 100644 lib/java/com/google/android/material/picker/DaysHeaderAdapter.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialCalendarView.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialDatePickerView.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialDateRangePickerDialog.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialDateRangePickerView.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialPickerDialog.java delete mode 100644 lib/java/com/google/android/material/picker/MaterialStyledDatePickerDialog.java delete mode 100644 lib/java/com/google/android/material/picker/MonthInYear.java delete mode 100644 lib/java/com/google/android/material/picker/MonthInYearAdapter.java delete mode 100644 lib/java/com/google/android/material/picker/res/layout/date_picker_actions.xml delete mode 100644 lib/java/com/google/android/material/picker/res/layout/date_picker_calendar_days.xml delete mode 100644 lib/java/com/google/android/material/picker/res/layout/date_picker_calendar_days_header.xml delete mode 100644 lib/java/com/google/android/material/picker/res/layout/date_picker_dialog.xml delete mode 100644 lib/java/com/google/android/material/picker/res/layout/date_picker_header.xml delete mode 100644 lib/java/com/google/android/material/picker/res/values/attrs.xml delete mode 100644 lib/java/com/google/android/material/picker/res/values/strings.xml delete mode 100644 lib/javatests/com/google/android/material/picker/AndroidManifest.xml delete mode 100644 lib/javatests/com/google/android/material/picker/MonthInYearAdapterTest.java delete mode 100644 lib/javatests/com/google/android/material/picker/MonthInYearTest.java diff --git a/catalog/java/io/material/catalog/picker/PickerMainDemoFragment.java b/catalog/java/io/material/catalog/picker/PickerMainDemoFragment.java index 9bad496a3..585bd76f7 100644 --- a/catalog/java/io/material/catalog/picker/PickerMainDemoFragment.java +++ b/catalog/java/io/material/catalog/picker/PickerMainDemoFragment.java @@ -22,12 +22,8 @@ import android.app.Dialog; import android.os.Bundle; import androidx.annotation.Nullable; import androidx.annotation.StringRes; -import androidx.annotation.StyleRes; import com.google.android.material.button.MaterialButton; import com.google.android.material.picker.MaterialDatePickerDialog; -import com.google.android.material.picker.MaterialDateRangePickerDialog; -import com.google.android.material.picker.MaterialPickerDialog; -import com.google.android.material.picker.MaterialStyledDatePickerDialog; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -50,25 +46,19 @@ public class PickerMainDemoFragment extends DemoFragment { frameworkTodayDatePicker(0)); addDialogLauncher( - dialogLaunchersLayout, R.string.cat_picker_date_material, materialStyledTodayDatePicker(0)); + dialogLaunchersLayout, + R.string.cat_picker_date_material, + materialTodayDatePicker(0)); addDialogLauncher( dialogLaunchersLayout, - R.string.cat_picker_styled_date_spinner, - materialStyledTodayDatePicker(getSpinnerTheme())); + R.string.cat_picker_date_spinner, + materialTodayDatePicker(getSpinnerTheme())); addDialogLauncher( dialogLaunchersLayout, - R.string.cat_picker_styled_date_calendar, - materialStyledTodayDatePicker(getCalendarTheme())); - - addDialogLauncher( - dialogLaunchersLayout, R.string.cat_picker_date_calendar, materialTodayDatePicker()); - - addDialogLauncher( - dialogLaunchersLayout, - R.string.cat_picker_date_range_calendar, - materialTodayDateRangePicker()); + R.string.cat_picker_date_calendar, + materialTodayDatePicker(getCalendarTheme())); return view; } @@ -89,7 +79,7 @@ public class PickerMainDemoFragment extends DemoFragment { viewGroup.addView(dialogLauncherButton); } - protected DatePickerDialog frameworkTodayDatePicker(@StyleRes int themeResId) { + protected DatePickerDialog frameworkTodayDatePicker(int themeResId) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); @@ -97,26 +87,12 @@ public class PickerMainDemoFragment extends DemoFragment { return new DatePickerDialog(getContext(), themeResId, null, year, month, day); } - // This demo is for a transient API. Once the API is set, the RestrictTo will be removed. @SuppressWarnings("RestrictTo") - protected DatePickerDialog materialStyledTodayDatePicker(@StyleRes int themeResId) { + protected DatePickerDialog materialTodayDatePicker(int themeResId) { Calendar calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); - return new MaterialStyledDatePickerDialog(getContext(), themeResId, null, year, month, day); + return new MaterialDatePickerDialog(getContext(), themeResId, null, year, month, day); } - - // This demo is for a transient API. Once the API is set, the RestrictTo will be removed. - @SuppressWarnings("RestrictTo") - protected MaterialPickerDialog materialTodayDatePicker() { - return new MaterialDatePickerDialog(getContext()); - } - - // This demo is for a transient API. Once the API is set, the RestrictTo will be removed. - @SuppressWarnings("RestrictTo") - protected MaterialPickerDialog materialTodayDateRangePicker() { - return new MaterialDateRangePickerDialog(getContext()); - } - } diff --git a/catalog/java/io/material/catalog/picker/res/values/strings.xml b/catalog/java/io/material/catalog/picker/res/values/strings.xml index f7df42194..328f71f6b 100644 --- a/catalog/java/io/material/catalog/picker/res/values/strings.xml +++ b/catalog/java/io/material/catalog/picker/res/values/strings.xml @@ -23,14 +23,9 @@ Default Date Picker - Material Styled Date Picker with no themeResId - - Material Styled Date Picker Spinner - - Material Styled Date Picker Calendar + Material Date Picker with no themeResId + + Material Date picker Spinner - Material Date Picker Calendar - - Material Date Range Picker Calendar - + Material Date picker Calendar diff --git a/catalog/java/io/material/catalog/tableofcontents/TocModule.java b/catalog/java/io/material/catalog/tableofcontents/TocModule.java index 1e9e55ae7..85ea734a9 100644 --- a/catalog/java/io/material/catalog/tableofcontents/TocModule.java +++ b/catalog/java/io/material/catalog/tableofcontents/TocModule.java @@ -30,7 +30,6 @@ import io.material.catalog.dialog.DialogDemoLandingFragment; import io.material.catalog.fab.FabFragment; import io.material.catalog.font.FontFragment; import io.material.catalog.menu.MenuFragment; -import io.material.catalog.picker.PickerDemoLandingFragment; import io.material.catalog.radiobutton.RadioButtonFragment; import io.material.catalog.shapetheming.ShapeThemingFragment; import io.material.catalog.switchmaterial.SwitchFragment; @@ -58,7 +57,6 @@ import io.material.catalog.transformation.TransformationFragment; FabFragment.Module.class, FontFragment.Module.class, MenuFragment.Module.class, - PickerDemoLandingFragment.Module.class, RadioButtonFragment.Module.class, ShapeThemingFragment.Module.class, SwitchFragment.Module.class, diff --git a/lib/java/com/google/android/material/picker/DaysHeaderAdapter.java b/lib/java/com/google/android/material/picker/DaysHeaderAdapter.java deleted file mode 100644 index fd427abc0..000000000 --- a/lib/java/com/google/android/material/picker/DaysHeaderAdapter.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.TextView; -import java.util.Calendar; -import java.util.Locale; - -/** - * A single row adapter representing the days of the week for {@link Calendar}. - * - *

This {@link android.widget.Adapter} respects the {@link Calendar#getFirstDayOfWeek()} - * determined by {@link Locale#getDefault()}. - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class DaysHeaderAdapter extends BaseAdapter { - - private final Calendar calendar; - private final int daysInWeek; - private final int firstDayOfWeek; - /** Style value from Calendar.NARROW_FORMAT unavailable before 1.8 */ - private static final int NARROW_FORMAT = 4; - private static final int CALENDAR_DAY_STYLE = - VERSION.SDK_INT >= VERSION_CODES.O ? NARROW_FORMAT : Calendar.SHORT; - - public DaysHeaderAdapter() { - calendar = Calendar.getInstance(); - daysInWeek = calendar.getMaximum(Calendar.DAY_OF_WEEK); - firstDayOfWeek = calendar.getFirstDayOfWeek(); - } - - @Override - public Integer getItem(int position) { - if (position >= daysInWeek) { - return null; - } - return positionToDayOfWeek(position); - } - - @Override - public long getItemId(int position) { - // There is only 1 row - return 0; - } - - @Override - public int getCount() { - return daysInWeek; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - TextView day = (TextView) convertView; - if (convertView == null) { - day = new TextView(parent.getContext()); - } - calendar.set(Calendar.DAY_OF_WEEK, positionToDayOfWeek(position)); - day.setText( - calendar.getDisplayName(Calendar.DAY_OF_WEEK, CALENDAR_DAY_STYLE, Locale.getDefault())); - return day; - } - - private int positionToDayOfWeek(int position) { - // Day Constants start at 1 - int dayConstant = position + firstDayOfWeek; - if (dayConstant > daysInWeek) { - dayConstant = dayConstant - daysInWeek; - } - return dayConstant; - } -} diff --git a/lib/java/com/google/android/material/picker/MaterialCalendarView.java b/lib/java/com/google/android/material/picker/MaterialCalendarView.java deleted file mode 100644 index 6984ab66a..000000000 --- a/lib/java/com/google/android/material/picker/MaterialCalendarView.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.content.Context; -import android.os.Build.VERSION; -import android.os.Build.VERSION_CODES; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import androidx.appcompat.widget.LinearLayoutCompat; -import android.util.AttributeSet; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.GridView; -import android.widget.LinearLayout; -import java.util.Calendar; - -/** - * View for a days of week {@link Calendar} represented as a header row of days labels and {@link - * GridView} of days backed by {@link MonthInYearAdapter}. - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public abstract class MaterialCalendarView extends LinearLayoutCompat { - - private final MonthInYearAdapter monthInYearAdapter; - - public MaterialCalendarView(Context context) { - this(context, null); - } - - public MaterialCalendarView(Context context, AttributeSet attrs) { - this(context, attrs, 0); - } - - public MaterialCalendarView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - setOrientation(LinearLayout.VERTICAL); - - LayoutInflater layoutInflater = LayoutInflater.from(context); - layoutInflater.inflate(R.layout.date_picker_calendar_days_header, this); - layoutInflater.inflate(R.layout.date_picker_calendar_days, this); - GridView daysHeader = findViewById(R.id.date_picker_calendar_days_header); - GridView daysGrid = findViewById(R.id.date_picker_calendar_days); - - Calendar calendar = Calendar.getInstance(); - MonthInYear monthInYear = - MonthInYear.create(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)); - monthInYearAdapter = new MonthInYearAdapter(context, monthInYear); - - daysHeader.setAdapter(new DaysHeaderAdapter()); - daysHeader.setNumColumns(monthInYear.daysInWeek); - daysGrid.setAdapter(monthInYearAdapter); - daysGrid.setNumColumns(monthInYear.daysInWeek); - daysGrid.setOnItemClickListener( - new OnItemClickListener() { - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - getOnItemClickListener().onItemClick(parent, view, position, id); - drawSelection(parent); - // Allows users of MaterialCalendarView to set an OnClickListener - if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { - callOnClick(); - } else { - performClick(); - } - } - }); - } - - protected final MonthInYearAdapter getMonthInYearAdapter() { - return monthInYearAdapter; - } - - @Nullable - protected abstract S getSelection(); - - protected abstract void drawSelection(AdapterView parent); - - protected abstract OnItemClickListener getOnItemClickListener(); -} diff --git a/lib/java/com/google/android/material/picker/MaterialDatePickerDialog.java b/lib/java/com/google/android/material/picker/MaterialDatePickerDialog.java index 080c82726..07c09f41b 100644 --- a/lib/java/com/google/android/material/picker/MaterialDatePickerDialog.java +++ b/lib/java/com/google/android/material/picker/MaterialDatePickerDialog.java @@ -18,45 +18,97 @@ package com.google.android.material.picker; import com.google.android.material.R; -import android.app.Dialog; +import android.app.DatePickerDialog; import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Color; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.os.Build.VERSION_CODES; +import android.os.Bundle; +import androidx.annotation.AttrRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.RestrictTo; import androidx.annotation.RestrictTo.Scope; -import java.util.Calendar; +import androidx.annotation.StyleRes; +import com.google.android.material.dialog.InsetDialogOnTouchListener; +import com.google.android.material.dialog.MaterialDialogs; +import com.google.android.material.resources.MaterialAttributes; +import com.google.android.material.shape.MaterialShapeDrawable; +import android.util.TypedValue; /** - * A {@link Dialog} with a header, {@link MaterialDatePickerView}, and set of actions. + * A Material version of {@link android.app.DatePickerDialog} * * @hide */ @RestrictTo(Scope.LIBRARY_GROUP) -public class MaterialDatePickerDialog extends MaterialPickerDialog { +public class MaterialDatePickerDialog extends DatePickerDialog { - private final MaterialDatePickerView materialDatePicker; + @AttrRes private static final int DEF_STYLE_ATTR = android.R.attr.datePickerStyle; - public MaterialDatePickerDialog(Context context) { + @StyleRes + private static final int DEF_STYLE_RES = + R.style.MaterialAlertDialog_MaterialComponents_Picker_Date_Spinner; + + private final Drawable background; + private final Rect backgroundInsets; + + public MaterialDatePickerDialog(@NonNull Context context) { this(context, 0); } - public MaterialDatePickerDialog(Context context, int themeResId) { - super(context, getThemeResource(context, R.attr.materialDatePickerDialogTheme, themeResId)); - // Ensure we are using the correctly themed context rather than the context that was passed in. + public MaterialDatePickerDialog(@NonNull Context context, int themeResId) { + this(context, themeResId, null, -1, -1, -1); + } + + public MaterialDatePickerDialog( + @NonNull Context context, + @Nullable OnDateSetListener listener, + int year, + int month, + int dayOfMonth) { + this(context, 0, listener, year, month, dayOfMonth); + } + + public MaterialDatePickerDialog( + @NonNull Context context, + int themeResId, + @Nullable OnDateSetListener listener, + int year, + int monthOfYear, + int dayOfMonth) { + + super(context, themeResId, listener, year, monthOfYear, dayOfMonth); context = getContext(); - materialDatePicker = new MaterialDatePickerView(context); - } - @Override - protected MaterialCalendarView getMaterialCalendarView() { - return materialDatePicker; - } + TypedValue colorSurfaceValue = + MaterialAttributes.resolveAttributeOrThrow( + getContext(), R.attr.colorSurface, getClass().getCanonicalName()); + int surfaceColor = colorSurfaceValue.data; - @Override - protected String getHeaderText() { - Calendar selectedDate = materialDatePicker.getSelection(); - if (selectedDate == null) { - return getContext().getResources().getString(R.string.header_prompt); + MaterialShapeDrawable materialShapeDrawable = + new MaterialShapeDrawable(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES); + // Pre-L, windowBackground is a second background behind the Picker Dialog. + if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { + materialShapeDrawable.setFillColor(ColorStateList.valueOf(surfaceColor)); + } else { + materialShapeDrawable.setFillColor(ColorStateList.valueOf(Color.TRANSPARENT)); } - String startString = getSimpleDateFormat().format(selectedDate.getTime()); - return getContext().getResources().getString(R.string.header_selected, startString); + + backgroundInsets = + MaterialDialogs.getDialogBackgroundInsets(context, DEF_STYLE_ATTR, DEF_STYLE_RES); + background = MaterialDialogs.insetDrawable(materialShapeDrawable, backgroundInsets); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + getWindow().setBackgroundDrawable(background); + getWindow() + .getDecorView() + .setOnTouchListener(new InsetDialogOnTouchListener(this, backgroundInsets)); } } diff --git a/lib/java/com/google/android/material/picker/MaterialDatePickerView.java b/lib/java/com/google/android/material/picker/MaterialDatePickerView.java deleted file mode 100644 index 2167e38e1..000000000 --- a/lib/java/com/google/android/material/picker/MaterialDatePickerView.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import androidx.core.view.ViewCompat; -import android.util.AttributeSet; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import java.util.Calendar; - -/** - * A {@link MaterialCalendarView} that supports single date selection - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MaterialDatePickerView extends MaterialCalendarView { - - private static final int DEF_STYLE_ATTR = R.attr.materialDatePickerStyle; - private static final ColorDrawable emptyColor = new ColorDrawable(Color.TRANSPARENT); - private static final ColorDrawable selectedColor = new ColorDrawable(Color.RED); - - private int selectedPosition = -1; - private final OnItemClickListener onItemClickListener; - - public MaterialDatePickerView(Context context) { - this(context, null); - } - - public MaterialDatePickerView(Context context, AttributeSet attrs) { - this(context, attrs, DEF_STYLE_ATTR); - } - - public MaterialDatePickerView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - onItemClickListener = - new OnItemClickListener() { - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - if (!getMonthInYearAdapter().withinMonth(position)) { - return; - } - selectedPosition = position; - } - }; - } - - @Override - protected OnItemClickListener getOnItemClickListener() { - return onItemClickListener; - } - - @Override - protected void drawSelection(AdapterView parent) { - for (int i = 0; i < parent.getCount(); i++) { - ViewCompat.setBackground( - parent.getChildAt(i), i == selectedPosition ? selectedColor : emptyColor); - } - } - - @Override - public Calendar getSelection() { - return getMonthInYearAdapter().getItem(selectedPosition); - } -} diff --git a/lib/java/com/google/android/material/picker/MaterialDateRangePickerDialog.java b/lib/java/com/google/android/material/picker/MaterialDateRangePickerDialog.java deleted file mode 100644 index a1b863deb..000000000 --- a/lib/java/com/google/android/material/picker/MaterialDateRangePickerDialog.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.app.Dialog; -import android.content.Context; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import android.util.Pair; -import java.util.Calendar; - -/** - * A {@link Dialog} with a header, {@link MaterialDateRangePickerView}, and set of actions. - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MaterialDateRangePickerDialog extends MaterialPickerDialog> { - - private final MaterialDateRangePickerView materialDateRangePicker; - - public MaterialDateRangePickerDialog(Context context) { - this(context, 0); - } - - public MaterialDateRangePickerDialog(Context context, int themeResId) { - super( - context, getThemeResource(context, R.attr.materialDateRangePickerDialogTheme, themeResId)); - // Ensure we are using the correctly themed context rather than the context that was passed in. - context = getContext(); - materialDateRangePicker = new MaterialDateRangePickerView(context); - } - - @Override - protected MaterialCalendarView> getMaterialCalendarView() { - return materialDateRangePicker; - } - - @Override - protected String getHeaderText() { - Pair startAndEnd = materialDateRangePicker.getSelection(); - if (startAndEnd == null) { - return getContext().getResources().getString(R.string.range_header_prompt); - } - String startString = getSimpleDateFormat().format(startAndEnd.first.getTime()); - String endString = getSimpleDateFormat().format(startAndEnd.second.getTime()); - return getContext() - .getResources() - .getString(R.string.range_header_selected, startString, endString); - } -} diff --git a/lib/java/com/google/android/material/picker/MaterialDateRangePickerView.java b/lib/java/com/google/android/material/picker/MaterialDateRangePickerView.java deleted file mode 100644 index c30f6510b..000000000 --- a/lib/java/com/google/android/material/picker/MaterialDateRangePickerView.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.content.Context; -import android.graphics.Color; -import android.graphics.drawable.ColorDrawable; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import androidx.core.view.ViewCompat; -import android.util.AttributeSet; -import android.util.Pair; -import android.view.View; -import android.widget.AdapterView; -import android.widget.AdapterView.OnItemClickListener; -import java.util.Calendar; - -/** - * A {@link MaterialCalendarView} that supports range selection - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MaterialDateRangePickerView extends MaterialCalendarView> { - - private static final int DEF_STYLE_ATTR = R.attr.materialDateRangePickerStyle; - - private static final ColorDrawable emptyColor = new ColorDrawable(Color.TRANSPARENT); - private static final ColorDrawable startColor = new ColorDrawable(Color.RED); - private static final ColorDrawable endColor = new ColorDrawable(Color.GREEN); - private static final ColorDrawable rangeColor = new ColorDrawable(Color.YELLOW); - - private final OnItemClickListener onItemClickListener; - private int selectedStartPosition = -1; - private int selectedEndPosition = -1; - - public MaterialDateRangePickerView(Context context) { - this(context, null); - } - - public MaterialDateRangePickerView(Context context, AttributeSet attrs) { - this(context, attrs, DEF_STYLE_ATTR); - } - - public MaterialDateRangePickerView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - onItemClickListener = - new OnItemClickListener() { - - @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - if (!getMonthInYearAdapter().withinMonth(position)) { - return; - } - if (selectedStartPosition < 0) { - selectedStartPosition = position; - } else if (selectedEndPosition < 0 && position > selectedStartPosition) { - selectedEndPosition = position; - } else { - selectedEndPosition = -1; - selectedStartPosition = position; - } - } - }; - } - - @Override - protected OnItemClickListener getOnItemClickListener() { - return onItemClickListener; - } - - @Override - protected void drawSelection(AdapterView parent) { - for (int i = 0; i < parent.getCount(); i++) { - ColorDrawable setColor = emptyColor; - if (i == selectedStartPosition) { - setColor = startColor; - } else if (i == selectedEndPosition) { - setColor = endColor; - } else if (i > selectedStartPosition && i < selectedEndPosition) { - setColor = rangeColor; - } - ViewCompat.setBackground(parent.getChildAt(i), setColor); - } - } - - @Override - @Nullable - public Pair getSelection() { - Calendar start = getStart(); - Calendar end = getEnd(); - if (start == null || end == null) { - return null; - } - return new Pair<>(getStart(), getEnd()); - } - - @Nullable - public Calendar getStart() { - return getMonthInYearAdapter().getItem(selectedStartPosition); - } - - @Nullable - public Calendar getEnd() { - return getMonthInYearAdapter().getItem(selectedEndPosition); - } -} diff --git a/lib/java/com/google/android/material/picker/MaterialPickerDialog.java b/lib/java/com/google/android/material/picker/MaterialPickerDialog.java deleted file mode 100644 index 6337528fc..000000000 --- a/lib/java/com/google/android/material/picker/MaterialPickerDialog.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.app.Dialog; -import android.content.Context; -import android.os.Bundle; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import androidx.annotation.StyleRes; -import android.util.TypedValue; -import android.view.View; -import android.widget.Button; -import android.widget.FrameLayout; -import android.widget.TextView; -import java.text.SimpleDateFormat; -import java.util.Locale; - -/** - * A {@link Dialog} with a header, {@link MaterialCalendarView}, and set of actions. - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public abstract class MaterialPickerDialog extends Dialog { - - private SimpleDateFormat simpleDateFormat = - new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault()); - - private TextView header; - private S selection; - private MaterialCalendarView materialCalendarView; - - @StyleRes - protected static final int getThemeResource( - Context context, int defaultThemeAttr, int themeResId) { - if (themeResId != 0) { - return themeResId; - } - TypedValue outValue = new TypedValue(); - context.getTheme().resolveAttribute(defaultThemeAttr, outValue, true); - return outValue.resourceId; - } - - public MaterialPickerDialog(Context context) { - this(context, 0); - } - - public MaterialPickerDialog(Context context, int themeResId) { - super(context, themeResId); - } - - /** - * Returns a {@link S} instance representing the selection or null if the user has not confirmed a - * selection. - */ - @Nullable - public final S getSelection() { - return selection; - } - - @Override - protected final void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - materialCalendarView = getMaterialCalendarView(); - - setContentView(R.layout.date_picker_dialog); - header = findViewById(R.id.date_picker_header_title); - FrameLayout calendarViewFrame = findViewById(R.id.date_picker_calendar_view_frame); - calendarViewFrame.addView(materialCalendarView); - materialCalendarView.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - updateHeader(); - } - }); - - Button confirmButton = findViewById(R.id.confirm_button); - Button cancelButton = findViewById(R.id.cancel_button); - - confirmButton.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - selection = materialCalendarView.getSelection(); - dismiss(); - } - }); - cancelButton.setOnClickListener( - new View.OnClickListener() { - @Override - public void onClick(View v) { - selection = null; - cancel(); - } - }); - updateHeader(); - } - - private void updateHeader() { - header.setText(getHeaderText()); - } - - public final void setSimpleDateFormat(SimpleDateFormat simpleDateFormat) { - this.simpleDateFormat = simpleDateFormat; - } - - public final SimpleDateFormat getSimpleDateFormat() { - return simpleDateFormat; - } - - protected abstract String getHeaderText(); - - protected abstract MaterialCalendarView getMaterialCalendarView(); -} diff --git a/lib/java/com/google/android/material/picker/MaterialStyledDatePickerDialog.java b/lib/java/com/google/android/material/picker/MaterialStyledDatePickerDialog.java deleted file mode 100644 index 404d923ef..000000000 --- a/lib/java/com/google/android/material/picker/MaterialStyledDatePickerDialog.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.app.DatePickerDialog; -import android.content.Context; -import android.content.res.ColorStateList; -import android.graphics.Color; -import android.graphics.Rect; -import android.graphics.drawable.Drawable; -import android.os.Build; -import android.os.Build.VERSION_CODES; -import android.os.Bundle; -import androidx.annotation.AttrRes; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import androidx.annotation.StyleRes; -import com.google.android.material.dialog.InsetDialogOnTouchListener; -import com.google.android.material.dialog.MaterialDialogs; -import com.google.android.material.resources.MaterialAttributes; -import com.google.android.material.shape.MaterialShapeDrawable; -import android.util.TypedValue; - -/** - * A Material version of {@link android.app.DatePickerDialog} - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MaterialStyledDatePickerDialog extends DatePickerDialog { - - @AttrRes private static final int DEF_STYLE_ATTR = android.R.attr.datePickerStyle; - - @StyleRes - private static final int DEF_STYLE_RES = - R.style.MaterialAlertDialog_MaterialComponents_Picker_Date_Spinner; - - private final Drawable background; - private final Rect backgroundInsets; - - public MaterialStyledDatePickerDialog(@NonNull Context context) { - this(context, 0); - } - - public MaterialStyledDatePickerDialog(@NonNull Context context, int themeResId) { - this(context, themeResId, null, -1, -1, -1); - } - - public MaterialStyledDatePickerDialog( - @NonNull Context context, - @Nullable OnDateSetListener listener, - int year, - int month, - int dayOfMonth) { - this(context, 0, listener, year, month, dayOfMonth); - } - - public MaterialStyledDatePickerDialog( - @NonNull Context context, - int themeResId, - @Nullable OnDateSetListener listener, - int year, - int monthOfYear, - int dayOfMonth) { - - super(context, themeResId, listener, year, monthOfYear, dayOfMonth); - context = getContext(); - - TypedValue colorSurfaceValue = - MaterialAttributes.resolveAttributeOrThrow( - getContext(), R.attr.colorSurface, getClass().getCanonicalName()); - int surfaceColor = colorSurfaceValue.data; - - MaterialShapeDrawable materialShapeDrawable = - new MaterialShapeDrawable(context, null, DEF_STYLE_ATTR, DEF_STYLE_RES); - // Pre-L, windowBackground is a second background behind the Picker Dialog. - if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { - materialShapeDrawable.setFillColor(ColorStateList.valueOf(surfaceColor)); - } else { - materialShapeDrawable.setFillColor(ColorStateList.valueOf(Color.TRANSPARENT)); - } - - backgroundInsets = - MaterialDialogs.getDialogBackgroundInsets(context, DEF_STYLE_ATTR, DEF_STYLE_RES); - background = MaterialDialogs.insetDrawable(materialShapeDrawable, backgroundInsets); - } - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - getWindow().setBackgroundDrawable(background); - getWindow() - .getDecorView() - .setOnTouchListener(new InsetDialogOnTouchListener(this, backgroundInsets)); - } -} diff --git a/lib/java/com/google/android/material/picker/MonthInYear.java b/lib/java/com/google/android/material/picker/MonthInYear.java deleted file mode 100644 index 780714878..000000000 --- a/lib/java/com/google/android/material/picker/MonthInYear.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import java.util.Arrays; -import java.util.Calendar; - -/** - * Contains convenience operations for a month within a specific year. - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MonthInYear { - - private final Calendar monthInYear; - public final int month; - public final int year; - public final int daysInWeek; - public final int daysInMonth; - - private MonthInYear(Calendar calendar) { - this.monthInYear = calendar; - monthInYear.set(Calendar.DAY_OF_MONTH, 1); - month = calendar.get(Calendar.MONTH); - year = calendar.get(Calendar.YEAR); - daysInWeek = monthInYear.getMaximum(Calendar.DAY_OF_WEEK); - daysInMonth = monthInYear.getActualMaximum(Calendar.DAY_OF_MONTH); - } - - /** - * Creates an instance of MonthInYear with the given parameters backed by a {@link Calendar}. - * - * @param year The year - * @param month The 0-index based month. Use {@link Calendar} constants (e.g., {@link - * Calendar#JANUARY} - * @return A MonthInYear object backed by a new {@link Calendar} instance - */ - public static MonthInYear create(int year, int month) { - Calendar calendar = Calendar.getInstance(); - calendar.set(Calendar.YEAR, year); - calendar.set(Calendar.MONTH, month); - return new MonthInYear(calendar); - } - - public int daysFromStartOfWeekToFirstOfMonth() { - int difference = monthInYear.get(Calendar.DAY_OF_WEEK) - monthInYear.getFirstDayOfWeek(); - if (difference < 0) { - difference = difference + daysInWeek; - } - return difference; - } - - /** - * Gets a {@link Calendar} for the specific day within the instance's month and year. - * - *

This method only guarantees validity with respect to {@link Calendar#isLenient()}. - * - * @param day The desired day within this month and year - * @return A new {@link Calendar} instance for the given day within the specified month and year - */ - public Calendar getDay(int day) { - Calendar calendar = ((Calendar) monthInYear.clone()); - calendar.set(Calendar.DAY_OF_MONTH, day); - return calendar; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (!(o instanceof MonthInYear)) { - return false; - } - MonthInYear that = (MonthInYear) o; - return month == that.month && year == that.year; - } - - @Override - public int hashCode() { - Object[] hashedFields = {month, year}; - return Arrays.hashCode(hashedFields); - } -} diff --git a/lib/java/com/google/android/material/picker/MonthInYearAdapter.java b/lib/java/com/google/android/material/picker/MonthInYearAdapter.java deleted file mode 100644 index c7abb36c6..000000000 --- a/lib/java/com/google/android/material/picker/MonthInYearAdapter.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright 2019 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.google.android.material.picker; - -import com.google.android.material.R; - -import android.content.Context; -import androidx.annotation.Nullable; -import androidx.annotation.RestrictTo; -import androidx.annotation.RestrictTo.Scope; -import com.google.android.material.resources.MaterialAttributes; -import android.util.TypedValue; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; -import android.widget.TextView; -import java.util.Calendar; - -/** - * Represents the days of a month with {@link TextView} instances for each day. - * - *

The number of rows is always equal to the maximum number of weeks that can exist across all - * months (e.g., 6 for the GregorianCalendar). - * - * @hide - */ -@RestrictTo(Scope.LIBRARY_GROUP) -public class MonthInYearAdapter extends BaseAdapter { - - private static final int MAXIMUM_WEEKS = - Calendar.getInstance().getMaximum(Calendar.WEEK_OF_MONTH); - - private final MonthInYear monthInYear; - private final int textViewSize; - - MonthInYearAdapter(Context context, MonthInYear monthInYear) { - this.monthInYear = monthInYear; - TypedValue minTouchTargetSizeValue = - MaterialAttributes.resolveAttribute(context, R.attr.minTouchTargetSize); - if (minTouchTargetSizeValue == null) { - textViewSize = (int) context.getResources().getDimension(R.dimen.mtrl_min_touch_target_size); - } else { - textViewSize = - (int) minTouchTargetSizeValue.getDimension(context.getResources().getDisplayMetrics()); - } - } - - /** - * Returns a {@link Calendar} object for the given grid position - * - * @param position Index for the item. 0 matches the {@link Calendar#getFirstDayOfWeek()} for the - * first week of the month represented by {@link MonthInYear}. - * @return An {@link Calendar} representing the Day at the position or null if the position does - * not represent a valid day in the month. - */ - @Nullable - @Override - public Calendar getItem(int position) { - if (position < monthInYear.daysFromStartOfWeekToFirstOfMonth() - || position > lastPositionInMonth()) { - return null; - } - return monthInYear.getDay(positionToDay(position)); - } - - @Override - public long getItemId(int position) { - return position / monthInYear.daysInWeek; - } - - /** - * Returns the number of days in a week times the maximum number of weeks in any possible month. - * This number is at least the number of days in the month. - * - * @return The maximum valid position index - */ - @Override - public int getCount() { - return monthInYear.daysInWeek * MAXIMUM_WEEKS; - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - TextView day = (TextView) convertView; - if (convertView == null) { - day = new TextView(parent.getContext()); - day.setHeight(textViewSize); - } - int offsetPosition = position - firstPositionInMonth(); - if (offsetPosition < 0 || offsetPosition >= monthInYear.daysInMonth) { - day.setVisibility(View.INVISIBLE); - } else { - day.setText(String.valueOf(offsetPosition + 1)); - day.setVisibility(View.VISIBLE); - } - return day; - } - - /** - * Returns the index of the first position which is part of the month. - * - *

For example, this returns the position index representing February 1st. Since position 0 - * represents a day which must be the first day of the week, the first position in the month may - * be greater than 0. - */ - public int firstPositionInMonth() { - return monthInYear.daysFromStartOfWeekToFirstOfMonth(); - } - - /** - * Returns the index of the last position which is part of the month. - * - *

For example, this returns the position index representing November 30th. Since position 0 - * represents a day which must be the first day of the week, the last position in the month may - * not match the number of days in the month. - */ - public int lastPositionInMonth() { - return monthInYear.daysFromStartOfWeekToFirstOfMonth() + monthInYear.daysInMonth - 1; - } - - /** - * Returns the day representing the provided adapter index - * - * @param position The adapter index - * @return The day corresponding to the adapter index. May be non-positive for position inputs - * less than {@link MonthInYearAdapter#firstPositionInMonth()}. - */ - public int positionToDay(int position) { - return position - monthInYear.daysFromStartOfWeekToFirstOfMonth() + 1; - } - - /** True when a provided adapter position is within the calendar month */ - public boolean withinMonth(int position) { - return position >= firstPositionInMonth() && position <= lastPositionInMonth(); - } -} diff --git a/lib/java/com/google/android/material/picker/res/layout/date_picker_actions.xml b/lib/java/com/google/android/material/picker/res/layout/date_picker_actions.xml deleted file mode 100644 index 5f831a781..000000000 --- a/lib/java/com/google/android/material/picker/res/layout/date_picker_actions.xml +++ /dev/null @@ -1,40 +0,0 @@ - - - - - -