• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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  */
35 @android.ravenwood.annotation.RavenwoodKeepWholeClass
36 public class DayOfMonthCursor extends MonthDisplayHelper {
37 
38     private int mRow;
39     private int mColumn;
40 
41     /**
42      * @param year The initial year.
43      * @param month The initial month.
44      * @param dayOfMonth The initial dayOfMonth.
45      * @param weekStartDay What dayOfMonth of the week the week should start,
46      *   in terms of {@link java.util.Calendar} constants such as
47      *   {@link java.util.Calendar#SUNDAY}.
48      */
DayOfMonthCursor(int year, int month, int dayOfMonth, int weekStartDay)49     public DayOfMonthCursor(int year, int month, int dayOfMonth, int weekStartDay) {
50         super(year, month, weekStartDay);
51         mRow = getRowOf(dayOfMonth);
52         mColumn = getColumnOf(dayOfMonth);
53     }
54 
55 
getSelectedRow()56     public int getSelectedRow() {
57         return mRow;
58     }
59 
getSelectedColumn()60     public int getSelectedColumn() {
61         return mColumn;
62     }
63 
setSelectedRowColumn(int row, int col)64     public void setSelectedRowColumn(int row, int col) {
65         mRow = row;
66         mColumn = col;
67     }
68 
getSelectedDayOfMonth()69     public int getSelectedDayOfMonth() {
70         return getDayAt(mRow, mColumn);
71     }
72 
73     /**
74      * @return 0 if the selection is in the current month, otherwise -1 or +1
75      * depending on whether the selection is in the first or last row.
76      */
getSelectedMonthOffset()77     public int getSelectedMonthOffset() {
78         if (isWithinCurrentMonth(mRow, mColumn)) {
79             return 0;
80         }
81         if (mRow == 0) {
82             return -1;
83         }
84         return 1;
85     }
86 
setSelectedDayOfMonth(int dayOfMonth)87     public void setSelectedDayOfMonth(int dayOfMonth) {
88         mRow = getRowOf(dayOfMonth);
89         mColumn = getColumnOf(dayOfMonth);
90     }
91 
isSelected(int row, int column)92     public boolean isSelected(int row, int column) {
93         return (mRow == row) && (mColumn == column);
94     }
95 
96     /**
97      * Move up one box, potentially flipping to the previous month.
98      * @return Whether the month was flipped to the previous month
99      *   due to the move.
100      */
up()101     public boolean up() {
102         if (isWithinCurrentMonth(mRow - 1, mColumn)) {
103             // within current month, just move up
104             mRow--;
105             return false;
106         }
107         // flip back to previous month, same column, first position within month
108         previousMonth();
109         mRow = 5;
110         while(!isWithinCurrentMonth(mRow, mColumn)) {
111             mRow--;
112         }
113         return true;
114     }
115 
116     /**
117      * Move down one box, potentially flipping to the next month.
118      * @return Whether the month was flipped to the next month
119      *   due to the move.
120      */
down()121     public boolean down() {
122         if (isWithinCurrentMonth(mRow + 1, mColumn)) {
123             // within current month, just move down
124             mRow++;
125             return false;
126         }
127         // flip to next month, same column, first position within month
128         nextMonth();
129         mRow = 0;
130         while (!isWithinCurrentMonth(mRow, mColumn)) {
131             mRow++;
132         }
133         return true;
134     }
135 
136     /**
137      * Move left one box, potentially flipping to the previous month.
138      * @return Whether the month was flipped to the previous month
139      *   due to the move.
140      */
left()141     public boolean left() {
142         if (mColumn == 0) {
143             mRow--;
144             mColumn = 6;
145         } else {
146             mColumn--;
147         }
148 
149         if (isWithinCurrentMonth(mRow, mColumn)) {
150             return false;
151         }
152 
153         // need to flip to last day of previous month
154         previousMonth();
155         int lastDay = getNumberOfDaysInMonth();
156         mRow = getRowOf(lastDay);
157         mColumn = getColumnOf(lastDay);
158         return true;
159     }
160 
161     /**
162      * Move right one box, potentially flipping to the next month.
163      * @return Whether the month was flipped to the next month
164      *   due to the move.
165      */
right()166     public boolean right() {
167         if (mColumn == 6) {
168             mRow++;
169             mColumn = 0;
170         } else {
171             mColumn++;
172         }
173 
174         if (isWithinCurrentMonth(mRow, mColumn)) {
175             return false;
176         }
177 
178         // need to flip to first day of next month
179         nextMonth();
180         mRow = 0;
181         mColumn = 0;
182         while (!isWithinCurrentMonth(mRow, mColumn)) {
183             mColumn++;
184         }
185         return true;
186     }
187 
188 }
189