Send Calendar Event via Chat (XMPP) Android - android

I'm just getting up to speed on Android, and I want to send calendar event to others from mobile. Same as whatsapp sending contacts. For sending Contact in chat (XMPP) I've used vCard. So for sending calendar event in chat what should I use?
I have searched a lot. But can't find something fruitful.
Please suggest the library or code snippet for sending calendar event in XMPP.
Thanks in advance.

You may refer to iCal Import Export allows you to import iCalender files to your calender without using google synchronization services.
Using this you can import all calendar events to iCalendar files and also export those events back to calendar. Using Calendar Provider you can fetch all calendar data, but requires android API level 14 or above while using iCal Import Export you can fetch all calendar data for lower API levels too.

manifest
<uses-permission android:name="android.permission.READ_CALENDAR" />
Code:
Cursor cursor = cr.query(Uri.parse("content://calendar/calendars"), new String[]{ "_id", "displayname" }, null, null, null);
cursor.moveToFirst();
String[] CalNames = new String[cursor.getCount()];
int[] CalIds = new int[cursor.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursor.getInt(0);
CalNames[i] = cursor.getString(1);
cursor.moveToNext();
}
cursor.close();
Fetching all events, and particular event is done by specifying range
ContentResolver contentResolver = getContentResolver();
Uri.Builder builder = Uri.parse(getCalendarUriBase() + "/instances/when").buildUpon();
long now = new Date().getTime();
ContentUris.appendId(builder, now - DateUtils.MILLIS_PER_DAY*10000);
ContentUris.appendId(builder, now + DateUtils.MILLIS_PER_DAY * 10000);
and then let's say you wish to log events ID from calendar with ID = 1
Cursor eventCursor = contentResolver.query(builder.build(),
new String[] { "event_id"}, "Calendars._id=" + 1,
null, "startDay ASC, startMinute ASC");
// For a full list of available columns see http://tinyurl.com/yfbg76w
while (eventCursor.moveToNext()) {
String uid2 = eventCursor.getString(0);
Log.v("eventID : ", uid2);
}
Now display this event in Listview select one of this for send as a text messsage and on receiver side :
Calendar cal = Calendar.getInstance();
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra("beginTime", cal.getTimeInMillis());
intent.putExtra("allDay", true);
intent.putExtra("rrule", "FREQ=YEARLY");
intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
intent.putExtra("title", HERE MSG WHICH YOU RECEIVE);
startActivity(intent);

There is no library AFAIK and it's really just a matter of retrieving the calender event and transforming it's data into XML. Unfortunately there is also no XEP on how calendar events should be represented in XMPP/XML so you have to come up with your own representation.

iCalendar/vCalendar is the calendar file format similar to vCard

You can add new field in vcard that save calender info example
VCard vCard = new VCard();
vCard.setField("calender","calender info");
calender info should be a sting, it can be a json format string.
user who receives it can load your vcard and use method
vCard.load(connection, Jid);
String string=vCard.getField("calender");

You really don't need any specific library for that purpose.
You can check out Calendar Provider
They have an awesome example on it too!
Be sure to use these permissions in your manifest.xml
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
And if you wish to send contacts then check this out Android: How to import contact from phone?
Vote this up if it helped you! I really need it. :)

Related

getting event ID after creating it using intent

