Android Open Source - Gcal_Importer Show Event List Activity






From Project

Back to project page Gcal_Importer.

License

The source code is released under:

Apache License

If you think the Android project Gcal_Importer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.crappo.android.androics;
/* www  .ja  v  a  2  s  .  c  om*/
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import net.fortuna.ical4j.model.ComponentList;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.PropertyList;
import net.fortuna.ical4j.model.component.VEvent;

import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Calendars;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShowEventListActivity extends Activity {

    private static final int RL_PADDING_TOP = 5;
    private static final int RL_PADDING_BOTTOM = 10;
    private static final int CLEARED_CHOICE = -1; // localTargetCal_ChosenPosition???????????????????????
    private static final int DEFAULT_CHECKED = 0; // import?????????????????????????????????????????????

    private RelativeLayout rl;
    private ListView lv;
    private String icsFileName;      // import?ICS??????????????(TopActivity???????intent?????????????)
    private ArrayList<CalendarDto> localCalDtoList = new ArrayList<CalendarDto>(); // ?????AccountType???????????????????
    private int localTargetCal_ChosenPosition = CLEARED_CHOICE; // ???????????????????import?????????????position
    private int icsExist = 0;    // ICS???????????????????Event???????? (Panel????????????)
    private int bothExist = 0;   // ICS????????????????????????????????????Event????????(Panel????????????)
    private int icsOnly = 0;     // ICS?????????????????????????Event????????(Panel????????????)

    final Locale locale = new Locale("ja", "JP");
    String appName;     // ?????????????AccountName
    String packageName; // ?????????????AccountType
    ProgressDialog progressBar;
    ShowEventListActivity activityObj;
    Model4EventList model; // Events???????????????????????????????AsyncTask???????????
    QueryToProvider queryClass; // Provider??????????delete?insert?query?????????????????????????????????????????
    Resources resources;
    String selectedFilePath;  // ?????????Activity????Intent??????????(ICS???????path)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_event_list);
        
        // ??????????????????????????????????????????????
        activityObj = this;
        selectedFilePath = getIntent().getExtras().getString("SELECTED_FILE_PATH");
        icsFileName = new File(selectedFilePath).getName();
        resources = getResources();
        packageName = resources.getResourcePackageName(R.layout.activity_top);
        appName = resources.getString(R.string.app_name);
        queryClass = new QueryToProvider();

        // [Import??????]??????????????????????????????????????????????????????????????????
        Button calSelectDialogBtn = (Button)findViewById(R.id.showeventlist_bt_selectdialog);
        calSelectDialogBtn.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                queryClass.getCalendars();
                showSingleSelectDialog(R.string.showeventlist_dialog_choice_calendar_to_import);
            }
        });
        
        // [Import??]?????????????
        Button runImportBtn = (Button) findViewById(R.id.showeventlist_bt_run_import);
        runImportBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(icsOnly == 0) { // ????????ICS??????????????????????????????????????????????????????????????????
                    Toast.makeText(activityObj, R.string.showeventlist_nothing_for_insert, Toast.LENGTH_LONG).show();
                } else if(localTargetCal_ChosenPosition != CLEARED_CHOICE) { // ????????????Impor???????????????????????
                    queryClass.insertEvents(); // ??????insert??
                    Toast.makeText(activityObj, R.string.showeventlist_import_now, Toast.LENGTH_LONG).show();
                } else { // ????????????????????????????????????????????????????insert????
                    localTargetCal_ChosenPosition = queryClass.createNewCalendar(
                          model.calendar.getProperties().getProperty("X-WR-CALNAME").getValue()
                        , model.calendar.getProperties().getProperty("X-WR-CALNAME").getValue()
                        , Color.rgb(0, 0, 0) // ?????????????????????????
                        , model.timezone.getID());
                    // ??????????????????????????????????????????????insert??
                    queryClass.insertEvents();
                    Toast.makeText(activityObj, R.string.showeventlist_import_now, Toast.LENGTH_LONG).show();
                }
            }
        });
        
