blob: af15c977b0b05261f39aa0fddf88a942d1791594 [file] [log] [blame]
[email protected]e8c62b92013-01-16 23:08:271// 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]e8c62b92013-01-16 23:08:278#include "base/android/jni_array.h"
[email protected]d778e0422013-03-06 18:10:229#include "base/android/jni_string.h"
[email protected]e8c62b92013-01-16 23:08:2710#include "base/android/scoped_java_ref.h"
11#include "base/logging.h"
[email protected]d778e0422013-03-06 18:10:2212#include "base/strings/string_split.h"
[email protected]3b3a30642013-06-11 19:48:3813#include "base/strings/string_util.h"
[email protected]c7057fbe2013-06-07 18:54:0114#include "base/strings/utf_string_conversions.h"
[email protected]e8c62b92013-01-16 23:08:2715#include "jni/SelectFileDialog_jni.h"
[email protected]f7c4c272013-10-31 07:36:0016#include "ui/base/android/window_android.h"
[email protected]f2263352014-02-04 13:09:4117#include "ui/shell_dialogs/selected_file_info.h"
[email protected]e8c62b92013-01-16 23:08:2718
19namespace ui {
20
21// static
22SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,
[email protected]0b59171c2013-06-04 08:38:3823 SelectFilePolicy* policy) {
[email protected]e8c62b92013-01-16 23:08:2724 return new SelectFileDialogImpl(listener, policy);
25}
26
27void SelectFileDialogImpl::OnFileSelected(JNIEnv* env,
28 jobject java_object,
[email protected]f2263352014-02-04 13:09:4129 jstring filepath,
30 jstring display_name) {
[email protected]e8c62b92013-01-16 23:08:2731 if (listener_) {
32 std::string path = base::android::ConvertJavaStringToUTF8(env, filepath);
[email protected]f2263352014-02-04 13:09:4133 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]e8c62b92013-01-16 23:08:2742 }
[email protected]e8c62b92013-01-16 23:08:2743}
44
45void SelectFileDialogImpl::OnFileNotSelected(
46 JNIEnv* env,
47 jobject java_object) {
48 if (listener_)
49 listener_->FileSelectionCanceled(NULL);
[email protected]e8c62b92013-01-16 23:08:2750}
51
52bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const {
[email protected]64e82b22014-03-05 00:20:0653 return listener_;
[email protected]e8c62b92013-01-16 23:08:2754}
55
56void SelectFileDialogImpl::ListenerDestroyed() {
57 listener_ = NULL;
58}
59
60void SelectFileDialogImpl::SelectFileImpl(
[email protected]0b59171c2013-06-04 08:38:3861 SelectFileDialog::Type type,
62 const base::string16& title,
[email protected]79f63882013-02-10 05:15:4563 const base::FilePath& default_path,
[email protected]e8c62b92013-01-16 23:08:2764 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]b7b4beb2013-07-09 14:06:5071 // 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]e8c62b92013-01-16 23:08:2776
[email protected]b7b4beb2013-07-09 14:06:5077 if (params) {
78 accept_types = *(reinterpret_cast<AcceptTypes*>(params));
[email protected]15ccd9a2013-05-29 08:15:2479 }
[email protected]e8c62b92013-01-16 23:08:2780
[email protected]e8c62b92013-01-16 23:08:2781 ScopedJavaLocalRef<jobjectArray> accept_types_java =
[email protected]b7b4beb2013-07-09 14:06:5082 base::android::ToJavaArrayOfStrings(env, accept_types.first);
[email protected]e8c62b92013-01-16 23:08:2783
84 Java_SelectFileDialog_selectFile(env, java_object_.obj(),
85 accept_types_java.obj(),
[email protected]b7b4beb2013-07-09 14:06:5086 accept_types.second,
[email protected]e8c62b92013-01-16 23:08:2787 owning_window->GetJavaObject().obj());
[email protected]e8c62b92013-01-16 23:08:2788}
89
90bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) {
91 return RegisterNativesImpl(env);
92}
93
94SelectFileDialogImpl::~SelectFileDialogImpl() {
95}
96
97SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
[email protected]0b59171c2013-06-04 08:38:3898 SelectFilePolicy* policy)
[email protected]64e82b22014-03-05 00:20:0699 : SelectFileDialog(listener, policy) {
[email protected]e8c62b92013-01-16 23:08:27100 JNIEnv* env = base::android::AttachCurrentThread();
101 java_object_.Reset(
[email protected]cfccf262013-11-11 23:27:54102 Java_SelectFileDialog_create(env, reinterpret_cast<intptr_t>(this)));
[email protected]e8c62b92013-01-16 23:08:27103}
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