RecurrencePickerDialog.java revision 86693b2f19243b64f5bb7a363bd756453f2b7d6c
1b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan/*
2b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * Copyright (C) 2013 The Android Open Source Project
3b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan *
4b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * Licensed under the Apache License, Version 2.0 (the "License");
5b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * you may not use this file except in compliance with the License.
6b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * You may obtain a copy of the License at
7b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan *
8b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan *      http://www.apache.org/licenses/LICENSE-2.0
9b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan *
10b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * Unless required by applicable law or agreed to in writing, software
11b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * distributed under the License is distributed on an "AS IS" BASIS,
12b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * See the License for the specific language governing permissions and
14b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan * limitations under the License.
15b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan */
16b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
17b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanpackage com.android.calendar.recurrencepicker;
18b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
19fb2631a67560abd54d3ad66f154bc30728262b54James Kungimport android.app.Activity;
20b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.app.DialogFragment;
21564f618e3fa48a8e4836fec0f216449b0ae809f9James Kungimport android.content.Context;
22fb2631a67560abd54d3ad66f154bc30728262b54James Kungimport android.content.res.Configuration;
23b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.content.res.Resources;
24b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.os.Bundle;
25b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.os.Parcel;
26b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.os.Parcelable;
27b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.text.Editable;
28b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.text.TextUtils;
29b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.text.TextWatcher;
30b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.text.format.DateUtils;
31b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.text.format.Time;
32b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.util.Log;
33b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.util.TimeFormatException;
34b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.view.LayoutInflater;
35b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.view.View;
36b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.view.View.OnClickListener;
37564f618e3fa48a8e4836fec0f216449b0ae809f9James Kungimport android.view.ViewGroup.LayoutParams;
38b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.view.ViewGroup;
39fb2631a67560abd54d3ad66f154bc30728262b54James Kungimport android.view.Window;
40b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.AdapterView;
41b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.AdapterView.OnItemSelectedListener;
42b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.ArrayAdapter;
43b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.Button;
44b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.CompoundButton;
45b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.CompoundButton.OnCheckedChangeListener;
46b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.EditText;
47b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.LinearLayout;
48b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.RadioButton;
49b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.RadioGroup;
50b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.Spinner;
51564f618e3fa48a8e4836fec0f216449b0ae809f9James Kungimport android.widget.Switch;
52564f618e3fa48a8e4836fec0f216449b0ae809f9James Kungimport android.widget.TableLayout;
53b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.TextView;
54b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.Toast;
55b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport android.widget.ToggleButton;
56b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
57b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport com.android.calendar.R;
58b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport com.android.calendar.Utils;
59564f618e3fa48a8e4836fec0f216449b0ae809f9James Kungimport com.android.calendar.CalendarController.ViewType;
60b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport com.android.calendarcommon2.EventRecurrence;
6156f42bfb36d710726051e8268a115697477add61James Kungimport com.android.datetimepicker.date.DatePickerDialog;
62b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
63b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport java.text.DateFormatSymbols;
64b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport java.util.ArrayList;
65b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport java.util.Arrays;
66b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanimport java.util.Calendar;
67b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
68b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chanpublic class RecurrencePickerDialog extends DialogFragment implements OnItemSelectedListener,
69b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        OnCheckedChangeListener, OnClickListener,
70b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        android.widget.RadioGroup.OnCheckedChangeListener, DatePickerDialog.OnDateSetListener {
71b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
72b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final String TAG = "RecurrencePickerDialog";
73b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
74b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    // in dp's
75b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    private static final int MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK = 450;
76b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
77b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Update android:maxLength in EditText as needed
78b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final int INTERVAL_MAX = 99;
79b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final int INTERVAL_DEFAULT = 1;
80b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Update android:maxLength in EditText as needed
81b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final int COUNT_MAX = 730;
82b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final int COUNT_DEFAULT = 5;
83b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
8456f42bfb36d710726051e8268a115697477add61James Kung    private DatePickerDialog mDatePickerDialog;
8556f42bfb36d710726051e8268a115697477add61James Kung
86564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private class RecurrenceModel implements Parcelable {
87b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
88b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Should match EventRecurrence.DAILY, etc
89b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int FREQ_DAILY = 0;
90b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int FREQ_WEEKLY = 1;
91b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int FREQ_MONTHLY = 2;
92b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int FREQ_YEARLY = 3;
93b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
94b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int END_NEVER = 0;
95b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int END_BY_DATE = 1;
96b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int END_BY_COUNT = 2;
97b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
98b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int MONTHLY_BY_DATE = 0;
99b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        static final int MONTHLY_BY_NTH_DAY_OF_WEEK = 1;
100b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
101564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        static final int STATE_NO_RECURRENCE = 0;
102564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        static final int STATE_RECURRENCE = 1;
103564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
104564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        int recurrenceState;
105564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
106b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
107b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * FREQ: Repeat pattern
108b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         *
109b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see FREQ_DAILY
110b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see FREQ_WEEKLY
111b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see FREQ_MONTHLY
112b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see FREQ_YEARLY
113b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
114564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        int freq = FREQ_WEEKLY;
115b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
116b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
117b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * INTERVAL: Every n days/weeks/months/years. n >= 1
118b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
119b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int interval = INTERVAL_DEFAULT;
120b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
121b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
122b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * UNTIL and COUNT: How does the the event end?
123b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         *
124b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see END_NEVER
125b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see END_BY_DATE
126b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see END_BY_COUNT
127b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see untilDate
128b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see untilCount
129b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
130b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int end;
131b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
132b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
133b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * UNTIL: Date of the last recurrence. Used when until == END_BY_DATE
134b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
135b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        Time endDate;
136b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
137b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
138b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * COUNT: Times to repeat. Use when until == END_BY_COUNT
139b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
140b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int endCount = COUNT_DEFAULT;
141b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
142b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
143b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * BYDAY: Days of the week to be repeated. Sun = 0, Mon = 1, etc
144b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
145b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        boolean[] weeklyByDayOfWeek = new boolean[7];
146b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
147b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
148b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * BYDAY AND BYMONTHDAY: How to repeat monthly events? Same date of the
149b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * month or Same nth day of week.
150b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         *
151b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see MONTHLY_BY_DATE
152b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * @see MONTHLY_BY_NTH_DAY_OF_WEEK
153b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
154b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int monthlyRepeat;
155b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
156b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
157b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * Day of the month to repeat. Used when monthlyRepeat ==
158b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * MONTHLY_BY_DATE
159b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
160b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int monthlyByMonthDay;
161b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
162b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
163b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * Day of the week to repeat. Used when monthlyRepeat ==
164b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * MONTHLY_BY_NTH_DAY_OF_WEEK
165b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
166b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int monthlyByDayOfWeek;
167b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
168b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /**
169b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * Nth day of the week to repeat. Used when monthlyRepeat ==
170b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * MONTHLY_BY_NTH_DAY_OF_WEEK 0=undefined, 1=1st, 2=2nd, etc
171b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
172b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int monthlyByNthDayOfWeek;
173b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
174b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /*
175b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * (generated method)
176b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
177b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
178b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public String toString() {
179b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return "Model [freq=" + freq + ", interval=" + interval + ", end=" + end + ", endDate="
180b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    + endDate + ", endCount=" + endCount + ", weeklyByDayOfWeek="
181b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    + Arrays.toString(weeklyByDayOfWeek) + ", monthlyRepeat=" + monthlyRepeat
182b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    + ", monthlyByMonthDay=" + monthlyByMonthDay + ", monthlyByDayOfWeek="
183b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    + monthlyByDayOfWeek + ", monthlyByNthDayOfWeek=" + monthlyByNthDayOfWeek + "]";
184b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
185b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
186b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
187b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public int describeContents() {
188b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return 0;
189b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
190b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
191564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        public RecurrenceModel() {
192b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
193b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
194b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
195b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public void writeToParcel(Parcel dest, int flags) {
196b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(freq);
197b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(interval);
198b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(end);
199b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(endDate.year);
200b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(endDate.month);
201b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(endDate.monthDay);
202b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(endCount);
203b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeBooleanArray(weeklyByDayOfWeek);
204b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(monthlyRepeat);
205b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(monthlyByMonthDay);
206b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(monthlyByDayOfWeek);
207b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dest.writeInt(monthlyByNthDayOfWeek);
208564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            dest.writeInt(recurrenceState);
209b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
210b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
211b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
212b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    class minMaxTextWatcher implements TextWatcher {
213b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        private int mMin;
214b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        private int mMax;
215b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        private int mDefault;
216b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
217b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public minMaxTextWatcher(int min, int defaultInt, int max) {
218b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mMin = min;
219b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mMax = max;
220b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mDefault = defaultInt;
221b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
222b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
223b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
224b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public void afterTextChanged(Editable s) {
225b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
226b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            boolean updated = false;
227b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            int value;
228b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            try {
229b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                value = Integer.parseInt(s.toString());
230b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } catch (NumberFormatException e) {
231b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                value = mDefault;
232b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
233b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
234b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (value < mMin) {
235b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                value = mMin;
236b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                updated = true;
237b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } else if (value > mMax) {
238b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                updated = true;
239b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                value = mMax;
240b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
241b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
242b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            // Update UI
243b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (updated) {
244b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                s.clear();
245b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                s.append(Integer.toString(value));
246b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
247b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
248564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            updateDoneButtonState();
249b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            onChange(value);
250b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
251b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
252b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /** Override to be called after each key stroke */
253b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        void onChange(int value) {
254b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
255b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
256b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
257b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
258b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
259b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
260b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        @Override
261b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        public void onTextChanged(CharSequence s, int start, int before, int count) {
262b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
263b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
264b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
265b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Resources mResources;
266b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private EventRecurrence mRecurrence = new EventRecurrence();
267b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Time mTime = new Time(); // TODO timezone?
268564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private RecurrenceModel mModel = new RecurrenceModel();
269b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Toast mToast;
270b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
271b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private final int[] TIME_DAY_TO_CALENDAR_DAY = new int[] {
272b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.SUNDAY,
273b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.MONDAY,
274b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.TUESDAY,
275b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.WEDNESDAY,
276b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.THURSDAY,
277b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.FRIDAY,
278b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Calendar.SATURDAY,
279b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    };
280b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
281b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Call mStringBuilder.setLength(0) before formatting any string or else the
282b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // formatted text will accumulate.
283b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // private final StringBuilder mStringBuilder = new StringBuilder();
284b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // private Formatter mFormatter = new Formatter(mStringBuilder);
285b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
286b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private View mView;
287b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
288b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Spinner mFreqSpinner;
289b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final int[] mFreqModelToEventRecurrence = {
290b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            EventRecurrence.DAILY,
291b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            EventRecurrence.WEEKLY,
292b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            EventRecurrence.MONTHLY,
293b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            EventRecurrence.YEARLY
294b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    };
295b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
296b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public static final String BUNDLE_START_TIME_MILLIS = "bundle_event_start_time";
297b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public static final String BUNDLE_TIME_ZONE = "bundle_event_time_zone";
298b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public static final String BUNDLE_RRULE = "bundle_event_rrule";
299b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
300b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final String BUNDLE_MODEL = "bundle_model";
301b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    private static final String BUNDLE_END_COUNT_HAS_FOCUS = "bundle_end_count_has_focus";
302b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
303b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private static final String FRAG_TAG_DATE_PICKER = "tag_date_picker_frag";
304b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
305564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private Switch mRepeatSwitch;
306564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
307b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private EditText mInterval;
308b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private TextView mIntervalPreText;
309b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private TextView mIntervalPostText;
310b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
311aafaa5f3d4d94d193186df868a76aed484a14255James Kung    private int mIntervalResId = -1;
312aafaa5f3d4d94d193186df868a76aed484a14255James Kung
313b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Spinner mEndSpinner;
314564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private TextView mEndDateTextView;
315b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private EditText mEndCount;
316564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private TextView mPostEndCount;
317564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private boolean mHidePostEndCount;
318b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
319b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private ArrayList<CharSequence> mEndSpinnerArray = new ArrayList<CharSequence>(3);
320564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private EndSpinnerAdapter mEndSpinnerAdapter;
321b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private String mEndNeverStr;
322b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    private String mEndDateLabel;
323564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private String mEndCountLabel;
324b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
325b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    /** Hold toggle buttons in the order per user's first day of week preference */
326b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private LinearLayout mWeekGroup;
327b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    private LinearLayout mWeekGroup2;
328b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Sun = 0
329b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private ToggleButton[] mWeekByDayButtons = new ToggleButton[7];
33086693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein    /** A double array of Strings to hold the 7x5 list of possible strings of the form:
33186693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein     *  "on every [Nth] [DAY_OF_WEEK]", e.g. "on every second Monday",
33286693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein     *  where [Nth] can be [first, second, third, fourth, last] */
33386693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein    private String[][] mMonthRepeatByDayOfWeekStrs;
334b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
335b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private LinearLayout mMonthGroup;
336b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private RadioGroup mMonthRepeatByRadioGroup;
337564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private RadioButton mRepeatMonthlyByNthDayOfWeek;
338564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private RadioButton mRepeatMonthlyByNthDayOfMonth;
339b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private String mMonthRepeatByDayOfWeekStr;
340b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
341b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private Button mDone;
342b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
343b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private OnRecurrenceSetListener mRecurrenceSetListener;
344b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
345b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public RecurrencePickerDialog() {
346b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
347b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
348b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    static public boolean canHandleRecurrenceRule(EventRecurrence er) {
349b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        switch (er.freq) {
350b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.DAILY:
351b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.MONTHLY:
352b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.YEARLY:
353b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.WEEKLY:
354b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
355b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            default:
356b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                return false;
357b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
358b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
359b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (er.count > 0 && !TextUtils.isEmpty(er.until)) {
360b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return false;
361b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
362b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
363b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Weekly: For "repeat by day of week", the day of week to repeat is in
364b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // er.byday[]
365b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
366b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        /*
367b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * Monthly: For "repeat by nth day of week" the day of week to repeat is
368b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * in er.byday[] and the "nth" is stored in er.bydayNum[]. Currently we
369b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         * can handle only one and only in monthly
370b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan         */
371b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int numOfByDayNum = 0;
372b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        for (int i = 0; i < er.bydayCount; i++) {
373b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (er.bydayNum[i] > 0) {
374b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                ++numOfByDayNum;
375b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
376b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
377b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
378b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (numOfByDayNum > 1) {
379b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return false;
380b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
381b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
382b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (numOfByDayNum > 0 && er.freq != EventRecurrence.MONTHLY) {
383b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return false;
384b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
385b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
386b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // The UI only handle repeat by one day of month i.e. not 9th and 10th
387b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // of every month
388b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (er.bymonthdayCount > 1) {
389b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            return false;
390b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
391b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
392b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (er.freq == EventRecurrence.MONTHLY) {
393b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (er.bydayCount > 1) {
394b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                return false;
395b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
396b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (er.bydayCount > 0 && er.bymonthdayCount > 0) {
397b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                return false;
398b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
399b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
400b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
401b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        return true;
402b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
403b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
404b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // TODO don't lose data when getting data that our UI can't handle
405564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    static private void copyEventRecurrenceToModel(final EventRecurrence er,
406564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            RecurrenceModel model) {
407b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Freq:
408b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        switch (er.freq) {
409b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.DAILY:
410564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                model.freq = RecurrenceModel.FREQ_DAILY;
411b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
412b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.MONTHLY:
413564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                model.freq = RecurrenceModel.FREQ_MONTHLY;
414b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
415b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.YEARLY:
416564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                model.freq = RecurrenceModel.FREQ_YEARLY;
417b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
418b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            case EventRecurrence.WEEKLY:
419564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                model.freq = RecurrenceModel.FREQ_WEEKLY;
420b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
421b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            default:
422b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                throw new IllegalStateException("freq=" + er.freq);
423b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
424b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
425b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Interval:
426b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (er.interval > 0) {
427b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            model.interval = er.interval;
428b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
429b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
430b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // End:
431b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // End by count:
432b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        model.endCount = er.count;
433b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (model.endCount > 0) {
434564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            model.end = RecurrenceModel.END_BY_COUNT;
435b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
436b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
437b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // End by date:
438b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (!TextUtils.isEmpty(er.until)) {
439b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (model.endDate == null) {
440b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                model.endDate = new Time();
441b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
442b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
443b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            try {
444b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                model.endDate.parse(er.until);
445b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } catch (TimeFormatException e) {
446b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                model.endDate = null;
447b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
448b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
449b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            // LIMITATION: The UI can only handle END_BY_DATE or END_BY_COUNT
450564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (model.end == RecurrenceModel.END_BY_COUNT && model.endDate != null) {
451b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                throw new IllegalStateException("freq=" + er.freq);
452b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
453b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
454564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            model.end = RecurrenceModel.END_BY_DATE;
455b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
456b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
457b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Weekly: repeat by day of week or Monthly: repeat by nth day of week
458b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // in the month
459b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        Arrays.fill(model.weeklyByDayOfWeek, false);
460b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (er.bydayCount > 0) {
461b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            int count = 0;
462b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            for (int i = 0; i < er.bydayCount; i++) {
463b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                int dayOfWeek = EventRecurrence.day2TimeDay(er.byday[i]);
464b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                model.weeklyByDayOfWeek[dayOfWeek] = true;
465b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
466564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (model.freq == RecurrenceModel.FREQ_MONTHLY && er.bydayNum[i] > 0) {
467b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    // LIMITATION: Can handle only (one) weekDayNum and only
468b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    // when
469b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    // monthly
470b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    model.monthlyByDayOfWeek = dayOfWeek;
471b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    model.monthlyByNthDayOfWeek = er.bydayNum[i];
472564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
473b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    count++;
474b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
475b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
476b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
477564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
478b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (er.bydayCount != 1) {
479b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    // Can't handle 1st Monday and 2nd Wed
480b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    throw new IllegalStateException("Can handle only 1 byDayOfWeek in monthly");
481b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
482b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (count != 1) {
483b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    throw new IllegalStateException(
484b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                            "Didn't specify which nth day of week to repeat for a monthly");
485b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
486b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
487b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
488b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
489b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Monthly by day of month
490564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
491b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (er.bymonthdayCount == 1) {
492564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
493b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    throw new IllegalStateException(
494b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                            "Can handle only by monthday or by nth day of week, not both");
495b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
496b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                model.monthlyByMonthDay = er.bymonthday[0];
497564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
498b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } else if (er.bymonthCount > 1) {
499b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                // LIMITATION: Can handle only one month day
500b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                throw new IllegalStateException("Can handle only one bymonthday");
501b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
502b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
503b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
504b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
505564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    static private void copyModelToEventRecurrence(final RecurrenceModel model,
506564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            EventRecurrence er) {
507564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (model.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
508b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            throw new IllegalStateException("There's no recurrence");
509b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
510b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
511b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Freq
512b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        er.freq = mFreqModelToEventRecurrence[model.freq];
513b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
514b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Interval
515b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (model.interval <= 1) {
516b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            er.interval = 0;
517b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else {
518b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            er.interval = model.interval;
519b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
520b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
521b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // End
522b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        switch (model.end) {
523564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.END_BY_DATE:
524b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (model.endDate != null) {
525b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    model.endDate.switchTimezone(Time.TIMEZONE_UTC);
526b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    model.endDate.normalize(false);
527b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.until = model.endDate.format2445();
528b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.count = 0;
529b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                } else {
530b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    throw new IllegalStateException("end = END_BY_DATE but endDate is null");
531b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
532b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
533564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.END_BY_COUNT:
534b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                er.count = model.endCount;
535b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                er.until = null;
536b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (er.count <= 0) {
537b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    throw new IllegalStateException("count is " + er.count);
538b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
539b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
540b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            default:
541b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                er.count = 0;
542b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                er.until = null;
543b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
544b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
545b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
546b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Weekly && monthly repeat patterns
547b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        er.bydayCount = 0;
548b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        er.bymonthdayCount = 0;
549b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
550b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        switch (model.freq) {
551564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_MONTHLY:
552564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
553b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (model.monthlyByMonthDay > 0) {
554b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        if (er.bymonthday == null || er.bymonthdayCount < 1) {
555b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                            er.bymonthday = new int[1];
556b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        }
557b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.bymonthday[0] = model.monthlyByMonthDay;
558b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.bymonthdayCount = 1;
559b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
560564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                } else if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
561b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (model.monthlyByNthDayOfWeek <= 0) {
562b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        throw new IllegalStateException("month repeat by nth week but n is "
563b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                                + model.monthlyByNthDayOfWeek);
564b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
565b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    int count = 1;
566b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (er.bydayCount < count || er.byday == null || er.bydayNum == null) {
567b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.byday = new int[count];
568b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.bydayNum = new int[count];
569b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
570b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.bydayCount = count;
571b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.byday[0] = EventRecurrence.timeDay2Day(model.monthlyByDayOfWeek);
572b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.bydayNum[0] = model.monthlyByNthDayOfWeek;
573b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
574b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
575564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_WEEKLY:
576b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                int count = 0;
577b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                for (int i = 0; i < 7; i++) {
578b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (model.weeklyByDayOfWeek[i]) {
579b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        count++;
580b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
581b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
582b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
583b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (er.bydayCount < count || er.byday == null || er.bydayNum == null) {
584b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.byday = new int[count];
585b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    er.bydayNum = new int[count];
586b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
587b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                er.bydayCount = count;
588b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
589b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                for (int i = 6; i >= 0; i--) {
590b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (model.weeklyByDayOfWeek[i]) {
591b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.bydayNum[--count] = 0;
592b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        er.byday[count] = EventRecurrence.timeDay2Day(i);
593b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
594b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
595b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                break;
596b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
597b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
598b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (!canHandleRecurrenceRule(er)) {
599b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            throw new IllegalStateException("UI generated recurrence that it can't handle. ER:"
600b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    + er.toString() + " Model: " + model.toString());
601b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
602b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
603b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
604b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
605b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public View onCreateView(LayoutInflater inflater, ViewGroup container,
606b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Bundle savedInstanceState) {
607b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mRecurrence.wkst = EventRecurrence.timeDay2Day(Utils.getFirstDayOfWeek(getActivity()));
608b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
609fb2631a67560abd54d3ad66f154bc30728262b54James Kung        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
610fb2631a67560abd54d3ad66f154bc30728262b54James Kung
611b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        boolean endCountHasFocus = false;
612b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (savedInstanceState != null) {
613564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            RecurrenceModel m = (RecurrenceModel) savedInstanceState.get(BUNDLE_MODEL);
614b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (m != null) {
615b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mModel = m;
616b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
617b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            endCountHasFocus = savedInstanceState.getBoolean(BUNDLE_END_COUNT_HAS_FOCUS);
618b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else {
619b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            Bundle b = getArguments();
620b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (b != null) {
621b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mTime.set(b.getLong(BUNDLE_START_TIME_MILLIS));
622b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
623b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                String tz = b.getString(BUNDLE_TIME_ZONE);
624b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (!TextUtils.isEmpty(tz)) {
625b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    mTime.timezone = tz;
626b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
627b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mTime.normalize(false);
628b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
629b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                // Time days of week: Sun=0, Mon=1, etc
630b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mModel.weeklyByDayOfWeek[mTime.weekDay] = true;
631b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                String rrule = b.getString(BUNDLE_RRULE);
632b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (!TextUtils.isEmpty(rrule)) {
633564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.recurrenceState = RecurrenceModel.STATE_RECURRENCE;
634b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    mRecurrence.parse(rrule);
635b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    copyEventRecurrenceToModel(mRecurrence, mModel);
636564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    // Leave today's day of week as checked by default in weekly view.
637564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    if (mRecurrence.bydayCount == 0) {
638564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        mModel.weeklyByDayOfWeek[mTime.weekDay] = true;
639564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    }
640b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
641564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
642b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } else {
643b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mTime.setToNow();
644b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
645b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
646b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
647b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mResources = getResources();
648fb2631a67560abd54d3ad66f154bc30728262b54James Kung        mView = inflater.inflate(R.layout.recurrencepicker, container, true);
649fb2631a67560abd54d3ad66f154bc30728262b54James Kung
650fb2631a67560abd54d3ad66f154bc30728262b54James Kung        final Activity activity = getActivity();
651fb2631a67560abd54d3ad66f154bc30728262b54James Kung        final Configuration config = activity.getResources().getConfiguration();
652564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
653564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mRepeatSwitch = (Switch) mView.findViewById(R.id.repeat_switch);
654564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mRepeatSwitch.setChecked(mModel.recurrenceState == RecurrenceModel.STATE_RECURRENCE);
655564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mRepeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
656564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
657564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            @Override
658564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
659564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mModel.recurrenceState = isChecked ? RecurrenceModel.STATE_RECURRENCE
660564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        : RecurrenceModel.STATE_NO_RECURRENCE;
661564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                togglePickerOptions();
662564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
663564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        });
664b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
665b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mFreqSpinner = (Spinner) mView.findViewById(R.id.freqSpinner);
666b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mFreqSpinner.setOnItemSelectedListener(this);
667b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        ArrayAdapter<CharSequence> freqAdapter = ArrayAdapter.createFromResource(getActivity(),
668b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                R.array.recurrence_freq, R.layout.recurrencepicker_freq_item);
669b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        freqAdapter.setDropDownViewResource(R.layout.recurrencepicker_freq_item);
670b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mFreqSpinner.setAdapter(freqAdapter);
671b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
672b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mInterval = (EditText) mView.findViewById(R.id.interval);
673b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mInterval.addTextChangedListener(new minMaxTextWatcher(1, INTERVAL_DEFAULT, INTERVAL_MAX) {
674b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            @Override
675b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            void onChange(int v) {
676aafaa5f3d4d94d193186df868a76aed484a14255James Kung                if (mIntervalResId != -1 && mInterval.getText().toString().length() > 0) {
677564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.interval = v;
678aafaa5f3d4d94d193186df868a76aed484a14255James Kung                    updateIntervalText();
679aafaa5f3d4d94d193186df868a76aed484a14255James Kung                    mInterval.requestLayout();
680aafaa5f3d4d94d193186df868a76aed484a14255James Kung                }
681b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
682b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        });
683b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mIntervalPreText = (TextView) mView.findViewById(R.id.intervalPreText);
684b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mIntervalPostText = (TextView) mView.findViewById(R.id.intervalPostText);
685b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
686b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndNeverStr = mResources.getString(R.string.recurrence_end_continously);
687b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        mEndDateLabel = mResources.getString(R.string.recurrence_end_date_label);
688564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mEndCountLabel = mResources.getString(R.string.recurrence_end_count_label);
689564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
690b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndSpinnerArray.add(mEndNeverStr);
691b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        mEndSpinnerArray.add(mEndDateLabel);
692564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mEndSpinnerArray.add(mEndCountLabel);
693b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndSpinner = (Spinner) mView.findViewById(R.id.endSpinner);
694b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndSpinner.setOnItemSelectedListener(this);
695564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mEndSpinnerAdapter = new EndSpinnerAdapter(getActivity(), mEndSpinnerArray,
696564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                R.layout.recurrencepicker_freq_item, R.layout.recurrencepicker_end_text);
697b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndSpinnerAdapter.setDropDownViewResource(R.layout.recurrencepicker_freq_item);
698b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndSpinner.setAdapter(mEndSpinnerAdapter);
699b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
700b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndCount = (EditText) mView.findViewById(R.id.endCount);
701b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndCount.addTextChangedListener(new minMaxTextWatcher(1, COUNT_DEFAULT, COUNT_MAX) {
702b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            @Override
703b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            void onChange(int v) {
704564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (mModel.endCount != v) {
705564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.endCount = v;
706564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    updateEndCountText();
707564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mEndCount.requestLayout();
708564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
709b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
710b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        });
711564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mPostEndCount = (TextView) mView.findViewById(R.id.postEndCount);
712564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
713564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mEndDateTextView = (TextView) mView.findViewById(R.id.endDate);
714b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mEndDateTextView.setOnClickListener(this);
715b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (mModel.endDate == null) {
716b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mModel.endDate = new Time(mTime);
717b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            switch (mModel.freq) {
718564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.FREQ_DAILY:
719564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.FREQ_WEEKLY:
720b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    mModel.endDate.month += 1;
721b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
722564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.FREQ_MONTHLY:
723b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    mModel.endDate.month += 3;
724b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
725564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.FREQ_YEARLY:
726b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    mModel.endDate.year += 3;
727b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
728b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
729b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mModel.endDate.normalize(false);
730b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
731b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
732b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mWeekGroup = (LinearLayout) mView.findViewById(R.id.weekGroup);
733b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        mWeekGroup2 = (LinearLayout) mView.findViewById(R.id.weekGroup2);
734b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
735b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // In Calendar.java day of week order e.g Sun = 1 ... Sat = 7
736b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        String[] dayOfWeekString = new DateFormatSymbols().getWeekdays();
73786693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein
73886693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs = new String[7][];
73986693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        // from Time.SUNDAY as 0 through Time.SATURDAY as 6
74086693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[0] = mResources.getStringArray(R.array.repeat_by_nth_sun);
74186693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[1] = mResources.getStringArray(R.array.repeat_by_nth_mon);
74286693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[2] = mResources.getStringArray(R.array.repeat_by_nth_tues);
74386693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[3] = mResources.getStringArray(R.array.repeat_by_nth_wed);
74486693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[4] = mResources.getStringArray(R.array.repeat_by_nth_thurs);
74586693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[5] = mResources.getStringArray(R.array.repeat_by_nth_fri);
74686693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein        mMonthRepeatByDayOfWeekStrs[6] = mResources.getStringArray(R.array.repeat_by_nth_sat);
747b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
748b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // In Time.java day of week order e.g. Sun = 0
749b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int idx = Utils.getFirstDayOfWeek(getActivity());
750b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
751b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // In Calendar.java day of week order e.g Sun = 1 ... Sat = 7
752b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        dayOfWeekString = new DateFormatSymbols().getShortWeekdays();
753b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
754b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        int numOfButtonsInRow1;
755b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        int numOfButtonsInRow2;
756b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
757b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        if (mResources.getConfiguration().screenWidthDp > MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK) {
758b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            numOfButtonsInRow1 = 7;
759b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            numOfButtonsInRow2 = 0;
760b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekGroup2.setVisibility(View.GONE);
761b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekGroup2.getChildAt(3).setVisibility(View.GONE);
762b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        } else {
763b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            numOfButtonsInRow1 = 4;
764b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            numOfButtonsInRow2 = 3;
765b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
766b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekGroup2.setVisibility(View.VISIBLE);
767b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            // Set rightmost button on the second row invisible so it takes up
768b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            // space and everything centers properly
769b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekGroup2.getChildAt(3).setVisibility(View.INVISIBLE);
770b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        }
771b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
772b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        /* First row */
773b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        for (int i = 0; i < 7; i++) {
774b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            if (i >= numOfButtonsInRow1) {
775b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan                mWeekGroup.getChildAt(i).setVisibility(View.GONE);
776b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan                continue;
777b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            }
778b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
779b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mWeekByDayButtons[idx] = (ToggleButton) mWeekGroup.getChildAt(i);
780b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mWeekByDayButtons[idx].setTextOff(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
781b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mWeekByDayButtons[idx].setTextOn(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
782b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mWeekByDayButtons[idx].setOnCheckedChangeListener(this);
783b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
784b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (++idx >= 7) {
785b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                idx = 0;
786b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
787b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
788b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
789b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        /* 2nd Row */
790b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        for (int i = 0; i < 3; i++) {
791b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            if (i >= numOfButtonsInRow2) {
792b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan                mWeekGroup2.getChildAt(i).setVisibility(View.GONE);
793b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan                continue;
794b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            }
795b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekByDayButtons[idx] = (ToggleButton) mWeekGroup2.getChildAt(i);
796b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekByDayButtons[idx].setTextOff(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
797b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekByDayButtons[idx].setTextOn(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
798b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mWeekByDayButtons[idx].setOnCheckedChangeListener(this);
799b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
800b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            if (++idx >= 7) {
801b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan                idx = 0;
802b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            }
803b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        }
804b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
805b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mMonthGroup = (LinearLayout) mView.findViewById(R.id.monthGroup);
806b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mMonthRepeatByRadioGroup = (RadioGroup) mView.findViewById(R.id.monthGroup);
807b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mMonthRepeatByRadioGroup.setOnCheckedChangeListener(this);
808564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mRepeatMonthlyByNthDayOfWeek = (RadioButton) mView
809b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                .findViewById(R.id.repeatMonthlyByNthDayOfTheWeek);
810564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mRepeatMonthlyByNthDayOfMonth = (RadioButton) mView
811564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                .findViewById(R.id.repeatMonthlyByNthDayOfMonth);
812b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
813b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mDone = (Button) mView.findViewById(R.id.done);
814b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mDone.setOnClickListener(this);
815b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
816564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        togglePickerOptions();
817b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        updateDialog();
818b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        if (endCountHasFocus) {
819b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            mEndCount.requestFocus();
820b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        }
821b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        return mView;
822b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
823b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
824564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private void togglePickerOptions() {
825564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
826564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mFreqSpinner.setEnabled(false);
827564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndSpinner.setEnabled(false);
828564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mIntervalPreText.setEnabled(false);
829564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mInterval.setEnabled(false);
830564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mIntervalPostText.setEnabled(false);
831564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mMonthRepeatByRadioGroup.setEnabled(false);
832564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndCount.setEnabled(false);
833564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mPostEndCount.setEnabled(false);
834564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndDateTextView.setEnabled(false);
835564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mRepeatMonthlyByNthDayOfWeek.setEnabled(false);
836564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mRepeatMonthlyByNthDayOfMonth.setEnabled(false);
837564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            for (Button button : mWeekByDayButtons) {
838564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                button.setEnabled(false);
839564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
840564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        } else {
841564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mView.findViewById(R.id.options).setEnabled(true);
842564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mFreqSpinner.setEnabled(true);
843564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndSpinner.setEnabled(true);
844564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mIntervalPreText.setEnabled(true);
845564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mInterval.setEnabled(true);
846564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mIntervalPostText.setEnabled(true);
847564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mMonthRepeatByRadioGroup.setEnabled(true);
848564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndCount.setEnabled(true);
849564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mPostEndCount.setEnabled(true);
850564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndDateTextView.setEnabled(true);
851564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mRepeatMonthlyByNthDayOfWeek.setEnabled(true);
852564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mRepeatMonthlyByNthDayOfMonth.setEnabled(true);
853564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            for (Button button : mWeekByDayButtons) {
854564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                button.setEnabled(true);
855564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
856564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
857564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        updateDoneButtonState();
858564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    }
859564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
860564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private void updateDoneButtonState() {
861564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
862564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mDone.setEnabled(true);
863564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return;
864564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
865564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
866564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mInterval.getText().toString().length() == 0) {
867564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mDone.setEnabled(false);
868564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return;
869564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
870564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
871564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mEndCount.getVisibility() == View.VISIBLE &&
872564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mEndCount.getText().toString().length() == 0) {
873564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mDone.setEnabled(false);
874564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return;
875564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
876564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
877564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mModel.freq == RecurrenceModel.FREQ_WEEKLY) {
878564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            for (CompoundButton b : mWeekByDayButtons) {
879564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (b.isChecked()) {
880564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mDone.setEnabled(true);
881564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    return;
882564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
883564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
884564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mDone.setEnabled(false);
885564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return;
886564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
887564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
888564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mDone.setEnabled(true);
889564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    }
890564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
891b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
892b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onSaveInstanceState(Bundle outState) {
893b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        super.onSaveInstanceState(outState);
894b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        outState.putParcelable(BUNDLE_MODEL, mModel);
895b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        if (mEndCount.hasFocus()) {
896b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan            outState.putBoolean(BUNDLE_END_COUNT_HAS_FOCUS, true);
897b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        }
898b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
899b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
900b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void updateDialog() {
901b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Interval
902b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // Checking before setting because this causes infinite recursion
903b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        // in afterTextWatcher
904b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        final String intervalStr = Integer.toString(mModel.interval);
905b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (!intervalStr.equals(mInterval.getText().toString())) {
906b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mInterval.setText(intervalStr);
907b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
908b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
909564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mFreqSpinner.setSelection(mModel.freq);
910564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mWeekGroup.setVisibility(mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
911564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mWeekGroup2.setVisibility(mModel.freq == RecurrenceModel.FREQ_WEEKLY ? View.VISIBLE : View.GONE);
912564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mMonthGroup.setVisibility(mModel.freq == RecurrenceModel.FREQ_MONTHLY ? View.VISIBLE : View.GONE);
913b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
914564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        switch (mModel.freq) {
915564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_DAILY:
916564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mIntervalResId = R.plurals.recurrence_interval_daily;
917564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                break;
918b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
919564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_WEEKLY:
920564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mIntervalResId = R.plurals.recurrence_interval_weekly;
921564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                for (int i = 0; i < 7; i++) {
922564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mWeekByDayButtons[i].setChecked(mModel.weeklyByDayOfWeek[i]);
923564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
924564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                break;
925b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
926564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_MONTHLY:
927564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mIntervalResId = R.plurals.recurrence_interval_monthly;
928b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
929564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
930564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfMonth);
931564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                } else if (mModel.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
932564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mMonthRepeatByRadioGroup.check(R.id.repeatMonthlyByNthDayOfTheWeek);
933564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
934b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
935564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (mMonthRepeatByDayOfWeekStr == null) {
936564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    if (mModel.monthlyByNthDayOfWeek == 0) {
937a044b42e8da182e4cbc5ba07c5aaa246ca6dc773Sam Blitzstein                        mModel.monthlyByNthDayOfWeek = (mTime.monthDay + 6) / 7;
938a044b42e8da182e4cbc5ba07c5aaa246ca6dc773Sam Blitzstein                        mModel.monthlyByDayOfWeek = mTime.weekDay;
939b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
940b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
94186693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein                    String[] monthlyByNthDayOfWeekStrs =
94286693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein                            mMonthRepeatByDayOfWeekStrs[mModel.monthlyByDayOfWeek];
94386693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein                    mMonthRepeatByDayOfWeekStr =
94486693b2f19243b64f5bb7a363bd756453f2b7d6cSam Blitzstein                            monthlyByNthDayOfWeekStrs[mModel.monthlyByNthDayOfWeek - 1];
945564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mRepeatMonthlyByNthDayOfWeek.setText(mMonthRepeatByDayOfWeekStr);
946564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
947564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                break;
948564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
949564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            case RecurrenceModel.FREQ_YEARLY:
950564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mIntervalResId = R.plurals.recurrence_interval_yearly;
951564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                break;
952564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
953564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        updateIntervalText();
954564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        updateDoneButtonState();
955564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
956564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        mEndSpinner.setSelection(mModel.end);
957564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mModel.end == RecurrenceModel.END_BY_DATE) {
958564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            final String dateStr = DateUtils.formatDateTime(getActivity(),
959564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.endDate.toMillis(false), DateUtils.FORMAT_NUMERIC_DATE);
960564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndDateTextView.setText(dateStr);
961564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        } else {
962564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (mModel.end == RecurrenceModel.END_BY_COUNT) {
963564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // Checking before setting because this causes infinite
964564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // recursion
965564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // in afterTextWatcher
966564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                final String countStr = Integer.toString(mModel.endCount);
967564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (!countStr.equals(mEndCount.getText().toString())) {
968564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mEndCount.setText(countStr);
969b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
970b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
971b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
972b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
973b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
974b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    /**
975b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan     * @param endDateString
976b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan     */
977b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    private void setEndSpinnerEndDateStr(final String endDateString) {
978b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        mEndSpinnerArray.set(1, endDateString);
979b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan        mEndSpinnerAdapter.notifyDataSetChanged();
980b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan    }
981b39a2e929a98ab329ce0f4e40b7b0fc617dc6575Michael Chan
982b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    private void doToast() {
983b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        Log.e(TAG, "Model = " + mModel.toString());
984b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        String rrule;
985564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
986b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            rrule = "Not repeating";
987b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else {
988b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            copyModelToEventRecurrence(mModel, mRecurrence);
989b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            rrule = mRecurrence.toString();
990b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
991b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
992b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (mToast != null) {
993b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mToast.cancel();
994b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
995b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mToast = Toast.makeText(getActivity(), rrule,
996b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                Toast.LENGTH_LONG);
997b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mToast.show();
998b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
999b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1000b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // TODO Test and update for Right-to-Left
1001aafaa5f3d4d94d193186df868a76aed484a14255James Kung    private void updateIntervalText() {
1002aafaa5f3d4d94d193186df868a76aed484a14255James Kung        if (mIntervalResId == -1) {
1003aafaa5f3d4d94d193186df868a76aed484a14255James Kung            return;
1004aafaa5f3d4d94d193186df868a76aed484a14255James Kung        }
1005aafaa5f3d4d94d193186df868a76aed484a14255James Kung
1006b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        final String INTERVAL_COUNT_MARKER = "%d";
1007aafaa5f3d4d94d193186df868a76aed484a14255James Kung        String intervalString = mResources.getQuantityString(mIntervalResId, mModel.interval);
1008b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int markerStart = intervalString.indexOf(INTERVAL_COUNT_MARKER);
1009b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1010b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (markerStart != -1) {
1011b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (markerStart == 0) {
1012b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mIntervalPreText.setText("");
1013b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } else {
1014b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                int postTextStart = markerStart + INTERVAL_COUNT_MARKER.length();
1015564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (postTextStart < intervalString.length() &&
1016564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        intervalString.charAt(postTextStart) == ' ') {
1017b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    postTextStart++;
1018b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
1019b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mIntervalPostText.setText(intervalString.subSequence(postTextStart,
1020b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        intervalString.length()));
1021b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1022b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                if (intervalString.charAt(markerStart - 1) == ' ') {
1023b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    markerStart--;
1024b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                }
1025b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mIntervalPreText.setText(intervalString.subSequence(0, markerStart));
1026b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
1027b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1028b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1029b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1030564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    /**
1031564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung     * Update the "Repeat for N events" end option with the proper string values
1032564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung     * based on the value that has been entered for N.
1033564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung     */
1034564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private void updateEndCountText() {
1035564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        final String END_COUNT_MARKER = "%d";
1036564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        String endString = mResources.getQuantityString(R.plurals.recurrence_end_count,
1037564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mModel.endCount);
1038564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        int markerStart = endString.indexOf(END_COUNT_MARKER);
1039564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1040564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        if (markerStart != -1) {
1041564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (markerStart == 0) {
1042564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                Log.e(TAG, "No text to put in to recurrence's end spinner.");
1043564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            } else {
1044564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                int postTextStart = markerStart + END_COUNT_MARKER.length();
1045564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (postTextStart < endString.length() &&
1046564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        endString.charAt(postTextStart) == ' ') {
1047564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    postTextStart++;
1048564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
1049564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mPostEndCount.setText(endString.subSequence(postTextStart,
1050564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        endString.length()));
1051564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1052564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
1053564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    }
1054564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1055b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Implements OnItemSelectedListener interface
1056b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Freq spinner
1057b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // End spinner
1058b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1059b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
1060b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (parent == mFreqSpinner) {
1061564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mModel.freq = position;
1062b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else if (parent == mEndSpinner) {
1063b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            switch (position) {
1064564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_NEVER:
1065564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.end = RecurrenceModel.END_NEVER;
1066b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
1067564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_BY_DATE:
1068564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.end = RecurrenceModel.END_BY_DATE;
1069b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
1070564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_BY_COUNT:
1071564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.end = RecurrenceModel.END_BY_COUNT;
1072b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1073b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    if (mModel.endCount <= 1) {
1074b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        mModel.endCount = 1;
1075b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    } else if (mModel.endCount > COUNT_MAX) {
1076b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                        mModel.endCount = COUNT_MAX;
1077b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    }
1078564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    updateEndCountText();
1079b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    break;
1080b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
1081564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndCount.setVisibility(mModel.end == RecurrenceModel.END_BY_COUNT ? View.VISIBLE
1082b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    : View.GONE);
1083564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndDateTextView.setVisibility(mModel.end == RecurrenceModel.END_BY_DATE ? View.VISIBLE
1084b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                    : View.GONE);
1085564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mPostEndCount.setVisibility(
1086564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mModel.end == RecurrenceModel.END_BY_COUNT  && !mHidePostEndCount?
1087564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            View.VISIBLE : View.GONE);
1088564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1089b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1090b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        updateDialog();
1091b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1092b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1093b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Implements OnItemSelectedListener interface
1094b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1095b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onNothingSelected(AdapterView<?> arg0) {
1096b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1097b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1098b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
109956f42bfb36d710726051e8268a115697477add61James Kung    public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
1100b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (mModel.endDate == null) {
1101b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mModel.endDate = new Time(mTime.timezone);
1102b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mModel.endDate.hour = mModel.endDate.minute = mModel.endDate.second = 0;
1103b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1104b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mModel.endDate.year = year;
1105b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mModel.endDate.month = monthOfYear;
1106b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mModel.endDate.monthDay = dayOfMonth;
1107b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mModel.endDate.normalize(false);
1108b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        updateDialog();
1109b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1110b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1111b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Implements OnCheckedChangeListener interface
1112b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Week repeat by day of week
1113b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1114b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
1115b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        int itemIdx = -1;
1116b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        for (int i = 0; i < 7; i++) {
1117b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            if (itemIdx == -1 && buttonView == mWeekByDayButtons[i]) {
1118b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                itemIdx = i;
1119b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                mModel.weeklyByDayOfWeek[i] = isChecked;
1120b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
1121b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1122b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        updateDialog();
1123b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1124b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1125b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Implements android.widget.RadioGroup.OnCheckedChangeListener interface
1126b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Month repeat by radio buttons
1127b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1128b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onCheckedChanged(RadioGroup group, int checkedId) {
1129b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (checkedId == R.id.repeatMonthlyByNthDayOfMonth) {
1130564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mModel.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
1131b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else if (checkedId == R.id.repeatMonthlyByNthDayOfTheWeek) {
1132564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mModel.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
1133b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1134b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        updateDialog();
1135b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1136b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1137b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Implements OnClickListener interface
1138b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // EndDate button
1139b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    // Done button
1140b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1141b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onClick(View v) {
1142b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        if (mEndDateTextView == v) {
114356f42bfb36d710726051e8268a115697477add61James Kung            if (mDatePickerDialog != null) {
114456f42bfb36d710726051e8268a115697477add61James Kung                mDatePickerDialog.dismiss();
1145b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
114656f42bfb36d710726051e8268a115697477add61James Kung            mDatePickerDialog = DatePickerDialog.newInstance(this, mModel.endDate.year,
114756f42bfb36d710726051e8268a115697477add61James Kung                    mModel.endDate.month, mModel.endDate.monthDay);
114856f42bfb36d710726051e8268a115697477add61James Kung            mDatePickerDialog.setFirstDayOfWeek(Utils.getFirstDayOfWeekAsCalendar(getActivity()));
114956f42bfb36d710726051e8268a115697477add61James Kung            mDatePickerDialog.setYearRange(Utils.YEAR_MIN, Utils.YEAR_MAX);
115056f42bfb36d710726051e8268a115697477add61James Kung            mDatePickerDialog.show(getFragmentManager(), FRAG_TAG_DATE_PICKER);
1151b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        } else if (mDone == v) {
1152b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            String rrule;
1153564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
1154b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                rrule = null;
1155b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            } else {
1156b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                copyModelToEventRecurrence(mModel, mRecurrence);
1157b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                rrule = mRecurrence.toString();
1158b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            }
1159b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            mRecurrenceSetListener.onRecurrenceSet(rrule);
1160b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan            dismiss();
1161b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1162b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1163b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1164b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    @Override
1165b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void onActivityCreated(Bundle savedInstanceState) {
1166b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        super.onActivityCreated(savedInstanceState);
116756f42bfb36d710726051e8268a115697477add61James Kung        mDatePickerDialog = (DatePickerDialog) getFragmentManager()
1168b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan                .findFragmentByTag(FRAG_TAG_DATE_PICKER);
116956f42bfb36d710726051e8268a115697477add61James Kung        if (mDatePickerDialog != null) {
117056f42bfb36d710726051e8268a115697477add61James Kung            mDatePickerDialog.setOnDateSetListener(this);
1171b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        }
1172b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1173b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1174b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public interface OnRecurrenceSetListener {
1175b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        void onRecurrenceSet(String rrule);
1176b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1177b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan
1178b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    public void setOnRecurrenceSetListener(OnRecurrenceSetListener l) {
1179b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan        mRecurrenceSetListener = l;
1180b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan    }
1181564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1182564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    private class EndSpinnerAdapter extends ArrayAdapter<CharSequence> {
1183564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        final String END_DATE_MARKER = "%s";
1184564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        final String END_COUNT_MARKER = "%d";
1185564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1186564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private LayoutInflater mInflater;
1187564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private int mItemResourceId;
1188564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private int mTextResourceId;
1189564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private ArrayList<CharSequence> mStrings;
1190564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private String mEndDateString;
1191564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        private boolean mUseFormStrings;
1192564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1193564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        /**
1194564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung         * @param context
1195564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung         * @param textViewResourceId
1196564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung         * @param objects
1197564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung         */
1198564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        public EndSpinnerAdapter(Context context, ArrayList<CharSequence> strings,
1199564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                int itemResourceId, int textResourceId) {
1200564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            super(context, itemResourceId, strings);
1201564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1202564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mItemResourceId = itemResourceId;
1203564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mTextResourceId = textResourceId;
1204564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mStrings = strings;
1205564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            mEndDateString = getResources().getString(R.string.recurrence_end_date);
1206564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1207564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            // If either date or count strings don't translate well, such that we aren't assured
1208564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            // to have some text available to be placed in the spinner, then we'll have to use
1209564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            // the more form-like versions of both strings instead.
1210564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            int markerStart = mEndDateString.indexOf(END_DATE_MARKER);
1211564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (markerStart <= 0) {
1212564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // The date string does not have any text before the "%s" so we'll have to use the
1213564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // more form-like strings instead.
1214564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mUseFormStrings = true;
1215564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            } else {
1216564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                String countEndStr = getResources().getQuantityString(
1217564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        R.plurals.recurrence_end_count, 1);
1218564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                markerStart = countEndStr.indexOf(END_COUNT_MARKER);
1219564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                if (markerStart <= 0) {
1220564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    // The count string does not have any text before the "%d" so we'll have to use
1221564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    // the more form-like strings instead.
1222564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    mUseFormStrings = true;
1223564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                }
1224564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1225564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1226564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (mUseFormStrings) {
1227564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // We'll have to set the layout for the spinner to be weight=0 so it doesn't
1228564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                // take up too much space.
1229564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                mEndSpinner.setLayoutParams(
1230564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        new TableLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));
1231564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1232564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
1233564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1234564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        @Override
1235564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        public View getView(int position, View convertView, ViewGroup parent) {
1236564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            View v;
1237564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            // Check if we can recycle the view
1238564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (convertView == null) {
1239564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                v = mInflater.inflate(mTextResourceId, parent, false);
1240564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            } else {
1241564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                v = convertView;
1242564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1243564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1244564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            TextView item = (TextView) v.findViewById(R.id.spinner_item);
1245564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            int markerStart;
1246564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            switch (position) {
1247564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_NEVER:
1248564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    item.setText(mStrings.get(RecurrenceModel.END_NEVER));
1249564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    break;
1250564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_BY_DATE:
1251564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    markerStart = mEndDateString.indexOf(END_DATE_MARKER);
1252564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1253564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    if (markerStart != -1) {
1254564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        if (mUseFormStrings || markerStart == 0) {
1255564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // If we get here, the translation of "Until" doesn't work correctly,
1256564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // so we'll just set the whole "Until a date" string.
1257564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            item.setText(mEndDateLabel);
1258564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        } else {
1259564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            if (mEndDateString.charAt(markerStart - 1) == ' ') {
1260564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                                markerStart--;
1261564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            }
1262564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            item.setText(mEndDateString.subSequence(0, markerStart));
1263564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        }
1264564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    }
1265564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    break;
1266564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                case RecurrenceModel.END_BY_COUNT:
1267564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    String endString = mResources.getQuantityString(R.plurals.recurrence_end_count,
1268564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            mModel.endCount);
1269564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    markerStart = endString.indexOf(END_COUNT_MARKER);
1270564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1271564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    if (markerStart != -1) {
1272564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        if (mUseFormStrings || markerStart == 0) {
1273564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // If we get here, the translation of "For" doesn't work correctly,
1274564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // so we'll just set the whole "For a number of events" string.
1275564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            item.setText(mEndCountLabel);
1276564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // Also, we'll hide the " events" that would have been at the end.
1277564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            mPostEndCount.setVisibility(View.GONE);
1278564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // Use this flag so the onItemSelected knows whether to show it later.
1279564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            mHidePostEndCount = true;
1280564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        } else {
1281564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            int postTextStart = markerStart + END_COUNT_MARKER.length();
1282564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            if (endString.charAt(postTextStart) == ' ') {
1283564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                                postTextStart++;
1284564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            }
1285564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            mPostEndCount.setText(endString.subSequence(postTextStart,
1286564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                                    endString.length()));
1287564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            // In case it's a recycled view that wasn't visible.
1288018f539c218940e0fad05c10552759e4fa2e77e4Sam Blitzstein                            if (mModel.end == RecurrenceModel.END_BY_COUNT) {
1289018f539c218940e0fad05c10552759e4fa2e77e4Sam Blitzstein                                mPostEndCount.setVisibility(View.VISIBLE);
1290018f539c218940e0fad05c10552759e4fa2e77e4Sam Blitzstein                            }
1291564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            if (endString.charAt(markerStart - 1) == ' ') {
1292564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                                markerStart--;
1293564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            }
1294564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                            item.setText(endString.subSequence(0, markerStart));
1295564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                        }
1296564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    }
1297564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    break;
1298564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                default:
1299564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    v = null;
1300564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                    break;
1301564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1302564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1303564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return v;
1304564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
1305564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1306564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        @Override
1307564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        public View getDropDownView(int position, View convertView, ViewGroup parent) {
1308564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            View v;
1309564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            // Check if we can recycle the view
1310564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            if (convertView == null) {
1311564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                v = mInflater.inflate(mItemResourceId, parent, false);
1312564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            } else {
1313564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung                v = convertView;
1314564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            }
1315564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1316564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            TextView item = (TextView) v.findViewById(R.id.spinner_item);
1317564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            item.setText(mStrings.get(position));
1318564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung
1319564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung            return v;
1320564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung        }
1321564f618e3fa48a8e4836fec0f216449b0ae809f9James Kung    }
1322b21c638ca11d9be3a3d9e7d28223bb4a3dab5f15Michael Chan}
1323