mirror of
https://github.com/material-components/material-components-android.git
synced 2026-02-20 08:39:55 +08:00
Automated g4 rollback of changelist 236879782
PiperOrigin-RevId: 236896313
This commit is contained in:
parent
8c9f9d5e01
commit
1cc0a2b4e4
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -23,14 +23,9 @@
|
||||
<!-- Launches a window with the default date picker used if the client makes no setting changes. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_base">Default Date Picker</string>
|
||||
<!-- Launches a window with a Material date picker used if the client just changes the class. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_date_material">Material Styled Date Picker with no themeResId</string>
|
||||
<!-- Launches a window with a styled android spinner based date picker. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_styled_date_spinner">Material Styled Date Picker Spinner</string>
|
||||
<!-- Launches a window with a styled android calendar (days of the week) based layout. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_styled_date_calendar">Material Styled Date Picker Calendar</string>
|
||||
<string name="cat_picker_date_material">Material Date Picker with no themeResId</string>
|
||||
<!-- Launches a window with a spinner based date picker. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_date_spinner">Material Date picker Spinner</string>
|
||||
<!-- Launches a window with a calendar (days of the week) based layout. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_date_calendar">Material Date Picker Calendar</string>
|
||||
<!-- Launches a window with a calendar (days of the week) based layout for choosing a range of dates. [CHAR LIMIT=60] -->
|
||||
<string name="cat_picker_date_range_calendar">Material Date Range Picker Calendar</string>
|
||||
|
||||
<string name="cat_picker_date_calendar">Material Date picker Calendar</string>
|
||||
</resources>
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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}.
|
||||
*
|
||||
* <p>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;
|
||||
}
|
||||
}
|
||||
@ -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<S> 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();
|
||||
}
|
||||
@ -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<Calendar> {
|
||||
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<Calendar> 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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Calendar> {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -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<Pair<Calendar, Calendar>> {
|
||||
|
||||
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<Pair<Calendar, Calendar>> getMaterialCalendarView() {
|
||||
return materialDateRangePicker;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getHeaderText() {
|
||||
Pair<Calendar, Calendar> 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);
|
||||
}
|
||||
}
|
||||
@ -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<Pair<Calendar, Calendar>> {
|
||||
|
||||
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<Calendar, Calendar> 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);
|
||||
}
|
||||
}
|
||||
@ -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<S> extends Dialog {
|
||||
|
||||
private SimpleDateFormat simpleDateFormat =
|
||||
new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
|
||||
|
||||
private TextView header;
|
||||
private S selection;
|
||||
private MaterialCalendarView<? extends S> 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<? extends S> getMaterialCalendarView();
|
||||
}
|
||||
@ -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));
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
*
|
||||
* <p>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);
|
||||
}
|
||||
}
|
||||
@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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();
|
||||
}
|
||||
}
|
||||
@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 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.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/date_picker_actions"
|
||||
android:gravity="end"
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<Button
|
||||
style="?attr/android:buttonBarNegativeButtonStyle"
|
||||
android:id="@+id/cancel_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/cancel"/>
|
||||
|
||||
<Button
|
||||
style="?attr/android:buttonBarPositiveButtonStyle"
|
||||
android:id="@+id/confirm_button"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/confirm"/>
|
||||
|
||||
</LinearLayout>
|
||||
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
|
||||
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/date_picker_calendar_days"
|
||||
android:textAppearance="?attr/textAppearanceBody1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:columnWidth="?attr/minTouchTargetSize"
|
||||
android:gravity="center"
|
||||
android:horizontalSpacing="0dp"
|
||||
android:verticalSpacing="0dp"/>
|
||||
@ -1,26 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
|
||||
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/date_picker_calendar_days_header"
|
||||
android:textAppearance="?attr/textAppearanceSubtitle1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:columnWidth="?attr/minTouchTargetSize"
|
||||
android:gravity="center"
|
||||
android:horizontalSpacing="0dp"
|
||||
android:verticalSpacing="0dp"/>
|
||||
@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 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.
|
||||
-->
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<include
|
||||
layout="@layout/date_picker_header"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"/>
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/date_picker_calendar_view_frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"/>
|
||||
|
||||
<include
|
||||
layout="@layout/date_picker_actions"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"/>
|
||||
|
||||
</LinearLayout>
|
||||
@ -1,31 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (C) 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.
|
||||
-->
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/date_picker_header"
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/date_picker_header_title"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="?attr/textAppearanceHeadline6"/>
|
||||
|
||||
</LinearLayout>
|
||||
@ -34,6 +34,7 @@
|
||||
<item name="android:headerMonthTextAppearance">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Header.Text</item>
|
||||
<item name="android:headerDayOfMonthTextAppearance">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Header.Text.Day</item>
|
||||
<item name="android:headerYearTextAppearance">@style/ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Header.Text</item>
|
||||
|
||||
</style>
|
||||
|
||||
<style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Header.Text" parent="TextAppearance.MaterialComponents.Headline6">
|
||||
|
||||
@ -1,23 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 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.
|
||||
-->
|
||||
<resources>
|
||||
<attr name="materialDatePickerStyle" format="reference"/>
|
||||
<attr name="materialDateRangePickerStyle" format="reference"/>
|
||||
<attr name="materialDatePickerDialogTheme" format="reference"/>
|
||||
<attr name="materialDateRangePickerDialogTheme" format="reference"/>
|
||||
|
||||
</resources>
|
||||
@ -1,24 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
~ Copyright (C) 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.
|
||||
-->
|
||||
<resources>
|
||||
<string name="range_header_prompt" description="Indicates that the user must take the action of picking dates within the calendar to form a range">Pick a Date Range</string>
|
||||
<string name="header_prompt" description="Indicates that the user must take the action of picking a date within the calendar">Pick a Date</string>
|
||||
<string name="range_header_selected">%1$s - %2$s</string>
|
||||
<string name="header_selected">%1$s</string>
|
||||
<string name="confirm" description="Button text to indicate that the widget will save the user's selection">Save</string>
|
||||
<string name="cancel" description="Button text to indicate that the widget will ignore the user's selection">Cancel</string>
|
||||
</resources>
|
||||
@ -28,8 +28,4 @@
|
||||
<item name="backgroundInsetBottom">@dimen/mtrl_alert_dialog_picker_background_inset</item>
|
||||
<item name="shapeAppearance">?attr/shapeAppearanceMediumComponent</item>
|
||||
</style>
|
||||
|
||||
<style name="Widget.MaterialComponents.MaterialCalendarView" parent="android:Widget"/>
|
||||
<style name="Widget.MaterialComponents.DateRangePicker" parent ="Widget.MaterialComponents.MaterialCalendarView"/>
|
||||
<style name="Widget.MaterialComponents.DatePicker" parent ="Widget.MaterialComponents.MaterialCalendarView"/>
|
||||
</resources>
|
||||
|
||||
@ -20,8 +20,4 @@
|
||||
<style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date" parent="ThemeOverlay.MaterialComponents.MaterialAlertDialog"/>
|
||||
<style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Spinner"/>
|
||||
<style name="ThemeOverlay.MaterialComponents.MaterialAlertDialog.Picker.Date.Calendar"/>
|
||||
|
||||
<style name="ThemeOverlay.MaterialComponents.MaterialCalendarView" parent="ThemeOverlay.MaterialComponents.Dialog"/>
|
||||
<style name="ThemeOverlay.MaterialComponents.DatePicker" parent="ThemeOverlay.MaterialComponents.MaterialCalendarView"/>
|
||||
<style name="ThemeOverlay.MaterialComponents.DateRangePicker" parent="ThemeOverlay.MaterialComponents.MaterialCalendarView"/>
|
||||
</resources>
|
||||
|
||||
@ -88,11 +88,6 @@
|
||||
<attr name="extendedFloatingActionButtonStyle"/>
|
||||
<!-- Style to use for FloatingActionButtons in this theme. -->
|
||||
<attr name="floatingActionButtonStyle"/>
|
||||
<!-- Style to use for MaterialCalendarView in this theme. -->
|
||||
<attr name="materialDatePickerStyle"/>
|
||||
<attr name="materialDateRangePickerStyle"/>
|
||||
<attr name="materialDatePickerDialogTheme"/>
|
||||
<attr name="materialDateRangePickerDialogTheme"/>
|
||||
<!-- Style to use for MaterialCardView in this theme. -->
|
||||
<attr name="materialCardViewStyle"/>
|
||||
<!-- Style to use for NavigationView in this theme. -->
|
||||
|
||||
@ -70,12 +70,6 @@
|
||||
<!-- Type styles -->
|
||||
<item name="textAppearanceLargePopupMenu">?attr/textAppearanceSubtitle1</item>
|
||||
<item name="textAppearanceSmallPopupMenu">?attr/textAppearanceSubtitle1</item>
|
||||
|
||||
<!-- Picker styles and themes. -->
|
||||
<item name="materialDatePickerStyle">@style/Widget.MaterialComponents.DatePicker</item>
|
||||
<item name="materialDateRangePickerStyle">@style/Widget.MaterialComponents.DateRangePicker</item>
|
||||
<item name="materialDatePickerDialogTheme">@style/ThemeOverlay.MaterialComponents.DatePicker</item>
|
||||
<item name="materialDateRangePickerDialogTheme">@style/ThemeOverlay.MaterialComponents.DateRangePicker</item>
|
||||
</style>
|
||||
|
||||
<style name="Base.V14.Theme.MaterialComponents.Light" parent="Base.V14.Theme.MaterialComponents.Light.Bridge">
|
||||
@ -129,12 +123,6 @@
|
||||
<!-- Type styles -->
|
||||
<item name="textAppearanceLargePopupMenu">?attr/textAppearanceSubtitle1</item>
|
||||
<item name="textAppearanceSmallPopupMenu">?attr/textAppearanceSubtitle1</item>
|
||||
|
||||
<!-- Picker styles and themes. -->
|
||||
<item name="materialDatePickerStyle">@style/Widget.MaterialComponents.DatePicker</item>
|
||||
<item name="materialDateRangePickerStyle">@style/Widget.MaterialComponents.DateRangePicker</item>
|
||||
<item name="materialDatePickerDialogTheme">@style/ThemeOverlay.MaterialComponents.DatePicker</item>
|
||||
<item name="materialDateRangePickerDialogTheme">@style/ThemeOverlay.MaterialComponents.DateRangePicker</item>
|
||||
</style>
|
||||
|
||||
<!-- Themes in the "Base.Theme" family vary based on the current platform
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
<?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.
|
||||
-->
|
||||
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
package="com.google.android.material.picker">
|
||||
|
||||
<uses-sdk
|
||||
tools:overrideLibrary="androidx.test.core"/>
|
||||
|
||||
<application/>
|
||||
</manifest>
|
||||
@ -1,283 +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 static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import android.content.Context;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.internal.DoNotInstrument;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@DoNotInstrument
|
||||
public class MonthInYearAdapterTest {
|
||||
|
||||
private static final Locale ISRAEL = new Locale("iw", "IL");
|
||||
|
||||
private Context context;
|
||||
private MonthInYearAdapter monthInYearFeb2019;
|
||||
private MonthInYearAdapter monthInYearFeb2016;
|
||||
private MonthInYearAdapter monthInYearJul2018;
|
||||
private MonthInYearAdapter monthInYearMarch2019;
|
||||
|
||||
@Before
|
||||
public void setupMonthInYearAdapters() {
|
||||
ApplicationProvider.getApplicationContext().setTheme(R.style.Theme_MaterialComponents_Light);
|
||||
AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
|
||||
context = activity.getApplicationContext();
|
||||
}
|
||||
|
||||
private void setupLocalizedCalendars(Locale locale) {
|
||||
Locale.setDefault(locale);
|
||||
monthInYearFeb2016 =
|
||||
new MonthInYearAdapter(context, MonthInYear.create(2016, Calendar.FEBRUARY));
|
||||
monthInYearJul2018 = new MonthInYearAdapter(context, MonthInYear.create(2018, Calendar.JULY));
|
||||
monthInYearFeb2019 =
|
||||
new MonthInYearAdapter(context, MonthInYear.create(2019, Calendar.FEBRUARY));
|
||||
monthInYearMarch2019 =
|
||||
new MonthInYearAdapter(context, MonthInYear.create(2019, Calendar.MARCH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsLastPositionMatchesMonthLength() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
assertLastPositionMatchesMonthLength();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrLastPositionMatchesMonthLength() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
assertLastPositionMatchesMonthLength();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlLastPositionMatchesMonthLength() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
assertLastPositionMatchesMonthLength();
|
||||
}
|
||||
|
||||
private void assertLastPositionMatchesMonthLength() {
|
||||
assertEquals(29, monthInYearFeb2016.positionToDay(monthInYearFeb2016.lastPositionInMonth()));
|
||||
assertEquals(31, monthInYearJul2018.positionToDay(monthInYearJul2018.lastPositionInMonth()));
|
||||
assertEquals(28, monthInYearFeb2019.positionToDay(monthInYearFeb2019.lastPositionInMonth()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsMaxPosition() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
Map<MonthInYearAdapter, Integer> localizedMaxPositionInMonth = new HashMap<>();
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2016, 29);
|
||||
localizedMaxPositionInMonth.put(monthInYearJul2018, 30);
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2019, 32);
|
||||
assertMaxPosition(localizedMaxPositionInMonth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrMaxPosition() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
Map<MonthInYearAdapter, Integer> localizedMaxPositionInMonth = new HashMap<>();
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2016, 28);
|
||||
localizedMaxPositionInMonth.put(monthInYearJul2018, 36);
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2019, 31);
|
||||
assertMaxPosition(localizedMaxPositionInMonth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlMaxPosition() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
Map<MonthInYearAdapter, Integer> localizedMaxPositionInMonth = new HashMap<>();
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2016, 29);
|
||||
localizedMaxPositionInMonth.put(monthInYearJul2018, 30);
|
||||
localizedMaxPositionInMonth.put(monthInYearFeb2019, 32);
|
||||
assertMaxPosition(localizedMaxPositionInMonth);
|
||||
}
|
||||
|
||||
private void assertMaxPosition(Map<MonthInYearAdapter, Integer> localizedMaxPositionInMonth) {
|
||||
assertEquals(
|
||||
(int) localizedMaxPositionInMonth.get(monthInYearFeb2016),
|
||||
monthInYearFeb2016.lastPositionInMonth());
|
||||
assertEquals(
|
||||
(int) localizedMaxPositionInMonth.get(monthInYearJul2018),
|
||||
monthInYearJul2018.lastPositionInMonth());
|
||||
assertEquals(
|
||||
(int) localizedMaxPositionInMonth.get(monthInYearFeb2019),
|
||||
monthInYearFeb2019.lastPositionInMonth());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsPositions() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
Map<MonthInYearAdapter, Integer> localizedPositionToDay =
|
||||
localizedPositionToDay = new HashMap<>();
|
||||
localizedPositionToDay.put(monthInYearFeb2016, 1);
|
||||
localizedPositionToDay.put(monthInYearJul2018, 7);
|
||||
localizedPositionToDay.put(monthInYearFeb2019, 11);
|
||||
assertPositionsForDays(localizedPositionToDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrPositions() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
Map<MonthInYearAdapter, Integer> localizedPositionToDay =
|
||||
localizedPositionToDay = new HashMap<>();
|
||||
localizedPositionToDay.put(monthInYearFeb2016, 2);
|
||||
localizedPositionToDay.put(monthInYearJul2018, 1);
|
||||
localizedPositionToDay.put(monthInYearFeb2019, 12);
|
||||
assertPositionsForDays(localizedPositionToDay);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlPositions() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
Map<MonthInYearAdapter, Integer> localizedPositionToDay =
|
||||
localizedPositionToDay = new HashMap<>();
|
||||
localizedPositionToDay.put(monthInYearFeb2016, 1);
|
||||
localizedPositionToDay.put(monthInYearJul2018, 7);
|
||||
localizedPositionToDay.put(monthInYearFeb2019, 11);
|
||||
assertPositionsForDays(localizedPositionToDay);
|
||||
}
|
||||
|
||||
private void assertPositionsForDays(Map<MonthInYearAdapter, Integer> localizedPositionToDay) {
|
||||
assertEquals(
|
||||
(int) localizedPositionToDay.get(monthInYearFeb2016), monthInYearFeb2016.positionToDay(1));
|
||||
assertEquals(
|
||||
(int) localizedPositionToDay.get(monthInYearJul2018), monthInYearJul2018.positionToDay(6));
|
||||
assertEquals(
|
||||
(int) localizedPositionToDay.get(monthInYearFeb2019), monthInYearFeb2019.positionToDay(15));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsStableCount() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
assertStableCount();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrStableCount() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
assertStableCount();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlStableCount() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
assertStableCount();
|
||||
}
|
||||
|
||||
private void assertStableCount() {
|
||||
assertEquals(6 * 7, monthInYearFeb2016.getCount());
|
||||
assertEquals(6 * 7, monthInYearJul2018.getCount());
|
||||
assertEquals(6 * 7, monthInYearFeb2019.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsPositionsWithinMonthReturnAValidItem() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
Collection<Integer> localizedNullPositionsInFebruary2019 = Arrays.asList(-5, 0, 4, 33, 100);
|
||||
Collection<Integer> localizedNonNullPositionsInFebruary2019 = Arrays.asList(5, 32);
|
||||
assertPositionsWithinMonthReturnAValidItem(
|
||||
localizedNullPositionsInFebruary2019, localizedNonNullPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrPositionsWithinMonthReturnAValidItem() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
Collection<Integer> localizedNullPositionsInFebruary2019 = Arrays.asList(-5, 0, 3, 32, 100);
|
||||
Collection<Integer> localizedNonNullPositionsInFebruary2019 = Arrays.asList(4, 31);
|
||||
assertPositionsWithinMonthReturnAValidItem(
|
||||
localizedNullPositionsInFebruary2019, localizedNonNullPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlPositionsWithinMonthReturnAValidItem() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
Collection<Integer> localizedNullPositionsInFebruary2019 = Arrays.asList(-5, 0, 4, 33, 100);
|
||||
Collection<Integer> localizedNonNullPositionsInFebruary2019 = Arrays.asList(5, 32);
|
||||
assertPositionsWithinMonthReturnAValidItem(
|
||||
localizedNullPositionsInFebruary2019, localizedNonNullPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
private void assertPositionsWithinMonthReturnAValidItem(
|
||||
Collection<Integer> localizedNullPositionsInFebruary2019,
|
||||
Collection<Integer> localizedNonNullPositionsInFebruary2019) {
|
||||
for (int position : localizedNullPositionsInFebruary2019) {
|
||||
assertNull(monthInYearFeb2019.getItem(position));
|
||||
}
|
||||
for (int position : localizedNonNullPositionsInFebruary2019) {
|
||||
assertNotNull(monthInYearFeb2019.getItem(position));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsDaysOfPositions() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
Map<Integer, Integer> localizedDaysOfPositionsInFebruary2019 = new HashMap<>();
|
||||
localizedDaysOfPositionsInFebruary2019.put(6, 2);
|
||||
localizedDaysOfPositionsInFebruary2019.put(32, 28);
|
||||
assertDaysOfPositions(localizedDaysOfPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrDaysOfPositions() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
Map<Integer, Integer> localizedDaysOfPositionsInFebruary2019 = new HashMap<>();
|
||||
localizedDaysOfPositionsInFebruary2019.put(6, 3);
|
||||
localizedDaysOfPositionsInFebruary2019.put(31, 28);
|
||||
assertDaysOfPositions(localizedDaysOfPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlDaysOfPositions() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
Map<Integer, Integer> localizedDaysOfPositionsInFebruary2019 = new HashMap<>();
|
||||
localizedDaysOfPositionsInFebruary2019.put(6, 2);
|
||||
localizedDaysOfPositionsInFebruary2019.put(32, 28);
|
||||
assertDaysOfPositions(localizedDaysOfPositionsInFebruary2019);
|
||||
}
|
||||
|
||||
private void assertDaysOfPositions(Map<Integer, Integer> localizedDaysOfPositionsInFebruary2019) {
|
||||
for (int day : localizedDaysOfPositionsInFebruary2019.keySet()) {
|
||||
assertEquals(
|
||||
(int) localizedDaysOfPositionsInFebruary2019.get(day),
|
||||
monthInYearFeb2019.getItem(day).get(Calendar.DAY_OF_MONTH));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRows() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
assertEquals(0, monthInYearFeb2019.getItemId(0));
|
||||
assertEquals(1, monthInYearFeb2019.getItemId(7));
|
||||
assertEquals(3, monthInYearFeb2019.getItemId(26));
|
||||
assertEquals(5, monthInYearMarch2019.getItemId(35));
|
||||
}
|
||||
}
|
||||
@ -1,136 +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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.internal.DoNotInstrument;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@DoNotInstrument
|
||||
public class MonthInYearTest {
|
||||
|
||||
private static final Locale ISRAEL = new Locale("iw", "IL");
|
||||
|
||||
private MonthInYear monthInYearFeb2016;
|
||||
private MonthInYear monthInYearJul2018;
|
||||
private MonthInYear monthInYearFeb2019;
|
||||
|
||||
private void setupLocalizedCalendars(Locale locale) {
|
||||
Locale.setDefault(locale);
|
||||
monthInYearFeb2016 = MonthInYear.create(2016, Calendar.FEBRUARY);
|
||||
monthInYearJul2018 = MonthInYear.create(2018, Calendar.JULY);
|
||||
monthInYearFeb2019 = MonthInYear.create(2019, Calendar.FEBRUARY);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsDaysInWeek() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
assertDaysInWeek();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrDaysInWeek() {
|
||||
;
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
assertDaysInWeek();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlDaysInWeek() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
assertDaysInWeek();
|
||||
}
|
||||
|
||||
private void assertDaysInWeek() {
|
||||
assertEquals(7, monthInYearFeb2016.daysInWeek);
|
||||
assertEquals(7, monthInYearJul2018.daysInWeek);
|
||||
assertEquals(7, monthInYearFeb2019.daysInWeek);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsDaysInMonth() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
assertDaysInMonth();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrDaysInMonth() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
assertDaysInMonth();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlDaysInMonth() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
assertDaysInMonth();
|
||||
}
|
||||
|
||||
private void assertDaysInMonth() {
|
||||
assertEquals(29, monthInYearFeb2016.daysInMonth);
|
||||
assertEquals(31, monthInYearJul2018.daysInMonth);
|
||||
assertEquals(28, monthInYearFeb2019.daysInMonth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUsDaysFromStart() {
|
||||
setupLocalizedCalendars(Locale.US);
|
||||
Map<MonthInYear, Integer> localizedStartOfWeekToStartOfMonth = new HashMap<>();
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2016, 1);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearJul2018, 0);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2019, 5);
|
||||
assertDaysFromStart(localizedStartOfWeekToStartOfMonth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrDaysFromStart() {
|
||||
setupLocalizedCalendars(Locale.FRANCE);
|
||||
Map<MonthInYear, Integer> localizedStartOfWeekToStartOfMonth = new HashMap<>();
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2016, 0);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearJul2018, 6);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2019, 4);
|
||||
assertDaysFromStart(localizedStartOfWeekToStartOfMonth);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIlDaysFromStart() {
|
||||
setupLocalizedCalendars(ISRAEL);
|
||||
Map<MonthInYear, Integer> localizedStartOfWeekToStartOfMonth = new HashMap<>();
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2016, 1);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearJul2018, 0);
|
||||
localizedStartOfWeekToStartOfMonth.put(monthInYearFeb2019, 5);
|
||||
assertDaysFromStart(localizedStartOfWeekToStartOfMonth);
|
||||
}
|
||||
|
||||
private void assertDaysFromStart(Map<MonthInYear, Integer> localizedStartOfWeekToStartOfMonth) {
|
||||
assertEquals(
|
||||
(int) localizedStartOfWeekToStartOfMonth.get(monthInYearFeb2016),
|
||||
monthInYearFeb2016.daysFromStartOfWeekToFirstOfMonth());
|
||||
assertEquals(
|
||||
(int) localizedStartOfWeekToStartOfMonth.get(monthInYearJul2018),
|
||||
monthInYearJul2018.daysFromStartOfWeekToFirstOfMonth());
|
||||
assertEquals(
|
||||
(int) localizedStartOfWeekToStartOfMonth.get(monthInYearFeb2019),
|
||||
monthInYearFeb2019.daysFromStartOfWeekToFirstOfMonth());
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user