Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 16fa6900 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

task: pass multiple event through intent to EditEventActivity

parent 69c69402
Loading
Loading
Loading
Loading
+76 −22
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ import android.os.Build;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.android.calendar.event.EditEventActivity;
@@ -27,19 +28,24 @@ import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import ws.xsoh.etar.R;

public class ImportActivity extends Activity {

    private PickCalendarDialogFragment mPickCalendarDialog;
    //private PickCalendarDialogFragment mPickCalendarDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.import_activity);
        if (!isValidIntent()) {
            Toast.makeText(this, R.string.cal_nothing_to_import, Toast.LENGTH_SHORT).show();
            finish();
@@ -138,6 +144,23 @@ public class ImportActivity extends Activity {

    private void parseCalFile() {
        Uri uri = getIntent().getData();
        /*int eventCount = IcalendarUtils.getEventCountFromCalendarFile(this, uri);
        if (eventCount == 1) {
            Intent calIntent = new Intent(Intent.ACTION_INSERT);
            calIntent.setType("vnd.android.cursor.item/event");
            calIntent.putExtra("FILE_URI", uri);
            startActivity(insertMultipleEventIntent);
        } else if (eventCount > 1) {
            Intent insertMultipleEventIntent = Intent(Intent.ACTION_INSERT_MULTIPLE); // to insert multiple file
            insertMultipleEventIntent.putExtra("File_URI", uri)
            startActivity(insertMultipleEventIntent);
        } else {
            showErrorToast();

        }
        finish();
        */

        VCalendar calendar = IcalendarUtils.readCalendarFromFile(this, uri);

        if (calendar == null) {
@@ -145,40 +168,61 @@ public class ImportActivity extends Activity {
            return;
        }

        LinkedList<VEvent> events = calendar.getAllEvents();
        final LinkedList<VEvent> events = calendar.getAllEvents();

        if (events == null || events.isEmpty()) {
            showErrorToast();
            return;
        }

        if (events.size() ==  1) {
            Log.d("Vincent", "Try to insert a single event");
            VEvent event = calendar.getAllEvents().getFirst();
            startInsertEventActivity(event);
        } else {
            final Intent calIntent = new Intent(Intent.ACTION_INSERT);
            calIntent.setType("vnd.android.cursor.item/event");

            final ArrayList<Bundle> bundleList = new ArrayList<>();
            for (VEvent event : events) {
                bundleList.add(putEventInBundle(event));
            }

            calIntent.putParcelableArrayListExtra("FAHIM", bundleList);
            startActivity(calIntent);
            finish();
            //displayPickCalendarDialog();
        }
    }

    private void displayPickCalendarDialog() {
        final FragmentManager fragmentManager = getFragmentManager();
        if (fragmentManager != null) {
                mPickCalendarDialog = new PickCalendarDialogFragment();
                mPickCalendarDialog.show(fragmentManager, PickCalendarDialogFragment.FRAGMENT_TAG);
            }
            //todo handle multi event  insertion
            //setContentView(R.layout.import_activity);
            Log.d("Vincent", "instanciate pickCalendarDialogFragment");
            //mPickCalendarDialog = new PickCalendarDialogFragment();
            Log.d("Vincent", "display Pick calendar dialog");
            //mPickCalendarDialog.show(fragmentManager, PickCalendarDialogFragment.FRAGMENT_TAG);

        } else {
            Log.d("Vincent", "fragmentManager is null");
        }
    }

    private void startInsertEventActivity(VEvent event) {
        Intent calIntent = new Intent(Intent.ACTION_INSERT);
        calIntent.setType("vnd.android.cursor.item/event");

        calIntent.putExtra(CalendarContract.Events.TITLE,
    private Bundle putEventInBundle(VEvent event) {
        Bundle bundle = new Bundle();
        bundle.putString(CalendarContract.Events.TITLE,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.SUMMARY)));
        calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION,
        bundle.putString(CalendarContract.Events.EVENT_LOCATION,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.LOCATION)));
        calIntent.putExtra(CalendarContract.Events.DESCRIPTION,
        bundle.putString(CalendarContract.Events.DESCRIPTION,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.DESCRIPTION)));
        calIntent.putExtra(ExtendedProperty.URL,
        bundle.putString(ExtendedProperty.URL,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.URL)));
        calIntent.putExtra(CalendarContract.Events.ORGANIZER,
        bundle.putString(CalendarContract.Events.ORGANIZER,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.ORGANIZER)));
        calIntent.putExtra(CalendarContract.Events.RRULE,
        bundle.putString(CalendarContract.Events.RRULE,
                IcalendarUtils.uncleanseString(event.getProperty(VEvent.RRULE)));

        if (event.mAttendees.size() > 0) {
@@ -187,20 +231,20 @@ public class ImportActivity extends Activity {
                builder.append(attendee.mEmail);
                builder.append(",");
            }
            calIntent.putExtra(Intent.EXTRA_EMAIL, builder.toString());
            bundle.putString(Intent.EXTRA_EMAIL, builder.toString());
        }

        String dtStart = event.getProperty(VEvent.DTSTART);
        String dtStartParam = event.getPropertyParameters(VEvent.DTSTART);
        if (!TextUtils.isEmpty(dtStart)) {
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
            bundle.putLong(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                    getLocalTimeFromString(dtStart, dtStartParam));
        }

        String dtEnd = event.getProperty(VEvent.DTEND);
        String dtEndParam = event.getPropertyParameters(VEvent.DTEND);
        if (dtEnd != null && !TextUtils.isEmpty(dtEnd)) {
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
            bundle.putLong(CalendarContract.EXTRA_EVENT_END_TIME,
                    getLocalTimeFromString(dtEnd, dtEndParam));
        } else {
            // Treat start date as end date if un-specified
@@ -213,16 +257,26 @@ public class ImportActivity extends Activity {


        if (isTimeStartOfDay(dtStart, dtStartParam)) {
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, isAllDay);
            bundle.putBoolean(CalendarContract.EXTRA_EVENT_ALL_DAY, isAllDay);
        }
        //Check if some special property which say it is a "All-Day" event.

        String microsoft_all_day_event = event.getProperty("X-MICROSOFT-CDO-ALLDAYEVENT");
        if (!TextUtils.isEmpty(microsoft_all_day_event) && microsoft_all_day_event.equals("TRUE")) {
            calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
            bundle.putBoolean(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
        }

        bundle.putBoolean(EditEventActivity.EXTRA_READ_ONLY, true);

        return bundle;
    }

        calIntent.putExtra(EditEventActivity.EXTRA_READ_ONLY, true);
    private void startInsertEventActivity(VEvent event) {
        Intent calIntent = new Intent(Intent.ACTION_INSERT);
        calIntent.setType("vnd.android.cursor.item/event");

        Bundle bundle = putEventInBundle(event);
        calIntent.putExtra("bundle", bundle);

        try {
            startActivity(calIntent);
+99 −67
Original line number Diff line number Diff line
@@ -23,9 +23,11 @@ import static android.provider.CalendarContract.EXTRA_EVENT_END_TIME;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.CalendarContract.Events;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
@@ -37,8 +39,10 @@ import com.android.calendar.CalendarController.EventInfo;
import com.android.calendar.CalendarEventModel.ReminderEntry;
import com.android.calendar.DynamicTheme;
import com.android.calendar.Utils;
import com.android.calendar.icalendar.IcalendarUtils;
import com.android.calendar.icalendar.VEvent;
import com.android.calendarcommon2.Time;

import com.android.calendar.icalendar.VCalendar;
import java.util.ArrayList;

import ws.xsoh.etar.R;
@@ -61,6 +65,8 @@ public class EditEventActivity extends AbstractCalendarActivity {

    private boolean mEventColorInitialized;

    private ArrayList<EventInfo> mEventInfos;

    private EventInfo mEventInfo;

    @Override
@@ -69,7 +75,12 @@ public class EditEventActivity extends AbstractCalendarActivity {

        dynamicTheme.onCreate(this);
        setContentView(R.layout.simple_frame_layout_material);
        mEventInfo = getEventInfoFromIntent(icicle);

        mEventInfos = getEventInfoFromIntent(icicle);


        if (mEventInfos.size() == 1) {
            mEventInfo = mEventInfos.get(0); //get first
            mReminders = getReminderEntriesFromIntent();
            mEventColorInitialized = getIntent().hasExtra(EXTRA_EVENT_COLOR);
            mEventColor = getIntent().getIntExtra(EXTRA_EVENT_COLOR, -1);
@@ -113,6 +124,9 @@ public class EditEventActivity extends AbstractCalendarActivity {
                ft.show(mEditFragment);
                ft.commit();
            }
        } else if(mEventInfos.size() > 1) {
            Toast.makeText(this, "Try to import " + mEventInfos.size() + "Bundle", Toast.LENGTH_SHORT).show();
        }
    }

    @SuppressWarnings("unchecked")
@@ -121,10 +135,17 @@ public class EditEventActivity extends AbstractCalendarActivity {
        return (ArrayList<ReminderEntry>) intent.getSerializableExtra(EXTRA_EVENT_REMINDERS);
    }

    private EventInfo getEventInfoFromIntent(Bundle icicle) {
        EventInfo info = new EventInfo();
    private ArrayList<EventInfo> getEventInfoFromIntent(Bundle icicle) {
        ArrayList<EventInfo> eventInfos = new ArrayList<>();

        final Intent intent = getIntent();

        ArrayList<Bundle> bundles = intent.getParcelableArrayListExtra("FAHIM");

        if (bundles == null) return eventInfos;

        for (Bundle bundle : bundles) {
            long eventId = -1;
        Intent intent = getIntent();
            Uri data = intent.getData();
            if (data != null) {
                try {
@@ -138,34 +159,45 @@ public class EditEventActivity extends AbstractCalendarActivity {
                eventId = icicle.getLong(BUNDLE_KEY_EVENT_ID);
            }

        boolean allDay = intent.getBooleanExtra(EXTRA_EVENT_ALL_DAY, false);
            final EventInfo eventInfo = loadEventInfoFromBundle(eventId, bundle);
            eventInfos.add(eventInfo);
        }

        return eventInfos;
    }

    private EventInfo loadEventInfoFromBundle(long eventId, Bundle bundle) {
        EventInfo eventInfo = new EventInfo();

        long begin = intent.getLongExtra(EXTRA_EVENT_BEGIN_TIME, -1);
        long end = intent.getLongExtra(EXTRA_EVENT_END_TIME, -1);
        boolean allDay = bundle.getBoolean(EXTRA_EVENT_ALL_DAY, false);

        long begin = bundle.getLong(EXTRA_EVENT_BEGIN_TIME, -1);
        long end = bundle.getLong(EXTRA_EVENT_END_TIME, -1);
        if (end != -1) {
            info.endTime = new Time();
            eventInfo.endTime = new Time();
            if (allDay) {
                info.endTime.setTimezone(Time.TIMEZONE_UTC);
                eventInfo.endTime.setTimezone(Time.TIMEZONE_UTC);
            }
            info.endTime.set(end);
            eventInfo.endTime.set(end);
        }
        if (begin != -1) {
            info.startTime = new Time();
            eventInfo.startTime = new Time();
            if (allDay) {
                info.startTime.setTimezone(Time.TIMEZONE_UTC);
                eventInfo.startTime.setTimezone(Time.TIMEZONE_UTC);
            }
            info.startTime.set(begin);
            eventInfo.startTime.set(begin);
        }
        info.id = eventId;
        info.eventTitle = intent.getStringExtra(Events.TITLE);
        info.calendarId = intent.getLongExtra(Events.CALENDAR_ID, -1);
        eventInfo.id = eventId;
        eventInfo.eventTitle = bundle.getString(Events.TITLE);
        eventInfo.calendarId = bundle.getLong(Events.CALENDAR_ID, -1);

        if (allDay) {
            info.extraLong = CalendarController.EXTRA_CREATE_ALL_DAY;
            eventInfo.extraLong = CalendarController.EXTRA_CREATE_ALL_DAY;
        } else {
            info.extraLong = 0;
            eventInfo.extraLong = 0;
        }
        return info;

        return eventInfo;
    }

    @Override
+19 −0
Original line number Diff line number Diff line
@@ -140,6 +140,25 @@ public class IcalendarUtils {
        return calendar;
    }

    public static int getEventCountFromCalendarFile(Context context, Uri uri) {
        /*
        TODO alternative method to avoid parsing everything:
        loop through each line in the file
        count "BEGIN:VEVENT" number
        count "END:VEVENT" number
        if event doesn't match : invalid file
        return VEVENT counter

        CHECK the VCalendar.populateFromString for idea
         */
        VCalendar calendar = readCalendarFromFile(context, uri);
        if (calendar != null)
            return calendar.getAllEvents().size();
        else return 0;
    }



    public static ArrayList<String> getStringArrayFromFile(Context context, Uri uri) {
        String scheme = uri.getScheme();
        InputStream inputStream = null;
+6 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>