diff options
Diffstat (limited to 'cgeo-calendar/src/cgeo/calendar/CalendarActivity.java')
-rw-r--r-- | cgeo-calendar/src/cgeo/calendar/CalendarActivity.java | 89 |
1 files changed, 52 insertions, 37 deletions
diff --git a/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java b/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java index 2fbfd05..e21de2b 100644 --- a/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java +++ b/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java @@ -1,5 +1,7 @@ package cgeo.calendar; +import org.eclipse.jdt.annotation.NonNull; + import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; @@ -16,7 +18,7 @@ public final class CalendarActivity extends Activity { /** Called when the activity is first created. */ @Override - public void onCreate(Bundle savedInstanceState) { + public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { @@ -35,7 +37,7 @@ public final class CalendarActivity extends Activity { } else { selectCalendarForAdding(entry); } - } catch (Exception e) { + } catch (final Exception e) { Log.e(LOG_TAG, e.getMessage(), e); finish(); } @@ -45,40 +47,10 @@ public final class CalendarActivity extends Activity { * Adds the cache to the Android-calendar if it is an event. * * @param entry + * new entry to be stored */ - private void selectCalendarForAdding(final CalendarEntry entry) { - final String[] projection = new String[] { "_id", "displayName" }; - final Uri calendarProvider = Compatibility.getCalendarProviderURI(); - - final Cursor cursor = managedQuery(calendarProvider, projection, "selected=1", null, null); - - if (cursor == null || cursor.getCount() <= 0) { - showToast(R.string.event_fail); - finish(); - return; - } - - final SparseArray<String> calendars = new SparseArray<>(); - cursor.moveToFirst(); - - final int indexId = cursor.getColumnIndex("_id"); - final int indexName = cursor.getColumnIndex("displayName"); - - do { - final String idString = cursor.getString(indexId); - if (idString != null) { - try { - int id = Integer.parseInt(idString); - final String calName = cursor.getString(indexName); - - if (id > 0 && calName != null) { - calendars.put(id, calName); - } - } catch (NumberFormatException e) { - Log.e(LOG_TAG, "CalendarActivity.selectCalendarForAdding", e); - } - } - } while (cursor.moveToNext()); + private void selectCalendarForAdding(@NonNull final CalendarEntry entry) { + final SparseArray<String> calendars = queryCalendars(); if (calendars.size() == 0) { showToast(R.string.event_fail); @@ -95,7 +67,7 @@ public final class CalendarActivity extends Activity { builder.setTitle(R.string.calendars); builder.setItems(items, new DialogInterface.OnClickListener() { @Override - public void onClick(DialogInterface dialog, int item) { + public void onClick(final DialogInterface dialog, final int item) { final int calendarId = calendars.keyAt(item); new AddEntry(entry, CalendarActivity.this, calendarId).addEntryToCalendar(); finish(); @@ -103,13 +75,56 @@ public final class CalendarActivity extends Activity { }); builder.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override - public void onCancel(DialogInterface dialog) { + public void onCancel(final DialogInterface dialog) { finish(); } }); builder.create().show(); } + @NonNull + private SparseArray<String> queryCalendars() { + final SparseArray<String> calendars = new SparseArray<>(); + + final Uri calendarProvider = Compatibility.getCalendarProviderURI(); + + Cursor cursor = null; + try { + final String[] projection = new String[]{"_id", "displayName"}; + cursor = getContentResolver().query(calendarProvider, projection, "selected=1", null, null); + + if (cursor == null) { + return calendars; + } + + cursor.moveToFirst(); + + final int indexId = cursor.getColumnIndex("_id"); + final int indexName = cursor.getColumnIndex("displayName"); + + do { + final String idString = cursor.getString(indexId); + if (idString != null) { + try { + final int id = Integer.parseInt(idString); + final String calName = cursor.getString(indexName); + + if (id > 0 && calName != null) { + calendars.put(id, calName); + } + } catch (final NumberFormatException e) { + Log.e(LOG_TAG, "CalendarActivity.selectCalendarForAdding", e); + } + } + } while (cursor.moveToNext()); + } finally { + if (cursor != null) { + cursor.close(); + } + } + return calendars; + } + public final void showToast(final int res) { final String text = getResources().getString(res); final Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG); |