date_time_chooser_android.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/android/date_time_chooser_android.h"
6
7#include "base/android/jni_string.h"
8#include "content/common/view_messages.h"
9#include "content/public/browser/android/content_view_core.h"
10#include "content/public/browser/render_view_host_observer.h"
11#include "jni/DateTimeChooserAndroid_jni.h"
12
13using base::android::AttachCurrentThread;
14using base::android::ConvertJavaStringToUTF16;
15using base::android::ConvertUTF8ToJavaString;
16
17
18namespace content {
19
20// Updates date/time via IPC to the RenderView
21class DateTimeChooserAndroid::DateTimeIPCSender :
22    public RenderViewHostObserver {
23 public:
24  explicit DateTimeIPCSender(RenderViewHost* sender);
25  virtual ~DateTimeIPCSender() {}
26  void ReplaceDateTime(int dialog_type,
27      int year, int month, int day, int hour, int minute, int second, int week);
28  void CancelDialog();
29
30 private:
31  DISALLOW_COPY_AND_ASSIGN(DateTimeIPCSender);
32};
33
34DateTimeChooserAndroid::DateTimeIPCSender::DateTimeIPCSender(
35    RenderViewHost* sender)
36  : RenderViewHostObserver(sender) {
37}
38
39void DateTimeChooserAndroid::DateTimeIPCSender::ReplaceDateTime(
40    int dialog_type,
41    int year, int month, int day, int hour, int minute, int second, int week) {
42  ViewHostMsg_DateTimeDialogValue_Params value;
43  value.year = year;
44  value.month = month;
45  value.day = day;
46  value.hour = hour;
47  value.minute = minute;
48  value.second = second;
49  value.week = week;
50  value.dialog_type = dialog_type;
51  Send(new ViewMsg_ReplaceDateTime(routing_id(), value));
52}
53
54void DateTimeChooserAndroid::DateTimeIPCSender::CancelDialog() {
55  Send(new ViewMsg_CancelDateTimeDialog(routing_id()));
56}
57
58// DateTimeChooserAndroid implementation
59DateTimeChooserAndroid::DateTimeChooserAndroid()
60  : sender_(NULL) {
61}
62
63DateTimeChooserAndroid::~DateTimeChooserAndroid() {
64}
65
66// static
67void DateTimeChooserAndroid::InitializeDateInputTypes(
68      int text_input_type_date, int text_input_type_date_time,
69      int text_input_type_date_time_local, int text_input_type_month,
70      int text_input_type_time, int text_input_type_week) {
71  JNIEnv* env = AttachCurrentThread();
72  Java_DateTimeChooserAndroid_initializeDateInputTypes(
73         env,
74         text_input_type_date, text_input_type_date_time,
75         text_input_type_date_time_local, text_input_type_month,
76         text_input_type_time, text_input_type_week);
77}
78
79void DateTimeChooserAndroid::ReplaceDateTime(
80    JNIEnv* env, jobject, int dialog_type,
81    int year, int month, int day, int hour, int minute, int second, int week) {
82  sender_->ReplaceDateTime(
83      dialog_type, year, month, day, hour, minute, second, week);
84}
85
86void DateTimeChooserAndroid::CancelDialog(JNIEnv* env, jobject) {
87  sender_->CancelDialog();
88}
89
90void DateTimeChooserAndroid::ShowDialog(
91    ContentViewCore* content, RenderViewHost* sender,
92    int type, int year, int month, int day,
93    int hour, int minute, int second, int week, double min, double max) {
94  if (sender_)
95    delete sender_;
96  sender_ = new DateTimeIPCSender(sender);
97
98  JNIEnv* env = AttachCurrentThread();
99  j_date_time_chooser_.Reset(Java_DateTimeChooserAndroid_createDateTimeChooser(
100      env, content->GetJavaObject().obj(),
101      reinterpret_cast<intptr_t>(this),
102      type, year, month, day, hour, minute, second, week, min, max));
103}
104
105// ----------------------------------------------------------------------------
106// Native JNI methods
107// ----------------------------------------------------------------------------
108bool RegisterDateTimeChooserAndroid(JNIEnv* env) {
109  bool registered = RegisterNativesImpl(env);
110  if (registered)
111    DateTimeChooserAndroid::InitializeDateInputTypes(
112        ui::TEXT_INPUT_TYPE_DATE,
113        ui::TEXT_INPUT_TYPE_DATE_TIME,
114        ui::TEXT_INPUT_TYPE_DATE_TIME_LOCAL,
115        ui::TEXT_INPUT_TYPE_MONTH,
116        ui::TEXT_INPUT_TYPE_TIME,
117        ui::TEXT_INPUT_TYPE_WEEK);
118  return registered;
119}
120
121}  // namespace content
122