I want to show calendar events (daily agenda) from specific calendars on an Android Wear watchface.
Is the Calendar Provider available for query in an Android Wear Watchface?
How do I make sure it stays in sync throughout the day?
Depends on what info you need to look up. See WearableCalendarContract (though I don't think they've posted the JavaDocs yet, so I've only found a little info here). I'm able to, for example, get a range of event instances with this:
long begin = _time.toMillis(false);
Uri.Builder builder = WearableCalendarContract.Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, begin);
ContentUris.appendId(builder, begin + DateUtils.DAY_IN_MILLIS);
Uri instURI = builder.build();
Cursor cursor = getContentResolver().query(instURI, INSTANCE_PROJECTION, null, null, null);
But my attempts at reading the event table to find out more about the instances haven't worked (note that the WearableCalendarContract doesn't include an Events URI). I've tried both authorities to no avail.
AFAIK, the content provider is not available. Take a look into how to sync data in here: http://developer.android.com/training/wearables/data-layer/index.html
Related
I have a question about JobScheduler and would be very happy if someone could solve my problem. I have a Trigger-based Job watching for changes in the Contact photo on an android phone:
static {
JobInfo.Builder builder = new JobInfo.Builder(JobIds.PHOTOS_CONTENT_JOB,
new ComponentName("com.android.agent", AgentService.class.getName()));
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(
ContactsContract.DisplayPhoto.CONTENT_URI,
JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
builder.setTriggerContentMaxDelay(1000);
JOB_INFO = builder.build();
}
I get a reaction from the phone and a URI, the problem I have is the only URI information I get is: content://com.android.contacts
What I need is some kind of information on what Contact just changed or what Id the photo just added have.
If someone could help, many thanks in advance.
(Not the same question as Android - Get only newest contacts that were added but the same answer)
Starting with API level 18, you can use Contacts.CONTACT_LAST_UPDATED_TIMESTAMP, so it's possible to query for all contacts that had been modified (or created) recently, and compare only those to your last cache of contact ids, and the delta would be the contacts created/modified since the last time your code ran.
I inserted a calender contract entry like this:
ContentValues values = new ContentValues();
values.put(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
values.put(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
values.put(CalendarContract.Calendars.NAME, name);
values.put(CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, getDisplayName(account));
values.put(CalendarContract.Calendars.CALENDAR_COLOR, 0xffff0000);
values.put(CalendarContract.Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_READ);
values.put(CalendarContract.Calendars.OWNER_ACCOUNT, getMailAddressOf(account));
values.put(CalendarContract.Calendars.CALENDAR_TIME_ZONE, TimeZone.getTimeZone("GMT").getDisplayName());
values.put(CalendarContract.Calendars.SYNC_EVENTS, 1);
Uri.Builder builder = CalendarContract.Calendars.CONTENT_URI.buildUpon();
builder.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, account.name);
builder.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, account.type);
builder.appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true");
Uri result = getContext().getContentResolver().insert(builder.build(), values);
This works great, I can enter calender entries etc. But I need to make it now read only so that the user cannot edit the calendar entry.
I thought that when I set CALENDAR_ACCESS_LEVEL to CAL_ACCESS_READ it should be enough to make the calendar read only.
Any idea how to achieve that? By the way I'm testing on a Pixel with Android O.
Don't make the user to be the organizer. When adding the event, the default organizer will be the user. To change it, add this line to the ContentValues when adding the event, assign an organizer email address like this
values.put(CalendarContract.Events.ORGANIZER,"an_organizer_name#gmail.com";
Since the user is not the organizer, the user can still delete the event but not able to edit the event content such as time, title, description, etc.
Well that took some time. In the end I found out that I have to change the mail address of the CalendarContract.Events.ORGANIZER value to prevent that I can edit the calender entry. Just as a hint for others how I sloved the problem I tried a lot of stupid things but then I had the idea to check how it is done in AOSP. So I just searched for "calendar aosp code" and found the code mirrowed on GitHub. After using the code search within the repo I found the method canModifyEvent(...). Now it became oblivious:
public static boolean canModifyEvent(CalendarEventModel model) {
return canModifyCalendar(model)
&& (model.mIsOrganizer || model.mGuestsCanModify);
}
Make sure that the event cannot been edited by guests and by don't be the organizer.
Happy Coding!
I'm trying to delete future instances of a google calendar recurring (repeated) event pro-grammatically using content resolver.
And to do this i have updated the rRule of the event , so for example if want to delete future instances of an event starting from date 11/11/2016
i edit the rRule string to look like so:
FREQ=DAILY;UNTIL=20161111;WKST=SU
However , when viewing google calendar application i find no changes and i find the event color only has changed to black color.
Some notes to keep in mind:
1- I'm using a third party library to so :
https://github.com/EverythingMe/easy-content-providers
CalendarProvider calendarProvider = new CalendarProvider(context);
Event event = calendarProvider.getEvent(eventId);
event.rRule = "FREQ=DAILY;UNTIL=20161111;WKST=SU";
calendarProvider.update(event);
and all functionalities in this library seem to work fine.
2- While reading pro-grammatically recurring events that have a specific UNTIL date in it's rRule , i have realized that also a field in the google event called "lastDate" is updated with one hour later after the UNTIL value ,so do i have to update also this field while updating the UNTIL value in the rRule?
The problem was with the library ,
calendarProvider.update(event);
does not seems to work with all event fields!
This might be a stretch, but I was wondering if it is possible to add functionality to an application that is already created for the android device. Specifically, I would like to send a broadcast whenever the user tries to make a new search in the internet browser. There might be another way to act only when the user searches the browser, but I thought this would be the easiest. If this isn't possible (or is completely the wrong way of going about this), please let me know. Any help is appreciated.
You can probably query the Browser provider and get search data from it. Here is some basic code on how to do it:
Cursor cursor = this.context.getContentResolver().query(Browser.SEARCHES_URI, null, null, null, null);
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
final int indexDate = cursor.getColumnIndex(Browser.SearchColumns.DATE);
final int indexTerm = cursor.getColumnIndex(Browser.SearchColumns.SEARCH);
String date = cursor.getString(indexDate);
String term = cursor.getString(indexTerm);
cursor.moveToNext();
}
}
cursor.close();
Keep in mind that it would be better if you run queries on a separate Thread using the Loader framework.
I do not think there is a broadcast sent when a new search is performed.
i developed an app-feature to create and sync a local phone calendar through an app. Now i have to sync this calendar online, best through the native phone account.
I tried to extend the URI's content values parameter (see API LVL < 8 parameter list below) by the account name and type but running after that into calendar freezes.
_id
sync_account
_sync_account_type
_sync_id
_sync_version
_sync_time
_sync_local_id
_sync_dirty
_sync_mark
url
name
displayName
hidden
color
access_level
selected
sync_events
location
timezone
ownerAccount
Is there another way to create an online syncable google calendar?
You cannot create a new one programatically.
Instead parse all available ones with
Uri.parse("content://com.android.calendar/calendars",null, null, null, null);
and fill in your events into a existing one.
But ensure, that your event got the same
sync_account
and
_sync_account_type