aboutsummaryrefslogtreecommitdiffstats
path: root/cgeo-calendar/src/cgeo/calendar/CalendarActivity.java
blob: 6ff94508225b53457b83b3acaaf028a3f03aec43 (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cgeo.calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public final class CalendarActivity extends Activity {
    private static final String LOG_TAG = "cgeo.calendar";
    private String shortDesc;
    private String hiddenDate;
    private String url;
    private String personalNote;
    private String name;
    private String location;
    private String coords;
    private Uri uri;

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

        try {
            uri = getIntent().getData();
            if (uri == null) {
                finish();
                return;
            }
            shortDesc = getParameter(ICalendar.PARAM_SHORT_DESC);
            hiddenDate = getParameter(ICalendar.PARAM_HIDDEN_DATE);
            url = getParameter(ICalendar.PARAM_URL);
            personalNote = getParameter(ICalendar.PARAM_NOTE);
            name = getParameter(ICalendar.PARAM_NAME);
            location = getParameter(ICalendar.PARAM_LOCATION);
            coords = getParameter(ICalendar.PARAM_COORDS);
            if (name.length() > 0 && hiddenDate.length() > 0) {
                selectCalendarForAdding();
            }
        } catch (Exception e) {
            Log.e(LOG_TAG, e.getMessage(), e);
            finish();
            return;
        }
    }

    private String getParameter(final String paramKey) {
        try {
            final String param = uri.getQueryParameter(paramKey);
            if (param == null) {
                return "";
            }
            return URLDecoder.decode(param, "UTF-8").trim();
        } catch (UnsupportedEncodingException e) {
        }
        return "";
    }

    /**
     * Adds the cache to the Android-calendar if it is an event.
     */
    private void selectCalendarForAdding() {
        final String[] projection = new String[] { "_id", "displayName" };
        final Uri calendarProvider = Compatibility.getCalendarProviderURI();

        // TODO: Handle missing provider
        final Cursor cursor = managedQuery(calendarProvider, projection, "selected=1", null, null);

        final Map<Integer, String> calendars = new HashMap<Integer, String>();
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                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) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                } while (cursor.moveToNext());
            }
            cursor.close();
        }

        if (calendars.isEmpty()) {
            return;
        }

        final CharSequence[] items = calendars.values().toArray(new CharSequence[calendars.size()]);

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.calendars);
        builder.setItems(items, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                final Integer[] keys = calendars.keySet().toArray(new Integer[calendars.size()]);
                final Integer calendarId = keys[item];
                addToCalendar(calendarId);
                finish();
            }
        });
        builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                finish();
            }
        });
        builder.create().show();
    }

    /**
     * @param calendars
     *
     * @param index
     *            The selected calendar
     */
    private void addToCalendar(Integer calendarId) {
        try {
            final Uri calendarProvider = Compatibility.getCalenderEventsProviderURI();

            // date
            final Date eventDate = new Date(Long.parseLong(hiddenDate));
            eventDate.setHours(0);
            eventDate.setMinutes(0);
            eventDate.setSeconds(0);

            // description
            final StringBuilder description = new StringBuilder();
            description.append(url);
            if (shortDesc.length() > 0) {
                // remove images in short description
                final Spanned spanned = Html.fromHtml(shortDesc, null, null);
                String text = spanned.toString();
                final ImageSpan[] spans = spanned.getSpans(0, spanned.length(), ImageSpan.class);
                for (int i = spans.length - 1; i >= 0; i--) {
                    text = text.substring(0, spanned.getSpanStart(spans[i])) + text.substring(spanned.getSpanEnd(spans[i]));
                }
                if (text.length() > 0) {
                    description.append("\n\n");
                    description.append(text);
                }
            }

            if (personalNote.length() > 0) {
                description.append("\n\n").append(Html.fromHtml(personalNote).toString());
            }

            // location
            final StringBuilder locBuffer = new StringBuilder();
            if (coords.length() > 0) {
                locBuffer.append(coords);
            }
            if (location.length() > 0) {
                boolean addParentheses = false;
                if (locBuffer.length() > 0) {
                    addParentheses = true;
                    locBuffer.append(" (");
                }

                locBuffer.append(Html.fromHtml(location).toString());
                if (addParentheses) {
                    locBuffer.append(')');
                }
            }

            // values
            final ContentValues event = new ContentValues();
            event.put("calendar_id", calendarId);
            event.put("dtstart", eventDate.getTime() + 43200000); // noon
            event.put("dtend", eventDate.getTime() + 43200000 + 3600000); // + one hour
            event.put("eventTimezone", "UTC");
            event.put("title", Html.fromHtml(name).toString());
            event.put("description", description.toString());

            if (locBuffer.length() > 0) {
                event.put("eventLocation", locBuffer.toString());
            }
            event.put("allDay", 1);
            event.put("hasAlarm", 0);

            getContentResolver().insert(calendarProvider, event);

            showToast(getResources().getString(R.string.event_success));
        } catch (Exception e) {
            showToast(getResources().getString(R.string.event_fail));

            Log.e(LOG_TAG, "CalendarActivity.addToCalendarFn: " + e.toString());
        }
    }

    public final void showToast(final String text) {
        final Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);

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

}