src/org/gege/caldavsyncadapter/caldav/entities/CalendarList.java

Tue, 10 Feb 2015 22:58:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 10 Feb 2015 22:58:00 +0100
changeset 11
82795cac7832
parent 0
fb9019fb1bf7
permissions
-rw-r--r--

Creatively merge and construct new revision for modified API features.

     1 package org.gege.caldavsyncadapter.caldav.entities;
     3 import android.accounts.Account;
     4 import android.content.ContentProviderClient;
     5 import android.database.Cursor;
     6 import android.net.Uri;
     7 import android.os.RemoteException;
     8 import android.provider.CalendarContract.Calendars;
    10 import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
    11 import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    13 import java.net.URI;
    14 import java.util.ArrayList;
    16 //import org.gege.caldavsyncadapter.CalendarColors;
    17 //import android.content.ContentUris;
    18 //import android.content.ContentValues;
    19 //import android.util.Log;
    21 public class CalendarList {
    22 //	private static final String TAG = "CalendarList";
    24     private java.util.ArrayList<DavCalendar> mList = new java.util.ArrayList<DavCalendar>();
    26     private Account mAccount = null;
    27     private ContentProviderClient mProvider = null;
    29     public CalendarSource Source = CalendarSource.undefined;
    31     public String ServerUrl = "";
    33     public CalendarList(Account account, ContentProviderClient provider, CalendarSource source, String serverUrl) {
    34         this.mAccount = account;
    35         this.mProvider = provider;
    36         this.Source = source;
    37         this.ServerUrl = serverUrl;
    38     }
    40 /*	public Calendar getCalendarByAndroidCalendarId(int calendarId) {
    41         Calendar Result = null;
    43 		for (Calendar Item : mList) {
    44 			if (Item.getAndroidCalendarId() == calendarId)
    45 				Result = Item;
    46 		}
    48 		return Result;
    49 	}*/
    51     public DavCalendar getCalendarByURI(URI calendarURI) {
    52         DavCalendar Result = null;
    54         for (DavCalendar Item : mList) {
    55             if (Item.getURI().equals(calendarURI))
    56                 Result = Item;
    57         }
    59         return Result;
    60     }
    62     public DavCalendar getCalendarByAndroidUri(Uri androidCalendarUri) {
    63         DavCalendar Result = null;
    65         for (DavCalendar Item : mList) {
    66             if (Item.getAndroidCalendarUri().equals(androidCalendarUri))
    67                 Result = Item;
    68         }
    70         return Result;
    71     }
    73     /**
    74      * function to get all calendars from client side android
    75      *
    76      * @return
    77      */
    78     public boolean readCalendarFromClient() {
    79         boolean Result = false;
    80         Cursor cur = null;
    82         Uri uri = Calendars.CONTENT_URI;
    84 		/* COMPAT: in the past, the serverurl was not stored within a calendar. (see #98)
    85          * so there was no chance to see which calendars belongs to a named account.
    86 		 * username + serverurl have to be unique
    87 		 * ((DavCalendar.SERVERURL = ?) OR (DavCalendar.SERVERURL IS NULL))
    88 		 */
    89         String selection = "(("
    90                 + Calendars.ACCOUNT_NAME + " = ?) AND " +
    91                 "(" + Calendars.ACCOUNT_TYPE + " = ?))";
    93         String[] selectionArgs = new String[]{
    94                 mAccount.name,
    95                 mAccount.type
    96         };
    98         String[] projection = new String[]{
    99                 Calendars._ID,
   100                 Calendars.NAME,
   101                 Calendars.ACCOUNT_NAME,
   102                 Calendars.ACCOUNT_TYPE
   103         };
   105         // Submit the query and get a Cursor object back.
   106         try {
   107             cur = mProvider.query(uri, null, selection, selectionArgs, Calendars._ID + " ASC");
   108         } catch (RemoteException e) {
   109             e.printStackTrace();
   110         }
   111         if (cur != null) {
   112             while (cur.moveToNext()) {
   113                 mList.add(new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl));
   114             }
   115             cur.close();
   116             Result = true;
   117         }
   119         return Result;
   120     }
   122     public boolean deleteCalendarOnClientSideOnly(android.content.Context context) {
   123         boolean Result = false;
   125         for (DavCalendar androidCalendar : this.mList) {
   126             if (!androidCalendar.foundServerSide) {
   127                 NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "calendar deleted: " + androidCalendar
   128                         .getCalendarDisplayName());
   129                 androidCalendar.deleteAndroidCalendar();
   130             }
   131         }
   133         return Result;
   134     }
   136     public void addCalendar(DavCalendar item) {
   137         item.setAccount(this.mAccount);
   138         item.setProvider(this.mProvider);
   139         item.ServerUrl = this.ServerUrl;
   140         this.mList.add(item);
   141     }
   143     public java.util.ArrayList<DavCalendar> getCalendarList() {
   144         return this.mList;
   145     }
   147     public void setAccount(Account account) {
   148         this.mAccount = account;
   149     }
   151     public void setProvider(ContentProviderClient provider) {
   152         this.mProvider = provider;
   153     }
   155     public ArrayList<Uri> getNotifyList() {
   156         ArrayList<Uri> Result = new ArrayList<Uri>();
   158         for (DavCalendar cal : this.mList) {
   159             Result.addAll(cal.getNotifyList());
   160         }
   162         return Result;
   163     }
   164 }

mercurial