Android app using Google calendar - Sync issue - android

I am trying to create an android app that interfaces with the Google Calendar.
I have followed the tutorial using content providers from here. Parts of this code are explained here.
I am facing the below issues.
I created a new calendar TestCalendar from my online from my laptop, and marked it as Selected. When I query for my calendars from the app, I can see this new calendar, but it is shown as unselected (selected=0). Any suggestions on why this could be happening ?
From my app, I add an event to the calendar by
getContentResolver().insert(eventsUri, event);
The event is reflected in the calendar on phone, but it is not reflected in the online version. To push this new event online, I have to manually Synchronize the calendar, or turn the Auto Sync on, which I believe is not the right way in which this should be done. Any suggestions/links which could help ?

1) Can you dump your calendar and post the result?
Notice:
Android < API Lvl 14 you must set selected = 1
Android > API Lvl 14 you must set visible = 1 (selected is not longer available)
Dump:
cursor = contentResolver.query(Uri.parse(CALENDAR_URI),null, null, null,null);
while (cursor.moveToNext()) {
for (int i = 0; i < cursor.getColumnCount(); i++) {
Log.e("XXX", cursor.getColumnName(i) + ": " + cursor.getString(i));
}
}
CALENDAR_URI = content://com.android.calendar/calendars (since Froyo) or content://calendar/ (before Froyo)
2) https://stackoverflow.com/a/11652415/411951

Related

Android - reading google calendar events DOES NOT WORK anymore (there seems to be some kind of cache which does not return proper/fresh data)

I have a simple app which reads google calendar events and displays them in list. It works flawlessly on all android version - except 10. Minimalistic example of code:
Uri.Builder builder = Instances.CONTENT_URI.buildUpon();
ContentUris.appendId(builder, timeFrom);
ContentUris.appendId(builder, timeTo);
String projection = new String[] { Instances.EVENT_ID, Instances.TITLE };
String selection = null;
String args = null;
String sort = null;
cursor = contentResolver.query(builder.build(), projection, selection, args, sort);
As I said - works perfectly, except android 10.
On android 10 I noticed interesting anomaly - whenever I make a change in official Google Calendar app, my app does not reflect changes immediately - it takes a while.
Its completely random, sometimes changes are immediate or take seconds to show, but often its minutes and sometimes even hours - but if I go to Google Calendar app and manually press refresh there, it helps (again its random - 90% times it works, but occasionaly it doesnt)>
I am almost 100% sure Google changed something in their API on android 10 - some kind of cache - so it behaves randomly and unreliable now. All calendar widgets I tried have same problem, so probably its not the fault of an app )besides, the code was pretty much copied from official webpage).
How to solve this problem? Why on android 10 changes are not immediate? Is there a way to programatically trigger syncing/refresh of Google calendar from within my app?

Is there a way to set up a push notification from Google Calendar to my android app?

I am using the Google Calendar API with the READ_CALENDAR permission to query certain soccer games from a calendar and display them integrated with results from my referee app. Currently I query the calendar onConnected which is not often enough if I add a new game in the user's calendar.
Obviously I can requery the calendar API whenever I display data, but I was hoping to listen for a push notification from the Calendar API saying that a new event had been added. The API here: https://developer.android.com/guide/topics/providers/calendar-provider.html doesn't mention anything.
However, here: https://developers.google.com/google-apps/calendar/v3/push talks about push notifications to your own server. Does anybody know a way to do the equivalent from an app?
UPDATE from answer. Does this code look reasonable, especially with respect to leaking resources?
private void updateGamesFromCalendar() {
//get Calendar games if allowed
if (checkCalendar()) {
//Look for events with the keyword list in the Title, and dates between today-7 and today+7
long nowMs = System.currentTimeMillis();
String[] eventSelectionArgs = new String[]{Long.toString(nowMs - TimeUnit.DAYS.toMillis(7)),Long.toString(nowMs + TimeUnit.DAYS.toMillis(7)) };
if ((mEventsCursor != null) && (mCalendarObserver != null)) mEventsCursor.unregisterContentObserver(mCalendarObserver);
mEventsCursor = getContentResolver().query(RefWatchSettings.uriEvents, RefWatchSettings.EVENT_PROJECTION, RefWatchSettings.whereClauseEvents, eventSelectionArgs, RefWatchSettings.orderByEvents);
if (mCalendarObserver == null) mCalendarObserver = new CalendarEventObserver(null);
mEventsCursor.registerContentObserver(mCalendarObserver);
//delete future Calendar games and replace with the new query
Game.removeFutureCalendarGames(mGames);
removeFutureCalendarGamesFromDataItem();
getGamesFromCalendar();
refreshGameFragments();
sendFutureGameNotifications();
}
}
Calendar events are retrieved with queries (see Google Doc). Queries return Cursor objects (see Cursor JavaDoc). And Cursor objects can be observed with registerContentObserver().
So make a query on calendar events, register a Content Observer and let your Cursor open. You should receive updates of events through your Content Observer.

