GoogleCalendarImporter
Googleカレンダーのデータを携帯のスケジューラで読み込めるようにするiアプリを作ってみました。iCal形式ならGoogleカレンダーである必要はないのだけれども、iアプリの仕様上、データをiアプリと同じサーバに置かないといけないので、完全に個人用ツール。
あとはScheduleクラスがaddEntry()しかできないので、重複排除みたいなことができないとか、ToDoの方はいじることができないとか、痒くてたまらない感じ。
一応ソース。エラー処理がほとんど入ってない、、、
import javax.microedition.io.*; import java.io.*; import java.util.*; import com.nttdocomo.ui.*; import com.nttdocomo.io.*; import com.nttdocomo.system.*; import com.nttdocomo.util.*; public class GoogleCalendarImporter extends IApplication { appCanvas m_canvas; public void start() { m_canvas = new appCanvas(); Display.setCurrent(m_canvas); } class appCanvas extends Canvas { public appCanvas() { setSoftLabel(SOFT_KEY_1,"ADD"); setSoftLabel(SOFT_KEY_2,"QUIT"); } public void paint(Graphics g) { g.drawString("Google Calendar Importer",0,30); } public void processEvent(int type,int param) { if (type==Display.KEY_RELEASED_EVENT) { if (param==Display.KEY_SOFT2) { terminate(); } if (param==Display.KEY_SOFT1) { String ics = getICSData("gc.ics"); Vector vevents = getVevents(ics); ScheduleDate sdate; for(int i = 0; i < vevents.size(); i++) { sdate = getSchedule((String)vevents.elementAt(i)); Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); int year = cal.get(Calendar.YEAR); int month = cal.get(Calendar.MONTH) + 1; int day = cal.get(Calendar.DATE); if((year < sdate.get(Calendar.YEAR)) || (((year == sdate.get(Calendar.YEAR)) && (month < sdate.get(Calendar.MONTH) + 1))) || (((year == sdate.get(Calendar.YEAR)) && (month == sdate.get(Calendar.MONTH) + 1)) && (day <= sdate.get(Calendar.DATE)))) { try { Schedule.addEntry(getSummary((String)vevents.elementAt(i)), sdate, false); } catch(InterruptedOperationException e) { } } } } } } String getSummary(String vevent) { String summary = "SUMMARY:"; try { int pos_s = vevent.indexOf(summary); int pos_e = vevent.indexOf("\n", pos_s); return vevent.substring(pos_s + summary.length(), pos_e); } catch (Exception e) { return vevent; } } int getInt(String str) { int start = 0; while(start < str.length() - 1) { if(str.charAt(start) == '0') { start++; } else { break; } } return Integer.parseInt(str.substring(start)); } ScheduleDate getSchedule(String vevent) { ScheduleDate sdate = new ScheduleDate(ScheduleDate.ONETIME); try { int pos_dt = vevent.indexOf("DTSTART"); int pos_s = vevent.indexOf(":", pos_dt); sdate.set(Calendar.YEAR, Integer.parseInt(vevent.substring(pos_s + 1, pos_s + 5))); sdate.set(Calendar.MONTH, getInt(vevent.substring(pos_s + 5, pos_s + 7)) - 1); sdate.set(Calendar.DATE, getInt(vevent.substring(pos_s + 7, pos_s + 9))); if(vevent.charAt(pos_s + 9) == 'T') { sdate.set(Calendar.HOUR_OF_DAY, getInt(vevent.substring(pos_s + 10, pos_s + 12))); sdate.set(Calendar.MINUTE, getInt(vevent.substring(pos_s + 12, pos_s + 14))); } } catch (Exception e) { } return sdate; } Vector getVevents(String ics) { Vector vevents = new Vector(); try { int pos_s = ics.indexOf("BEGIN:VEVENT"); int pos_e; while(pos_s >= 0) { pos_e = ics.indexOf("END:VEVENT", pos_s); vevents.addElement(ics.substring(pos_s, pos_e)); pos_s = ics.indexOf("BEGIN:VEVENT", pos_e); } } catch (Exception e) { } return vevents; } String getICSData(String ics) { HttpConnection wi_connect; try { String wi_url = new String(getSourceURL() + ics); wi_connect = (HttpConnection)Connector.open(wi_url,Connector.READ_WRITE,true); wi_connect.setRequestMethod(HttpConnection.GET); wi_connect.connect(); InputStream wi_stream = wi_connect.openInputStream(); InputStreamReader wi_buffer = new InputStreamReader(wi_stream); StringBuffer w_StringBuffer = new StringBuffer(); String w_buffer = ""; int w_char; while ((w_char = wi_buffer.read()) != -1) { w_StringBuffer.append((char)w_char); } w_buffer = (String)w_StringBuffer.toString(); wi_stream.close(); wi_connect.close(); return w_buffer; } catch(Throwable e) { System.out.println("getICSData() : " + e.getMessage()); } return ""; } } }