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

Tue, 10 Feb 2015 18:12:00 +0100

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

Import initial revisions of existing project AndroidCaldavSyncAdapater,
forked from upstream repository at 27e8a0f8495c92e0780d450bdf0c7cec77a03a55.

     1 package org.gege.caldavsyncadapter.caldav.entities;
     3 import java.net.URI;
     4 import java.util.ArrayList;
     6 //import org.gege.caldavsyncadapter.CalendarColors;
     7 import org.gege.caldavsyncadapter.caldav.entities.DavCalendar.CalendarSource;
     8 import org.gege.caldavsyncadapter.syncadapter.notifications.NotificationsHelper;
    10 import android.accounts.Account;
    11 import android.content.ContentProviderClient;
    12 //import android.content.ContentUris;
    13 //import android.content.ContentValues;
    14 import android.database.Cursor;
    15 import android.net.Uri;
    16 import android.os.RemoteException;
    17 import android.provider.CalendarContract.Calendars;
    18 //import android.util.Log;
    20 public class CalendarList {
    21 //	private static final String TAG = "CalendarList";
    23 	private java.util.ArrayList<DavCalendar> mList = new java.util.ArrayList<DavCalendar>();
    25 	private Account mAccount = null;
    26 	private ContentProviderClient mProvider = null;
    28 	public CalendarSource Source = CalendarSource.undefined;
    30 	public String ServerUrl = "";
    32 	public CalendarList(Account account, ContentProviderClient provider, CalendarSource source, String serverUrl) {
    33 		this.mAccount = account;
    34 		this.mProvider = provider;
    35 		this.Source = source;
    36 		this.ServerUrl = serverUrl;
    37 	}
    39 /*	public Calendar getCalendarByAndroidCalendarId(int calendarId) {
    40 		Calendar Result = null;
    42 		for (Calendar Item : mList) {
    43 			if (Item.getAndroidCalendarId() == calendarId)
    44 				Result = Item;
    45 		}
    47 		return Result;
    48 	}*/
    50 	public DavCalendar getCalendarByURI(URI calendarURI) {
    51 		DavCalendar Result = null;
    53 		for (DavCalendar Item : mList) {
    54 			if (Item.getURI().equals(calendarURI))
    55 				Result = Item;
    56 		}
    58 		return Result;
    59 	}
    61 	public DavCalendar getCalendarByAndroidUri(Uri androidCalendarUri) {
    62 		DavCalendar Result = null;
    64 		for (DavCalendar Item : mList) {
    65 			if (Item.getAndroidCalendarUri().equals(androidCalendarUri))
    66 				Result = Item;
    67 		}
    69 		return Result;
    70 	}
    72 	/**
    73 	 * function to get all calendars from client side android
    74 	 * @return
    75 	 */
    76 	public boolean readCalendarFromClient() {
    77 		boolean Result = false;
    78 		Cursor cur = null;
    80 		Uri uri = Calendars.CONTENT_URI;
    82 		/* COMPAT: in the past, the serverurl was not stored within a calendar. (see #98)
    83 		 * so there was no chance to see which calendars belongs to a named account.
    84 		 * username + serverurl have to be unique
    85 		 * ((DavCalendar.SERVERURL = ?) OR (DavCalendar.SERVERURL IS NULL))
    86 		 */
    87 		String selection = "(" + "(" + Calendars.ACCOUNT_NAME +  " = ?) AND " + 
    88 		                         "(" + Calendars.ACCOUNT_TYPE +  " = ?) AND " +
    89 		                         "((" + DavCalendar.SERVERURL +   " = ?) OR " +
    90 		                         "(" + DavCalendar.SERVERURL +   " IS NULL)) AND " +
    91 		                         "(" + Calendars.OWNER_ACCOUNT + " = ?)" +
    92 		                   ")";
    93 		String[] selectionArgs = new String[] {	mAccount.name, 
    94 												mAccount.type,
    95 												this.ServerUrl,
    96 												mAccount.name
    97 											}; 
    99 		// Submit the query and get a Cursor object back. 
   100 		try {
   101 			cur = mProvider.query(uri, null, selection, selectionArgs, null);
   102 		} catch (RemoteException e) {
   103 			e.printStackTrace();
   104 		}
   106 		if (cur != null) {
   107 			while (cur.moveToNext()) {
   108 				mList.add(new DavCalendar(mAccount, mProvider, cur, this.Source, this.ServerUrl));
   109 			}
   110 			cur.close();
   111 			Result = true;
   112 		}
   114 		return Result;
   115 	}
   117 	public boolean deleteCalendarOnClientSideOnly(android.content.Context context) {
   118 		boolean Result = false;
   120 		for (DavCalendar androidCalendar : this.mList) {
   121 			if (!androidCalendar.foundServerSide) {
   122 				NotificationsHelper.signalSyncErrors(context, "CalDAV Sync Adapter", "calendar deleted: " + androidCalendar.getCalendarDisplayName());
   123 				androidCalendar.deleteAndroidCalendar();
   124 			}
   125 		}
   127 		return Result;
   128 	}
   130 	public void addCalendar(DavCalendar item) {
   131 		item.setAccount(this.mAccount);
   132 		item.setProvider(this.mProvider);
   133 		item.ServerUrl = this.ServerUrl;
   134 		this.mList.add(item);
   135 	}
   136 	public java.util.ArrayList<DavCalendar> getCalendarList() {
   137 		return this.mList;
   138 	}
   140 	public void setAccount(Account account) {
   141 		this.mAccount = account;
   142 	}
   143 	public void setProvider(ContentProviderClient provider) {
   144 		this.mProvider = provider;
   145 	}
   146 	public ArrayList<Uri> getNotifyList() {
   147 		ArrayList<Uri> Result = new ArrayList<Uri>();
   149 		for (DavCalendar cal : this.mList) {
   150 			Result.addAll(cal.getNotifyList());
   151 		}
   153 		return Result;
   154 	}
   155 }

mercurial