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.
5 #include "select_file_dialog_android.h"
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/string_util.h"
13 #include "base/strings/string_split.h"
14 #include "base/utf_string_conversions.h"
15 #include "jni/SelectFileDialog_jni.h"
16 #include "ui/gfx/android/window_android.h"
21 SelectFileDialogImpl
* SelectFileDialogImpl::Create(Listener
* listener
,
22 ui::SelectFilePolicy
* policy
) {
23 return new SelectFileDialogImpl(listener
, policy
);
26 void SelectFileDialogImpl::OnFileSelected(JNIEnv
* env
,
30 std::string path
= base::android::ConvertJavaStringToUTF8(env
, filepath
);
31 listener_
->FileSelected(base::FilePath(path
), 0, NULL
);
37 void SelectFileDialogImpl::OnFileNotSelected(
39 jobject java_object
) {
41 listener_
->FileSelectionCanceled(NULL
);
46 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow
) const {
50 void SelectFileDialogImpl::ListenerDestroyed() {
54 void SelectFileDialogImpl::SelectFileImpl(
55 ui::SelectFileDialog::Type type
,
56 const string16
& title
,
57 const base::FilePath
& default_path
,
58 const SelectFileDialog::FileTypeInfo
* file_types
,
60 const std::string
& default_extension
,
61 gfx::NativeWindow owning_window
,
63 JNIEnv
* env
= base::android::AttachCurrentThread();
65 std::vector
<string16
> accept_types
=
66 *(reinterpret_cast<std::vector
<string16
>*>(params
));
68 // The last string in params is expected to be the string with capture value.
69 ScopedJavaLocalRef
<jstring
> capture_value
=
70 base::android::ConvertUTF16ToJavaString(env
,
71 StringToLowerASCII(accept_types
.back()));
72 base::android::CheckException(env
);
73 accept_types
.pop_back();
75 // The rest params elements are expected to be accept_types.
76 ScopedJavaLocalRef
<jobjectArray
> accept_types_java
=
77 base::android::ToJavaArrayOfStrings(env
, accept_types
);
79 Java_SelectFileDialog_selectFile(env
, java_object_
.obj(),
80 accept_types_java
.obj(),
82 owning_window
->GetJavaObject().obj());
86 bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv
* env
) {
87 return RegisterNativesImpl(env
);
90 SelectFileDialogImpl::~SelectFileDialogImpl() {
93 SelectFileDialogImpl::SelectFileDialogImpl(Listener
* listener
,
94 ui::SelectFilePolicy
* policy
)
95 : ui::SelectFileDialog(listener
, policy
),
97 JNIEnv
* env
= base::android::AttachCurrentThread();
99 Java_SelectFileDialog_create(env
, reinterpret_cast<jint
>(this)));
102 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
107 SelectFileDialog
* CreateAndroidSelectFileDialog(
108 SelectFileDialog::Listener
* listener
,
109 SelectFilePolicy
* policy
) {
110 return SelectFileDialogImpl::Create(listener
, policy
);