FileDocCategorySizeDatePackage
IcsImportActivity.javaAPI DocAndroid 1.5 API8151Wed May 06 22:42:42 BST 2009com.android.calendar

IcsImportActivity

public class IcsImportActivity extends android.app.Activity

Fields Summary
private static final String
TAG
private android.view.View
mView
private android.widget.Button
mImportButton
private android.widget.Button
mCancelButton
private android.widget.Spinner
mCalendars
private android.widget.ImageView
mCalendarIcon
private android.widget.TextView
mNumEvents
private ICalendar.Component
mCalendar
private View.OnClickListener
mImportListener
private View.OnClickListener
mCancelListener
Constructors Summary
Methods Summary
private voidimportCalendar()


        ContentResolver cr = getContentResolver();

        int numImported = 0;
        ContentValues values = new ContentValues();

        for (ICalendar.Component component : mCalendar.getComponents()) {
            if ("VEVENT".equals(component.getName())) {
                CalendarInfo calInfo =
                        (CalendarInfo) mCalendars.getSelectedItem();
                if (Calendar.Events.insertVEvent(cr, component, calInfo.id,
                        Calendar.Events.STATUS_CONFIRMED, values) != null) {
                    ++numImported;
                }
            }
        }
        // TODO: display how many were imported.
    
protected voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);
        setContentView(R.layout.ics_import_activity);
        mView = findViewById(R.id.import_ics);

        mCalendarIcon = (ImageView) findViewById(R.id.calendar_icon);
        mCalendars = (Spinner) findViewById(R.id.calendars);
        populateCalendars();

        mImportButton = (Button) findViewById(R.id.import_button);
        mImportButton.setOnClickListener(mImportListener);
        mCancelButton = (Button) findViewById(R.id.cancel_button);
        mCancelButton.setOnClickListener(mCancelListener);

        mNumEvents = (TextView) findViewById(R.id.num_events);

        Intent intent = getIntent();
        String data = intent.getStringExtra("ics");
        if (data == null) {
            Uri content = intent.getData();
            if (content != null) {
                InputStream is = null;
                try {
                    is = getContentResolver().openInputStream(content);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buf = new byte[8096];
                    int bytesRead = -1;
                    int pos = 0;
                    while ((bytesRead = is.read(buf)) != -1) {
                        baos.write(buf, pos, bytesRead);
                        pos += bytesRead;
                    }
                    data = new String(baos.toByteArray(), "UTF-8");
                } catch (FileNotFoundException fnfe) {
                    Log.w(TAG, "Could not open data.", fnfe);
                } catch (IOException ioe) {
                    Log.w(TAG, "Could not read data.", ioe);
                } finally {
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException ioe) {
                            Log.w(TAG, "Could not close InputStream.", ioe);
                        }
                    }
                }
            }
        }
        if (data == null) {
            Log.w(TAG, "No iCalendar data to parse.");
            finish();
            return;
        }
        parseCalendar(data);
    
private voidparseCalendar(java.lang.String data)

        mCalendar = null;
        try {
            mCalendar = ICalendar.parseCalendar(data);
        } catch (ICalendar.FormatException fe) {
            if (Config.LOGD) {
                Log.d(TAG, "Could not parse iCalendar.", fe);
                // TODO: show an error message.
                finish();
                return;
            }
        }
        if (mCalendar.getComponents() == null) {
            Log.d(TAG, "No events in iCalendar.");
            finish();
            return;
        }
        int numEvents = 0;
        for (ICalendar.Component component : mCalendar.getComponents()) {
            if ("VEVENT".equals(component.getName())) {
                // TODO: display a list of the events (start time, title) in
                // the UI?
                ++numEvents;
            }
        }
        // TODO: special-case a single-event calendar.  switch to the
        // EventActivity, once the EventActivity supports displaying data that
        // is passed in via the extras.
        // OR, we could flip things around, where the EventActivity handles ICS
        // import by default, and delegates to the IcsImportActivity if it finds
        // that there are more than one event in the iCalendar.  that would
        // avoid an extra activity launch for the expected common case of
        // importing a single event.
        mNumEvents.setText(Integer.toString(numEvents));
    
private voidpopulateCalendars()

        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(Calendar.Calendars.CONTENT_URI,
                      new String[] { Calendar.Calendars._ID,
                                     Calendar.Calendars.DISPLAY_NAME,
                                     Calendar.Calendars.SELECTED,
                                     Calendar.Calendars.ACCESS_LEVEL },
                      Calendar.Calendars.SELECTED + "=1 AND "
                          + Calendar.Calendars.ACCESS_LEVEL + ">="
                          + Calendar.Calendars.CONTRIBUTOR_ACCESS,
                      null, null /* sort order */);

        ArrayList<CalendarInfo> items = new ArrayList<CalendarInfo>();
        try {
            // TODO: write a custom adapter that wraps the cursor?
            int idColumn = c.getColumnIndex(Calendar.Calendars._ID);
            int nameColumn = c.getColumnIndex(Calendar.Calendars.DISPLAY_NAME);
            while (c.moveToNext()) {
                long id = c.getLong(idColumn);
                String name = c.getString(nameColumn);
                items.add(new CalendarInfo(id, name));
            }
        } finally {
            c.deactivate();
        }

        mCalendars.setAdapter(new ArrayAdapter<CalendarInfo>(this,
                android.R.layout.simple_spinner_item, items));