//        Button testBtn = (Button)findViewById(R.id.showeventlist_bt_test);
//        testBtn.setOnClickListener(new OnClickListener(){
//            @Override
//            public void onClick(View v) {
//                queryClass.getEvents();
//                Toast.makeText(activityObj, "Show Logcat.", Toast.LENGTH_LONG).show();
//            }
//        });

        progressBar = new ProgressDialog(this);
        try { // ?????????Activity???????????????????ICS???????path?????????????????????????????AsyncTask????????
            new ShowEventListAsync(activityObj).execute(selectedFilePath);
        } catch (Exception e) {
            Toast.makeText(this, "Error Ocurred. (in ShowEventListAsync)", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        model = null; // ?????????????????????????????????????????????????????????
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { // ????????????????/???????????Activity????????????????
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.showeventlist, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.showeventlist_menu_create_new_calendar:
          // ??????????????????(??????????queryClass.createNewCalendar()???????????)
          localTargetCal_ChosenPosition = queryClass.createNewCalendar(
            model.calendar.getProperties().getProperty("X-WR-CALNAME").getValue()
              , model.calendar.getProperties().getProperty("X-WR-CALNAME").getValue()
              , Color.rgb(0, 0, 0) // ?????????????????????????
              , model.timezone.getID());
          return true;
      case R.id.showeventlist_menu_del_calendar:
          // ??????????????????????????????????????????????(??/???????showSingleSelectDialog()???????????)
          queryClass.getCalendars();
          showSingleSelectDialog(R.string.showeventlist_menu_del_calendar);
          return true;
      }
      return super.onOptionsItemSelected(item);
    }

    void showProgressBar() { // ???????????????progressBar????????????????????
        progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressBar.setMessage(resources.getString(R.string.showeventlist_dialog_spinner));
        progressBar.show();
    }

    /* 
     * onCreate????????????????AsyncTask????????????????????????????????????????
     * ?ICS???????????????????????????
     * ?ICS???????????????????????Event???????Panel????????
     * ?ListView???adapter??????????setAdapter?????
     */
    void refreshLayout(){
        makeHeader();
        ComponentList components = model.calendar.getComponents("VEVENT");
        queryClass.compareToCalendarProvider();
        lv = (ListView)findViewById(R.id.showeventlist_listView);
        lv.setAdapter(new ViewHolderAdapter(this, 0, components));
        queryClass.getCalendars();
    }

    /* CompareAsync??????????????????????? Layout????????(Panel)????????????? */
    void refreshPanelView(){
        TextView tvIcs = (TextView)findViewById(R.id.showeventlist_tv_exist_ics_num);
        tvIcs.setText("" + icsExist);
        TextView tvBoth = (TextView)findViewById(R.id.showeventlist_tv_exist_local_num);
        tvBoth.setText("" + bothExist);
        if (bothExist > 0)  tvBoth.setTextColor(Color.BLUE);
        else        tvBoth.setTextColor(Color.BLACK);
        TextView tvIcsOnly = (TextView)findViewById(R.id.showeventlist_tv_exist_ics_only_num);
        tvIcsOnly.setText("" + icsOnly);
        if (icsOnly > 0)  tvIcsOnly.setTextColor(Color.RED);
        else        tvIcsOnly.setTextColor(Color.BLACK);
    }

    /*
     * ???????Header?????????????????import?ICS?????????????????????????
     * ??????????????????????????????????????showSetting???????????/???????????????????????????????????????
     */
    private void makeHeader() {
        ArrayList<String> headerTitles = new ArrayList<String>();
        headerTitles.add("Calendar FileName"); // ???????????Properties????????TopActivity????intent??????????????????????????
        PropertyList propList = model.getPropertyList();
        for(int i=0; i<propList.size(); i++) { // properties????????????Name?????????
            Property p = (Property)propList.get(i);
            headerTitles.add(p.getName());
        }
        boolean[] showSetting = new boolean[] { // ????????????????????????????????????????(true/false)?????????????????????????
                  true    // Calendar FileName
                , false    // propList.getProperty("PRODID").getValue()
                , false    // propList.getProperty("VERSION").getValue()
                , false    // propList.getProperty("CALSCALE").getValue()
                , false    // propList.getProperty("METHOD").getValue()
                , true    // propList.getProperty("X-WR-CALNAME").getValue()
                , false    // propList.getProperty("X-WR-TIMEZONE").getValue()
                , true    // propList.getProperty("X-WR-CALDESC").getValue()
                , false    // for something
                , false    // for something
        };
        rl = (RelativeLayout)findViewById(R.id.showeventlist_relativelayout);
        int childCnt = 0; // RelativeLayout????????????????????????????????????????counter(header???????????????????????2?????????)
        for (int i=0; i<headerTitles.size(); i++) { //
            TextView tvTitle = (TextView)rl.getChildAt(childCnt++); // ?????
            HorizontalScrollView hs = (HorizontalScrollView)rl.getChildAt(childCnt++);
            TextView tvValue = (TextView)hs.getChildAt(0); // ?????
            if(showSetting[i]) { // true???????????????????
                if(i==0) {
                    tvValue.setText(icsFileName); // ??????????????????????"Calendar FileName"??????????????
                } else { // ????????Property???????????????????????
                    String value;
                    if((value = propList.getProperty(headerTitles.get(i)).getValue()) != null)    tvValue.setText(value);
                }
            } else { // false?????????????????????????????????????????
                tvTitle.setHeight(0);
                tvTitle.setVisibility(ViewGroup.INVISIBLE);
                hs.setVisibility(ViewGroup.INVISIBLE);
                tvValue.setHeight(0);
            }
        }
    }

    /* ?????????????????????????????? */
    private void clearChoice() {
        localTargetCal_ChosenPosition = CLEARED_CHOICE;
        localCalDtoList.clear();
    }

    /* API 4.0????ContentProvider???URI????????????????????????????????????????????URI????????? */
    private Uri getCalProvider() {
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return Calendars.CONTENT_URI;
        } else {
            return Uri.parse("content://com.android.calendar/calendars");
        }
    }

    /* API 4.0????ContentProvider???URI????????????????????????????????????????????URI????????? */
    private Uri getEventProvider() {
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return CalendarContract.Events.CONTENT_URI;
        } else {
            return Uri.parse("content://com.android.calendar/events");
        }
    }

    /*
     * ContentProvider????????????????????Uri???????????????????????
     * API 14??????(CALLER_IS_SYNCADAPTER,"true")?query???append????????????????????????????????????????
     */
    private Uri asSyncAdapter(Uri uri, String account, String accountType) {
        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return uri.buildUpon()
                    .appendQueryParameter(android.provider.CalendarContract.CALLER_IS_SYNCADAPTER,"true")
                    .appendQueryParameter(Calendars.ACCOUNT_NAME, account)
                    .appendQueryParameter(Calendars.ACCOUNT_TYPE, accountType).build();
        } else {
            return uri.buildUpon()
                    .appendQueryParameter("_sync_account", account)
                    .appendQueryParameter("_sync_account_type", accountType).build();
        }
     }

    /*
     * ICS???????????DTSTART??????????Parse??????DateTime????????????????????
     * ???????????DateTime???????????getTime()???UNIX????????????????????
     */
    private DateTime getDateTime(String strDateTime) {
        DateTime dt = null;
        try { //?????????????????????????????????????
            if(strDateTime.length() == 8)    dt = new DateTime(strDateTime + "T000000");
            else                             dt = new DateTime(strDateTime);
        } catch (ParseException e) { e.printStackTrace(); }
        return dt;
    }

    /*
     *  ???????????????Dialog?????
     */
    private void showSingleSelectDialog(int stringId) {
        if(localCalDtoList.size() == 0) { // ?????????????????????????????????????????????????????Toast??????
            Toast.makeText(this, R.string.showeventlist_choicedialog_first, Toast.LENGTH_LONG).show();
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(ShowEventListActivity.this);
            ArrayList<String> calAttrArray = new ArrayList<String>();
            // daialog????????????????????????????????????
            for(CalendarDto calDto : localCalDtoList) {
                calAttrArray.add(calDto.toString());
            }
            String[] localCalStrs = (String[])calAttrArray.toArray(new String[0]);
            
            // ????????????????daialog??????????
            dialog.setSingleChoiceItems(
                  localCalStrs
                , DEFAULT_CHECKED
                , new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int whichButton) {
                            localTargetCal_ChosenPosition = whichButton;
                        }
                    }
                );
            
            // dialog??????????
            final String dialogTitle = resources.getString(stringId);
            dialog.setTitle(dialogTitle);
        
            dialog.setPositiveButton( // OK???????????(????????????????????)
                  "OK"
                , new DialogInterface.OnClickListener() { // ???????????
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // ????????????????????????????????OK??????????????????????????????????????????????
                        if(localTargetCal_ChosenPosition == CLEARED_CHOICE)
                            localTargetCal_ChosenPosition = DEFAULT_CHECKED;
                        // [Import??????]????????????????????Menu???[???????]????????????????????????????????????id??????????
                        if(dialogTitle.equals(resources.getString(R.string.showeventlist_menu_del_calendar)))
                            queryClass.delCalendar(); // [???????]????????????????????????
                    }
                  }
                );
            dialog.show();
        }
    }

    /* ListView???????????????View????????????Holder */
    private class ViewHolder {
        ArrayList<TextView> tvArray = new ArrayList<TextView>();
        RelativeLayout rlay;

        ViewHolder(RelativeLayout rlay) { // xml??????????layout?????????????????????????tvArray????????????????????
            this.rlay = rlay;
            rlay.setPadding(0, RL_PADDING_TOP, 0, RL_PADDING_BOTTOM);
            for(int i=0; i<rlay.getChildCount(); i++) {
                TextView tv = (TextView)rlay.getChildAt(i);
                tvArray.add(tv);
            }
        }
    }

    /* ViewHolder?ListView???VEvent?????????????????????????adapter */
    private class ViewHolderAdapter extends ArrayAdapter<ComponentList> {
        ViewHolder holder;
        VEvent event;
        @SuppressWarnings("unchecked")
        public ViewHolderAdapter (ShowEventListActivity activityObj, int resource, ComponentList objects) {
            super(activityObj, resource, objects);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // ?????if????ArrayAdapter??????????convertView???????????????????????????????????????????
            if (convertView == null) {
                holder = new ViewHolder((RelativeLayout)getLayoutInflater().inflate(R.layout.layout_for_showeventlist, null));
                convertView = holder.rlay;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            
            // event???????????????????????????????????????????????
            Object Obj = getItem(position);
            event = (VEvent)Obj;
            if(event == null)    for (TextView entry : holder.tvArray) { setDummyForView(entry); }
            else                 for (TextView entry : holder.tvArray) { setTextToView(entry); }

            return convertView;
        }

        void setDummyForView(TextView entry) { // event???null??????????????????????????????????????????????
            switch(entry.getId()){
            case R.id.showeventlist_dtstart:
                entry.setText("????");
                break;
            case R.id.showeventlist_dtend:
                entry.setText("????");
                break;
            case R.id.showeventlist_summary:
                entry.setText("????????");
                break;
            case R.id.showeventlist_location:
                entry.setText("??");
                break;
            }
        }

        void setTextToView(TextView entry) { // holder???????????View???event????????????????????
            if( entry != null) {
                String str;
                switch(entry.getId()){
                case R.id.showeventlist_dtstart: // getTime()???UnixTimeStamp?????????????????????????????????
                    if(event.getStartDate() != null){
                        str = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", locale).format(getDateTime(event.getStartDate().getValue()).getTime());
                        entry.setText(str);
                    } else { entry.setText(""); }
                    break;
                case R.id.showeventlist_dtend: // getTime()???UnixTimeStamp?????????????????????????????????
                    if(event.getEndDate() != null){
                        str = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", locale).format(getDateTime(event.getEndDate().getValue()).getTime());
                        entry.setText(str);
                    } else { entry.setText(""); }
                    break;
                case R.id.showeventlist_summary: // event???????
                    if(event.getProperties().getProperty(Property.SUMMARY) != null) {
                            str = event.getProperties().getProperty(Property.SUMMARY).getValue();
                            entry.setText(str);
                    } else { entry.setText(""); }
                    break;
                case R.id.showeventlist_location: // event?????
                    if(event.getProperties().getProperty(Property.LOCATION) != null){
                        str = event.getProperties().getProperty(Property.LOCATION).getValue();
                        entry.setText(str);
                    } else { entry.setText(""); }
                    break;
                }
            } else {
                Log.e("setTextToView", "entry is Null!");
            }
        }
    }

    /*
     * ????????????????????
     * ????????????????????????????????????????query??????????????????????????????
     */
    private class QueryToProvider {

        /*
         * ?????????????????????(????????????????)??????CalendarDto??????????????????
         * AccountType??????????packageName?????????????????????????????????
         * ??????????????????????CalendarDto? localCalDtoList ???add?????????????
         */
        void getCalendars(){
            clearChoice(); // ?????????????????????????????????????????????????
            String selection = null;
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                selection = "( " + Calendars.ACCOUNT_TYPE + " = ? )";
            } else {
                selection = "( " + "_sync_account_type" + " = ? )";
            }
            String[] selectionArgs = new String[] { packageName }; // AccountType?????????????????
            Cursor c = null;
            c = getContentResolver().query(getCalProvider(), null, selection, selectionArgs, null);
            if ( c != null && c.moveToFirst()) {
                do { // Hit?????????????????calDto??????????????????
                    CalendarDto calDto = null;
                    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                        calDto = new CalendarDto(c.getString(c.getColumnIndex(Calendars.NAME)), c.getString(c.getColumnIndex(Calendars.CALENDAR_COLOR)));
                        calDto.setOwnerAccount(c.getString(c.getColumnIndex(Calendars.OWNER_ACCOUNT)));
                        calDto.setAccount_type(c.getString(c.getColumnIndex(Calendars.ACCOUNT_TYPE)));
                        calDto.setAccount_name(c.getString(c.getColumnIndex(Calendars.ACCOUNT_NAME)));
                        calDto.setCalendar_displayName(c.getString(c.getColumnIndex(Calendars.CALENDAR_DISPLAY_NAME)));
                    } else {
                        calDto = new CalendarDto(c.getString(c.getColumnIndex("name")), c.getString(c.getColumnIndex("color")));
                        calDto.setOwnerAccount(c.getString(c.getColumnIndex("ownerAccount")));
                        calDto.setAccount_type(c.getString(c.getColumnIndex("_sync_account_type")));
                        calDto.setAccount_name(c.getString(c.getColumnIndex("_sync_account")));
                        calDto.setCalendar_displayName(c.getString(c.getColumnIndex("displayName")));
                    }
                    calDto.set_id(c.getString(c.getColumnIndex(Calendars._ID)));
                    localCalDtoList.add(calDto);
                } while (c.moveToNext());
            }
            c.close();
        }

        /*
         * ???????import????????????????????????????????????
         * Calendars.ACCOUNT_NAME ??? Calendars.ACCOUNT_TYPE ?????????????????????????????(????????????????????????????????????)?
         * ?????????????????????????????localCalDtoList??????????????????
         */
        int createNewCalendar(String calendarName, String displayName, int color, String timezone) {
          calendarName = calendarName + "_" + new SimpleDateFormat("yyyyMMdd_HHmmss", locale).format(new Date());
            ContentResolver contentResolver = getContentResolver();
            ContentValues calVal = new ContentValues();
            String result = null;
            String strId = null;
            String ownerStr = "owner_" +  new SimpleDateFormat("yyyyMMdd_HHmmss", locale).format(new Date()) + "_" + packageName;
            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                calVal.put(Calendars.ACCOUNT_NAME, appName);
                calVal.put(Calendars.ACCOUNT_TYPE, packageName);
                calVal.put(Calendars.NAME, calendarName);
                calVal.put(Calendars.CALENDAR_DISPLAY_NAME, displayName);
                calVal.put(Calendars.CALENDAR_COLOR, color);
                calVal.put(Calendars.CALENDAR_ACCESS_LEVEL, 700);
                calVal.put(Calendars.SYNC_EVENTS, 1);
                calVal.put(Calendars.CALENDAR_TIME_ZONE, timezone);
                calVal.put(Calendars.OWNER_ACCOUNT, ownerStr);
            } else {
                calVal.put("_sync_account", appName);
                calVal.put("_sync_account_type", packageName);
                calVal.put("name", calendarName);
                calVal.put("displayName", displayName);
                calVal.put("color", color);
                calVal.put("access_level", 700);
                calVal.put("sync_events", 1);
                calVal.put("timezone", timezone);
                calVal.put("ownerAccount", ownerStr);
            }
            // API 14??????CalendarProvider?????????????????????SyncAdapter??????????????????????????????????
            // ?????????????????????Uri?build???????? asSyncAdapter()??????????????????????
            result = contentResolver.insert(asSyncAdapter(getCalProvider(), appName, packageName), calVal).toString();
            // ????????????????????????????_id? strId ???????????
            strId = result.replaceAll(getCalProvider().toString() + "/", "");
            int index = strId.lastIndexOf('?');
            strId = index < 0 ? strId : strId.substring(0, index);

            localCalDtoList.clear(); // localCalDtoList ???????????
            queryClass.getCalendars();
            String toastStr = resources.getString(R.string.showeventlist_new_calendar_created);
            toastStr += "\n" + localCalDtoList.get(localCalDtoList.size()-1);
            Toast.makeText(activityObj,  toastStr, Toast.LENGTH_LONG).show();
            int ret = localCalDtoList.size() - 1;
            for(int i=0;i<localCalDtoList.size();i++) { // localCalDtoList????position(???????)???????????_id?????
                CalendarDto dto = localCalDtoList.get(i);
                if(dto.get_id().equals(strId)) {
                    ret = i;
                }
            }
            return ret;
        }

        /*
         * ?????????????????????????????????????????????
         * ???????????????????????????????????????????????????????
         */
        void delCalendar() {
            String delTargetIdStr = localCalDtoList.get(localTargetCal_ChosenPosition).get_id();
            String delTargetDispName = localCalDtoList.get(localTargetCal_ChosenPosition).getCalendar_displayName();
            ContentResolver cr = getContentResolver();
            Uri calUri = ContentUris.withAppendedId(asSyncAdapter(getCalProvider(), appName, packageName), Long.parseLong(delTargetIdStr));
            cr.delete(calUri, null, null);
            clearChoice();
            queryClass.getCalendars();
            queryClass.compareToCalendarProvider();
            String toastStr = resources.getString(R.string.showeventlist_done_calendar_delete);
            toastStr += "\n" + delTargetIdStr + ":" + delTargetDispName;
            Toast.makeText(activityObj, toastStr, Toast.LENGTH_LONG).show();
        }

        /*
         * ICS???????????????????????????????????? ??????????????????????????????
         * ??????????????????????
         *   ??Activity?????????????
         *   ?insertEvents()?????????????
         *   ?delCalendar()?????????????
         *   ???????????????CompareAsync??????
         */
        void compareToCalendarProvider() {
            icsExist = 0;
            bothExist = 0;
            icsOnly = 0;
            ArrayList<VEvent> eventList = new ArrayList<VEvent>();
            ComponentList components = model.calendar.getComponents("VEVENT");
            VEvent event;
            for(Object obj : components) { // ??ArrayList????????????????????????(AsyncTask?????????????????)
                if( obj instanceof VEvent) {
                    icsExist++; // VEvent???????????????????
                    event = (VEvent)obj;
                    eventList.add(event);
                }
            }
            VEvent[] eventArray = eventList.toArray(new VEvent[0]); // ????????????AsyncTask???????
            new CompareAsync().execute(eventArray);
        }

        /*
         * ICS???????????????????????????????????
         * ICS????????????????????????Event??????????????????????????????????????insert?????????
         */
        void insertEvents() {
            icsExist = 0;
            bothExist = 0;
            icsOnly = 0;
            ArrayList<VEvent> eventList = new ArrayList<VEvent>();
            ComponentList components = model.calendar.getComponents("VEVENT");
            VEvent event;
            for(Object obj : components) { // ??ArrayList????????????????????????(AsyncTask?????????????????)
                if( obj instanceof VEvent) {
                    icsExist++; // VEvent???????????????????
                    event = (VEvent)obj;
                    eventList.add(event);
                }
            }
            VEvent[] eventArray = eventList.toArray(new VEvent[0]); // ????????????AsyncTask???????
            new InsertAsync().execute(eventArray);
        }

