Advertisement
Guest User

Calendar Event

a guest
Feb 19th, 2011
2,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.22 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011 John Malizia
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  *
  16.  * If you use this in your application, please make not of it so
  17.  * the user can see. Thank you.
  18.  */
  19. package com.studentspet.full;
  20.  
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.Map.Entry;
  24.  
  25. import android.content.ContentValues;
  26. import android.content.Context;
  27. import android.content.SharedPreferences;
  28. import android.database.Cursor;
  29. import android.net.Uri;
  30. import android.os.Build;
  31. import android.preference.PreferenceManager;
  32. import android.util.Log;
  33.  
  34. public class CalendarEvent
  35. {
  36.     private int calendarID = -1;
  37.     static String contentProvider;
  38.     static Uri remindersUri;
  39.     static Uri eventsUri;
  40.     static Uri calendars;
  41.     static String eventsTable;
  42.     static long EVENT_DURATION = 3600000;
  43.     static long REMINDER_TIME = 0;
  44.    
  45.     public Context context = null;
  46.  
  47.     /**
  48.      * @param a
  49.      *            Constructor
  50.      */
  51.     public CalendarEvent(Context c)
  52.     {
  53.         int sdk;
  54.        
  55.         try
  56.         {
  57.             sdk = new Integer(Build.VERSION.SDK_INT).intValue();
  58.         }
  59.         catch(Exception e)
  60.         {
  61.             sdk = 9;
  62.         }
  63.        
  64.         this.context = c;
  65.         if(sdk >= 8)
  66.         {
  67.             //2.2 or higher
  68.             eventsTable = "view_events";
  69.             contentProvider = "com.android.calendar";
  70.         }
  71.         else
  72.         {
  73.             //anything older
  74.             eventsTable = "Events";
  75.             contentProvider = "calendar";
  76.         }
  77.        
  78.         remindersUri = Uri.parse(String.format("content://%s/reminders",contentProvider));
  79.         eventsUri = Uri.parse(String.format("content://%s/events",contentProvider));
  80.         calendars = Uri.parse(String.format("content://%s/calendars",contentProvider));
  81.     }
  82.    
  83.  
  84.     /**
  85.      * @param strTitle
  86.      *            as the Title of the event
  87.      * @param strDescription
  88.      *            as the description of the event
  89.      * @param startTime
  90.      *            as the time in millis the event begins
  91.      */
  92.     public long insertEvent(String strTitle, String strDescription, long startTime, long endTime) throws Exception
  93.     {
  94.         if (calendarID != -1)
  95.         {
  96.             // Source: http://sgap.springnote.com/pages/5150959
  97.  
  98.             ContentValues event = new ContentValues();
  99.             event.put(EventColumns.ID, calendarID);
  100.             event.put(EventColumns.TITLE, strTitle);
  101.             event.put(EventColumns.DESC, strDescription);
  102.             event.put(EventColumns.START, startTime);
  103.             event.put(EventColumns.END, endTime);
  104.             event.put(EventColumns.ALLDAY, "0");
  105.             event.put(EventColumns.STATUS, "1");
  106.             event.put(EventColumns.VIS, "0");
  107.             event.put(EventColumns.TRANS, "0");
  108.             event.put(EventColumns.ALARM, "1");
  109.             return Long.parseLong(context.getContentResolver().insert(eventsUri, event).getLastPathSegment());
  110.  
  111.         }
  112.         return -1;
  113.         //throw exception
  114.  
  115.     }
  116.  
  117.     public long insertEvent(HashMap<String, String> args)
  118.     {
  119.         //Convert to use ContentValues?
  120.         if (calendarID != -1)
  121.         {           // Source: http://sgap.springnote.com/pages/5150959
  122.             ContentValues event = new ContentValues();
  123.             event.put(EventColumns.ID, calendarID);
  124.            
  125.             event.putAll(HashMapToContentValues(args));
  126.             event.put(EventColumns.ALLDAY, "0");
  127.             event.put(EventColumns.STATUS, "1");
  128.             event.put(EventColumns.VIS, "0");
  129.             event.put(EventColumns.TRANS, "0");
  130.             event.put(EventColumns.ALARM, "1");
  131.  
  132.             return Long.parseLong(context.getContentResolver().insert(eventsUri, event).getLastPathSegment());
  133.         }
  134.         //throw exception
  135.         return -1;
  136.  
  137.     }
  138.  
  139.     public void addReminder(long eventID)
  140.     {
  141.         try
  142.         {
  143.             if (contains(eventID))
  144.             {
  145.                 ContentValues values = new ContentValues();
  146.  
  147.                 values.put("event_id", eventID);
  148.                 values.put("method", 1);
  149.                 values.put("minutes", REMINDER_TIME);
  150.                 context.getContentResolver().insert(remindersUri, values);
  151.  
  152.                 Log.d("Calendar Event", "Reminder Added for event " + eventID);
  153.  
  154.             }
  155.             else
  156.                 Log.d("Calendar Event", "Reminder Not Added");
  157.         }
  158.         catch(Exception e)
  159.         {
  160.            
  161.         }
  162.  
  163.     }
  164.  
  165.     public void updateReminder(long eventID, int minutes)
  166.     {
  167.  
  168.         Uri updateReminderUri = Uri.withAppendedPath(remindersUri, String.valueOf(eventID));
  169.         ContentValues values = new ContentValues();
  170.         values.put(ReminderColumns.ID, eventID);
  171.         values.put(ReminderColumns.METHOD, 1);
  172.         values.put(ReminderColumns.TIME, minutes);
  173.  
  174.         context.getContentResolver().update(updateReminderUri, values, null, null);
  175.         Log.d("Calendar Event", "Alarm Updated");
  176.     }
  177.    
  178.     public void deleteReminder(long eventID)
  179.     {
  180.  
  181.         Uri reminderUri = Uri.withAppendedPath(remindersUri, String.valueOf(eventID));
  182.         context.getContentResolver().delete(reminderUri, null, null);
  183.         Log.d("Calendar Event", "Reminder deleted");
  184.     }
  185.  
  186.     public boolean containsAlarm(long eventID)
  187.     {
  188.         String selection = ReminderColumns.ID + "=" + eventID;
  189.         String[] projection = new String[] { ReminderColumns.METHOD };
  190.         Cursor cursor = context.getContentResolver().query(remindersUri, projection, selection, null, null);
  191.  
  192.         if (cursor.getCount() > 0)
  193.         {
  194.             cursor.close();
  195.             Log.d("Calendar Event", "Contains Reminder");
  196.             return true;
  197.         }
  198.         if(cursor != null)
  199.             cursor.close();
  200.         Log.d("Calendar Event", "Does not contain a reminder for " + eventID);
  201.         return false;
  202.     }
  203.  
  204.     /**
  205.      * Removes the eventID passed Returns the number of rows removed
  206.      *
  207.      * @param iEventID
  208.      *            as the eventID to remove
  209.      */
  210.     public int removeEvent(long iEventID)
  211.     {
  212.         Log.d("studentspet", "removing event.. " + iEventID);
  213.         if (calendarID != -1 && iEventID != -1)
  214.         {
  215.             if (this.contains(iEventID))
  216.             {
  217.                 Uri deleteEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(iEventID));
  218.                 return context.getContentResolver().delete(deleteEventUri, null, null);
  219.             }
  220.         }
  221.         return -1;
  222.     }
  223.  
  224.     /**
  225.      * Returns boolean specifying if the passed EventID is in the Calendar
  226.      *
  227.      * @param iEventID
  228.      */
  229.     public boolean contains(long iEventID)
  230.     {
  231.         if (calendarID != -1)
  232.         {
  233.             //Wrong table name for android 2.2
  234.             String[] projection = new String[] { EventColumns.TITLE, CalendarColumns.ID };
  235.             Cursor managedCursor = context.getContentResolver().query(eventsUri, projection, eventsTable+"._id=" + iEventID, null, null);
  236.             while (managedCursor.moveToNext())
  237.             {
  238.                 managedCursor.close();
  239.                 return true;
  240.             }
  241.             if(managedCursor != null)
  242.                 managedCursor.close();
  243.         }
  244.        
  245.  
  246.         return false;
  247.     }
  248.  
  249.     /**
  250.      * Returns the number of rows updated
  251.      *
  252.      * @param iEventID
  253.      *            as the eventID to update
  254.      * @param whereArgs
  255.      *            as the Set arguments see Where class
  256.      */
  257.     public int updateEvent(long iEventID, HashMap<String, String> args)
  258.     {
  259.         if (calendarID != -1)
  260.         {
  261.             if (contains(iEventID))
  262.             {
  263.                 Uri updateEventUri = Uri.withAppendedPath(eventsUri, String.valueOf(iEventID));
  264.                 return context.getContentResolver().update(updateEventUri, HashMapToContentValues(args), null, null);
  265.             }
  266.  
  267.         }
  268.        
  269.         return -1;
  270.  
  271.     }
  272.    
  273.     private ContentValues HashMapToContentValues(HashMap<String,String> hm)
  274.     {
  275.         ContentValues cv = new ContentValues();
  276.         for (Entry<String, String> kvp : hm.entrySet())
  277.         {
  278.             cv.put(kvp.getKey(), kvp.getValue());
  279.         }
  280.        
  281.         return cv;
  282.        
  283.     }
  284.  
  285.     /**
  286.      * @return ArrayList as list of calendars available
  287.      */
  288.     //needs rewritten
  289.     public ArrayList<CalendarListItem> getCalendars()
  290.     {
  291.         ArrayList<CalendarListItem> calendarList = new ArrayList<CalendarListItem>();
  292.         String[] projection = new String[] { CalendarColumns.ID, CalendarColumns.NAME, CalendarColumns.DISPLAYNAME};
  293.         Cursor managedCursor = context.getContentResolver().query(calendars, projection, null, null, null);
  294.  
  295.         if (managedCursor.getCount() > 0)
  296.         {
  297.             int nameColumn = managedCursor.getColumnIndex(CalendarColumns.NAME);
  298.             int displayNameColumn = managedCursor.getColumnIndex(CalendarColumns.DISPLAYNAME);
  299.             int idColumn = managedCursor.getColumnIndex(CalendarColumns.ID);
  300.             while (managedCursor.moveToNext())
  301.             {
  302.                 if (!managedCursor.isNull(nameColumn))
  303.                     calendarList.add(new CalendarListItem(managedCursor.getString(nameColumn), managedCursor.getLong(idColumn)));
  304.                 else
  305.                     calendarList.add(new CalendarListItem(managedCursor.getString(displayNameColumn), managedCursor.getLong(idColumn)));
  306.             }
  307.         }
  308.         if(managedCursor != null)
  309.             managedCursor.close();
  310.        
  311.         return calendarList;
  312.  
  313.     }
  314.  
  315.     /**
  316.      *
  317.      * @return calendarID as selected calendar's id
  318.      */
  319.     public long getSelectedCalendar()
  320.     {
  321.         return calendarID;
  322.     }
  323.  
  324.     /**
  325.      * @param newCalendarID
  326.      *            as new calendar
  327.      */
  328.     public void setSelectedCalendar(int newCalendarID)
  329.     {
  330.         calendarID = newCalendarID;
  331.     }
  332.    
  333.     class CalendarException extends Exception
  334.     {
  335.         private static final long serialVersionUID = 0;
  336.  
  337.         public CalendarException(String desc)
  338.         {
  339.             super(desc);       
  340.         }
  341.     }
  342.    
  343.    
  344.    
  345.  
  346.     public static class CalendarListItem
  347.     {
  348.  
  349.         String name;
  350.         long id;
  351.  
  352.         public CalendarListItem(String calendarName, long calendarID)
  353.         {
  354.             this.name = calendarName;
  355.             this.id = calendarID;
  356.         }
  357.     }
  358.    
  359.     public static class EventColumns
  360.     {
  361.         public static final String ID = "calendar_id";
  362.         public static final String TITLE = "title";
  363.         public static final String DESC = "description";
  364.         public static final String START = "dtstart";
  365.         public static final String END = "dtend";
  366.         public static final String ALLDAY = "allDay";
  367.         public static final String STATUS = "eventStatus";
  368.         public static final String VIS = "visibility";
  369.         public static final String TRANS = "transparency";
  370.         public static final String ALARM = "hasAlarm";
  371.     }
  372.    
  373.     public static class ReminderColumns
  374.     {
  375.         public static final String ID = "event_id";
  376.         public static final String METHOD = "method";
  377.         public static final String TIME = "minutes";
  378.     }
  379.    
  380.     public static class CalendarColumns
  381.     {
  382.         public static final String ID = "_id";
  383.         public static final String TITLE = "title";
  384.         public static final String NAME = "name";
  385.         public static final String DISPLAYNAME = "displayName";
  386.     }
  387.  
  388.  
  389. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement