summaryrefslogtreecommitdiffstats
path: root/content/browser/android/date_time_chooser_android.cc
blob: 95bf2f3a59dd202ff18a3f773a1b55b875e79c51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/android/date_time_chooser_android.h"

#include "base/android/jni_string.h"
#include "content/common/view_messages.h"
#include "content/public/browser/android/content_view_core.h"
#include "content/public/browser/render_view_host_observer.h"
#include "jni/DateTimeChooserAndroid_jni.h"

using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF8ToJavaString;


namespace content {

// Updates date/time via IPC to the RenderView
class DateTimeChooserAndroid::DateTimeIPCSender :
    public RenderViewHostObserver {
 public:
  explicit DateTimeIPCSender(RenderViewHost* sender);
  virtual ~DateTimeIPCSender() {}
  void ReplaceDateTime(int dialog_type,
      int year, int month, int day, int hour, int minute, int second);
  void CancelDialog();

 private:
  DISALLOW_COPY_AND_ASSIGN(DateTimeIPCSender);
};

DateTimeChooserAndroid::DateTimeIPCSender::DateTimeIPCSender(
    RenderViewHost* sender)
  : RenderViewHostObserver(sender) {
}

void DateTimeChooserAndroid::DateTimeIPCSender::ReplaceDateTime(
    int dialog_type,
    int year, int month, int day, int hour, int minute, int second) {
  ViewHostMsg_DateTimeDialogValue_Params value;
  value.year = year;
  value.month = month;
  value.day = day;
  value.hour = hour;
  value.minute = minute;
  value.second = second;
  value.dialog_type = dialog_type;
  Send(new ViewMsg_ReplaceDateTime(routing_id(), value));
}

void DateTimeChooserAndroid::DateTimeIPCSender::CancelDialog() {
  Send(new ViewMsg_CancelDateTimeDialog(routing_id()));
}

// DateTimeChooserAndroid implementation
DateTimeChooserAndroid::DateTimeChooserAndroid() {
}

DateTimeChooserAndroid::~DateTimeChooserAndroid() {
}

// static
void DateTimeChooserAndroid::InitializeDateInputTypes(
      int text_input_type_date, int text_input_type_date_time,
      int text_input_type_date_time_local, int text_input_type_month,
      int text_input_type_time) {
  JNIEnv* env = AttachCurrentThread();
  Java_DateTimeChooserAndroid_initializeDateInputTypes(
         env,
         text_input_type_date, text_input_type_date_time,
         text_input_type_date_time_local, text_input_type_month,
         text_input_type_time);
}

void DateTimeChooserAndroid::ReplaceDateTime(
    JNIEnv* env, jobject, int dialog_type,
    int year, int month, int day, int hour, int minute, int second) {
  sender_->ReplaceDateTime(
      dialog_type, year, month, day, hour, minute, second);
}

void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) {
  sender_->CancelDialog();
}

void DateTimeChooserAndroid::ShowDialog(
    ContentViewCore* content, RenderViewHost* sender,
    int type, int year, int month, int day,
    int hour, int minute, int second) {
  sender_.reset(new DateTimeIPCSender(sender));
  JNIEnv* env = AttachCurrentThread();
  j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser(
      env, content->GetJavaObject().obj(),
      reinterpret_cast<intptr_t>(this),
      type, year, month, day, hour, minute, second));
}

// ----------------------------------------------------------------------------
// Native JNI methods
// ----------------------------------------------------------------------------
bool RegisterDateTimeChooserAndroid(JNIEnv* env) {
  bool registered = RegisterNativesImpl(env);
  if (registered)
    DateTimeChooserAndroid::InitializeDateInputTypes(
        ui::TEXT_INPUT_TYPE_DATE,
        ui::TEXT_INPUT_TYPE_DATE_TIME,
        ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL,
        ui::TEXT_INPUT_TYPE_MONTH,
        ui::TEXT_INPUT_TYPE_TIME);
  return registered;
}

}  // namespace content