aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/lockclock/calendar/CalendarWidgetService.java
blob: a0732800416758f5bfeaf508fe3f8c91bf8719f9 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * Copyright (C) 2013 The CyanogenMod Project (DvTonder)
 *
 * 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.cyanogenmod.lockclock.calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.text.style.StyleSpan;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;
import android.widget.RemoteViewsService.RemoteViewsFactory;

import com.cyanogenmod.lockclock.ClockWidgetProvider;
import com.cyanogenmod.lockclock.ClockWidgetService;
import com.cyanogenmod.lockclock.R;
import com.cyanogenmod.lockclock.calendar.CalendarInfo.EventInfo;
import com.cyanogenmod.lockclock.misc.Constants;
import com.cyanogenmod.lockclock.misc.Preferences;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

public class CalendarWidgetService extends RemoteViewsService {

    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new CalendarRemoteViewsFactory(this.getApplicationContext(), intent);
    }

}

class CalendarRemoteViewsFactory implements RemoteViewsFactory {
    private static final String TAG = "CalendarRemoteViewsFactory";
    private static boolean D = Constants.DEBUG;

    private static final long UPCOMING_EVENT_HOURS_IN_MILLIS =
            Constants.CALENDAR_UPCOMING_EVENTS_FROM_HOUR * 60L * 60L * 1000L;
    private static final long DAY_IN_MILLIS = 24L * 60L * 60L * 1000L;

    private Context mContext;
    private CalendarInfo mCalendarInfo = new CalendarInfo();

    public CalendarRemoteViewsFactory(Context applicationContext, Intent intent) {
        mContext = applicationContext;
    }

    @Override
    public int getCount() {
        return mCalendarInfo.getEvents().size();
    }

    @Override
    public long getItemId(int position) {
        return mCalendarInfo.getEvents().get(position).id;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    private SpannableString getSpannableString(String text, boolean bold) {
        if (text == null) {
            return null;
        }
        SpannableString spanText = new SpannableString(text);
        if (bold) {
            spanText.setSpan(new StyleSpan(Typeface.BOLD), 0, text.length(), 0);
        }
        return spanText;
    }

    private long getStartOfDay() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTimeInMillis();
    }

    private boolean isUpcoming(EventInfo event) {
        long startOfDay = getStartOfDay();
        long now = System.currentTimeMillis();
        long endOfUpcoming;

        if (startOfDay + UPCOMING_EVENT_HOURS_IN_MILLIS > now) {
            endOfUpcoming = startOfDay + DAY_IN_MILLIS;
        } else {
            endOfUpcoming = startOfDay + 2 * DAY_IN_MILLIS;
        }
        return event.start < endOfUpcoming;
    }

    @Override
    public RemoteViews getViewAt(int position) {
        if (position < 0 || position >= mCalendarInfo.getEvents().size()) {
            return null;
        }

        boolean highlightNext = Preferences.calendarHighlightUpcomingEvents(mContext);
        boolean nextBold = Preferences.calendarUpcomingEventsBold(mContext);
        int color, detailsColor;
        final RemoteViews itemViews = new RemoteViews(mContext.getPackageName(),
                R.layout.calendar_item);
        final EventInfo event = mCalendarInfo.getEvents().get(position);

        // Add the event text fields
        if (highlightNext && isUpcoming(event)) {
            color = Preferences.calendarUpcomingEventsFontColor(mContext);
            detailsColor = Preferences.calendarUpcomingEventsDetailsFontColor(mContext);
            itemViews.setTextViewText(R.id.calendar_event_title, getSpannableString(event.title, nextBold));
            itemViews.setTextViewText(R.id.calendar_event_details, getSpannableString(event.description, nextBold));
        } else {
            color = Preferences.calendarFontColor(mContext);
            detailsColor = Preferences.calendarDetailsFontColor(mContext);
            itemViews.setTextViewText(R.id.calendar_event_title, event.title);
            itemViews.setTextViewText(R.id.calendar_event_details, event.description);
        }
        itemViews.setTextColor(R.id.calendar_event_title, color);
        itemViews.setTextColor(R.id.calendar_event_details, detailsColor);
        if (D) Log.v(TAG, "Showing at position " + position + " event: " + event.title);

        final Intent fillInIntent = new Intent();
        fillInIntent.setData(ContentUris.withAppendedId(Events.CONTENT_URI, event.id));
        // work around stock calendar not displaying the correct date with only uri
        fillInIntent.putExtra("beginTime", event.start);
        fillInIntent.putExtra("endTime", event.end);
        fillInIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NO_HISTORY
                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        itemViews.setOnClickFillInIntent(R.id.calendar_item, fillInIntent);

        return itemViews;
    }

