[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 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" |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 8 | #include "base/android/jni_array.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 9 | #include "base/android/jni_string.h" |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 10 | #include "base/android/scoped_java_ref.h" |
| 11 | #include "base/logging.h" |
[email protected] | d778e042 | 2013-03-06 18:10:22 | [diff] [blame] | 12 | #include "base/strings/string_split.h" |
[email protected] | 3b3a3064 | 2013-06-11 19:48:38 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | c7057fbe | 2013-06-07 18:54:01 | [diff] [blame] | 14 | #include "base/strings/utf_string_conversions.h" |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 15 | #include "jni/SelectFileDialog_jni.h" |
jaekyun | e4f9eed | 2015-02-24 02:06:58 | [diff] [blame] | 16 | #include "ui/android/window_android.h" |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame^] | 17 | #include "ui/shell_dialogs/select_file_policy.h" |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 18 | #include "ui/shell_dialogs/selected_file_info.h" |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 19 | |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 20 | using base::android::ConvertJavaStringToUTF8; |
torne | 8656011 | 2016-08-04 15:59:04 | [diff] [blame] | 21 | using base::android::JavaParamRef; |
| 22 | using base::android::ScopedJavaLocalRef; |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 23 | |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 24 | namespace ui { |
| 25 | |
| 26 | // static |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame^] | 27 | SelectFileDialogImpl* SelectFileDialogImpl::Create( |
| 28 | Listener* listener, |
| 29 | std::unique_ptr<SelectFilePolicy> policy) { |
| 30 | return new SelectFileDialogImpl(listener, std::move(policy)); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 31 | } |
| 32 | |
torne | bb13c83 | 2015-12-07 12:49:14 | [diff] [blame] | 33 | void SelectFileDialogImpl::OnFileSelected( |
| 34 | JNIEnv* env, |
| 35 | const JavaParamRef<jobject>& java_object, |
| 36 | const JavaParamRef<jstring>& filepath, |
| 37 | const JavaParamRef<jstring>& display_name) { |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 38 | if (!listener_) |
| 39 | return; |
| 40 | |
| 41 | std::string path = ConvertJavaStringToUTF8(env, filepath); |
| 42 | std::string file_name = ConvertJavaStringToUTF8(env, display_name); |
| 43 | base::FilePath file_path = base::FilePath(path); |
| 44 | ui::SelectedFileInfo file_info; |
| 45 | file_info.file_path = file_path; |
| 46 | file_info.local_path = file_path; |
| 47 | if (!file_name.empty()) |
| 48 | file_info.display_name = file_name; |
| 49 | |
| 50 | listener_->FileSelectedWithExtraInfo(file_info, 0, NULL); |
| 51 | } |
| 52 | |
torne | bb13c83 | 2015-12-07 12:49:14 | [diff] [blame] | 53 | void SelectFileDialogImpl::OnMultipleFilesSelected( |
| 54 | JNIEnv* env, |
| 55 | const JavaParamRef<jobject>& java_object, |
| 56 | const JavaParamRef<jobjectArray>& filepaths, |
| 57 | const JavaParamRef<jobjectArray>& display_names) { |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 58 | if (!listener_) |
| 59 | return; |
| 60 | |
| 61 | std::vector<ui::SelectedFileInfo> selected_files; |
| 62 | |
| 63 | jsize length = env->GetArrayLength(filepaths); |
| 64 | DCHECK(length == env->GetArrayLength(display_names)); |
| 65 | for (int i = 0; i < length; ++i) { |
| 66 | std::string path = ConvertJavaStringToUTF8( |
| 67 | env, static_cast<jstring>(env->GetObjectArrayElement(filepaths, i))); |
| 68 | std::string display_name = ConvertJavaStringToUTF8( |
| 69 | env, |
| 70 | static_cast<jstring>(env->GetObjectArrayElement(display_names, i))); |
| 71 | |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 72 | base::FilePath file_path = base::FilePath(path); |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 73 | |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 74 | ui::SelectedFileInfo file_info; |
| 75 | file_info.file_path = file_path; |
| 76 | file_info.local_path = file_path; |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 77 | file_info.display_name = display_name; |
| 78 | |
| 79 | selected_files.push_back(file_info); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 80 | } |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 81 | |
| 82 | listener_->MultiFilesSelectedWithExtraInfo(selected_files, NULL); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void SelectFileDialogImpl::OnFileNotSelected( |
| 86 | JNIEnv* env, |
torne | bb13c83 | 2015-12-07 12:49:14 | [diff] [blame] | 87 | const JavaParamRef<jobject>& java_object) { |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 88 | if (listener_) |
| 89 | listener_->FileSelectionCanceled(NULL); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const { |
[email protected] | 64e82b2 | 2014-03-05 00:20:06 | [diff] [blame] | 93 | return listener_; |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void SelectFileDialogImpl::ListenerDestroyed() { |
| 97 | listener_ = NULL; |
| 98 | } |
| 99 | |
| 100 | void SelectFileDialogImpl::SelectFileImpl( |
[email protected] | 0b59171c | 2013-06-04 08:38:38 | [diff] [blame] | 101 | SelectFileDialog::Type type, |
| 102 | const base::string16& title, |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 103 | const base::FilePath& default_path, |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 104 | const SelectFileDialog::FileTypeInfo* file_types, |
| 105 | int file_type_index, |
| 106 | const std::string& default_extension, |
| 107 | gfx::NativeWindow owning_window, |
| 108 | void* params) { |
| 109 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 110 | |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 111 | // The first element in the pair is a list of accepted types, the second |
| 112 | // indicates whether the device's capture capabilities should be used. |
| 113 | typedef std::pair<std::vector<base::string16>, bool> AcceptTypes; |
| 114 | AcceptTypes accept_types = std::make_pair(std::vector<base::string16>(), |
| 115 | false); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 116 | |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 117 | if (params) |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 118 | accept_types = *(reinterpret_cast<AcceptTypes*>(params)); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 119 | |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 120 | ScopedJavaLocalRef<jobjectArray> accept_types_java = |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 121 | base::android::ToJavaArrayOfStrings(env, accept_types.first); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 122 | |
[email protected] | 98dd29f | 2014-08-06 16:04:35 | [diff] [blame] | 123 | bool accept_multiple_files = SelectFileDialog::SELECT_OPEN_MULTI_FILE == type; |
| 124 | |
torne | 948f366 | 2016-08-16 15:10:44 | [diff] [blame] | 125 | Java_SelectFileDialog_selectFile(env, java_object_, accept_types_java, |
| 126 | accept_types.second, accept_multiple_files, |
| 127 | owning_window->GetJavaObject()); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 130 | SelectFileDialogImpl::~SelectFileDialogImpl() { |
| 131 | } |
| 132 | |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame^] | 133 | SelectFileDialogImpl::SelectFileDialogImpl( |
| 134 | Listener* listener, |
| 135 | std::unique_ptr<SelectFilePolicy> policy) |
| 136 | : SelectFileDialog(listener, std::move(policy)) { |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 137 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 138 | java_object_.Reset( |
[email protected] | cfccf26 | 2013-11-11 23:27:54 | [diff] [blame] | 139 | Java_SelectFileDialog_create(env, reinterpret_cast<intptr_t>(this))); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { |
| 143 | NOTIMPLEMENTED(); |
| 144 | return false; |
| 145 | } |
| 146 | |
Brett Wilson | 804e83c | 2017-08-18 22:57:33 | [diff] [blame^] | 147 | SelectFileDialog* CreateSelectFileDialog( |
| 148 | SelectFileDialog::Listener* listener, |
| 149 | std::unique_ptr<SelectFilePolicy> policy) { |
| 150 | return SelectFileDialogImpl::Create(listener, std::move(policy)); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | } // namespace ui |