Merge "Fix bug #7205589 CalendarView's WeekView rows are LTR in RTL locales" into jb-mr1-dev

This commit is contained in:
Fabrice Di Meglio
2012-09-23 11:11:03 -07:00
committed by Android (Google) Code Review

View File

@ -1660,16 +1660,34 @@ public class CalendarView extends FrameLayout {
* @return True if a day was found for the given location. * @return True if a day was found for the given location.
*/ */
public boolean getDayFromLocation(float x, Calendar outCalendar) { public boolean getDayFromLocation(float x, Calendar outCalendar) {
int dayStart = mShowWeekNumber ? mWidth / mNumCells : 0; final boolean isLayoutRtl = isLayoutRtl();
if (x < dayStart || x > mWidth) {
int start;
int end;
if (isLayoutRtl) {
start = 0;
end = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth;
} else {
start = mShowWeekNumber ? mWidth / mNumCells : 0;
end = mWidth;
}
if (x < start || x > end) {
outCalendar.clear(); outCalendar.clear();
return false; return false;
} }
// Selection is (x - start) / (pixels/day) == (x -s) * day / pixels
int dayPosition = (int) ((x - dayStart) * mDaysPerWeek // Selection is (x - start) / (pixels/day) which is (x - start) * day / pixels
/ (mWidth - dayStart)); int dayPosition = (int) ((x - start) * mDaysPerWeek / (end - start));
if (isLayoutRtl) {
dayPosition = mDaysPerWeek - 1 - dayPosition;
}
outCalendar.setTimeInMillis(mFirstDay.getTimeInMillis()); outCalendar.setTimeInMillis(mFirstDay.getTimeInMillis());
outCalendar.add(Calendar.DAY_OF_MONTH, dayPosition); outCalendar.add(Calendar.DAY_OF_MONTH, dayPosition);
return true; return true;
} }
@ -1694,12 +1712,25 @@ public class CalendarView extends FrameLayout {
mTempRect.top = mWeekSeperatorLineWidth; mTempRect.top = mWeekSeperatorLineWidth;
mTempRect.bottom = mHeight; mTempRect.bottom = mHeight;
final boolean isLayoutRtl = isLayoutRtl();
if (isLayoutRtl) {
mTempRect.left = 0;
mTempRect.right = mSelectedLeft - 2;
} else {
mTempRect.left = mShowWeekNumber ? mWidth / mNumCells : 0; mTempRect.left = mShowWeekNumber ? mWidth / mNumCells : 0;
mTempRect.right = mSelectedLeft - 2; mTempRect.right = mSelectedLeft - 2;
}
canvas.drawRect(mTempRect, mDrawPaint); canvas.drawRect(mTempRect, mDrawPaint);
if (isLayoutRtl) {
mTempRect.left = mSelectedRight + 3;
mTempRect.right = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth;
} else {
mTempRect.left = mSelectedRight + 3; mTempRect.left = mSelectedRight + 3;
mTempRect.right = mWidth; mTempRect.right = mWidth;
}
canvas.drawRect(mTempRect, mDrawPaint); canvas.drawRect(mTempRect, mDrawPaint);
} }
@ -1709,14 +1740,29 @@ public class CalendarView extends FrameLayout {
* @param canvas The canvas to draw on * @param canvas The canvas to draw on
*/ */
private void drawWeekNumbersAndDates(Canvas canvas) { private void drawWeekNumbersAndDates(Canvas canvas) {
float textHeight = mDrawPaint.getTextSize(); final float textHeight = mDrawPaint.getTextSize();
int y = (int) ((mHeight + textHeight) / 2) - mWeekSeperatorLineWidth; final int y = (int) ((mHeight + textHeight) / 2) - mWeekSeperatorLineWidth;
int nDays = mNumCells; final int nDays = mNumCells;
final int divisor = 2 * nDays;
mDrawPaint.setTextAlign(Align.CENTER); mDrawPaint.setTextAlign(Align.CENTER);
mDrawPaint.setTextSize(mDateTextSize); mDrawPaint.setTextSize(mDateTextSize);
int i = 0; int i = 0;
int divisor = 2 * nDays;
if (isLayoutRtl()) {
for (; i < nDays - 1; i++) {
mMonthNumDrawPaint.setColor(mFocusDay[i] ? mFocusedMonthDateColor
: mUnfocusedMonthDateColor);
int x = (2 * i + 1) * mWidth / divisor;
canvas.drawText(mDayNumbers[nDays - 1 - i], x, y, mMonthNumDrawPaint);
}
if (mShowWeekNumber) {
mDrawPaint.setColor(mWeekNumberColor);
int x = mWidth - mWidth / divisor;
canvas.drawText(mDayNumbers[0], x, y, mDrawPaint);
}
} else {
if (mShowWeekNumber) { if (mShowWeekNumber) {
mDrawPaint.setColor(mWeekNumberColor); mDrawPaint.setColor(mWeekNumberColor);
int x = mWidth / divisor; int x = mWidth / divisor;
@ -1730,6 +1776,7 @@ public class CalendarView extends FrameLayout {
canvas.drawText(mDayNumbers[i], x, y, mMonthNumDrawPaint); canvas.drawText(mDayNumbers[i], x, y, mMonthNumDrawPaint);
} }
} }
}
/** /**
* Draws a horizontal line for separating the weeks. * Draws a horizontal line for separating the weeks.
@ -1747,8 +1794,16 @@ public class CalendarView extends FrameLayout {
} }
mDrawPaint.setColor(mWeekSeparatorLineColor); mDrawPaint.setColor(mWeekSeparatorLineColor);
mDrawPaint.setStrokeWidth(mWeekSeperatorLineWidth); mDrawPaint.setStrokeWidth(mWeekSeperatorLineWidth);
float x = mShowWeekNumber ? mWidth / mNumCells : 0; float startX;
canvas.drawLine(x, 0, mWidth, 0, mDrawPaint); float stopX;
if (isLayoutRtl()) {
startX = 0;
stopX = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth;
} else {
startX = mShowWeekNumber ? mWidth / mNumCells : 0;
stopX = mWidth;
}
canvas.drawLine(startX, 0, stopX, 0, mDrawPaint);
} }
/** /**
@ -1781,15 +1836,21 @@ public class CalendarView extends FrameLayout {
*/ */
private void updateSelectionPositions() { private void updateSelectionPositions() {
if (mHasSelectedDay) { if (mHasSelectedDay) {
final boolean isLayoutRtl = isLayoutRtl();
int selectedPosition = mSelectedDay - mFirstDayOfWeek; int selectedPosition = mSelectedDay - mFirstDayOfWeek;
if (selectedPosition < 0) { if (selectedPosition < 0) {
selectedPosition += 7; selectedPosition += 7;
} }
if (mShowWeekNumber) { if (mShowWeekNumber && !isLayoutRtl) {
selectedPosition++; selectedPosition++;
} }
if (isLayoutRtl) {
mSelectedLeft = (mDaysPerWeek - 1 - selectedPosition) * mWidth / mNumCells;
} else {
mSelectedLeft = selectedPosition * mWidth / mNumCells; mSelectedLeft = selectedPosition * mWidth / mNumCells;
mSelectedRight = (selectedPosition + 1) * mWidth / mNumCells; }
mSelectedRight = mSelectedLeft + mWidth / mNumCells;
} }
} }