| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import java.util.Calendar; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class MonthDisplayHelper { |
| 28 |
|
| 29 |
|
| 30 |
private final int mWeekStartDay; |
| 31 |
|
| 32 |
|
| 33 |
private Calendar mCalendar; |
| 34 |
|
| 35 |
|
| 36 |
private int mNumDaysInMonth; |
| 37 |
private int mNumDaysInPrevMonth; |
| 38 |
private int mOffset; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public MonthDisplayHelper(int year, int month, int weekStartDay) { |
| 47 |
|
| 48 |
if (weekStartDay < Calendar.SUNDAY || weekStartDay > Calendar.SATURDAY) { |
| 49 |
throw new IllegalArgumentException(); |
| 50 |
} |
| 51 |
mWeekStartDay = weekStartDay; |
| 52 |
|
| 53 |
mCalendar = Calendar.getInstance(); |
| 54 |
mCalendar.set(Calendar.YEAR, year); |
| 55 |
mCalendar.set(Calendar.MONTH, month); |
| 56 |
mCalendar.set(Calendar.DAY_OF_MONTH, 1); |
| 57 |
mCalendar.set(Calendar.HOUR_OF_DAY, 0); |
| 58 |
mCalendar.set(Calendar.MINUTE, 0); |
| 59 |
mCalendar.set(Calendar.SECOND, 0); |
| 60 |
mCalendar.getTimeInMillis(); |
| 61 |
|
| 62 |
recalculate(); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public MonthDisplayHelper(int year, int month) { |
| 67 |
this(year, month, Calendar.SUNDAY); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public int getYear() { |
| 72 |
return mCalendar.get(Calendar.YEAR); |
| 73 |
} |
| 74 |
|
| 75 |
public int getMonth() { |
| 76 |
return mCalendar.get(Calendar.MONTH); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
public int getWeekStartDay() { |
| 81 |
return mWeekStartDay; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public int getFirstDayOfMonth() { |
| 89 |
return mCalendar.get(Calendar.DAY_OF_WEEK); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public int getNumberOfDaysInMonth() { |
| 96 |
return mNumDaysInMonth; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public int getOffset() { |
| 106 |
return mOffset; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public int[] getDigitsForRow(int row) { |
| 116 |
if (row < 0 || row > 5) { |
| 117 |
throw new IllegalArgumentException("row " + row |
| 118 |
+ " out of range (0-5)"); |
| 119 |
} |
| 120 |
|
| 121 |
int [] result = new int[7]; |
| 122 |
for (int column = 0; column < 7; column++) { |
| 123 |
result[column] = getDayAt(row, column); |
| 124 |
} |
| 125 |
|
| 126 |
return result; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public int getDayAt(int row, int column) { |
| 135 |
|
| 136 |
if (row == 0 && column < mOffset) { |
| 137 |
return mNumDaysInPrevMonth + column - mOffset + 1; |
| 138 |
} |
| 139 |
|
| 140 |
int day = 7 * row + column - mOffset + 1; |
| 141 |
|
| 142 |
return (day > mNumDaysInMonth) ? |
| 143 |
day - mNumDaysInMonth : day; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public int getRowOf(int day) { |
| 150 |
return (day + mOffset - 1) / 7; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public int getColumnOf(int day) { |
| 157 |
return (day + mOffset - 1) % 7; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public void previousMonth() { |
| 164 |
mCalendar.add(Calendar.MONTH, -1); |
| 165 |
recalculate(); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public void nextMonth() { |
| 172 |
mCalendar.add(Calendar.MONTH, 1); |
| 173 |
recalculate(); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public boolean isWithinCurrentMonth(int row, int column) { |
| 180 |
|
| 181 |
if (row < 0 || column < 0 || row > 5 || column > 6) { |
| 182 |
return false; |
| 183 |
} |
| 184 |
|
| 185 |
if (row == 0 && column < mOffset) { |
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
int day = 7 * row + column - mOffset + 1; |
| 190 |
if (day > mNumDaysInMonth) { |
| 191 |
return false; |
| 192 |
} |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
private void recalculate() { |
| 199 |
|
| 200 |
mNumDaysInMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 201 |
|
| 202 |
mCalendar.add(Calendar.MONTH, -1); |
| 203 |
mNumDaysInPrevMonth = mCalendar.getActualMaximum(Calendar.DAY_OF_MONTH); |
| 204 |
mCalendar.add(Calendar.MONTH, 1); |
| 205 |
|
| 206 |
int firstDayOfMonth = getFirstDayOfMonth(); |
| 207 |
int offset = firstDayOfMonth - mWeekStartDay; |
| 208 |
if (offset < 0) { |
| 209 |
offset += 7; |
| 210 |
} |
| 211 |
mOffset = offset; |
| 212 |
} |
| 213 |
} |