Skip to content

Commit

Permalink
Figure out the display name for a content uri.
Browse files Browse the repository at this point in the history
Plumb it through the File listener so blink can display the right
content on <input type=file> elements.

BUG=338929
NOTRY=true

Review URL: https://codereview.chromium.org/150283002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@248707 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
miguelg@chromium.org committed Feb 4, 2014
1 parent 22f27c9 commit f226335
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
39 changes: 32 additions & 7 deletions ui/android/java/src/org/chromium/ui/base/SelectFileDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
Expand Down Expand Up @@ -128,6 +129,28 @@ private File getFileForImageCapture() {
return photoFile;
}

/**
* @return the display name of the @code uri if present in the database
* or an empty string otherwise.
*/
private String resolveFileName(Uri uri, ContentResolver contentResolver) {
Cursor cursor = null;
try {
cursor = contentResolver.query(uri, null, null, null, null);

if (cursor != null && cursor.getCount() >= 1) {
cursor.moveToFirst();
int index = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
if (index > -1) return cursor.getString(index);
}
} finally {
if (cursor != null ) {
cursor.close();
}
}
return "";
}

/**
* Callback method to handle the intent results and pass on the path to the native
* SelectFileDialog.
Expand All @@ -147,7 +170,7 @@ public void onIntentCompleted(WindowAndroid window, int resultCode,
if (results == null) {
// If we have a successful return but no data, then assume this is the camera returning
// the photo that we requested.
nativeOnFileSelected(mNativeSelectFileDialog, mCameraOutputUri.getPath());
nativeOnFileSelected(mNativeSelectFileDialog, mCameraOutputUri.getPath(), "");

// Broadcast to the media scanner that there's a new photo on the device so it will
// show up right away in the gallery (rather than waiting until the next time the media
Expand All @@ -157,15 +180,17 @@ public void onIntentCompleted(WindowAndroid window, int resultCode,
return;
}

if ("file".equals(results.getData().getScheme())) {
if (ContentResolver.SCHEME_FILE.equals(results.getData().getScheme())) {
nativeOnFileSelected(mNativeSelectFileDialog,
results.getData().getSchemeSpecificPart());
results.getData().getSchemeSpecificPart(), "");
return;
}

if (results.getScheme() != null
&& results.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
nativeOnFileSelected(mNativeSelectFileDialog, results.getData().toString());
if (ContentResolver.SCHEME_CONTENT.equals(results.getScheme())) {
nativeOnFileSelected(mNativeSelectFileDialog,
results.getData().toString(),
resolveFileName(results.getData(),
contentResolver));
return;
}

Expand Down Expand Up @@ -233,6 +258,6 @@ private static SelectFileDialog create(long nativeSelectFileDialog) {
}

private native void nativeOnFileSelected(long nativeSelectFileDialogImpl,
String filePath);
String filePath, String displayName);
private native void nativeOnFileNotSelected(long nativeSelectFileDialogImpl);
}
3 changes: 3 additions & 0 deletions ui/shell_dialogs/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
per-file *android*=aurimas@chromium.org
per-file *android*=miguelg@chromium.org
per-file *android*=qinmin@chromium.org
3 changes: 3 additions & 0 deletions ui/shell_dialogs/android/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
aurimas@chromium.org
miguelg@chromium.org
qinmin@chromium.org
14 changes: 12 additions & 2 deletions ui/shell_dialogs/select_file_dialog_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "base/strings/utf_string_conversions.h"
#include "jni/SelectFileDialog_jni.h"
#include "ui/base/android/window_android.h"
#include "ui/shell_dialogs/selected_file_info.h"

namespace ui {

Expand All @@ -25,10 +26,19 @@ SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,

void SelectFileDialogImpl::OnFileSelected(JNIEnv* env,
jobject java_object,
jstring filepath) {
jstring filepath,
jstring display_name) {
if (listener_) {
std::string path = base::android::ConvertJavaStringToUTF8(env, filepath);
listener_->FileSelected(base::FilePath(path), 0, NULL);
std::string file_name =
base::android::ConvertJavaStringToUTF8(env, display_name);
base::FilePath file_path = base::FilePath(path);
ui::SelectedFileInfo file_info;
file_info.file_path = file_path;
file_info.local_path = file_path;
if (!file_name.empty())
file_info.display_name = file_name;
listener_->FileSelectedWithExtraInfo(file_info, 0, NULL);
}

is_running_ = false;
Expand Down
5 changes: 4 additions & 1 deletion ui/shell_dialogs/select_file_dialog_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class SelectFileDialogImpl : public SelectFileDialog {
static SelectFileDialogImpl* Create(Listener* listener,
SelectFilePolicy* policy);

void OnFileSelected(JNIEnv* env, jobject java_object, jstring filepath);
void OnFileSelected(JNIEnv* env,
jobject java_object,
jstring filepath,
jstring display_name);
void OnFileNotSelected(JNIEnv* env, jobject java_object);

// From SelectFileDialog
Expand Down

0 comments on commit f226335

Please sign in to comment.