はうすてんぼぶ

コードかいてて疑問に思ったことや、興味あることをつらつらと暇なときに書く場所、ここはそんな場所

GoogleカレンダーのMyカレンダー一覧を取得…できますん

湿気と気温で熱くてとろける

ComicAlarmで書籍の発売日をGoogleカレンダーに登録する際に
「登録先のMyカレンダーを設定」できるようにしたのだけれども
どうもHTC製のもの(特にDesire)だとMyカレンダーの一覧を取得できないらしくてうぐぐ…
というかイベントの登録もできない、らしいので∩(・x・)∩オテアゲ!

自分のGalaxySだと普通に取得できるのでさらにうぐぐ
なんなんだろUriが行けないのか属性名がいけないのか…な?

と思って

ちょっと関連のある記事を見つけ、その著者のrmiyaさんに質問させていただいたのですが、
Uriはそのままでも大丈夫とのことでした
http://d.hatena.ne.jp/rmiya/20100725#1280028729

少し話は変わりますが、
rmiyaさんの記事や外国サイトをぐるぐる回ってると
カレンダーのUri持ってくるときの処理はだいたい以下の2つかなぁという結論に至りました

android.os.Build.VERSION.SDK_INTを使わない方法
http://stackoverflow.com/questions/3571818/calendar-contentprovider-url-on-android-phones-with-sense-ui

private String getCalendarUriBase() {

String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}


android.os.Build.VERSION.SDK_INTを使う方法は
rmiyaさんの記事
http://d.hatena.ne.jp/rmiya/20100725#1280028729
をご参照ください

最終的に上の二つの方法を併せて

if (android.os.Build.VERSION.SDK_INT <= 7 )
{
//the old way
calUri = Uri.parse("content://calendar/calendars");
eventUri = Uri.parse("content://calendar/events");
}
else
{
//the new way
String uriBase = getCalendarUriBase();
calUri = Uri.parse(uriBase+"calendars");
eventUri = Uri.parse(uriBase+"events");
}

な感じにしてます
たぶん片方の方法だけで十分なのですが、
HTC製の端末のときでも何とかしてくれるかもしれん、
という根拠の無い期待からわざわざOSのAPIレベルで区別した後に、再度チェックしてます

でまぁ、Uriが悪くないとなると属性名がいけないのか?
と思って程度の低い英語読解力で色々サイトを見てみたけどクリティカルな記事は見つからなかった(´・ω・`)

今は下のような感じで一覧を持ってこようとしています

String projection = new String { "_id", "name" };//idはプライマリーキー、nameはカレンダー名
String selection = "access_level" + "=?";
String selectionArgs = new String { "700" };
Cursor managedCursor = managedQuery(calUri, projection, selection,
selectionArgs, null);
if(managedCursor.moveToFirst()){
int idColumnIndex = managedCursor.getColumnIndex("_id");
int nameColumnIndex = managedCursor.getColumnIndex("name");
Log.d(LOG_TITLE,"calendarNum -> "+managedCursor.getColumnCount());
Log.d(LOG_TITLE,"id column index: "+idColumnIndex+" / name column index:"+nameColumnIndex);
do{
Log.d(LOG_TITLE,"calender info ["+managedCursor.getInt(idColumnIndex)+","+managedCursor.getString(nameColumnIndex)+"]");
}while(managedCursor.moveToNext());
}else{
Log.d(LOG_TITLE,"calendarNum -> 0");
}

この方法だとどうもHTC製の一部端末では何も取得できないとのことorz

ううn、どっかに落ちてないかなDesire
明日学校でDesire持ってる友人にちょっとデバッグさせて、ってお願いしてみるかなー