Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added key listener to allow d-pad navigation of days in month view. S… #220

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.annotation.SuppressLint;
import android.app.UiModeManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
Expand Down Expand Up @@ -183,4 +185,14 @@ private static boolean resolveBoolean(Context context, @AttrRes int attr, boolea
a.recycle();
}
}

/**
* Indicates if app is running on Android TV device
* @param context The context to use as reference for the test
* @return true if device is Android TV, false if other device type
*/
public static boolean isTv(Context context) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public interface DatePickerController {
boolean isOutOfRange(int year, int month, int day);

void tryVibrate();

void focusYear();

void focusMonthDays();

void focusDialogButtons();
}
Original file line number Diff line number Diff line change
Expand Up @@ -983,4 +983,24 @@ public void notifyOnDateListener() {
mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH));
}
}

@Override
public void focusYear() {
setCurrentView(YEAR_VIEW);
}

@Override
public void focusMonthDays() {
setCurrentView(MONTH_AND_DAY_VIEW);
}

@Override
public void focusDialogButtons() {
if (getView() != null) {
Button okButton = (Button) getView().findViewById(R.id.ok);
if (okButton != null) {
okButton.requestFocus();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ protected void setUpListView() {
setFadingEdgeLength(0);
// Make the scrolling behavior nicer
setFriction(ViewConfiguration.getScrollFriction() * mFriction);
// Turn off focus so that MonthView can get key events, for Android TV
setFocusable(false);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be wrapped in Utils.isTv(getContext());?

setFocusableInTouchMode(false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
import android.support.v4.widget.ExploreByTouchHelper;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.wdullaer.materialdatetimepicker.R;
import com.wdullaer.materialdatetimepicker.TypefaceHelper;
import com.wdullaer.materialdatetimepicker.Utils;
import com.wdullaer.materialdatetimepicker.date.MonthAdapter.CalendarDay;

import java.security.InvalidParameterException;
Expand Down Expand Up @@ -239,6 +241,11 @@ public MonthView(Context context, AttributeSet attr, DatePickerController contro
ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
mLockAccessibilityDelegate = true;

if (Utils.isTv(getContext())) {
setFocusable(true);
setFocusableInTouchMode(true);
}

// Sets up any standard paints that will be used
initView();
}
Expand Down Expand Up @@ -286,6 +293,168 @@ public boolean onTouchEvent(@NonNull MotionEvent event) {
return true;
}

/**
* Key listener for D-pad navigation between days, particularly used for Android TV
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (Utils.isTv(getContext())) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
// move focus to ok button
mController.focusDialogButtons();
return true;
}
Calendar currentDate = Calendar.getInstance();
currentDate.set(mYear, mMonth, mSelectedDay);
Calendar newDate = Calendar.getInstance();
newDate.set(mYear, mMonth, mSelectedDay);
int dayDrawOffset = findDayOffset();
if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
newDate.add(Calendar.DAY_OF_MONTH, -7);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
newDate.add(Calendar.DAY_OF_MONTH, 7);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
int firstLeftSideDay = dayDrawOffset == 0 ? 1 : 7 - dayDrawOffset + 1; // +1 to correct that days start at 1, not 0
int dayColumn = (mSelectedDay % 7) - firstLeftSideDay;
int MAX_ITERATIONS = 10;
int count = 0;
while (dayColumn < 0) {
dayColumn += 7;
if (++count > MAX_ITERATIONS) {
break;
}
}
if (dayColumn == 0) {
// move focus left off month days, i.e. to year picker
mController.focusYear();
return true;
}
boolean dayIsLeftMostSelectable = true;
for (int i = 1 ; i <= dayColumn ; i++) {
if (mSelectedDay - i < 1) {
break;
}
if (!mController.isOutOfRange(mYear, mMonth, mSelectedDay - i)) {
dayIsLeftMostSelectable = false;
break;
}
}
if (dayIsLeftMostSelectable) {
// move focus left off month days, i.e. to year picker
mController.focusYear();
return true;
}
// else
newDate.add(Calendar.DAY_OF_MONTH, -1);
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
int firstLeftSideDay = dayDrawOffset == 0 ? 1 : 7 - dayDrawOffset + 1; // +1 to correct that days start at 1, not 0
int dayColumn = (mSelectedDay % 7) - firstLeftSideDay;
int MAX_ITERATIONS = 10;
int count = 0;
while (dayColumn < 0) {
dayColumn += 7;
if (++count > MAX_ITERATIONS) {
break;
}
}
if (dayColumn == 6) {
// move focus right off month days, i.e. to ok button
mController.focusDialogButtons();
return true;
}
boolean dayIsRightMostSelectable = true;
Calendar monthCalendar = Calendar.getInstance();
monthCalendar.set(mYear, mMonth, 1);
int numDaysInMonth = monthCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1 ; i <= (6 - dayColumn) ; i++) {
if (mSelectedDay + i > numDaysInMonth) {
break;
}
if (!mController.isOutOfRange(mYear, mMonth, mSelectedDay + i)) {
dayIsRightMostSelectable = false;
break;
}
}
if (dayIsRightMostSelectable) {
// move focus right off month days, i.e. to ok button
mController.focusDialogButtons();
return true;
}
// else
newDate.add(Calendar.DAY_OF_MONTH, 1);
}
if (newDate.get(Calendar.DAY_OF_MONTH) != mSelectedDay) {
if (mOnDayClickListener != null) {
boolean isDateAllowed = !mController.isOutOfRange(
newDate.get(Calendar.YEAR),
newDate.get(Calendar.MONTH),
newDate.get(Calendar.DAY_OF_MONTH));
if (isDateAllowed) {
mOnDayClickListener.onDayClick(this, new CalendarDay(
newDate.get(Calendar.YEAR),
newDate.get(Calendar.MONTH),
newDate.get(Calendar.DAY_OF_MONTH)));
return true;
}
int MAX_MONTHS_TO_SEARCH = 12;
if (newDate.before(currentDate)) {
// first (this) month
int year = currentDate.get(Calendar.YEAR);
int month = currentDate.get(Calendar.MONTH);
newDate.set(year, month, 1);
for (int i = (mSelectedDay - 1) ; i >= 1 ; i--) {
if (!mController.isOutOfRange(year, month, i)) {
mOnDayClickListener.onDayClick(this, new CalendarDay(year, month, i));
return true;
}
}
// try previous months up to search limit
for (int j = 0 ; j < MAX_MONTHS_TO_SEARCH ; j++) {
newDate.add(Calendar.MONTH, -1);
year = newDate.get(Calendar.YEAR);
month = newDate.get(Calendar.MONTH);
for (int i = newDate.getActualMaximum(Calendar.DAY_OF_MONTH) ; i >= 1 ; i--) {
if (!mController.isOutOfRange(year, month, i)) {
mOnDayClickListener.onDayClick(this, new CalendarDay(year, month, i));
return true;
}
}
}
// give up
} else if (newDate.after(currentDate)) {
// first (this) month
int year = currentDate.get(Calendar.YEAR);
int month = currentDate.get(Calendar.MONTH);
newDate.set(year, month, 1);
int numDaysInMonth = newDate.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = (mSelectedDay + 1) ; i <= numDaysInMonth ; i++) {
if (!mController.isOutOfRange(year, month, i)) {
mOnDayClickListener.onDayClick(this, new CalendarDay(year, month, i));
return true;
}
}
// try next months up to search limit
for (int j = 0 ; j < MAX_MONTHS_TO_SEARCH ; j++) {
newDate.add(Calendar.MONTH, 1);
year = newDate.get(Calendar.YEAR);
month = newDate.get(Calendar.MONTH);
numDaysInMonth = newDate.getActualMaximum(Calendar.DAY_OF_MONTH);
for (int i = 1 ; i <= numDaysInMonth ; i++) {
if (!mController.isOutOfRange(year, month, i)) {
mOnDayClickListener.onDayClick(this, new CalendarDay(year, month, i));
return true;
}
}
}
// give up
}
}
}
}
return false;
}

/**
* Sets up the text and style properties for painting. Override this if you
* want to use a different paint.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.StateListDrawable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
Expand All @@ -29,6 +30,7 @@
import android.widget.TextView;

import com.wdullaer.materialdatetimepicker.R;
import com.wdullaer.materialdatetimepicker.Utils;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog.OnDateChangedListener;

import java.util.ArrayList;
Expand Down Expand Up @@ -66,6 +68,9 @@ public YearPickerView(Context context, DatePickerController controller) {
setSelector(new StateListDrawable());
setDividerHeight(0);
onDateChanged();
if (Utils.isTv(getContext())) {
setItemsCanFocus(true);
}
}

private void init(Context context) {
Expand Down Expand Up @@ -107,7 +112,7 @@ public YearAdapter(Context context, int resource, List<String> objects) {
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(final int position, View convertView, ViewGroup parent) {
TextViewWithCircularIndicator v = (TextViewWithCircularIndicator)
super.getView(position, convertView, parent);
v.setAccentColor(mController.getAccentColor(), mController.isThemeDark());
Expand All @@ -118,6 +123,55 @@ public View getView(int position, View convertView, ViewGroup parent) {
if (selected) {
mSelectedView = v;
}
if (Utils.isTv(getContext())) {
v.setFocusable(true);
v.setFocusableInTouchMode(true);
if (selected) {
mSelectedView.post(new Runnable() {
@Override
public void run() {
mSelectedView.requestFocus();
}
});
}
v.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TextViewWithCircularIndicator focusedView = (TextViewWithCircularIndicator) v;
if (focusedView != mSelectedView) {
if (mSelectedView != null) {
mSelectedView.drawIndicator(false);
mSelectedView.requestLayout();
}
focusedView.drawIndicator(true);
focusedView.requestLayout();
mSelectedView = focusedView;
}
mAdapter.notifyDataSetChanged();
}
}
});
v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
performItemClick(v, position, 0);
}
});
v.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
mController.focusMonthDays();
return true;
} else if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
mController.focusDialogButtons();
return true;
}
return false;
}
});
}
return v;
}
}
Expand Down
Loading