This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 548
Expand file tree
/
Copy pathRecurrencePickerDialogFragment.java
More file actions
1429 lines (1244 loc) · 54.7 KB
/
RecurrencePickerDialogFragment.java
File metadata and controls
1429 lines (1244 loc) · 54.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.codetroopers.betterpickers.recurrencepicker;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;
import androidx.appcompat.widget.SwitchCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.text.format.Time;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TimeFormatException;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import com.codetroopers.betterpickers.OnDialogDismissListener;
import com.codetroopers.betterpickers.R;
import com.codetroopers.betterpickers.calendardatepicker.CalendarDatePickerDialogFragment;
import java.text.DateFormatSymbols;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
public class RecurrencePickerDialogFragment extends DialogFragment implements OnItemSelectedListener,
OnCheckedChangeListener, OnClickListener,
android.widget.RadioGroup.OnCheckedChangeListener,
CalendarDatePickerDialogFragment.OnDateSetListener {
private static final String TAG = "RecurrencePickerDialogFragment";
// in dp's
private static final int MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK = 450;
// Update android:maxLength in EditText as needed
private static final int INTERVAL_MAX = 99;
private static final int INTERVAL_DEFAULT = 1;
// Update android:maxLength in EditText as needed
private static final int COUNT_MAX = 730;
private static final int COUNT_DEFAULT = 5;
// Special cases in monthlyByNthDayOfWeek
private static final int FIFTH_WEEK_IN_A_MONTH = 5;
public static final int LAST_NTH_DAY_OF_WEEK = -1;
private CalendarDatePickerDialogFragment mDatePickerDialog;
private OnDialogDismissListener mDismissCallback;
private static class RecurrenceModel implements Parcelable {
// Should match EventRecurrence.DAILY, etc
static final int FREQ_HOURLY = 0;
static final int FREQ_DAILY = 1;
static final int FREQ_WEEKLY = 2;
static final int FREQ_MONTHLY = 3;
static final int FREQ_YEARLY = 4;
static final int END_NEVER = 0;
static final int END_BY_DATE = 1;
static final int END_BY_COUNT = 2;
static final int MONTHLY_BY_DATE = 0;
static final int MONTHLY_BY_NTH_DAY_OF_WEEK = 1;
static final int STATE_NO_RECURRENCE = 0;
static final int STATE_RECURRENCE = 1;
int recurrenceState;
/**
* FREQ: Repeat pattern
*
* @see #FREQ_DAILY
* @see #FREQ_WEEKLY
* @see #FREQ_MONTHLY
* @see #FREQ_YEARLY
* @see #FREQ_HOURLY
*/
int freq = FREQ_WEEKLY;
/**
* INTERVAL: Every n days/weeks/months/years. n >= 1
*/
int interval = INTERVAL_DEFAULT;
/**
* UNTIL and COUNT: How does the the event end?
*
* @see #END_NEVER
* @see #END_BY_DATE
* @see #END_BY_COUNT
*/
int end;
/**
* UNTIL: Date of the last recurrence. Used when until == END_BY_DATE
*/
Time endDate;
/**
* COUNT: Times to repeat. Use when until == END_BY_COUNT
*/
int endCount = COUNT_DEFAULT;
/**
* BYDAY: Days of the week to be repeated. Sun = 0, Mon = 1, etc
*/
boolean[] weeklyByDayOfWeek = new boolean[7];
/**
* BYDAY AND BYMONTHDAY: How to repeat monthly events? Same date of the month or Same nth day of week.
*
* @see #MONTHLY_BY_DATE
* @see #MONTHLY_BY_NTH_DAY_OF_WEEK
*/
int monthlyRepeat;
/**
* Day of the month to repeat. Used when monthlyRepeat == MONTHLY_BY_DATE
*/
int monthlyByMonthDay;
/**
* Day of the week to repeat. Used when monthlyRepeat == MONTHLY_BY_NTH_DAY_OF_WEEK
*/
int monthlyByDayOfWeek;
/**
* Nth day of the week to repeat. Used when monthlyRepeat == MONTHLY_BY_NTH_DAY_OF_WEEK 0=undefined, -1=Last,
* 1=1st, 2=2nd, ..., 5=5th
* <p/>
* We support 5th, just to handle backwards capabilities with old bug, but it gets converted to -1 once edited.
*/
int monthlyByNthDayOfWeek;
/**
* Force to hide switch to force user to select a reccurency
*/
boolean forceHideSwitchButton;
/*
* (generated method)
*/
@Override
public String toString() {
return "Model [freq=" + freq + ", interval=" + interval + ", end=" + end + ", endDate="
+ endDate + ", endCount=" + endCount + ", weeklyByDayOfWeek="
+ Arrays.toString(weeklyByDayOfWeek) + ", monthlyRepeat=" + monthlyRepeat
+ ", monthlyByMonthDay=" + monthlyByMonthDay + ", monthlyByDayOfWeek="
+ monthlyByDayOfWeek + ", monthlyByNthDayOfWeek=" + monthlyByNthDayOfWeek + "]";
}
public RecurrenceModel() {
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(recurrenceState);
dest.writeInt(freq);
dest.writeInt(interval);
dest.writeInt(end);
dest.writeInt(endDate.year);
dest.writeInt(endDate.month);
dest.writeInt(endDate.monthDay);
dest.writeInt(endCount);
dest.writeBooleanArray(weeklyByDayOfWeek);
dest.writeInt(monthlyRepeat);
dest.writeInt(monthlyByMonthDay);
dest.writeInt(monthlyByDayOfWeek);
dest.writeInt(monthlyByNthDayOfWeek);
dest.writeByte((byte) (forceHideSwitchButton ? 1 : 0));
}
private RecurrenceModel(Parcel in) {
this.recurrenceState = in.readInt();
this.freq = in.readInt();
this.interval = in.readInt();
this.end = in.readInt();
this.endDate = new Time();
this.endDate.year = in.readInt();
this.endDate.month = in.readInt();
this.endDate.monthDay = in.readInt();
this.endCount = in.readInt();
this.weeklyByDayOfWeek = in.createBooleanArray();
this.monthlyRepeat = in.readInt();
this.monthlyByMonthDay = in.readInt();
this.monthlyByDayOfWeek = in.readInt();
this.monthlyByNthDayOfWeek = in.readInt();
this.forceHideSwitchButton = in.readByte() != 0;
}
public static final Creator<RecurrenceModel> CREATOR = new Creator<RecurrenceModel>() {
public RecurrenceModel createFromParcel(Parcel source) {
return new RecurrenceModel(source);
}
public RecurrenceModel[] newArray(int size) {
return new RecurrenceModel[size];
}
};
}
class minMaxTextWatcher implements TextWatcher {
private int mMin;
private int mMax;
private int mDefault;
public minMaxTextWatcher(int min, int defaultInt, int max) {
mMin = min;
mMax = max;
mDefault = defaultInt;
}
@Override
public void afterTextChanged(Editable s) {
boolean updated = false;
int value;
try {
value = Integer.parseInt(s.toString());
} catch (NumberFormatException e) {
value = mDefault;
}
if (value < mMin) {
value = mMin;
updated = true;
} else if (value > mMax) {
updated = true;
value = mMax;
}
// Update UI
if (updated) {
s.clear();
s.append(Integer.toString(value));
}
updateDoneButtonState();
onChange(value);
}
/**
* Override to be called after each key stroke
*/
void onChange(int value) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
private Resources mResources;
private EventRecurrence mRecurrence = new EventRecurrence();
private Time mTime = new Time(); // TODO timezone?
private RecurrenceModel mModel = new RecurrenceModel();
private Toast mToast;
private final int[] TIME_DAY_TO_CALENDAR_DAY = new int[]{
Calendar.SUNDAY,
Calendar.MONDAY,
Calendar.TUESDAY,
Calendar.WEDNESDAY,
Calendar.THURSDAY,
Calendar.FRIDAY,
Calendar.SATURDAY,
};
// Call mStringBuilder.setLength(0) before formatting any string or else the
// formatted text will accumulate.
// private final StringBuilder mStringBuilder = new StringBuilder();
// private Formatter mFormatter = new Formatter(mStringBuilder);
private View mView;
private Spinner mFreqSpinner;
private static final int[] mFreqModelToEventRecurrence = {
EventRecurrence.HOURLY,
EventRecurrence.DAILY,
EventRecurrence.WEEKLY,
EventRecurrence.MONTHLY,
EventRecurrence.YEARLY
};
public static final String BUNDLE_START_TIME_MILLIS = "bundle_event_start_time";
public static final String BUNDLE_TIME_ZONE = "bundle_event_time_zone";
public static final String BUNDLE_RRULE = "bundle_event_rrule";
public static final String BUNDLE_HIDE_SWITCH_BUTTON = "bundle_hide_switch_button";
private static final String BUNDLE_MODEL = "bundle_model";
private static final String BUNDLE_END_COUNT_HAS_FOCUS = "bundle_end_count_has_focus";
private static final String FRAG_TAG_DATE_PICKER = "tag_date_picker_frag";
private SwitchCompat mRepeatSwitch;
private EditText mInterval;
private TextView mIntervalPreText;
private TextView mIntervalPostText;
private int mIntervalResId = -1;
private Spinner mEndSpinner;
private TextView mEndDateTextView;
private EditText mEndCount;
private TextView mPostEndCount;
private boolean mHidePostEndCount;
private ArrayList<CharSequence> mEndSpinnerArray = new ArrayList<CharSequence>(3);
private EndSpinnerAdapter mEndSpinnerAdapter;
private String mEndNeverStr;
private String mEndDateLabel;
private String mEndCountLabel;
/**
* Hold toggle buttons in the order per user's first day of week preference
*/
private LinearLayout mWeekGroup;
private LinearLayout mWeekGroup2;
// Sun = 0
private ToggleButton[] mWeekByDayButtons = new ToggleButton[7];
/**
* A double array of Strings to hold the 7x5 list of possible strings of the form: "on every [Nth] [DAY_OF_WEEK]",
* e.g. "on every second Monday", where [Nth] can be [first, second, third, fourth, last]
*/
private String[][] mMonthRepeatByDayOfWeekStrs;
private LinearLayout mMonthGroup;
private RadioGroup mMonthRepeatByRadioGroup;
private RadioButton mRepeatMonthlyByNthDayOfWeek;
private RadioButton mRepeatMonthlyByNthDayOfMonth;
private String mMonthRepeatByDayOfWeekStr;
private Button mDoneButton;
private OnRecurrenceSetListener mRecurrenceSetListener;
public RecurrencePickerDialogFragment() {
}
static public boolean isSupportedMonthlyByNthDayOfWeek(int num) {
// We only support monthlyByNthDayOfWeek when it is greater then 0 but less then 5.
// Or if -1 when it is the last monthly day of the week.
return (num > 0 && num <= FIFTH_WEEK_IN_A_MONTH) || num == LAST_NTH_DAY_OF_WEEK;
}
static public boolean canHandleRecurrenceRule(EventRecurrence er) {
switch (er.freq) {
case EventRecurrence.HOURLY:
case EventRecurrence.DAILY:
case EventRecurrence.MONTHLY:
case EventRecurrence.YEARLY:
case EventRecurrence.WEEKLY:
break;
default:
return false;
}
if (er.count > 0 && !TextUtils.isEmpty(er.until)) {
return false;
}
// Weekly: For "repeat by day of week", the day of week to repeat is in
// er.byday[]
/*
* Monthly: For "repeat by nth day of week" the day of week to repeat is
* in er.byday[] and the "nth" is stored in er.bydayNum[]. Currently we
* can handle only one and only in monthly
*/
int numOfByDayNum = 0;
for (int i = 0; i < er.bydayCount; i++) {
if (isSupportedMonthlyByNthDayOfWeek(er.bydayNum[i])) {
++numOfByDayNum;
}
}
if (numOfByDayNum > 1) {
return false;
}
if (numOfByDayNum > 0 && er.freq != EventRecurrence.MONTHLY) {
return false;
}
// The UI only handle repeat by one day of month i.e. not 9th and 10th
// of every month
if (er.bymonthdayCount > 1) {
return false;
}
if (er.freq == EventRecurrence.MONTHLY) {
if (er.bydayCount > 1) {
return false;
}
if (er.bydayCount > 0 && er.bymonthdayCount > 0) {
return false;
}
}
return true;
}
// TODO don't lose data when getting data that our UI can't handle
static private void copyEventRecurrenceToModel(final EventRecurrence er,
RecurrenceModel model) {
// Freq:
switch (er.freq) {
case EventRecurrence.HOURLY:
model.freq = RecurrenceModel.FREQ_HOURLY;
break;
case EventRecurrence.DAILY:
model.freq = RecurrenceModel.FREQ_DAILY;
break;
case EventRecurrence.MONTHLY:
model.freq = RecurrenceModel.FREQ_MONTHLY;
break;
case EventRecurrence.YEARLY:
model.freq = RecurrenceModel.FREQ_YEARLY;
break;
case EventRecurrence.WEEKLY:
model.freq = RecurrenceModel.FREQ_WEEKLY;
break;
default:
throw new IllegalStateException("freq=" + er.freq);
}
// Interval:
if (er.interval > 0) {
model.interval = er.interval;
}
// End:
// End by count:
model.endCount = er.count;
if (model.endCount > 0) {
model.end = RecurrenceModel.END_BY_COUNT;
}
// End by date:
if (!TextUtils.isEmpty(er.until)) {
if (model.endDate == null) {
model.endDate = new Time();
}
try {
model.endDate.parse(er.until);
} catch (TimeFormatException e) {
model.endDate = null;
}
// LIMITATION: The UI can only handle END_BY_DATE or END_BY_COUNT
if (model.end == RecurrenceModel.END_BY_COUNT && model.endDate != null) {
throw new IllegalStateException("freq=" + er.freq);
}
model.end = RecurrenceModel.END_BY_DATE;
}
// Weekly: repeat by day of week or Monthly: repeat by nth day of week
// in the month
Arrays.fill(model.weeklyByDayOfWeek, false);
if (er.bydayCount > 0) {
int count = 0;
for (int i = 0; i < er.bydayCount; i++) {
int dayOfWeek = EventRecurrence.day2TimeDay(er.byday[i]);
model.weeklyByDayOfWeek[dayOfWeek] = true;
if (model.freq == RecurrenceModel.FREQ_MONTHLY &&
isSupportedMonthlyByNthDayOfWeek(er.bydayNum[i])) {
// LIMITATION: Can handle only (one) weekDayNum in nth or last and only
// when
// monthly
model.monthlyByDayOfWeek = dayOfWeek;
model.monthlyByNthDayOfWeek = er.bydayNum[i];
model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK;
count++;
}
}
if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
if (er.bydayCount != 1) {
// Can't handle 1st Monday and 2nd Wed
throw new IllegalStateException("Can handle only 1 byDayOfWeek in monthly");
}
if (count != 1) {
throw new IllegalStateException(
"Didn't specify which nth day of week to repeat for a monthly");
}
}
}
// Monthly by day of month
if (model.freq == RecurrenceModel.FREQ_MONTHLY) {
if (er.bymonthdayCount == 1) {
if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
throw new IllegalStateException(
"Can handle only by monthday or by nth day of week, not both");
}
model.monthlyByMonthDay = er.bymonthday[0];
model.monthlyRepeat = RecurrenceModel.MONTHLY_BY_DATE;
} else if (er.bymonthCount > 1) {
// LIMITATION: Can handle only one month day
throw new IllegalStateException("Can handle only one bymonthday");
}
}
}
static private void copyModelToEventRecurrence(final RecurrenceModel model, EventRecurrence eventRecurrence) {
if (model.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
throw new IllegalStateException("There's no recurrence");
}
// Freq
eventRecurrence.freq = mFreqModelToEventRecurrence[model.freq];
// Interval
if (model.interval <= 1) {
eventRecurrence.interval = 0;
} else {
eventRecurrence.interval = model.interval;
}
// End
switch (model.end) {
case RecurrenceModel.END_BY_DATE:
if (model.endDate != null) {
model.endDate.switchTimezone(Time.TIMEZONE_UTC);
model.endDate.normalize(false);
eventRecurrence.until = model.endDate.format2445();
eventRecurrence.count = 0;
} else {
throw new IllegalStateException("end = END_BY_DATE but endDate is null");
}
break;
case RecurrenceModel.END_BY_COUNT:
eventRecurrence.count = model.endCount;
eventRecurrence.until = null;
if (eventRecurrence.count <= 0) {
throw new IllegalStateException("count is " + eventRecurrence.count);
}
break;
default:
eventRecurrence.count = 0;
eventRecurrence.until = null;
break;
}
// Weekly && monthly repeat patterns
eventRecurrence.bydayCount = 0;
eventRecurrence.bymonthdayCount = 0;
switch (model.freq) {
case RecurrenceModel.FREQ_MONTHLY:
if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_DATE) {
if (model.monthlyByMonthDay > 0) {
if (eventRecurrence.bymonthday == null || eventRecurrence.bymonthdayCount < 1) {
eventRecurrence.bymonthday = new int[1];
}
eventRecurrence.bymonthday[0] = model.monthlyByMonthDay;
eventRecurrence.bymonthdayCount = 1;
}
} else if (model.monthlyRepeat == RecurrenceModel.MONTHLY_BY_NTH_DAY_OF_WEEK) {
if (!isSupportedMonthlyByNthDayOfWeek(model.monthlyByNthDayOfWeek)) {
throw new IllegalStateException("month repeat by nth week but n is "
+ model.monthlyByNthDayOfWeek);
}
int count = 1;
if (eventRecurrence.bydayCount < count || eventRecurrence.byday == null || eventRecurrence.bydayNum == null) {
eventRecurrence.byday = new int[count];
eventRecurrence.bydayNum = new int[count];
}
eventRecurrence.bydayCount = count;
eventRecurrence.byday[0] = EventRecurrence.timeDay2Day(model.monthlyByDayOfWeek);
eventRecurrence.bydayNum[0] = model.monthlyByNthDayOfWeek;
}
break;
case RecurrenceModel.FREQ_WEEKLY:
int count = 0;
for (int i = 0; i < 7; i++) {
if (model.weeklyByDayOfWeek[i]) {
count++;
}
}
if (eventRecurrence.bydayCount < count || eventRecurrence.byday == null || eventRecurrence.bydayNum == null) {
eventRecurrence.byday = new int[count];
eventRecurrence.bydayNum = new int[count];
}
eventRecurrence.bydayCount = count;
for (int i = 6; i >= 0; i--) {
if (model.weeklyByDayOfWeek[i]) {
eventRecurrence.bydayNum[--count] = 0;
eventRecurrence.byday[count] = EventRecurrence.timeDay2Day(i);
}
}
break;
}
if (!canHandleRecurrenceRule(eventRecurrence)) {
throw new IllegalStateException("UI generated recurrence that it can't handle. ER:"
+ eventRecurrence.toString() + " Model: " + model.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRecurrence.wkst = EventRecurrence.timeDay2Day(Utils.getFirstDayOfWeek(getActivity()));
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
boolean endCountHasFocus = false;
if (savedInstanceState != null) {
RecurrenceModel m = (RecurrenceModel) savedInstanceState.get(BUNDLE_MODEL);
if (m != null) {
mModel = m;
}
endCountHasFocus = savedInstanceState.getBoolean(BUNDLE_END_COUNT_HAS_FOCUS);
} else {
Bundle bundle = getArguments();
if (bundle != null) {
mTime.set(bundle.getLong(BUNDLE_START_TIME_MILLIS));
String tz = bundle.getString(BUNDLE_TIME_ZONE);
if (!TextUtils.isEmpty(tz)) {
mTime.timezone = tz;
}
mTime.normalize(false);
// Time days of week: Sun=0, Mon=1, etc
mModel.weeklyByDayOfWeek[mTime.weekDay] = true;
String rrule = bundle.getString(BUNDLE_RRULE);
if (!TextUtils.isEmpty(rrule)) {
mModel.recurrenceState = RecurrenceModel.STATE_RECURRENCE;
mRecurrence.parse(rrule);
copyEventRecurrenceToModel(mRecurrence, mModel);
// Leave today's day of week as checked by default in weekly view.
if (mRecurrence.bydayCount == 0) {
mModel.weeklyByDayOfWeek[mTime.weekDay] = true;
}
}
mModel.forceHideSwitchButton = bundle.getBoolean(BUNDLE_HIDE_SWITCH_BUTTON, false);
} else {
mTime.setToNow();
}
}
mResources = getResources();
mView = inflater.inflate(R.layout.recurrencepicker, container, true);
final Activity activity = getActivity();
final Configuration config = activity.getResources().getConfiguration();
mRepeatSwitch = (SwitchCompat) mView.findViewById(R.id.repeat_switch);
if (mModel.forceHideSwitchButton) {
mRepeatSwitch.setChecked(true);
mRepeatSwitch.setVisibility(View.GONE);
mModel.recurrenceState = RecurrenceModel.STATE_RECURRENCE;
} else {
mRepeatSwitch.setChecked(mModel.recurrenceState == RecurrenceModel.STATE_RECURRENCE);
mRepeatSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mModel.recurrenceState = isChecked ? RecurrenceModel.STATE_RECURRENCE : RecurrenceModel.STATE_NO_RECURRENCE;
togglePickerOptions();
}
});
}
mFreqSpinner = (Spinner) mView.findViewById(R.id.freqSpinner);
mFreqSpinner.setOnItemSelectedListener(this);
ArrayAdapter<CharSequence> freqAdapter = ArrayAdapter.createFromResource(getActivity(),
R.array.recurrence_freq, R.layout.recurrencepicker_freq_item);
freqAdapter.setDropDownViewResource(R.layout.recurrencepicker_freq_item);
mFreqSpinner.setAdapter(freqAdapter);
mInterval = (EditText) mView.findViewById(R.id.interval);
mInterval.addTextChangedListener(new minMaxTextWatcher(1, INTERVAL_DEFAULT, INTERVAL_MAX) {
@Override
void onChange(int v) {
if (mIntervalResId != -1 && mInterval.getText().toString().length() > 0) {
mModel.interval = v;
updateIntervalText();
mInterval.requestLayout();
}
}
});
mIntervalPreText = (TextView) mView.findViewById(R.id.intervalPreText);
mIntervalPostText = (TextView) mView.findViewById(R.id.intervalPostText);
mEndNeverStr = mResources.getString(R.string.recurrence_end_continously);
mEndDateLabel = mResources.getString(R.string.recurrence_end_date_label);
mEndCountLabel = mResources.getString(R.string.recurrence_end_count_label);
mEndSpinnerArray.add(mEndNeverStr);
mEndSpinnerArray.add(mEndDateLabel);
mEndSpinnerArray.add(mEndCountLabel);
mEndSpinner = (Spinner) mView.findViewById(R.id.endSpinner);
mEndSpinner.setOnItemSelectedListener(this);
mEndSpinnerAdapter = new EndSpinnerAdapter(getActivity(), mEndSpinnerArray,
R.layout.recurrencepicker_freq_item, R.layout.recurrencepicker_end_text);
mEndSpinnerAdapter.setDropDownViewResource(R.layout.recurrencepicker_freq_item);
mEndSpinner.setAdapter(mEndSpinnerAdapter);
mEndCount = (EditText) mView.findViewById(R.id.endCount);
mEndCount.addTextChangedListener(new minMaxTextWatcher(1, COUNT_DEFAULT, COUNT_MAX) {
@Override
void onChange(int v) {
if (mModel.endCount != v) {
mModel.endCount = v;
updateEndCountText();
mEndCount.requestLayout();
}
}
});
mPostEndCount = (TextView) mView.findViewById(R.id.postEndCount);
mEndDateTextView = (TextView) mView.findViewById(R.id.endDate);
mEndDateTextView.setOnClickListener(this);
if (mModel.endDate == null) {
mModel.endDate = new Time(mTime);
switch (mModel.freq) {
case RecurrenceModel.FREQ_HOURLY:
case RecurrenceModel.FREQ_DAILY:
case RecurrenceModel.FREQ_WEEKLY:
mModel.endDate.month += 1;
break;
case RecurrenceModel.FREQ_MONTHLY:
mModel.endDate.month += 3;
break;
case RecurrenceModel.FREQ_YEARLY:
mModel.endDate.year += 3;
break;
}
mModel.endDate.normalize(false);
}
mWeekGroup = (LinearLayout) mView.findViewById(R.id.weekGroup);
mWeekGroup2 = (LinearLayout) mView.findViewById(R.id.weekGroup2);
// In Calendar.java day of week order e.g Sun = 1 ... Sat = 7
String[] dayOfWeekString = new DateFormatSymbols().getWeekdays();
mMonthRepeatByDayOfWeekStrs = new String[7][];
// from Time.SUNDAY as 0 through Time.SATURDAY as 6
mMonthRepeatByDayOfWeekStrs[0] = mResources.getStringArray(R.array.repeat_by_nth_sun);
mMonthRepeatByDayOfWeekStrs[1] = mResources.getStringArray(R.array.repeat_by_nth_mon);
mMonthRepeatByDayOfWeekStrs[2] = mResources.getStringArray(R.array.repeat_by_nth_tues);
mMonthRepeatByDayOfWeekStrs[3] = mResources.getStringArray(R.array.repeat_by_nth_wed);
mMonthRepeatByDayOfWeekStrs[4] = mResources.getStringArray(R.array.repeat_by_nth_thurs);
mMonthRepeatByDayOfWeekStrs[5] = mResources.getStringArray(R.array.repeat_by_nth_fri);
mMonthRepeatByDayOfWeekStrs[6] = mResources.getStringArray(R.array.repeat_by_nth_sat);
// In Time.java day of week order e.g. Sun = 0
int idx = Utils.getFirstDayOfWeek(getActivity());
// In Calendar.java day of week order e.g Sun = 1 ... Sat = 7
dayOfWeekString = new DateFormatSymbols().getShortWeekdays();
int numOfButtonsInRow1;
int numOfButtonsInRow2;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
// Get screen width in dp first
Display display = getActivity().getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = outMetrics.widthPixels / density;
if (dpWidth > MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK) {
numOfButtonsInRow1 = 7;
numOfButtonsInRow2 = 0;
mWeekGroup2.setVisibility(View.GONE);
mWeekGroup2.getChildAt(3).setVisibility(View.GONE);
} else {
numOfButtonsInRow1 = 4;
numOfButtonsInRow2 = 3;
mWeekGroup2.setVisibility(View.VISIBLE);
// Set rightmost button on the second row invisible so it takes up
// space and everything centers properly
mWeekGroup2.getChildAt(3).setVisibility(View.INVISIBLE);
}
} else if (mResources.getConfiguration().screenWidthDp > MIN_SCREEN_WIDTH_FOR_SINGLE_ROW_WEEK) {
numOfButtonsInRow1 = 7;
numOfButtonsInRow2 = 0;
mWeekGroup2.setVisibility(View.GONE);
mWeekGroup2.getChildAt(3).setVisibility(View.GONE);
} else {
numOfButtonsInRow1 = 4;
numOfButtonsInRow2 = 3;
mWeekGroup2.setVisibility(View.VISIBLE);
// Set rightmost button on the second row invisible so it takes up
// space and everything centers properly
mWeekGroup2.getChildAt(3).setVisibility(View.INVISIBLE);
}
/* First row */
for (int i = 0; i < 7; i++) {
if (i >= numOfButtonsInRow1) {
mWeekGroup.getChildAt(i).setVisibility(View.GONE);
continue;
}
mWeekByDayButtons[idx] = (ToggleButton) mWeekGroup.getChildAt(i);
mWeekByDayButtons[idx].setTextOff(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
mWeekByDayButtons[idx].setTextOn(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
mWeekByDayButtons[idx].setOnCheckedChangeListener(this);
if (++idx >= 7) {
idx = 0;
}
}
/* 2nd Row */
for (int i = 0; i < 3; i++) {
if (i >= numOfButtonsInRow2) {
mWeekGroup2.getChildAt(i).setVisibility(View.GONE);
continue;
}
mWeekByDayButtons[idx] = (ToggleButton) mWeekGroup2.getChildAt(i);
mWeekByDayButtons[idx].setTextOff(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
mWeekByDayButtons[idx].setTextOn(dayOfWeekString[TIME_DAY_TO_CALENDAR_DAY[idx]]);
mWeekByDayButtons[idx].setOnCheckedChangeListener(this);
if (++idx >= 7) {
idx = 0;
}
}
mMonthGroup = (LinearLayout) mView.findViewById(R.id.monthGroup);
mMonthRepeatByRadioGroup = (RadioGroup) mView.findViewById(R.id.monthGroup);
mMonthRepeatByRadioGroup.setOnCheckedChangeListener(this);
mRepeatMonthlyByNthDayOfWeek = (RadioButton) mView
.findViewById(R.id.repeatMonthlyByNthDayOfTheWeek);
mRepeatMonthlyByNthDayOfMonth = (RadioButton) mView
.findViewById(R.id.repeatMonthlyByNthDayOfMonth);
mDoneButton = (Button) mView.findViewById(R.id.done_button);
mDoneButton.setOnClickListener(this);
Button cancelButton = (Button) mView.findViewById(R.id.cancel_button);
//FIXME no text color for this one ?
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
togglePickerOptions();
updateDialog();
if (endCountHasFocus) {
mEndCount.requestFocus();
}
return mView;
}
@Override
public void onDismiss(DialogInterface dialoginterface) {
super.onDismiss(dialoginterface);
if (mDismissCallback != null) {
mDismissCallback.onDialogDismiss(dialoginterface);
}
}
private void togglePickerOptions() {
if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
mFreqSpinner.setEnabled(false);
mEndSpinner.setEnabled(false);
mIntervalPreText.setEnabled(false);
mInterval.setEnabled(false);
mIntervalPostText.setEnabled(false);
mMonthRepeatByRadioGroup.setEnabled(false);
mEndCount.setEnabled(false);
mPostEndCount.setEnabled(false);
mEndDateTextView.setEnabled(false);
mRepeatMonthlyByNthDayOfWeek.setEnabled(false);
mRepeatMonthlyByNthDayOfMonth.setEnabled(false);
for (Button button : mWeekByDayButtons) {
button.setEnabled(false);
}
} else {
mView.findViewById(R.id.options).setEnabled(true);
mFreqSpinner.setEnabled(true);
mEndSpinner.setEnabled(true);
mIntervalPreText.setEnabled(true);
mInterval.setEnabled(true);
mIntervalPostText.setEnabled(true);
mMonthRepeatByRadioGroup.setEnabled(true);
mEndCount.setEnabled(true);
mPostEndCount.setEnabled(true);
mEndDateTextView.setEnabled(true);
mRepeatMonthlyByNthDayOfWeek.setEnabled(true);
mRepeatMonthlyByNthDayOfMonth.setEnabled(true);
for (Button button : mWeekByDayButtons) {
button.setEnabled(true);
}
}
updateDoneButtonState();
}
private void updateDoneButtonState() {
if (mModel.recurrenceState == RecurrenceModel.STATE_NO_RECURRENCE) {
mDoneButton.setEnabled(true);
return;
}
if (mInterval.getText().toString().length() == 0) {
mDoneButton.setEnabled(false);
return;
}
if (mEndCount.getVisibility() == View.VISIBLE &&
mEndCount.getText().toString().length() == 0) {
mDoneButton.setEnabled(false);
return;
}
if (mModel.freq == RecurrenceModel.FREQ_WEEKLY) {
for (CompoundButton b : mWeekByDayButtons) {
if (b.isChecked()) {
mDoneButton.setEnabled(true);
return;
}
}
mDoneButton.setEnabled(false);
return;
}
mDoneButton.setEnabled(true);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);