I found this article very useful http://developer.android.com/guide/topics/providers/calendar-provider.html#query?
But here i found the ACCOUNT_NAME queried in the calendar is being written statically
I am stuck here Since my problem is ..what if i want user's gmail address through some query
i.e not statically specifying the address rather getting it through some code
PS: i am very newbie to android. Apologies if this ones a stupid question
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
final String[] EVENT_PROJECTION = new String[] {
Calendars._ID, // 0
Calendars.ACCOUNT_NAME, // 1
Calendars.CALENDAR_DISPLAY_NAME, // 2
Calendars.OWNER_ACCOUNT // 3
};
// The indices for the projection array above.
final int PROJECTION_ID_INDEX = 0;
final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
final int PROJECTION_DISPLAY_NAME_INDEX = 2;
final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
// Run query
Cursor cur = null;
ContentResolver cr = app.getContentResolver();
Uri uri = Calendars.CONTENT_URI;
String selection = "((" + Calendars.ACCOUNT_NAME + " = ?) AND ("
+ Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] { "sample#gmail.com",
"com.google", "sample#gmail.com" };
// Submit the query and get a Cursor object back.
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs,
null);
// Use the cursor to step through the returned records
while (cur.moveToNext()) {
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
}
Calendar beginTime = Calendar.getInstance();
Calendar ceaseTime = Calendar.getInstance();
ContentValues cv = new ContentValues();
HashMap<String, String> map = (HashMap<String, String>) params[0];
String startDate[] = map.get("startDate").toString().split("-");
String startTime[] = map.get("startTime").toString().split(":");
long beginMilis=0,ceaseMilis=0;
beginTime.set(Integer.parseInt(startDate[2]),
Integer.parseInt(startDate[1]),
Integer.parseInt(startDate[0]),
Integer.parseInt(startTime[0]),
Integer.parseInt(startTime[1]));
beginMilis = beginTime.getTimeInMillis();
String endDate[] = map.get("endDate").toString().split("-");
String endTime[] = map.get("endTime").toString().split(":");
TimeZone tz = TimeZone.getDefault();
ceaseTime.set(Integer.parseInt(endDate[2]),
Integer.parseInt(endDate[1]),
Integer.parseInt(endDate[0]),
Integer.parseInt(endTime[0]),
Integer.parseInt(endTime[1]));
ceaseMilis = ceaseTime.getTimeInMillis();
cv.put(Events.DTSTART, beginMilis);
cv.put(Events.DTEND, ceaseMilis);
cv.put(Events.TITLE, map.get("title").toString());
cv.put(Events.DESCRIPTION, map.get("description").toString());
cv.put(Events.CALENDAR_ID, String.valueOf(calID));
cv.put(Events.EVENT_TIMEZONE, tz.getDisplayName());
uri = cr.insert(Events.CONTENT_URI, cv);
Related
I am unable to get recurring events from google calandar. Can any one help me with this.
I have also used "Instances" table to get the recurring events but not. Can any one guide me how to fetch data from "Instances" table. It should be a great help for me.
I used following code to read events.
Uri uri = CalendarContract.Events.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.TITLE,
CalendarContract.Events.EVENT_COLOR,
CalendarContract.Events.CALENDAR_ID,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.ORGANIZER,
// If status = 2 it is cancelled by organizer
CalendarContract.Events.STATUS,
// If self attendee status = 2 it is declined by user
CalendarContract.Events.SELF_ATTENDEE_STATUS,
CalendarContract.Events.RDATE,
CalendarContract.Events.LAST_DATE
};
String[] calendarID;
for (int i = 0; i < EmailId.size(); i++) {
calendarID = new String[]{EmailId.get(i)};
Cursor c = context.getContentResolver().query(uri, projection,
CalendarContract.Instances.CALENDAR_ID + " = ?", calendarID, CalendarContract.Events.DTSTART);
if (c.moveToFirst()) {
do {
Evnt e = new Evnt();
e._id = c.getLong(0);
e.startDate = "";
e.startDate = getDate(c.getString(1));
e.startTime = "";
e.startTime = getTime(c.getString(1));
e.endDate = "";
e.endDate = getDate(c.getString(2));
e.endTime = "";
e.endTime = getTime(c.getString(2));
e.all_day = c.getInt(3);
e.title = c.getString(4);
e.color = c.getInt(5);
e.calendar_id = c.getLong(6);
e.desc = c.getString(7);
e.organizer = c.getString(8);
e.nine = c.getString(9);
e.ten = c.getString(10);
e.eleven = c.getString(11);
e.twelve = "";
e.twelve = getDate(c.getString(12));
ourEvents.add(e);
} while (c.moveToNext());
}
I would like to have a calendar view that would present all of the events of a public google calendar.
I have an OAuth 2.0 client ID for my user and my calendar is set to PUBLIC.
Then I went trough the Calendar Provider tutorial (https://developer.android.com/guide/topics/providers/calendar-provider.html) and use this code:
public class MainActivity extends Activity {
// Projection array. Creating indices for this array instead of doing
// dynamic lookups improves performance.
public static final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars._ID, // 0
CalendarContract.Calendars.ACCOUNT_NAME, // 1
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME, // 2
CalendarContract.Calendars.OWNER_ACCOUNT // 3
};
// The indices for the projection array above.
private static final int PROJECTION_ID_INDEX = 0;
private static final int PROJECTION_ACCOUNT_NAME_INDEX = 1;
private static final int PROJECTION_DISPLAY_NAME_INDEX = 2;
private static final int PROJECTION_OWNER_ACCOUNT_INDEX = 3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Run query
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = CalendarContract.Calendars.CONTENT_URI;
String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND ("
+ CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[]{"****#gmail.com", "google.com",
"****#gmail.com"};
// Submit the query and get a Cursor object back.
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
// Use the cursor to step through the returned records
while (cur.moveToNext()) {
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
}
}
}
Unfortunately, the Cursor stayed empty. I was also trying to define the ACCOUNT_TYPE to "LOCAL", but it didn't work.
Target SDK set to 21, and i have all the necessary permissions.
How can I retrieve the events of my calendar? Please help /:
After my android app sync event to google calendar and i see color google android calendar but doesn't change google web calendar event color. What i to do wrong?
Name is accountName.
protected void fetchCalendarForUser(String name) {
Cursor cur = null;
ContentResolver cr = getContentResolver();
Uri uri = CalendarContract.Calendars.CONTENT_URI;
String selection = "((" + CalendarContract.Calendars.ACCOUNT_NAME + " = ?) AND ("
+ CalendarContract.Calendars.ACCOUNT_TYPE + " = ?) AND ("
+ CalendarContract.Calendars.OWNER_ACCOUNT + " = ?))";
String[] selectionArgs = new String[] {name, "com.google", name};
cur = cr.query(uri, EVENT_PROJECTION, selection, selectionArgs, null);
cur.moveToNext();
long calID = 0;
String displayName = null;
String accountName = null;
String ownerName = null;
// Get the field values
calID = cur.getLong(PROJECTION_ID_INDEX);
displayName = cur.getString(PROJECTION_DISPLAY_NAME_INDEX);
accountName = cur.getString(PROJECTION_ACCOUNT_NAME_INDEX);
ownerName = cur.getString(PROJECTION_OWNER_ACCOUNT_INDEX);
int i = 0;
List<Date> laststart = new ArrayList<Date>();
List<Date> lastend = new ArrayList<Date>();
boolean equal=false;
for (Map.Entry<String, ArrayList<CalendarEntry>> item : calendarEntriesByDay.entrySet()) {
for (CalendarEntry entry : item.getValue()) {
for(int j=0;j<laststart.size();j++)
{
if(laststart.get(j) == entry.getStart()){
if(lastend.get(j) == entry.getEnd()){
equal=true;
}
}
}
if(equal==false) {
createEventOnCalendar(calID, entry);
i++;
}
else
{
equal=false;
}
laststart.add(entry.getStart());
lastend.add(entry.getEnd());
}
}
if (i > 0) {
Crouton.makeText(this, getResources().getString(eu.rerisoft.servicebus.R.string.google_calendar_add_success, displayName, String.valueOf(i)), Style.ALERT).show();
} else {
Crouton.makeText(this, eu.rerisoft.servicebus.R.string.google_calendar_add_error, Style.ALERT).show();
}
}
protected long createEventOnCalendar(long calID, CalendarEntry event) {
String colorString = getResources().getString(event.getColorRes());
int eventColor = Color.parseColor(colorString);
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(CalendarContract.Events.DTSTART, event.getStart().getTime());
values.put(CalendarContract.Events.DTEND, event.getEnd().getTime());
values.put(CalendarContract.Events.TITLE, event.getTitle());
values.put(CalendarContract.Events.EVENT_COLOR, eventColor);
values.put(CalendarContract.Events.DESCRIPTION, event.getDescription());
values.put(CalendarContract.Events.CALENDAR_ID, calID);
values.put(CalendarContract.Events.EVENT_TIMEZONE, TimeZone.getTimeZone("UTC").getDisplayName());
Uri uri = cr.insert(CalendarContract.Events.CONTENT_URI, values);
long eventID = Long.parseLong(uri.getLastPathSegment());
return eventID;
}
Sorry for bad english.
I have done same thing before 3 months:
String appointmentColorCode = "Color_which_Is_Currently_Getting_From_Calendar";
int mColorCode = 0;
mColorCode = (0xff000000 + Integer.parseInt(appointmentColorCode));
You have to give Color_which_Is_Currently_Getting_From_Calendar as you are getting from calendar.
mColorCode will be your final Color code.'
Done
So when I query the CalendarProvider for a list of events for my personal calendar, I'm getting some really strange start and end times. For example I see a result in the query that is 1970-01-13, but in my Google Calendar app it appears properly as 2015-02-01. All the events I got as a result have strange times like this.
My query is below.
String[] mProjection = new String[]{
Events.CALENDAR_ID,
Events.ORGANIZER,
Events.TITLE,
Events.DTSTART,
Events.DTEND,
Events._ID
};
ContentResolver cr = getActivity().getContentResolver();
Uri uri = Events.CONTENT_URI;
String selection = Events.CALENDAR_ID + " = ?";
String[] selectionArgs = new String[] {"2"};
Cursor cur = cr.query(uri, mProjection, selection, selectionArgs, null);
Log.i(TAG, "events " + cur.getCount());
TextView textView = (TextView) rootView.findViewById(R.id.dummy_text);
while (cur.moveToNext()) {
int calID = cur.getInt(0);
String organizer = cur.getString(1);
String title = cur.getString(2);
int start = cur.getInt(3);
int end = cur.getInt(4);
int event_id = cur.getInt(5);
Calendar startCal = Calendar.getInstance();
startCal.setTimeInMillis(start);
Calendar endCal = Calendar.getInstance();
endCal.setTimeInMillis(end);
Here's my code for converting the times to be readable.
public static String getHumanDate(int year, int month, int dayOfMonth) {
String monthZero = "";
String dayOfMonthZero = "";
if ((month + 1) < 10)
monthZero = "0";
if (dayOfMonth < 10)
dayOfMonthZero = "0";
return year + "-" + monthZero + (month + 1) + "-" + dayOfMonthZero + dayOfMonth;
}
Figured it out. Should have used
long start = cur.getLong(3);
long end = cur.getLong(4);
Rather than int.
I am collecting dates of calls from the Calllog, but there is a problem with the dates. I used simpledateformat to format the numbers, but i get false data: years from 1903 to 1948 and from 1954-2036. This is the code I am using:
final Context context = getApplicationContext();
final String[] projection = null;
final String selection = android.provider.CallLog.Calls.NUMBER + "='+3620455351684'";
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor c = null;
try{
c = context.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, selection, null, sortOrder);
while (c.moveToNext()) {
String callLogID = c.getString(c.getColumnIndex(android.provider.CallLog.Calls._ID));
int numberColumn = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int dateColumn = c.getColumnIndex(android.provider.CallLog.Calls.DATE);
int typeColumn = c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
int outgoingtypeColumn = c.getColumnIndex(android.provider.CallLog.Calls.TYPE + "='2'");
int durationColumn = c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int person = c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int duration = c.getInt(durationColumn);
int callDate = c.getInt(dateColumn);
int callType = c.getInt(typeColumn);
String number = c.getString(numberColumn);
String personname = c.getString(person);
SimpleDateFormat datePattern = new SimpleDateFormat ("yyyy-MM-dd");;
datePattern.setTimeZone(TimeZone.getTimeZone("GMT"));
String date_str = datePattern.format(new Date(callDate*1000L));
arr_allcallsduration.add(Integer.toString(duration));
arr_calls.add(Integer.toString(duration) + " : " + number + " : " + date_str);
}
}catch(Exception ex){
}finally{
c.close();
}
Output:
56 : +3620455351684 : 1948-08-02
425 : +3620455351684 : 1947-06-17
337 : +3620455351684 : 1947-06-13
etc.
I found the solution. The callDate should be declared as long:
long callDate = c.getLong(dateColumn);
One other modification:
String date_str = datePattern.format(new Date(callDate));
Hope this helps others!