(first of all, sorry if any of this sounds stupid, but I'm a total nood regarding android)
I'm working on an organizer app where I want to create a Calendar Event using an intend, and afterwards I have to save the events ID in my app so I can open it up again to edit it and so my app can read the events start date.
the code I'm using, I got from this question:
How can I find out the result of my calendar intent?
The intend works but I can't get the rest to work. also, "EventUtility" and "mContext" show up as errors.
p.s. I also get a warning about a missing Permission but I have no idea where to add that
protected void kalender() {
long event_id = EventUtility.getNewEventId(mContext.getContentResolver());
Intent newEvent = new Intent(Intent.ACTION_INSERT)
.setData(CalendarContract.Events.CONTENT_URI)
.putExtra(CalendarContract.Events.TITLE, "Abgabe Termin")
.putExtra(CalendarContract.Events.AVAILABILITY, CalendarContract.Events.AVAILABILITY_BUSY)
.putExtra(CalendarContract.Events.HAS_ALARM, true)
.putExtra("allDay", true);
startActivity(newEvent);
}
public static long getNewEventId(ContentResolver cr) {
Cursor cursor = cr.query(CalendarContract.Events.CONTENT_URI, new String[]{"MAX(_id) as max_id"}, null, null, "_id");
cursor.moveToFirst();
long max_val = cursor.getLong(cursor.getColumnIndex("max_id"));
return max_val + 1;
}

Why is EVENT_END_TIMEZONE not used?

When changing the timezone of an event with both EVENT_TIMEZONE and EVENT_END_TIMEZONE set, for all calendar apps I tried (aCalendar 0.99.8, Google's default calendar 4.4.2), EVENT_END_TIMEZONE is not updated to the new timezone, although the end time is displayed according to the new timezone.
Why isn't the end timezone set to the same value as the start/regular timezone? Am I missing some other timezone-related fields that have to be set in order for EVENT_END_TIMEZONE to be used / updated?
As I was asked to provide some code, here is one possible way to define an event showing the described behavior (note that, depending on the context of your code, you may need to use a different URI and/or add additional fields):
final ContentResolver cr = /* ... */;
final long calendarId = /* ... */;
final ContentValues values = new ContentValues();
values.put(CalendarContract.Events.CALENDAR_ID, calendarId);
values.put(CalendarContract.Events.TITLE, "Test");
values.put(CalendarContract.Events.DTSTART, 1428307200000L);
values.put(CalendarContract.Events.EVENT_TIMEZONE, "Europe/Berlin");
values.put(CalendarContract.Events.DTEND, 1428314400000L);
values.put(CalendarContract.Events.EVENT_END_TIMEZONE,"Europe/Berlin");
final Uri result = cr.insert(CalendarContract.Events.CONTENT_URI, values);
Once the event is changed to another timezone using any of the two apps listed above, its EVENT_END_TIMEZONE is preserved ("Europe/Berlin"), while the event's end is displayed relative to the new timezone (something different from "Europe/Berlin").

Can't set dates of android calendar events when using ContentValues api

I'm trying to insert events in to a calendar i created. I'm using the following code to do so... it's taken from google's documentation on CalendarProvider.
The problem is that no matter what values i put in DTSTART or DTEND the event is always created as a single day event and the date is the current date.
I've also tried to use Intent(Intent.ACTION_EDIT) with .putExtra for the dates... but sadly this doesn't help as well... neither the dates are set nor it gives me to modify them in the edit intent.
Any Idea what might be causing this behavior?
ContentValues event = new ContentValues();
event.put(Events.CALENDAR_ID, e.getCalendarId());
event.put(Events.TITLE, e.getTitle());
event.put(Events.DESCRIPTION, e.getDescription());
event.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().getDisplayName());
event.put(Events.EVENT_END_TIMEZONE, TimeZone.getDefault().getDisplayName());
event.put(Events.GUESTS_CAN_MODIFY, 1);
event.put(Events.IS_ORGANIZER, 1);
event.put(Events.DTSTART, e.getDateStart());
event.put(Events.DTEND, e.getDateEnd());
event.put(Events.ALL_DAY, 1);
event.putNull(Events.DURATION);
event.putNull(Events.RRULE);
event.putNull(Events.RDATE);
Uri eventUri = context.getContentResolver().insert(Events.CONTENT_URI, event);
int eventID = Integer.parseInt(eventUri.getLastPathSegment());

How to add SMS with specific date in android

Hi stackoverflow I'm trying to develop an application to add SMS to prrogrammatically, I'm using the following code to add SMS
private void addSMS()
{
Uri uri = Uri.parse("content://sms/");
ContentValues cv2 = new ContentValues();
cv2.put("address", "+91956322222");
cv2.put("date", "1309632433677");
cv2.put("read", 1);
cv2.put("type", 2);
cv2.put("body", "Hey");
getContentResolver().insert(uri, cv2);
cv2.clear();
}
Permissions :
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>
Problem is the Time of the message, it's displaying the time we added the message, but not the date we have passed in the list of messages, but when I open the message we added then the time will be correct as our input, please help me to solve this riddle.
Thanks.
This is a known problem I think. Try to add this line in the end:
getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);
If you want more explanation you should check this out!
Here is the working code, Thanks to #Amulya Khare
private void addSMS()
{
Uri uri = Uri.parse("content://sms/");
ContentValues cv2 = new ContentValues();
cv2.put("address", "+91956322222");
cv2.put("date", "1309632433677");
cv2.put("read", 1);
cv2.put("type", 2);
cv2.put("body", "Hey");
getContentResolver().insert(uri, cv2);
/** This is very important line to solve the problem */
getContentResolver().delete(Uri.parse("content://sms/conversations/-1"), null, null);
cv2.clear();
}
Please follow the link to get more information Android programatically inserted SMS have incorrect timestamp in Messaging apps

Create new synced calendar with android api

