9
9
import androidx .annotation .UiThread ;
10
10
11
11
import org .chromium .base .Callback ;
12
+ import org .chromium .content .browser .input .DateTimeChooserAndroid ;
12
13
import org .chromium .content .browser .input .SelectPopup ;
13
14
import org .chromium .content .browser .input .SelectPopupItem ;
15
+ import org .chromium .content .browser .picker .DateTimeSuggestion ;
16
+ import org .chromium .content .browser .picker .InputDialogContainer ;
17
+ import org .chromium .content .browser .picker .MonthPicker ;
18
+ import org .chromium .content .browser .picker .WeekPicker ;
19
+ import org .chromium .ui .base .ime .TextInputType ;
14
20
import org .chromium .wolvic .ColorChooserManager ;
15
21
import org .chromium .wolvic .UserDialogManagerBridge ;
16
22
17
23
import com .igalia .wolvic .browser .api .WAllowOrDeny ;
18
24
import com .igalia .wolvic .browser .api .WResult ;
19
25
import com .igalia .wolvic .browser .api .WSession ;
20
26
27
+ import java .text .ParseException ;
28
+ import java .text .SimpleDateFormat ;
21
29
import java .util .ArrayList ;
22
30
import java .util .Arrays ;
31
+ import java .util .Calendar ;
32
+ import java .util .Date ;
23
33
import java .util .List ;
34
+ import java .util .Locale ;
35
+ import java .util .TimeZone ;
24
36
25
37
class PromptDelegateImpl implements UserDialogManagerBridge .Delegate {
26
38
private final WSession .PromptDelegate mDelegate ;
@@ -41,6 +53,7 @@ public PromptDelegateImpl(WSession.PromptDelegate mDelegate, SessionImpl mSessio
41
53
42
54
SelectPopup .setFactory (new SelectPopupFactory ());
43
55
ColorChooserManager .setFactory (new ColorChooserFactory ());
56
+ DateTimeChooserAndroid .setFactory (new DateTimeChooserFactory ());
44
57
}
45
58
46
59
public WSession .PromptDelegate getDelegate () { return this .mDelegate ; }
@@ -557,4 +570,136 @@ public WSession.PromptDelegate.PromptResponse dismiss() {
557
570
return new PromptResponseImpl ();
558
571
}
559
572
}
573
+
574
+ public class DateTimeChooserFactory implements DateTimeChooserAndroid .Factory {
575
+ public DateTimeChooserBridge create (Context context , InputDialogContainer .InputActionDelegate delegate ) {
576
+ return new DateTimeChooserBridge (context , delegate );
577
+ }
578
+ }
579
+
580
+ public class DateTimeChooserBridge extends InputDialogContainer {
581
+ private final DateTimePrompt mDatePrompt ;
582
+
583
+ public DateTimeChooserBridge (Context context , InputDialogContainer .InputActionDelegate delegate ) {
584
+ super (context , delegate );
585
+ mDatePrompt = new DateTimePrompt (delegate );
586
+ }
587
+
588
+ @ Override
589
+ public void showDialog (final int type , final double value ,
590
+ double min , double max , double step ,
591
+ DateTimeSuggestion [] suggestions ) {
592
+ // Not implemented for |suggestions| and |step| yet.
593
+ mDatePrompt .setDateTime (type , value , min , max );
594
+
595
+ try {
596
+ final WSession .PromptDelegate delegate = mSession .getPromptDelegate ();
597
+ delegate .onDateTimePrompt (mSession , mDatePrompt );
598
+ } catch (WindowManager .BadTokenException e ) {
599
+ mDatePrompt .markComplete ();
600
+ }
601
+ }
602
+
603
+ @ Override
604
+ public void dismissDialog () {
605
+ if (!mDatePrompt .isComplete ())
606
+ mDatePrompt .dismiss ();
607
+ }
608
+ }
609
+
610
+ public static class DateTimePrompt extends BasePromptImpl implements WSession .PromptDelegate .DateTimePrompt {
611
+ private final InputDialogContainer .InputActionDelegate mDelegate ;
612
+ @ DatetimeType int mType ;
613
+ String mDefaultValue ;
614
+ String mMinValue ;
615
+ String mMaxValue ;
616
+
617
+ SimpleDateFormat mFormatter ;
618
+
619
+ public DateTimePrompt (InputDialogContainer .InputActionDelegate delegate ) {
620
+ mDelegate = delegate ;
621
+ }
622
+
623
+ public void setDateTime (int type , double value , double min , double max ) {
624
+ String format ;
625
+ if (type == TextInputType .DATE ) {
626
+ mType = Type .DATE ;
627
+ format = "yyyy-MM-dd" ;
628
+ } else if (type == TextInputType .TIME ) {
629
+ mType = Type .TIME ;
630
+ format = "HH:mm" ;
631
+ } else if (type == TextInputType .DATE_TIME || type == TextInputType .DATE_TIME_LOCAL ) {
632
+ mType = Type .DATETIME_LOCAL ;
633
+ format = "yyyy-MM-dd'T'HH:mm" ;
634
+ } else if (type == TextInputType .MONTH ) {
635
+ mType = Type .MONTH ;
636
+ format = "yyyy-MM" ;
637
+ value = MonthPicker .createDateFromValue (value ).getTimeInMillis ();
638
+ } else if (type == TextInputType .WEEK ) {
639
+ mType = Type .WEEK ;
640
+ format = "yyyy-'W'ww" ;
641
+ value = WeekPicker .createDateFromValue (value ).getTimeInMillis ();
642
+ } else {
643
+ throw new IllegalArgumentException ();
644
+ }
645
+
646
+ Date defaultDate ;
647
+ if (Double .isNaN (value )) {
648
+ defaultDate = new Date (System .currentTimeMillis ());
649
+ } else {
650
+ defaultDate = new Date ((long ) value );
651
+ }
652
+
653
+ mFormatter = new SimpleDateFormat (format , Locale .ROOT );
654
+ mFormatter .setTimeZone (TimeZone .getTimeZone ("UTC" ));
655
+ mDefaultValue = mFormatter .format (defaultDate );
656
+ mMinValue = mFormatter .format (new Date ((long ) min ));
657
+ mMaxValue = mFormatter .format (new Date ((long ) max ));
658
+ }
659
+
660
+ @ Override
661
+ @ DatetimeType
662
+ public int type () { return mType ; }
663
+
664
+ @ Override
665
+ @ Nullable
666
+ public String defaultValue () { return mDefaultValue ; }
667
+
668
+ @ Override
669
+ @ Nullable
670
+ public String minValue () { return mMinValue ; }
671
+
672
+ @ Override
673
+ @ Nullable
674
+ public String maxValue () { return mMaxValue ; }
675
+
676
+ @ UiThread
677
+ @ NonNull
678
+ @ Override
679
+ public WSession .PromptDelegate .PromptResponse confirm (@ NonNull final String datetime ) {
680
+ markComplete ();
681
+ try {
682
+ if (datetime != null && !datetime .isEmpty ()) {
683
+ Date date = mFormatter .parse (datetime );
684
+ Calendar cal = mFormatter .getCalendar ();
685
+ cal .setTime (date );
686
+ mDelegate .replaceDateTime (cal .getTimeInMillis ());
687
+ } else {
688
+ mDelegate .replaceDateTime (Double .NaN );
689
+ }
690
+ } catch (final ParseException e ) {
691
+ }
692
+ return new PromptResponseImpl ();
693
+ }
694
+
695
+ @ NonNull
696
+ @ Override
697
+ public WSession .PromptDelegate .PromptResponse dismiss () {
698
+ if (!isComplete ()) {
699
+ markComplete ();
700
+ mDelegate .cancelDateTimeDialog ();
701
+ }
702
+ return new PromptResponseImpl ();
703
+ }
704
+ }
560
705
}
0 commit comments