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#include "ui/shell_dialogs/selected_file_info.h" 18 19namespace ui { 20 21// static 22SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener, 23 SelectFilePolicy* policy) { 24 return new SelectFileDialogImpl(listener, policy); 25} 26 27void SelectFileDialogImpl::OnFileSelected(JNIEnv* env, 28 jobject java_object, 29 jstring filepath, 30 jstring display_name) { 31 if (listener_) { 32 std::string path = base::android::ConvertJavaStringToUTF8(env, filepath); 33 std::string file_name = 34 base::android::ConvertJavaStringToUTF8(env, display_name); 35 base::FilePath file_path = base::FilePath(path); 36 ui::SelectedFileInfo file_info; 37 file_info.file_path = file_path; 38 file_info.local_path = file_path; 39 if (!file_name.empty()) 40 file_info.display_name = file_name; 41 listener_->FileSelectedWithExtraInfo(file_info, 0, NULL); 42 } 43} 44 45void SelectFileDialogImpl::OnFileNotSelected( 46 JNIEnv* env, 47 jobject java_object) { 48 if (listener_) 49 listener_->FileSelectionCanceled(NULL); 50} 51 52bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const { 53 return listener_; 54} 55 56void SelectFileDialogImpl::ListenerDestroyed() { 57 listener_ = NULL; 58} 59 60void SelectFileDialogImpl::SelectFileImpl( 61 SelectFileDialog::Type type, 62 const base::string16& title, 63 const base::FilePath& default_path, 64 const SelectFileDialog::FileTypeInfo* file_types, 65 int file_type_index, 66 const std::string& default_extension, 67 gfx::NativeWindow owning_window, 68 void* params) { 69 JNIEnv* env = base::android::AttachCurrentThread(); 70 71 // The first element in the pair is a list of accepted types, the second 72 // indicates whether the device's capture capabilities should be used. 73 typedef std::pair<std::vector<base::string16>, bool> AcceptTypes; 74 AcceptTypes accept_types = std::make_pair(std::vector<base::string16>(), 75 false); 76 77 if (params) { 78 accept_types = *(reinterpret_cast<AcceptTypes*>(params)); 79 } 80 81 ScopedJavaLocalRef<jobjectArray> accept_types_java = 82 base::android::ToJavaArrayOfStrings(env, accept_types.first); 83 84 Java_SelectFileDialog_selectFile(env, java_object_.obj(), 85 accept_types_java.obj(), 86 accept_types.second, 87 owning_window->GetJavaObject().obj()); 88} 89 90bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) { 91 return RegisterNativesImpl(env); 92} 93 94SelectFileDialogImpl::~SelectFileDialogImpl() { 95} 96 97SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener, 98 SelectFilePolicy* policy) 99 : SelectFileDialog(listener, policy) { 100 JNIEnv* env = base::android::AttachCurrentThread(); 101 java_object_.Reset( 102 Java_SelectFileDialog_create(env, reinterpret_cast<intptr_t>(this))); 103} 104 105bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { 106 NOTIMPLEMENTED(); 107 return false; 108} 109 110SelectFileDialog* CreateAndroidSelectFileDialog( 111 SelectFileDialog::Listener* listener, 112 SelectFilePolicy* policy) { 113 return SelectFileDialogImpl::Create(listener, policy); 114} 115 116} // namespace ui 117