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