[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" |
[email protected] | f7c4c27 | 2013-10-31 07:36:00 | [diff] [blame] | 16 | #include "ui/base/android/window_android.h" |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 17 | #include "ui/shell_dialogs/selected_file_info.h" |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 18 | |
| 19 | namespace ui { |
| 20 | |
| 21 | // static |
| 22 | SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener, |
[email protected] | 0b59171c | 2013-06-04 08:38:38 | [diff] [blame] | 23 | SelectFilePolicy* policy) { |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 24 | return new SelectFileDialogImpl(listener, policy); |
| 25 | } |
| 26 | |
| 27 | void SelectFileDialogImpl::OnFileSelected(JNIEnv* env, |
| 28 | jobject java_object, |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 29 | jstring filepath, |
| 30 | jstring display_name) { |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 31 | if (listener_) { |
| 32 | std::string path = base::android::ConvertJavaStringToUTF8(env, filepath); |
[email protected] | f226335 | 2014-02-04 13:09:41 | [diff] [blame] | 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); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 42 | } |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void SelectFileDialogImpl::OnFileNotSelected( |
| 46 | JNIEnv* env, |
| 47 | jobject java_object) { |
| 48 | if (listener_) |
| 49 | listener_->FileSelectionCanceled(NULL); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const { |
[email protected] | 64e82b2 | 2014-03-05 00:20:06 | [diff] [blame^] | 53 | return listener_; |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void SelectFileDialogImpl::ListenerDestroyed() { |
| 57 | listener_ = NULL; |
| 58 | } |
| 59 | |
| 60 | void SelectFileDialogImpl::SelectFileImpl( |
[email protected] | 0b59171c | 2013-06-04 08:38:38 | [diff] [blame] | 61 | SelectFileDialog::Type type, |
| 62 | const base::string16& title, |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 63 | const base::FilePath& default_path, |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 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 | |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 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); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 76 | |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 77 | if (params) { |
| 78 | accept_types = *(reinterpret_cast<AcceptTypes*>(params)); |
[email protected] | 15ccd9a | 2013-05-29 08:15:24 | [diff] [blame] | 79 | } |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 80 | |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 81 | ScopedJavaLocalRef<jobjectArray> accept_types_java = |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 82 | base::android::ToJavaArrayOfStrings(env, accept_types.first); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 83 | |
| 84 | Java_SelectFileDialog_selectFile(env, java_object_.obj(), |
| 85 | accept_types_java.obj(), |
[email protected] | b7b4beb | 2013-07-09 14:06:50 | [diff] [blame] | 86 | accept_types.second, |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 87 | owning_window->GetJavaObject().obj()); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) { |
| 91 | return RegisterNativesImpl(env); |
| 92 | } |
| 93 | |
| 94 | SelectFileDialogImpl::~SelectFileDialogImpl() { |
| 95 | } |
| 96 | |
| 97 | SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener, |
[email protected] | 0b59171c | 2013-06-04 08:38:38 | [diff] [blame] | 98 | SelectFilePolicy* policy) |
[email protected] | 64e82b2 | 2014-03-05 00:20:06 | [diff] [blame^] | 99 | : SelectFileDialog(listener, policy) { |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 100 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 101 | java_object_.Reset( |
[email protected] | cfccf26 | 2013-11-11 23:27:54 | [diff] [blame] | 102 | Java_SelectFileDialog_create(env, reinterpret_cast<intptr_t>(this))); |
[email protected] | e8c62b9 | 2013-01-16 23:08:27 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { |
| 106 | NOTIMPLEMENTED(); |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | SelectFileDialog* CreateAndroidSelectFileDialog( |
| 111 | SelectFileDialog::Listener* listener, |
| 112 | SelectFilePolicy* policy) { |
| 113 | return SelectFileDialogImpl::Create(listener, policy); |
| 114 | } |
| 115 | |
| 116 | } // namespace ui |