• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/logging.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "jni/SelectFileDialog_jni.h"
16 #include "ui/base/android/window_android.h"
17 #include "ui/shell_dialogs/selected_file_info.h"
18 
19 namespace ui {
20 
21 // static
Create(Listener * listener,SelectFilePolicy * policy)22 SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,
23                                                    SelectFilePolicy* policy) {
24   return new SelectFileDialogImpl(listener, policy);
25 }
26 
OnFileSelected(JNIEnv * env,jobject java_object,jstring filepath,jstring display_name)27 void SelectFileDialogImpl::OnFileSelected(JNIEnv* env,
28                                           jobject java_object,
29                                           jstring filepath,
30                                           jstring display_name) {
31   if (listener_) {
32     std::string path = base::android::ConvertJavaStringToUTF8(env, filepath);
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);
42   }
43 
44   is_running_ = false;
45 }
46 
OnFileNotSelected(JNIEnv * env,jobject java_object)47 void SelectFileDialogImpl::OnFileNotSelected(
48     JNIEnv* env,
49     jobject java_object) {
50   if (listener_)
51     listener_->FileSelectionCanceled(NULL);
52 
53   is_running_ = false;
54 }
55 
IsRunning(gfx::NativeWindow) const56 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const {
57   return is_running_;
58 }
59 
ListenerDestroyed()60 void SelectFileDialogImpl::ListenerDestroyed() {
61   listener_ = NULL;
62 }
63 
SelectFileImpl(SelectFileDialog::Type type,const base::string16 & title,const base::FilePath & default_path,const SelectFileDialog::FileTypeInfo * file_types,int file_type_index,const std::string & default_extension,gfx::NativeWindow owning_window,void * params)64 void SelectFileDialogImpl::SelectFileImpl(
65     SelectFileDialog::Type type,
66     const base::string16& title,
67     const base::FilePath& default_path,
68     const SelectFileDialog::FileTypeInfo* file_types,
69     int file_type_index,
70     const std::string& default_extension,
71     gfx::NativeWindow owning_window,
72     void* params) {
73   JNIEnv* env = base::android::AttachCurrentThread();
74 
75   // The first element in the pair is a list of accepted types, the second
76   // indicates whether the device's capture capabilities should be used.
77   typedef std::pair<std::vector<base::string16>, bool> AcceptTypes;
78   AcceptTypes accept_types = std::make_pair(std::vector<base::string16>(),
79                                             false);
80 
81   if (params) {
82     accept_types = *(reinterpret_cast<AcceptTypes*>(params));
83   }
84 
85   ScopedJavaLocalRef<jobjectArray> accept_types_java =
86       base::android::ToJavaArrayOfStrings(env, accept_types.first);
87 
88   Java_SelectFileDialog_selectFile(env, java_object_.obj(),
89                                    accept_types_java.obj(),
90                                    accept_types.second,
91                                    owning_window->GetJavaObject().obj());
92   is_running_ = true;
93 }
94 
RegisterSelectFileDialog(JNIEnv * env)95 bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) {
96   return RegisterNativesImpl(env);
97 }
98 
~SelectFileDialogImpl()99 SelectFileDialogImpl::~SelectFileDialogImpl() {
100 }
101 
SelectFileDialogImpl(Listener * listener,SelectFilePolicy * policy)102 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
103                                            SelectFilePolicy* policy)
104     : SelectFileDialog(listener, policy), is_running_(false) {
105   JNIEnv* env = base::android::AttachCurrentThread();
106   java_object_.Reset(
107       Java_SelectFileDialog_create(env, reinterpret_cast<intptr_t>(this)));
108 }
109 
HasMultipleFileTypeChoicesImpl()110 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
111   NOTIMPLEMENTED();
112   return false;
113 }
114 
CreateAndroidSelectFileDialog(SelectFileDialog::Listener * listener,SelectFilePolicy * policy)115 SelectFileDialog* CreateAndroidSelectFileDialog(
116     SelectFileDialog::Listener* listener,
117     SelectFilePolicy* policy) {
118   return SelectFileDialogImpl::Create(listener, policy);
119 }
120 
121 }  // namespace ui
122