    @Override
    public int getViewTypeCount() {
        // There's only one view type for the events
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public void onCreate() {
        updateCalendarInfo(mContext);
        updatePanelVisibility();
    }

    @Override
    public void onDataSetChanged() {
        if (D) Log.v(TAG, "onDataSetChanged()");
        updateCalendarInfo(mContext);
        updatePanelVisibility();
    }

    private void updateCalendarInfo(Context context) {
        // Load the settings
        Set<String> calendarList = Preferences.calendarsToDisplay(context);
        boolean remindersOnly = Preferences.showEventsWithRemindersOnly(context);
        boolean hideAllDay = !Preferences.showAllDayEvents(context);
        long lookAhead = Preferences.lookAheadTimeInMs(context);

        if (D) Log.d(TAG, "Checking for calendar events...");
        getCalendarEvents(context, lookAhead, calendarList, remindersOnly, hideAllDay);
        scheduleCalendarUpdate(context);
    }

    /**
     * Trigger the hiding of the Calendar panel if there are no events to display
     */
    private void updatePanelVisibility() {
        if (!mCalendarInfo.hasEvents()) {
            if (D) Log.v(TAG, "No events - Hide calendar panel");
            Intent updateIntent = new Intent(mContext, ClockWidgetProvider.class);
            updateIntent.setAction(ClockWidgetService.ACTION_HIDE_CALENDAR);
            mContext.sendBroadcast(updateIntent);
        }
    }

    /**
     * Get the next set of calendar events (up to MAX_CALENDAR_ITEMS) within a
     * certain look-ahead time. Result is stored in the CalendarInfo object
     */
    private void getCalendarEvents(Context context, long lookahead, Set<String> calendars,
            boolean remindersOnly, boolean hideAllDay) {
        long now = System.currentTimeMillis();
        long later = now + lookahead;
        CalendarInfo newCalendarInfo = new CalendarInfo();

        // Build the 'where' clause
        StringBuilder where = new StringBuilder();
        if (remindersOnly) {
            where.append(CalendarContract.Events.HAS_ALARM + "=1");
        }
        if (hideAllDay) {
            if (remindersOnly) {
                where.append(" AND ");
            }
            where.append(CalendarContract.Events.ALL_DAY + "!=1");
        }
        if (calendars != null && calendars.size() > 0) {
            if (remindersOnly || hideAllDay) {
                where.append(" AND ");
            }
            where.append(CalendarContract.Events.CALENDAR_ID + " in (");
            int i = 0;
            for (String s : calendars) {
                where.append(s);
                if (i != calendars.size() - 1) {
                    where.append(",");
                }
                i++;
            }
            where.append(") ");
        }

        // Projection array
        String[] projection = new String[] {
                CalendarContract.Instances.EVENT_ID,
                CalendarContract.Events.TITLE,
                CalendarContract.Instances.BEGIN,
                CalendarContract.Instances.END,
                CalendarContract.Events.DESCRIPTION,
                CalendarContract.Events.EVENT_LOCATION,
                CalendarContract.Events.ALL_DAY,
        };

        // all day events are stored in UTC, that is why we need to fetch events after 'later'
        Uri uri = Uri.withAppendedPath(CalendarContract.Instances.CONTENT_URI,
                String.format("%d/%d", now - DAY_IN_MILLIS, later + DAY_IN_MILLIS));
        Cursor cursor = context.getContentResolver().query(uri, projection,
                where.toString(), null, CalendarContract.Instances.BEGIN + " ASC");

        if (cursor != null) {
            // The indices for the projection array
            final int indexEventId = cursor.getColumnIndex(CalendarContract.Instances.EVENT_ID);
            final int indexTitle = cursor.getColumnIndex(CalendarContract.Events.TITLE);
            final int indexBeginTime = cursor.getColumnIndex(CalendarContract.Instances.BEGIN);
            final int indexEndTime = cursor.getColumnIndex(CalendarContract.Instances.END);
            final int indexDescription = cursor.getColumnIndex(CalendarContract.Events.DESCRIPTION);
            final int indexLocation = cursor.getColumnIndex(CalendarContract.Events.EVENT_LOCATION);
            final int indexAllDay = cursor.getColumnIndex(CalendarContract.Events.ALL_DAY);

            final int showLocation = Preferences.calendarLocationMode(context);
            final int showDescription = Preferences.calendarDescriptionMode(context);
            final Time time = new Time();
            int eventCount = 0;

            // Iterate through returned rows to a maximum number of calendar events
            while (cursor.moveToNext() && eventCount < Constants.MAX_CALENDAR_ITEMS) {
                final long eventId = cursor.getLong(indexEventId);
                final String title = cursor.getString(indexTitle);
                long begin = cursor.getLong(indexBeginTime);
                long end = cursor.getLong(indexEndTime);
                final String description = cursor.getString(indexDescription);
                final String location = cursor.getString(indexLocation);
                final boolean allDay = cursor.getInt(indexAllDay) != 0;
                int format = 0;

                if (allDay) {
                    begin = convertUtcToLocal(time, begin);
                    end = convertUtcToLocal(time, end);
                }

                if (end < now || begin > later) {
                    continue;
                }

                if (D) Log.v(TAG, "Adding event: " + title + " with id: " + eventId);

                // Start building the event details string
                // Starting with the date
                StringBuilder sb = new StringBuilder();

                if (allDay) {
                    format = Constants.CALENDAR_FORMAT_ALLDAY;
                } else if (DateUtils.isToday(begin)) {
                    format = Constants.CALENDAR_FORMAT_TODAY;
                } else {
                    format = Constants.CALENDAR_FORMAT_FUTURE;
                }
                if (allDay || begin == end) {
                    sb.append(DateUtils.formatDateTime(context, begin, format));
                } else {
                    sb.append(DateUtils.formatDateRange(context, begin, end, format));
                }

                // Add the event location if it should be shown
                if (showLocation != Preferences.SHOW_NEVER && !TextUtils.isEmpty(location)) {
                    switch (showLocation) {
                        case Preferences.SHOW_FIRST_LINE:
                            int stringEnd = location.indexOf('\n');
                            if (stringEnd == -1) {
                                sb.append(": " + location);
                            } else {
                                sb.append(": " + location.substring(0, stringEnd));
                            }
                            break;
                        case Preferences.SHOW_ALWAYS:
                            sb.append(": " + location);
                            break;
                    }
                }

                // Add the event description if it should be shown
                if (showDescription != Preferences.SHOW_NEVER
                        && !TextUtils.isEmpty(description)) {
                    // Show the appropriate separator
                    if (showLocation == Preferences.SHOW_NEVER) {
                        sb.append(": ");
                    } else {
                        sb.append(" - ");
                    }

                    switch (showDescription) {
                        case Preferences.SHOW_FIRST_LINE:
                            int stringEnd = description.indexOf('\n');
                            if (stringEnd == -1) {
                                sb.append(description);
                            } else {
                                sb.append(description.substring(0, stringEnd));
                            }
                            break;
                        case Preferences.SHOW_ALWAYS:
                            sb.append(description);
                            break;
                    }
                }

                // Add the event details to the CalendarInfo object and move to next record
                newCalendarInfo.addEvent(new EventInfo(eventId, title, sb.toString(), begin,
                        end, allDay));
                eventCount++;
            }
            cursor.close();
            mCalendarInfo = newCalendarInfo;
        }

        // check for first event outside of lookahead window
        long endOfLookahead = now + lookahead;
        long minUpdateTime = getMinUpdateFromNow(endOfLookahead);

        // don't bother with querying if the end result is later than the minimum update time anyway
        if (endOfLookahead < minUpdateTime) {
            if (where.length() > 0) {
                where.append(" AND ");
            }
            where.append(CalendarContract.Instances.BEGIN);
            where.append(" > ");
            where.append(endOfLookahead);

            uri = Uri.withAppendedPath(CalendarContract.Instances.CONTENT_URI,
                    String.format("%d/%d", endOfLookahead, minUpdateTime));
            projection = new String[] {
                    CalendarContract.Instances.BEGIN
            };
            cursor = context.getContentResolver().query(uri, projection, where.toString(), null,
                    CalendarContract.Instances.BEGIN + " ASC limit 1");

            if (cursor != null) {
                if (cursor.moveToFirst()) {
                    mCalendarInfo.setFollowingEventStart(cursor.getLong(0));
                }
                cursor.close();
            }
        }
    }

    private static long convertUtcToLocal(Time time, long utcTime) {
        time.timezone = Time.TIMEZONE_UTC;
        time.set(utcTime);
        time.timezone = Time.getCurrentTimezone();
        return time.normalize(true);
    }

    private static long getMinUpdateFromNow(long now) {
        // we update at least once a day
        return now + DAY_IN_MILLIS;
    }

    // ===============================================================================================
    // Update timer related functionality
    // ===============================================================================================
    /**
     * Calculates and returns the next time we should push widget updates.
     */
    private long calculateUpdateTime(Context context) {
        final long now = System.currentTimeMillis();
        final boolean highlightNext = Preferences.calendarHighlightUpcomingEvents(mContext);
        long lookAhead = Preferences.lookAheadTimeInMs(context);
        long minUpdateTime = getMinUpdateFromNow(now);

        // Check if there is a calendar event earlier
        for (EventInfo event : mCalendarInfo.getEvents()) {
            final long end = event.end;
            final long start = event.start;
            if (now < start) {
                minUpdateTime = Math.min(minUpdateTime, start);
            }
            if (now < end) {
                minUpdateTime = Math.min(minUpdateTime, end);
            }
        }

        if (mCalendarInfo.getFollowingEventStart() > 0) {
            // Make sure to update when the next event gets into the lookahead window
            minUpdateTime = Math.min(minUpdateTime, mCalendarInfo.getFollowingEventStart()
                    - lookAhead);
        }

        if (highlightNext) {
            // Update at midnight and at 8pm if highlighting of upcoming events is enabled
            final long startOfDay = getStartOfDay();
            if (now < startOfDay + UPCOMING_EVENT_HOURS_IN_MILLIS
                    && startOfDay + UPCOMING_EVENT_HOURS_IN_MILLIS < minUpdateTime) {
                minUpdateTime = startOfDay + UPCOMING_EVENT_HOURS_IN_MILLIS;
            } else if (startOfDay + DAY_IN_MILLIS < minUpdateTime) {
                minUpdateTime = startOfDay + DAY_IN_MILLIS;
            }
        }

        // Construct a log entry in human readable form
        if (D) {
            Date date1 = new Date(now);
            Date date2 = new Date(minUpdateTime);
            Log.i(TAG, "cLock: It is now " + DateFormat.getTimeFormat(context).format(date1)
                    + ", next widget update on " + DateFormat.getDateFormat(context).format(date2)
                    + " at " + DateFormat.getTimeFormat(context).format(date2));
        }

        // Return the next update time
        return minUpdateTime;
    }

    /**
     * Schedule an alarm to trigger an update at the next weather refresh or at
     * the next event time boundary (start/end).
     */
    private void scheduleCalendarUpdate(Context context) {
        PendingIntent pi = ClockWidgetService.getRefreshIntent(context);
        long updateTime = calculateUpdateTime(context);

        // Clear any old alarms and schedule the new alarm
        // Since the updates are now only done very infrequently, it can wake the device to ensure
        // the latest date is available when the user turns the screen on after a few hours sleep
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.cancel(pi);
        if (updateTime > 0) {
            am.set(AlarmManager.RTC_WAKEUP, updateTime, pi);
        }
    }

    @Override
    public void onDestroy() {
        mCalendarInfo.clearEvents();
    }
}