From 33a310344b48c88a78bc79618bd0b42dd3a4ed36 Mon Sep 17 00:00:00 2001 From: Tal Hacohen Date: Tue, 28 Mar 2017 19:05:49 +0300 Subject: [PATCH] Import - Add all events to selected calendar --- .../syncadapter/resource/LocalCalendar.java | 5 ++ .../ui/importlocal/ImportActivity.java | 73 +++++++++++++++++-- 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.java b/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.java index 11c11841..79e4443a 100644 --- a/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.java +++ b/app/src/main/java/com/etesync/syncadapter/resource/LocalCalendar.java @@ -130,6 +130,11 @@ public class LocalCalendar extends AndroidCalendar implements LocalCollection { return (LocalEvent[])queryEvents(Events._SYNC_ID + " IS NULL AND " + Events.ORIGINAL_ID + " IS NULL", null); } + + public LocalEvent[] getAll() throws CalendarStorageException { + return (LocalEvent[])queryEvents(null, null); + } + @Override public LocalEvent getByUid(String uid) throws CalendarStorageException { LocalEvent[] ret = (LocalEvent[]) queryEvents(Events._SYNC_ID + " =? ", new String[]{uid}); diff --git a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java index 1346688d..2606b7d5 100644 --- a/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java +++ b/app/src/main/java/com/etesync/syncadapter/ui/importlocal/ImportActivity.java @@ -2,16 +2,18 @@ package com.etesync.syncadapter.ui.importlocal; import android.accounts.Account; import android.app.Activity; +import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.graphics.Typeface; +import android.os.AsyncTask; import android.os.Bundle; +import android.provider.CalendarContract; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.widget.ArrayAdapter; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.TextView; @@ -25,9 +27,11 @@ import com.etesync.syncadapter.resource.LocalCalendar; import com.etesync.syncadapter.resource.LocalEvent; import com.etesync.syncadapter.ui.ImportFragment; -import java.util.HashMap; import java.util.List; +import at.bitfire.ical4android.CalendarStorageException; +import at.bitfire.ical4android.Event; + public class ImportActivity extends AppCompatActivity { public final static String EXTRA_ACCOUNT = "account", EXTRA_COLLECTION_INFO = "collectionInfo"; @@ -86,11 +90,7 @@ public class ImportActivity extends AppCompatActivity { listCalendar.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView aExpandableListView, View aView, int groupPosition, int childPosition, long aL) { - Toast.makeText(ImportActivity.this, - calendarAccountList.get(groupPosition).calendars.get(childPosition).toString(), - Toast.LENGTH_SHORT).show(); - //todo import - LocalCalendar localCalendar = (LocalCalendar) calendarAccountList.get(groupPosition).calendars.get(childPosition); + new ImportEvents().execute(calendarAccountList.get(groupPosition).calendars.get(childPosition)); return false; } }); @@ -138,7 +138,7 @@ public class ImportActivity extends AppCompatActivity { @Override public Object getChild(int groupPosition, int childPosititon) { return calendarAccounts.get(groupPosition).calendars - .get(childPosititon).toString(); + .get(childPosititon).getDisplayName(); } @Override @@ -216,4 +216,61 @@ public class ImportActivity extends AppCompatActivity { return true; } } + + protected class ImportEvents extends AsyncTask { + ProgressDialog progressDialog; + + @Override + protected void onPreExecute() { + progressDialog = new ProgressDialog(ImportActivity.this); + progressDialog.setTitle(R.string.import_dialog_title); + progressDialog.setMessage(getString(R.string.import_dialog_adding_entries)); + progressDialog.setCanceledOnTouchOutside(false); + progressDialog.setIndeterminate(false); + progressDialog.setIcon(R.drawable.ic_import_export_black); + progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + progressDialog.show(); + } + + @Override + protected Boolean doInBackground(LocalCalendar... calendars) { + return importEvents(calendars[0]); + } + + @Override + protected void onProgressUpdate(Integer... progress) { + if (progressDialog != null) + progressDialog.setProgress(progress[0]); + } + + @Override + protected void onPostExecute(Boolean result) { + progressDialog.dismiss(); + } + + private boolean importEvents(LocalCalendar fromCalendar) { + try { + LocalCalendar localCalendar = LocalCalendar.findByName(account, + ImportActivity.this.getContentResolver().acquireContentProviderClient(CalendarContract.CONTENT_URI), + LocalCalendar.Factory.INSTANCE, info.url); + LocalEvent[] localEvents = fromCalendar.getAll(); + progressDialog.setMax(localEvents.length); + int progress = 0; + for (LocalEvent currentLocalEvent : localEvents) { + Event event = currentLocalEvent.getEvent(); + try { + LocalEvent localEvent = new LocalEvent(localCalendar, event, event.uid, null); + localEvent.addAsDirty(); + } catch (CalendarStorageException e) { + e.printStackTrace(); + } + publishProgress(++progress); + } + return true; + } catch (Exception aE) { + aE.printStackTrace(); + return false; + } + } + } }