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/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"
21 SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,
22 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 SelectFileDialog::Type type,
56 const base::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 // 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>(),
72 accept_types = *(reinterpret_cast<AcceptTypes*>(params));
75 ScopedJavaLocalRef<jobjectArray> accept_types_java =
76 base::android::ToJavaArrayOfStrings(env, accept_types.first);
78 Java_SelectFileDialog_selectFile(env, java_object_.obj(),
79 accept_types_java.obj(),
81 owning_window->GetJavaObject().obj());
85 bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) {
86 return RegisterNativesImpl(env);
89 SelectFileDialogImpl::~SelectFileDialogImpl() {
92 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
93 SelectFilePolicy* policy)
94 : SelectFileDialog(listener, policy), is_running_(false) {
95 JNIEnv* env = base::android::AttachCurrentThread();
97 Java_SelectFileDialog_create(env, reinterpret_cast<jint>(this)));
100 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
105 SelectFileDialog* CreateAndroidSelectFileDialog(
106 SelectFileDialog::Listener* listener,
107 SelectFilePolicy* policy) {
108 return SelectFileDialogImpl::Create(listener, policy);