1

I'm making a basic Dashclock extension that polls CalendarContract.Events for a list of all calendar events synced to the user's device, retrieve the one that's scheduled to happen the soonest, and post the time, title, location, and desctiption. Here's my code:

public class FullCalService extends DashClockExtension {
    public static final String[] FIELDS = { Events._ID, Events.TITLE,
            Events.ALL_DAY, Events.EVENT_LOCATION, Events.DTSTART,
            Events.EVENT_TIMEZONE, Events.DESCRIPTION };

    public FullCalService() {
    }

    @Override
    protected void onUpdateData(int arg0) {
        TimeZone tz = TimeZone.getDefault();
        long currentTimeMillis = Calendar.getInstance().getTimeInMillis() - tz.getRawOffset();
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(this);
        Cursor c;
        if (prefs.getBoolean("allDayAllowed", false)) {
            c = getContentResolver().query(
                    Events.CONTENT_URI,
                    FIELDS,
                    new StringBuilder().append("(").append(Events.DTSTART)
                            .append(" >= ?)").toString(),
                    new String[] { Long.toString(currentTimeMillis) },
                    Events.DTSTART, null);
        } else {
            c = getContentResolver().query(
                    Events.CONTENT_URI,
                    FIELDS,
                    new StringBuilder().append("((").append(Events.ALL_DAY)
                            .append("= ?) AND (").append(Events.DTSTART)
                            .append(" >= ?))").toString(),
                    new String[] { Integer.toString(0),
                            Long.toString(currentTimeMillis) }, Events.DTSTART,
                    null);
        }
        if (c.moveToFirst()) {
            long eventTimeMillis = c.getLong(c.getColumnIndex(Events.DTSTART));
//          if (tz.inDaylightTime(new Date(eventTimeMillis))) {
//              eventTimeMillis += tz.getDSTSavings();
//          }
            //Log.d("DesCal service", "Value of hoursToReveal: "+prefs.getString("hoursToReveal", "1"));
            if (eventTimeMillis < currentTimeMillis + 360000
                    * Integer.parseInt(prefs.getString("hoursToReveal", "1"))) {
                String title = c.getString(c.getColumnIndex(Events.TITLE));
                String loc = c.getString(c
                        .getColumnIndex(Events.EVENT_LOCATION));
                String time = DateUtils.formatDateTime(this, eventTimeMillis,
                        DateUtils.FORMAT_SHOW_TIME);
                String desc = c.getString(c.getColumnIndex(Events.DESCRIPTION));
                StringBuilder expandedBody = new StringBuilder(time);
                if (!loc.isEmpty()){
                    expandedBody.append(" - ").append(loc);
                }
                expandedBody.append("\n").append(desc);
                String uri = new StringBuilder(
                        "content://com.android.calendar/events/").append(
                        c.getLong(c.getColumnIndex(Events._ID))).toString();
                publishUpdate(new ExtensionData()
                        .visible(true)
                        .status(time)
                        .expandedTitle(title)
                        .expandedBody(expandedBody.toString())
                        .icon(R.drawable.ic_dash_cal)
                        .clickIntent(
                                new Intent(Intent.ACTION_VIEW, Uri.parse(uri))));
                c.close();
            } else {
                publishUpdate(new ExtensionData().visible(false));
                c.close();
            }
        } else {
            publishUpdate(new ExtensionData().visible(false));
            c.close();
        }
    }
}

Upon first install, it appeared to work just fine. However, after the event began, it would not grab any future events. Is there a reason why the extension will not refresh itself?

MowDownJoe
  • 728
  • 1
  • 11
  • 29

1 Answers1

1

How are you triggering further updates? You need to manually specify when you'd like to have onUpdateData called, e.g. when there's a change to a content provider, or when the screen turns on, etc. Extensions by default refresh only every 30 minutes or so. See the source for the built in calendar extension for example code.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
  • To be honest, even with the default refresh rate, it doesn't seem to be getting new events. I would assume that `onUpdateData` would grab a new cursor each time it's called, but something there must not be happening. – MowDownJoe Oct 09 '13 at 23:45