summaryrefslogtreecommitdiffstats
path: root/src/com/android/calendar/EventGeometry.java
blob: ebfa6a3e7410621a53f21e7a845c0be8dda27cb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.calendar;

import android.graphics.Rect;

public class EventGeometry {
    // This is the space from the grid line to the event rectangle.
    private int mCellMargin = 0;

    private float mMinuteHeight;
    
    private float mHourGap;
    private float mMinEventHeight;
    
    void setCellMargin(int cellMargin) {
        mCellMargin = cellMargin;
    }
    
    void setHourGap(float gap) {
        mHourGap = gap;
    }
    
    void setMinEventHeight(float height) {
        mMinEventHeight = height;
    }
    
    void setHourHeight(float height) {
        mMinuteHeight = height / 60.0f;
    }
    
    // Computes the rectangle coordinates of the given event on the screen.
    // Returns true if the rectangle is visible on the screen.
    boolean computeEventRect(int date, int left, int top, int cellWidth, Event event) {
        if (event.allDay) {
            return false;
        }

        float cellMinuteHeight = mMinuteHeight;
        int startDay = event.startDay;
        int endDay = event.endDay;
        
        if (startDay > date || endDay < date) {
            return false;
        }
        
        int startTime = event.startTime;
        int endTime = event.endTime;
        
        // If the event started on a previous day, then show it starting
        // at the beginning of this day.
        if (startDay < date) {
            startTime = 0;
        }
        
        // If the event ends on a future day, then show it extending to
        // the end of this day.
        if (endDay > date) {
            endTime = CalendarView.MINUTES_PER_DAY;
        }

        int col = event.getColumn();
        int maxCols = event.getMaxColumns();
        int startHour = startTime / 60;
        int endHour = endTime / 60;

        // If the end point aligns on a cell boundary then count it as
        // ending in the previous cell so that we don't cross the border
        // between hours.
        if (endHour * 60 == endTime)
            endHour -= 1;

        event.top = top;
        event.top += (int) (startTime * cellMinuteHeight);
        event.top += startHour * mHourGap;

        event.bottom = top;
        event.bottom += (int) (endTime * cellMinuteHeight);
        event.bottom += endHour * mHourGap;

        // Make the rectangle be at least mMinEventHeight pixels high
        if (event.bottom < event.top + mMinEventHeight) {
            event.bottom = event.top + mMinEventHeight;
        }
        
        float colWidth = (float) (cellWidth - 2 * mCellMargin) / (float) maxCols;
        event.left = left + mCellMargin + col * colWidth;
        event.right = event.left + colWidth;
        return true;
    }
    
    // Computes the busy bits.  For each interval containing "interval" minutes,
    // the busy bit for that interval is set to 1 if the given event overlaps
    // that interval.
    void computeBusyBits(int firstDate, int numDays, byte[][] busyBits, Event event, int interval) {
        if (event.allDay) {
            return;
        }

        int endDate = firstDate + numDays;
        int startDay = event.startDay;
        int endDay = event.endDay;
        if (startDay >= endDate || endDay < firstDate) {
            return;
        }

        int startTime = event.startTime;
        
        int day = startDay;
        
        // If the event started on a previous day, then show it starting
        // at the beginning of this day.
        if (day < firstDate) {
            day = firstDate;
            startTime = 0;
        }
        
        if (endDay >= endDate) {
            endDay = endDate - 1;
        }
        
        int dayIndex = day - firstDate;
        while (day <= endDay) {
            int endTime = event.endTime;
            // If the event ends on a future day, then show it extending to
            // the end of this day.
            if (endDay > day) {
                endTime = CalendarView.MINUTES_PER_DAY;
            }

            int startInterval = startTime / interval;
            int endInterval = (endTime + interval - 1) / interval;
            
            for (int ii = startInterval; ii < endInterval; ii++) {
                busyBits[dayIndex][ii] = 1;
            }
            day += 1;
            dayIndex += 1;
            startTime = 0;
        }
    }
    
    /**
     * Returns true if this event intersects the selection region.
     */
    boolean eventIntersectsSelection(Event event, Rect selection) {
        if (event.left < selection.right && event.right >= selection.left
                && event.top < selection.bottom && event.bottom >= selection.top) {
            return true;
        }
        return false;
    }
    
    /**
     * Computes the distance from the given point to the given event.
     */
    float pointToEvent(float x, float y, Event event) {
        float left = event.left;
        float right = event.right;
        float top = event.top;
        float bottom = event.bottom;
        
        if (x >= left) {
            if (x <= right) {
                if (y >= top) {
                    if (y <= bottom) {
                        // x,y is inside the event rectangle
                        return 0f;
                    }
                    // x,y is below the event rectangle
                    return y - bottom;
                }
                // x,y is above the event rectangle
                return top - y;
            }
            
            // x > right
            float dx = x - right;
            if (y < top) {
                // the upper right corner
                float dy = top - y;
                return (float) Math.sqrt(dx * dx + dy * dy);
            }
            if (y > bottom) {
                // the lower right corner
                float dy = y - bottom;
                return (float) Math.sqrt(dx * dx + dy * dy);
            }
            // x,y is to the right of the event rectangle
            return dx;
        }
        // x < left
        float dx = left - x;
        if (y < top) {
            // the upper left corner
            float dy = top - y;
            return (float) Math.sqrt(dx * dx + dy * dy);
        }
        if (y > bottom) {
            // the lower left corner
            float dy = y - bottom;
            return (float) Math.sqrt(dx * dx + dy * dy);
        }
        // x,y is to the left of the event rectangle
        return dx;
    }
}