//        /*
//         * for Debug
//         * EventProvider????????????????????????Log??????????????????????????
//         */
//        public void getEvents() {
//           Cursor cursor = null;
////           cursor = getContentResolver().query(getEventProvider(), null, null, null, null);
//           cursor = getContentResolver().query(getEventProvider(), null, " (ownerAccount like ?) ", new String[]{"owner_%"}, null);
//
//           if(cursor.moveToFirst()) {
//               do {
//                   int position = cursor.getPosition();
//                   for(int i=0;i<cursor.getColumnCount();i++) {
//                       Log.v("getEvents:" + position, "(" + i + ")" + cursor.getColumnName(i) + ":" + cursor.getString(i));
//                   }
//               } while (cursor.moveToNext());
//           }
//           cursor.close();
//       }
        
    }
    
    /* queryClass ??? compareToCalendarProvider()??????????????????????????????????AsyncTask??? */
    private class CompareAsync extends AsyncTask<VEvent, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showProgressBar();
        }
        
        @Override
        protected Void doInBackground(VEvent... eventArray) {
            String selection = null;
            String[] selectionArgs = null;
            Cursor cursor = null;
            for(VEvent event : eventArray){
                String uid = event.getUid().getValue();
                // EventsProvider??????Event???UID???ICS??????uid???????????????????????????????SQL??????????????????
                if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    selectionArgs = new String[] { uid };
                    selection = "(" + CalendarContract.Events.SYNC_DATA1 + " = ?)";
                } else { // API 14???????????URL?????????????????uid????????????????????????????like??????%???????
                    selectionArgs = new String[] { "%/" + uid.split("@")[0] };
                    selection = "( _sync_id like ? ) ";
                }
                cursor = getContentResolver().query(getEventProvider(), null, selection, selectionArgs, null);
                if(cursor != null) { // cursor???null??????????????????????????(Uri???????????????????????????null???????????????????????????)
                    if(cursor.moveToFirst()) { // SQL????????1???????Hit??????????????????????????
                        do {
                            bothExist++; // ICS???????????????????????????????Event???????????bothExist?increment????
                        } while (cursor.moveToNext());
                    }
                    cursor.close();
                } else {
                    Log.e("compareToCalendarProvider", "cursor is null.");
                }
            }
            return null;
        }
        
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            icsOnly = icsExist - bothExist;
            progressBar.dismiss();
            refreshPanelView(); // layout???panel?????????(??????????)????
        }
    }
    
    /* queryClass ??? insertEvents()??????????????????????????????????AsyncTask??? */
    private class InsertAsync extends AsyncTask<VEvent, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showProgressBar();
        }
        
        @Override
        protected Void doInBackground(VEvent... eventArray) {
            ContentResolver contentResolver = ShowEventListActivity.this.getContentResolver();
            ContentValues values = new ContentValues();

            String selection = null;
            String[] selectionArgs = null;
            Cursor cursor = null;
            for(VEvent event : eventArray){
                String uid = event.getUid().getValue();
                // EventsProvider??????Event???UID???ICS??????uid???????????????????????????????SQL??????????????????
                if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                    selectionArgs = new String[] { uid };
                    selection = "(" + CalendarContract.Events.SYNC_DATA1 + " = ?)";
                } else { // API 14???????????URL?????????????????uid????????????????????????????like??????%???????
                    selectionArgs = new String[] { "%/" + uid.split("@")[0] };
                    selection = "( _sync_id like ? ) ";
                }
                cursor = getContentResolver().query(getEventProvider(), null, selection, selectionArgs, null);

                if(cursor != null) { // cursor???null??????????????????????????(Uri???????????????????????????null???????????????????????????)
                    if(cursor.moveToFirst())    bothExist++;
                    else { // Event???UID???????????????Provider?????????????????insert?????
                        Property prop = null;
                        long startMillis = 0; // DtStart???????????????????
                        long endMillis = 0;   // DtEnd  ???????????????????
                        // DtStart ??? DtEnd ?????ical4j???VEvent???????????????????????????????????????????
                        if( (prop = event.getProperties().getProperty(Property.DTSTART)) != null ) {
                            startMillis = getDateTime(prop.getValue()).getTime();
                            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                                values.put(CalendarContract.Events.DTSTART, startMillis);
                            } else {
                                values.put("dtstart", startMillis);
                            }
                        }
                        if( (prop = event.getProperties().getProperty(Property.DTEND)) != null ) {
                            endMillis = getDateTime(prop.getValue()).getTime();
                            if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                                values.put(CalendarContract.Events.DTEND, endMillis);
                            } else {
                                values.put("dtend", endMillis);
                            }
                        }

                        // DtStart???DtEnd??????????????put?????????????
                        if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                            if( (prop = event.getProperties().getProperty(Property.SUMMARY)) != null )        values.put(CalendarContract.Events.TITLE, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.LOCATION)) != null )        values.put(CalendarContract.Events.EVENT_LOCATION, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.DESCRIPTION)) != null )    values.put(CalendarContract.Events.DESCRIPTION, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.DURATION)) != null )        values.put(CalendarContract.Events.DURATION, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.RDATE)) != null )        values.put(CalendarContract.Events.RDATE, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.RRULE)) != null )        values.put(CalendarContract.Events.RRULE, prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.UID)) != null )            values.put(CalendarContract.Events.SYNC_DATA1, prop.getValue());
                            values.put(CalendarContract.Events.EVENT_TIMEZONE, model.timezone.getID());

                            values.put(CalendarContract.Events.CALENDAR_ID, localCalDtoList.get(localTargetCal_ChosenPosition).get_id());
                        } else {
                            if( (prop = event.getProperties().getProperty(Property.SUMMARY)) != null )        values.put("title", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.LOCATION)) != null )        values.put("eventLocation", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.DESCRIPTION)) != null )    values.put("description", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.DURATION)) != null )        values.put("duration", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.RDATE)) != null )        values.put("rdate", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.RRULE)) != null )        values.put("rrule", prop.getValue());
                            if( (prop = event.getProperties().getProperty(Property.UID)) != null )            values.put("_sync_id", "http://dummy.crappo.net/calendar/feeds/account/private/full/" + prop.getValue().split("@")[0]);
                            values.put("eventStatus", 1); // ????????????????????????????????1???????????????????????????????????????????????????????API 14?????API????????????????????????????????
                            values.put("eventTimezone", model.timezone.getID());

                            values.put("calendar_id", localCalDtoList.get(localTargetCal_ChosenPosition).get_id());
                        }

                        // AccountName ??? AccountType??????????????????????
                        contentResolver.insert(asSyncAdapter(getEventProvider(), appName, packageName), values);
                        values.clear();
                        bothExist++;
                    }
                    cursor.close();
                } else {
                    Log.e("insertEvents", "cursor is null.");
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            icsOnly = icsExist - bothExist;
            progressBar.dismiss();
            refreshPanelView(); // layout???panel?????????(insert????????)????
        }

    }

    class CalendarDto {
        String ownerAccount;
        String _id;
        String name;
        String account_type;
        String account_name;
        String calendar_displayName;
        String calendar_color;
        public CalendarDto(String name, String calendar_color) {
            this.name = name;
            this.calendar_color = calendar_color;
            ownerAccount = "owner_" +  new SimpleDateFormat("yyyyMMdd_HHmmss", locale).format(new Date()) + "_" + packageName;
            account_type = packageName;
            account_name = appName;
        }
        public String getOwnerAccount() {
            return ownerAccount;
        }
        public void setOwnerAccount(String ownerAccount) {
            this.ownerAccount = ownerAccount;
        }
        public String get_id() {
            return _id;
        }
        public void set_id(String _id) {
            this._id = _id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAccount_type() {
            return account_type;
        }
        public void setAccount_type(String account_type) {
            this.account_type = account_type;
        }
        public String getAccount_name() {
            return account_name;
        }
        public void setAccount_name(String account_name) {
            this.account_name = account_name;
        }
        public String getCalendar_displayName() {
            return calendar_displayName;
        }
        public void setCalendar_displayName(String calendar_displayName) {
            this.calendar_displayName = calendar_displayName;
        }
        public String getCalendar_color() {
            return calendar_color;
        }
        public void setCalendar_color(String calendar_color) {
            this.calendar_color = calendar_color;
        }

        @Override
        public String toString() {
            String str = "ID :" + _id;
            str += "\n(Name:" + name + ")";
            return str;
        }

//        @Override
//        public String toString() {
//            String str = "ownerAcnt:" + ownerAccount;
//            str += "\n_id:" + _id;
//            str += "\nname:" + name;
//            str += "\nacntType:" + account_type;
//            str += "\nacntName:" + account_name;
//            str += "\ndisplayName:" + calendar_displayName;
//            str += "\ncolor:" + calendar_color;
//            return str;
//        }

    }

}




Java Source Code List

net.crappo.android.androics.AccountAuthService.java
net.crappo.android.androics.AndroIcsAuthenticator.java
net.crappo.android.androics.BrowserForDownLoadActivity.java
net.crappo.android.androics.Model4EventList.java
net.crappo.android.androics.Model4Top.java
net.crappo.android.androics.ShowEventListActivity.java
net.crappo.android.androics.ShowEventListAsync.java
net.crappo.android.androics.SimpleFileDialog.java
net.crappo.android.androics.TopActivity.java
net.crappo.android.androics.TopIcsListAsync.java
net.crappo.android.androics.ZipInOutMethods.java