OLD | NEW |
(Empty) | |
| 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_string.h" |
| 9 #include "base/android/jni_array.h" |
| 10 #include "base/android/scoped_java_ref.h" |
| 11 #include "base/logging.h" |
| 12 #include "base/string_split.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 #include "jni/SelectFileDialog_jni.h" |
| 16 #include "ui/gfx/android/window_android.h" |
| 17 |
| 18 namespace ui { |
| 19 |
| 20 SelectFileDialog* CreateAndroidSelectFileDialog( |
| 21 SelectFileDialog::Listener* listener, |
| 22 SelectFilePolicy* policy) { |
| 23 return SelectFileDialogImpl::Create(listener, policy); |
| 24 } |
| 25 |
| 26 // static |
| 27 SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener, |
| 28 ui::SelectFilePolicy* policy) { |
| 29 return new SelectFileDialogImpl(listener, policy); |
| 30 } |
| 31 |
| 32 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener, |
| 33 ui::SelectFilePolicy* policy) |
| 34 : ui::SelectFileDialog(listener, policy), |
| 35 is_running_(false) { |
| 36 JNIEnv* env = base::android::AttachCurrentThread(); |
| 37 java_object_.Reset( |
| 38 Java_SelectFileDialog_create(env, reinterpret_cast<jint>(this))); |
| 39 } |
| 40 |
| 41 SelectFileDialogImpl::~SelectFileDialogImpl() { |
| 42 } |
| 43 |
| 44 void SelectFileDialogImpl::OnFileSelected(JNIEnv* env, |
| 45 jobject, |
| 46 jstring filepath) { |
| 47 if (listener_) { |
| 48 std::string path = base::android::ConvertJavaStringToUTF8(env, filepath); |
| 49 listener_->FileSelected(FilePath(path), 0, NULL); |
| 50 } |
| 51 |
| 52 is_running_ = false; |
| 53 } |
| 54 |
| 55 void SelectFileDialogImpl::OnFileNotSelected(JNIEnv*, jobject) { |
| 56 if (listener_) |
| 57 listener_->FileSelectionCanceled(NULL); |
| 58 |
| 59 is_running_ = false; |
| 60 } |
| 61 |
| 62 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const { |
| 63 return is_running_; |
| 64 } |
| 65 |
| 66 void SelectFileDialogImpl::ListenerDestroyed() { |
| 67 listener_ = 0; |
| 68 } |
| 69 |
| 70 void SelectFileDialogImpl::SelectFileImpl( |
| 71 ui::SelectFileDialog::Type type, |
| 72 const string16& title, |
| 73 const FilePath& default_path, |
| 74 const SelectFileDialog::FileTypeInfo* file_types, |
| 75 int file_type_index, |
| 76 const std::string& default_extension, |
| 77 gfx::NativeWindow owning_window, |
| 78 void* params) { |
| 79 JNIEnv* env = base::android::AttachCurrentThread(); |
| 80 |
| 81 std::vector<string16> accept_types = |
| 82 *(reinterpret_cast<std::vector<string16>*>(params)); |
| 83 |
| 84 // The last string in params is expected to be the string with capture value. |
| 85 ScopedJavaLocalRef<jstring> capture_value = |
| 86 base::android::ConvertUTF16ToJavaString(env, |
| 87 StringToLowerASCII(accept_types.back())); |
| 88 base::android::CheckException(env); |
| 89 accept_types.pop_back(); |
| 90 |
| 91 // The rest params elements are expected to be accept_types. |
| 92 ScopedJavaLocalRef<jobjectArray> accept_types_java = |
| 93 base::android::ToJavaArrayOfStrings(env, accept_types); |
| 94 |
| 95 Java_SelectFileDialog_selectFile(env, java_object_.obj(), |
| 96 accept_types_java.obj(), |
| 97 capture_value.obj(), |
| 98 owning_window->GetJavaObject().obj()); |
| 99 is_running_ = true; |
| 100 } |
| 101 |
| 102 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() { |
| 103 NOTIMPLEMENTED(); |
| 104 return false; |
| 105 } |
| 106 |
| 107 bool RegisterSelectFileDialog(JNIEnv* env) { |
| 108 return RegisterNativesImpl(env); |
| 109 } |
| 110 |
| 111 } // namespace ui |
OLD | NEW |