aboutsummaryrefslogtreecommitdiffstats
path: root/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java
blob: e21de2b501fe1b8d1fded80852e7ba34ddcd0a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cgeo.calendar;

import org.eclipse.jdt.annotation.NonNull;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.widget.Toast;

public final class CalendarActivity extends Activity {
    static final String LOG_TAG = "cgeo.calendar";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            final Uri uri = getIntent().getData();
            if (uri == null) {
                finish();
                return;
            }
            final CalendarEntry entry = new CalendarEntry(uri);
            if (!entry.isValid()) {
                return;
            }
            if (Compatibility.isLevel14()) {
                new AddEntryLevel14(entry, this).addEntryToCalendar();
                finish();
            } else {
                selectCalendarForAdding(entry);
            }
        } catch (final Exception e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            finish();
        }
    }

    /**
     * Adds the cache to the Android-calendar if it is an event.
     *
     * @param entry
     *         new entry to be stored
     */
    private void selectCalendarForAdding(@NonNull final CalendarEntry entry) {
        final SparseArray<String> calendars = queryCalendars();

        if (calendars.size() == 0) {
            showToast(R.string.event_fail);
            finish();
            return;
        }

        final String[] items = new String[calendars.size()];
        for (int i = 0; i < calendars.size(); i++) {
            items[i] = calendars.valueAt(i);
        }

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.calendars);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int item) {
                final int calendarId = calendars.keyAt(item);
                new AddEntry(entry, CalendarActivity.this, calendarId).addEntryToCalendar();
                finish();
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            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);

        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, 100);
        toast.show();
    }
}