1146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project/*
2146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
3146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
4146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * you may not use this file except in compliance with the License.
6146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * You may obtain a copy of the License at
7146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
8146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project *
10146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * See the License for the specific language governing permissions and
14146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project * limitations under the License.
15146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project */
16146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
17146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectpackage com.android.calendar;
18146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
19146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectimport android.graphics.Rect;
20146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
21146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Projectpublic class EventGeometry {
22146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    // This is the space from the grid line to the event rectangle.
23146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    private int mCellMargin = 0;
24146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
25146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    private float mMinuteHeight;
261b57aa6488359a09a8ff280468a3629be2490000Erik
27146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    private float mHourGap;
28146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    private float mMinEventHeight;
291b57aa6488359a09a8ff280468a3629be2490000Erik
30146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    void setCellMargin(int cellMargin) {
31146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        mCellMargin = cellMargin;
32146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
331b57aa6488359a09a8ff280468a3629be2490000Erik
34146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    void setHourGap(float gap) {
35146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        mHourGap = gap;
36146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
371b57aa6488359a09a8ff280468a3629be2490000Erik
38146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    void setMinEventHeight(float height) {
39146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        mMinEventHeight = height;
40146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
411b57aa6488359a09a8ff280468a3629be2490000Erik
42146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    void setHourHeight(float height) {
43146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        mMinuteHeight = height / 60.0f;
44146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
451b57aa6488359a09a8ff280468a3629be2490000Erik
46146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    // Computes the rectangle coordinates of the given event on the screen.
47146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    // Returns true if the rectangle is visible on the screen.
48146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    boolean computeEventRect(int date, int left, int top, int cellWidth, Event event) {
49146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (event.allDay) {
50146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return false;
51146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
52146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
53146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float cellMinuteHeight = mMinuteHeight;
54146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int startDay = event.startDay;
55146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int endDay = event.endDay;
561b57aa6488359a09a8ff280468a3629be2490000Erik
57146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (startDay > date || endDay < date) {
58146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return false;
59146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
601b57aa6488359a09a8ff280468a3629be2490000Erik
61146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int startTime = event.startTime;
62146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int endTime = event.endTime;
631b57aa6488359a09a8ff280468a3629be2490000Erik
64146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // If the event started on a previous day, then show it starting
65146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // at the beginning of this day.
66146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (startDay < date) {
67146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            startTime = 0;
68146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
691b57aa6488359a09a8ff280468a3629be2490000Erik
70146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // If the event ends on a future day, then show it extending to
71146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // the end of this day.
72146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (endDay > date) {
73146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            endTime = CalendarView.MINUTES_PER_DAY;
74146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
75146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
76146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int col = event.getColumn();
77146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int maxCols = event.getMaxColumns();
78146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int startHour = startTime / 60;
79146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        int endHour = endTime / 60;
80146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
81146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // If the end point aligns on a cell boundary then count it as
82146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // ending in the previous cell so that we don't cross the border
83146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // between hours.
84146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (endHour * 60 == endTime)
85146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            endHour -= 1;
86146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
87146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.top = top;
88146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.top += (int) (startTime * cellMinuteHeight);
89146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.top += startHour * mHourGap;
90146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
91146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.bottom = top;
92146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.bottom += (int) (endTime * cellMinuteHeight);
93146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.bottom += endHour * mHourGap;
94146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
95146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // Make the rectangle be at least mMinEventHeight pixels high
96146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (event.bottom < event.top + mMinEventHeight) {
97146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            event.bottom = event.top + mMinEventHeight;
98146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
991b57aa6488359a09a8ff280468a3629be2490000Erik
100146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float colWidth = (float) (cellWidth - 2 * mCellMargin) / (float) maxCols;
101146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.left = left + mCellMargin + col * colWidth;
102146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        event.right = event.left + colWidth;
103146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        return true;
104146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
105146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project
106146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    /**
107146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * Returns true if this event intersects the selection region.
108146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     */
109146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    boolean eventIntersectsSelection(Event event, Rect selection) {
110146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (event.left < selection.right && event.right >= selection.left
111146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                && event.top < selection.bottom && event.bottom >= selection.top) {
112146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return true;
113146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
114146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        return false;
115146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
1161b57aa6488359a09a8ff280468a3629be2490000Erik
117146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    /**
118146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     * Computes the distance from the given point to the given event.
119146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project     */
120146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    float pointToEvent(float x, float y, Event event) {
121146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float left = event.left;
122146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float right = event.right;
123146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float top = event.top;
124146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float bottom = event.bottom;
1251b57aa6488359a09a8ff280468a3629be2490000Erik
126146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (x >= left) {
127146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            if (x <= right) {
128146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                if (y >= top) {
129146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                    if (y <= bottom) {
130146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                        // x,y is inside the event rectangle
131146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                        return 0f;
132146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                    }
133146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                    // x,y is below the event rectangle
134146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                    return y - bottom;
135146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                }
136146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                // x,y is above the event rectangle
137146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                return top - y;
138146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            }
1391b57aa6488359a09a8ff280468a3629be2490000Erik
140146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            // x > right
141146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            float dx = x - right;
142146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            if (y < top) {
143146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                // the upper right corner
144146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                float dy = top - y;
145146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                return (float) Math.sqrt(dx * dx + dy * dy);
146146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            }
147146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            if (y > bottom) {
148146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                // the lower right corner
149146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                float dy = y - bottom;
150146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project                return (float) Math.sqrt(dx * dx + dy * dy);
151146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            }
152146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            // x,y is to the right of the event rectangle
153146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return dx;
154146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
155146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // x < left
156146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        float dx = left - x;
157146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (y < top) {
158146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            // the upper left corner
159146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            float dy = top - y;
160146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return (float) Math.sqrt(dx * dx + dy * dy);
161146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
162146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        if (y > bottom) {
163146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            // the lower left corner
164146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            float dy = y - bottom;
165146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project            return (float) Math.sqrt(dx * dx + dy * dy);
166146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        }
167146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        // x,y is to the left of the event rectangle
168146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project        return dx;
169146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project    }
170146de36083f6ce8b7e8a1f974d3990594a36bfecThe Android Open Source Project}
171