- add sources.
[platform/framework/web/crosswalk.git] / src / ui / shell_dialogs / select_file_dialog_android.cc
1 // Copyright (c) 2012 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 "select_file_dialog_android.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/logging.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "jni/SelectFileDialog_jni.h"
16 #include "ui/base/android/window_android.h"
17
18 namespace ui {
19
20 // static
21 SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,
22                                                    SelectFilePolicy* policy) {
23   return new SelectFileDialogImpl(listener, policy);
24 }
25
26 void SelectFileDialogImpl::OnFileSelected(JNIEnv* env,
27                                           jobject java_object,
28                                           jstring filepath) {
29   if (listener_) {
30     std::string path = base::android::ConvertJavaStringToUTF8(env, filepath);
31     listener_->FileSelected(base::FilePath(path), 0, NULL);
32   }
33
34   is_running_ = false;
35 }
36
37 void SelectFileDialogImpl::OnFileNotSelected(
38     JNIEnv* env,
39     jobject java_object) {
40   if (listener_)
41     listener_->FileSelectionCanceled(NULL);
42
43   is_running_ = false;
44 }
45
46 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const {
47   return is_running_;
48 }
49
50 void SelectFileDialogImpl::ListenerDestroyed() {
51   listener_ = NULL;
52 }
53
54 void SelectFileDialogImpl::SelectFileImpl(
55     SelectFileDialog::Type type,
56     const base::string16& title,
57     const base::FilePath& default_path,
58     const SelectFileDialog::FileTypeInfo* file_types,
59     int file_type_index,
60     const std::string& default_extension,
61     gfx::NativeWindow owning_window,
62     void* params) {
63   JNIEnv* env = base::android::AttachCurrentThread();
64
65   // The first element in the pair is a list of accepted types, the second
66   // indicates whether the device's capture capabilities should be used.
67   typedef std::pair<std::vector<base::string16>, bool> AcceptTypes;
68   AcceptTypes accept_types = std::make_pair(std::vector<base::string16>(),
69                                             false);
70
71   if (params) {
72     accept_types = *(reinterpret_cast<AcceptTypes*>(params));
73   }
74
75   ScopedJavaLocalRef<jobjectArray> accept_types_java =
76       base::android::ToJavaArrayOfStrings(env, accept_types.first);
77
78   Java_SelectFileDialog_selectFile(env, java_object_.obj(),
79                                    accept_types_java.obj(),
80                                    accept_types.second,
81                                    owning_window->GetJavaObject().obj());
82   is_running_ = true;
83 }
84
85 bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) {
86   return RegisterNativesImpl(env);
87 }
88
89 SelectFileDialogImpl::~SelectFileDialogImpl() {
90 }
91
92 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
93                                            SelectFilePolicy* policy)
94     : SelectFileDialog(listener, policy), is_running_(false) {
95   JNIEnv* env = base::android::AttachCurrentThread();
96   java_object_.Reset(
97       Java_SelectFileDialog_create(env, reinterpret_cast<jint>(this)));
98 }
99
100 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
101   NOTIMPLEMENTED();
102   return false;
103 }
104
105 SelectFileDialog* CreateAndroidSelectFileDialog(
106     SelectFileDialog::Listener* listener,
107     SelectFilePolicy* policy) {
108   return SelectFileDialogImpl::Create(listener, policy);
109 }
110
111 }  // namespace ui