1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.util;
18
19/**
20 * Helps control and display a month view of a calendar that has a current
21 * selected day.
22 * <ul>
23 *   <li>Keeps track of current month, day, year</li>
24 *   <li>Keeps track of current cursor position (row, column)</li>
25 *   <li>Provides methods to help display the calendar</li>
26 *   <li>Provides methods to move the cursor up / down / left / right.</li>
27 * </ul>
28 *
29 * This should be used by anyone who presents a month view to users and wishes
30 * to behave consistently with other widgets and apps; if we ever change our
31 * mind about when to flip the month, we can change it here only.
32 *
33 * @hide
34 */
35public class DayOfMonthCursor extends MonthDisplayHelper {
36
37    private int mRow;
38    private int mColumn;
39
40    /**
41     * @param year The initial year.
42     * @param month The initial month.
43     * @param dayOfMonth The initial dayOfMonth.
44     * @param weekStartDay What dayOfMonth of the week the week should start,
45     *   in terms of {@link java.util.Calendar} constants such as
46     *   {@link java.util.Calendar#SUNDAY}.
47     */
48    public DayOfMonthCursor(int year, int month, int dayOfMonth, int weekStartDay) {
49        super(year, month, weekStartDay);
50        mRow = getRowOf(dayOfMonth);
51        mColumn = getColumnOf(dayOfMonth);
52    }
53
54
55    public int getSelectedRow() {
56        return mRow;
57    }
58
59    public int getSelectedColumn() {
60        return mColumn;
61    }
62
63    public void setSelectedRowColumn(int row, int col) {
64        mRow = row;
65        mColumn = col;
66    }
67
68    public int getSelectedDayOfMonth() {
69        return getDayAt(mRow, mColumn);
70    }
71
72    /**
73     * @return 0 if the selection is in the current month, otherwise -1 or +1
74     * depending on whether the selection is in the first or last row.
75     */
76    public int getSelectedMonthOffset() {
77        if (isWithinCurrentMonth(mRow, mColumn)) {
78            return 0;
79        }
80        if (mRow == 0) {
81            return -1;
82        }
83        return 1;
84    }
85
86    public void setSelectedDayOfMonth(int dayOfMonth) {
87        mRow = getRowOf(dayOfMonth);
88        mColumn = getColumnOf(dayOfMonth);
89    }
90
91    public boolean isSelected(int row, int column) {
92        return (mRow == row) && (mColumn == column);
93    }
94
95    /**
96     * Move up one box, potentially flipping to the previous month.
97     * @return Whether the month was flipped to the previous month
98     *   due to the move.
99     */
100    public boolean up() {
101        if (isWithinCurrentMonth(mRow - 1, mColumn)) {
102            // within current month, just move up
103            mRow--;
104            return false;
105        }
106        // flip back to previous month, same column, first position within month
107        previousMonth();
108        mRow = 5;
109        while(!isWithinCurrentMonth(mRow, mColumn)) {
110            mRow--;
111        }
112        return true;
113    }
114
115    /**
116     * Move down one box, potentially flipping to the next month.
117     * @return Whether the month was flipped to the next month
118     *   due to the move.
119     */
120    public boolean down() {
121        if (isWithinCurrentMonth(mRow + 1, mColumn)) {
122            // within current month, just move down
123            mRow++;
124            return false;
125        }
126        // flip to next month, same column, first position within month
127        nextMonth();
128        mRow = 0;
129        while (!isWithinCurrentMonth(mRow, mColumn)) {
130            mRow++;
131        }
132        return true;
133    }
134
135    /**
136     * Move left one box, potentially flipping to the previous month.
137     * @return Whether the month was flipped to the previous month
138     *   due to the move.
139     */
140    public boolean left() {
141        if (mColumn == 0) {
142            mRow--;
143            mColumn = 6;
144        } else {
145            mColumn--;
146        }
147
148        if (isWithinCurrentMonth(mRow, mColumn)) {
149            return false;
150        }
151
152        // need to flip to last day of previous month
153        previousMonth();
154        int lastDay = getNumberOfDaysInMonth();
155        mRow = getRowOf(lastDay);
156        mColumn = getColumnOf(lastDay);
157        return true;
158    }
159
160    /**
161     * Move right one box, potentially flipping to the next month.
162     * @return Whether the month was flipped to the next month
163     *   due to the move.
164     */
165    public boolean right() {
166        if (mColumn == 6) {
167            mRow++;
168            mColumn = 0;
169        } else {
170            mColumn++;
171        }
172
173        if (isWithinCurrentMonth(mRow, mColumn)) {
174            return false;
175        }
176
177        // need to flip to first day of next month
178        nextMonth();
179        mRow = 0;
180        mColumn = 0;
181        while (!isWithinCurrentMonth(mRow, mColumn)) {
182            mColumn++;
183        }
184        return true;
185    }
186
187}
188