Delete calendar event on Android (Samsung S-Planner)

I am working with the native calendar, and I want to delete the events in native calendar. Provided that the event ID is known.
for (long eventID : eventIDList) {
// delete from native calendar
Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID);
int isDeleted = mContentResolver.delete(deleteUri, null, null);
Log.i("LOG", "Event deleted: " + String.valueOf(isDeleted));
}
I have checked with the log. It returns "1" which means 1 row is deleted.
It works well on normal Android Device, and the record is deleted.
But the problem appears in Samsung's Android device (S-Planner). After the delete cmd run, isDeleted returns 1. Sadly when I do a query through ContentResolver, the record supposed to be deleted still exists. (In S-Planner, the deleted won't be shown, but it is meaningless for me in this project)
Is there any workout to fix it?
** I have checked the delete cmd works fine on non-samsung devices, and isDelete returns 1 for all devices.
Thank you!!

"SQLiteException: no such column: mimetype" when using ContactsContract in Android

I want to delete an entry from Android's internal database table ContactsContract, i.e. an event such as a birthday should be removed from the contact's entry.
The following code works pretty well, but some users (ca. 1%) have this crashing with an SQLException. So is there anything wrong in my code or is it just that their device doesn't support Android's ContactsContract correctly?
try {
ArrayList<Long> rawContactIDs = getRawContactID(o.getID());
int rawContactCount = rawContactIDs.size();
for (int r = 0; r < rawContactCount; r++) {
long rawContactID = rawContactIDs.get(r);
String where = ContactsContract.Data.MIMETYPE+" = ? AND "+ContactsContract.Data.RAW_CONTACT_ID+" = ? AND "+ContactsContract.CommonDataKinds.Event.TYPE+" = ?";
String[] selection = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, String.valueOf(MY_RAW_CONTACT_ID), String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) };
getContentResolver().delete(ContactsContract.Data.CONTENT_URI, where, selection);
}
}
catch (Exception e) {}
The exception that is thrown is:
android.database.sqlite.SQLiteException: no such column: mimetype: , while
compiling: DELETE FROM data WHERE mimetype = ? AND raw_contact_id = ? AND
data2 = ?
at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
at
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
at
android.content.ContentProviderProxy.delete(ContentProviderNative.java:472)
at android.content.ContentResolver.delete(ContentResolver.java:700)
at ... MY_ACTIVITY ...
It is possible for device manufacturers to change the implementation of a ContentProvider that ships with the AOSP. In theory, those changes would be caught by the Compatibility Test Suite (CTS), so that only devices that don't break the API will run your app when shipped through the Play Store.
That being said...
The CTS is far from complete
The CTS does not protect you if you intentionally distribute your app beyond the Play Store, and somebody with an incompatible device runs your app
The CTS does not protect you if you unintentionally distribute your app beyond the Play Store (i.e., your app's been pirated), and somebody with an incompatible device runs your app
Various device manufacturers pirate the Play Store app itself
So, when you see problems that would appear to originate from within an OS-supplied ContentProvider, but those problems are infrequent and/or are on unrecognized devices, don't panic. You still might choose to somehow fail gracefully in this case (by wrapping the failing calls in your own exception handler), but it's unlikely that your code is really the source of the difficulty.
The ContactsContract API has been available since level 5. Is it possible the users reporting this issue are < level 5 (Donut, Cupcake)?

Get the list of enabled alarm using default sdk?

I've been seeking how to simply get a list of all enabled alarm.
So far I found two things:
String nextAlarm = Settings.System.getString(getContentResolver(),
Settings.System.NEXT_ALARM_FORMATTED);
And this project but it uses:
public static final Uri CONTENT_URI =
Uri.parse("content://com.android.deskclock/alarm");
ContentResolver cr = getContentResolver();
Cursor c = null;
c = cr.query(
CONTENT_URI, ALARM_QUERY_COLUMNS,
null, null, DEFAULT_SORT_ORDER);
And as said by CommonsWare here: "This is not part of the Android SDK. It may not work on all devices. It may not work on all current Android versions. It may not work on future Android versions. It is not documented. It is not supported".
So far, the first one works but gives only one alarm when I want all of them. And the second one is just not good.
So how can I get a list of upcoming alarm ?
Thanks in advance.
So how can I get a list of upcoming alarm ?
You can't, any more than you can get the "list of upcoming alarm" from the hundreds of other alarm clock apps that users use (distributed on the Market, on devices that replace com.android.deskclock, etc.).

Categories

Resources