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