i'm trying to create a calendar on my account to fill with events that i get from some websites. I've searched and found some new android 4.0 calendar example that i've modified to obtain what i need. The problem is that the calendar is created, filled with events but not synced with google calendar, so in the next sync it is erased. The funcion i use are these:
This is the one for add the new calendar if don't alreay exist:
public static Uri createCalendarWithName(Context ctx, String name,String accountName) {
Uri target = Uri.parse(CalendarContract.Calendars.CONTENT_URI.toString());
target = target.buildUpon().appendQueryParameter(CalendarContract.CALLER_IS_SYNCADAPTER, "true")
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_NAME, accountName)
.appendQueryParameter(CalendarContract.Calendars.ACCOUNT_TYPE, "com.google").build();
ContentValues values = new ContentValues();
values.put(Calendars.ACCOUNT_NAME, accountName);
values.put(Calendars.ACCOUNT_TYPE, "com.google");
values.put(Calendars.NAME, name);
values.put(Calendars.CALENDAR_DISPLAY_NAME, name);
values.put(Calendars.CALENDAR_COLOR, 0x00FF00);
values.put(Calendars.CALENDAR_ACCESS_LEVEL, CalendarContract.Calendars.CAL_ACCESS_ROOT);
values.put(Calendars.OWNER_ACCOUNT, accountName);
values.put(Calendars.VISIBLE, 1);
values.put(Calendars.SYNC_EVENTS, 1);
values.put(Calendars.CALENDAR_TIME_ZONE, "Europe/Rome");
values.put(Calendars.CAN_PARTIALLY_UPDATE, 1);
values.put(Calendars.CAL_SYNC1, "https://www.google.com/calendar/feeds/" + accountName + "/private/full");
values.put(Calendars.CAL_SYNC2, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC3, "https://www.google.com/calendar/feeds/default/allcalendars/full/" + accountName);
values.put(Calendars.CAL_SYNC4, 1);
values.put(Calendars.CAL_SYNC5, 0);
values.put(Calendars.CAL_SYNC8, System.currentTimeMillis());
Uri newCalendar = ctx.getContentResolver().insert(target, values);
return newCalendar;
}
and that one create the new event without interaction:
public static Uri createEventWithName(Context ctx, long id, String name, String data) {
long startMillis = 0;
long endMillis = 0;
int id2=(int)id;
String[] divisi = data.split("/");
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012,Integer.parseInt(divisi[0])-1, Integer.parseInt(divisi[1]));
endMillis = endTime.getTimeInMillis();
ContentValues cv = new ContentValues();
cv.put(Events.TITLE, name);
cv.put(Events.DTSTART, startMillis);
cv.put(Events.DTEND, endMillis);
cv.put(Events.CALENDAR_ID, id2);
Log.d("aggiungo a calendario",Integer.toString(id2));
cv.put(Events.EVENT_TIMEZONE, TimeZone.getDefault().toString());
//cv.put(Events.RRULE, "FREQ=DAILY;INTERVAL=2");
Uri newEvent = ctx.getContentResolver().insert(CalendarContract.Events.CONTENT_URI, cv);
return newEvent;
}
I'm not so experienced in Android programming so i think it's a stupid question =) i've read that accountName and Account Type must be the same that the one stored on android device, else the event is cancelled. I get accountName from android api and i think they are correct. The account type seems to work for other....
Thanks to anybody that help me!
Not possible for now.
After a lot of googling I could not find a way to create new syncable calendar in Gmail account (account type "com.google"). I've tried these scenarios:
1. without SyncAdapter and I get the same behaviour - calendar appears in external Google Calendar app with all events I added but shortly after (e.g. after couple of seconds) all events and calendar disappear.
2. with SyncAdapter and com.google authenticator - I get an error during the process
3. with SyncAdapter and com.example authenticator - possible to create calendar but not in Gmail account
Note you can create new account and create new calendar for that account (using SyncAdapter, scenario 3) but it's not a Gmail account so calendar is not syncable with Gmail Calendar (e.g. if you login to Gmail account in web browser and open Google Calendar that calendar you've just created will not show).
from http://developer.android.com/reference/android/provider/CalendarContract.html
public static final String ACCOUNT_TYPE_LOCAL
Added in API level 14 A special account type for calendars not
associated with any account. Normally calendars that do not match an
account on the device will be removed. Setting the account_type on a
calendar to this will prevent it from being wiped if it does not match
an existing account.
See Also ACCOUNT_TYPE Constant Value: "LOCAL"
Same issue i was facing. After making the below change now i am seeing the events permanently sticking to my calendar.
In the addEvent method add the below line
Uri uri = getContentResolver().insert(asSyncAdapter(Events.CONTENT_URI,"abc#gmail.com","com.gmail"), cv);
Code for asSyncAdapter is as below
static Uri asSyncAdapter(Uri uri, String account, String accountType) {
return uri.buildUpon()
.appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
.appendQueryParameter(Calendars.ACCOUNT_NAME, account)
.appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
}
According to the Javadoc for CalendarContract.Calendars:
"Calendars are designed to be primarily managed by a sync adapter and inserting new calendars should be done as a sync adapter."
If you are not a sync adapter (if you are just an arbitrary app, you are not a sync adapter), that may explain why your new calendar is not getting synchronized like you would expect.

Categories

Resources