Fix for :

Issue #131 &  Issue #132 : 
Crash when sharing contents from other apps to ownCloud
This commit is contained in:
zerginator 2013-04-28 11:33:20 +02:00
parent 92dc4c568c
commit 002e1bf9e3

View file

@ -26,13 +26,6 @@ import java.util.List;
import java.util.Stack; import java.util.Stack;
import java.util.Vector; import java.util.Vector;
import com.owncloud.android.authenticator.AccountAuthenticator;
import com.owncloud.android.datamodel.DataStorageManager;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.network.OwnCloudClientUtils;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.app.AlertDialog; import android.app.AlertDialog;
@ -49,7 +42,9 @@ import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import android.provider.MediaStore.Images.Media; import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Video;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.Window; import android.view.Window;
@ -60,7 +55,13 @@ import android.widget.EditText;
import android.widget.SimpleAdapter; import android.widget.SimpleAdapter;
import android.widget.Toast; import android.widget.Toast;
import com.owncloud.android.R; import com.owncloud.android.authenticator.AccountAuthenticator;
import com.owncloud.android.datamodel.DataStorageManager;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.network.OwnCloudClientUtils;
import eu.alefzero.webdav.WebdavClient; import eu.alefzero.webdav.WebdavClient;
/** /**
@ -78,7 +79,6 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
private ArrayList<Parcelable> mStreamsToUpload; private ArrayList<Parcelable> mStreamsToUpload;
private boolean mCreateDir; private boolean mCreateDir;
private String mUploadPath; private String mUploadPath;
private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE };
private DataStorageManager mStorageManager; private DataStorageManager mStorageManager;
private OCFile mFile; private OCFile mFile;
@ -162,28 +162,6 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
} }
}); });
return builder.create(); return builder.create();
/*case DIALOG_GET_DIRNAME:
final EditText dirName = new EditText(getBaseContext());
builder.setView(dirName);
builder.setTitle(R.string.uploader_info_dirname);
String pathToUpload;
if (mParents.empty()) {
pathToUpload = "/";
} else {
mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, mParents.peek()), null,
null, null, null);
mCursor.moveToFirst();
pathToUpload = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH))
+ mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_NAME)).replace(" ", "%20"); // TODO don't make this ; use WebdavUtils.encode in the right moment
}
a a = new a(pathToUpload, dirName);
builder.setPositiveButton(R.string.common_ok, a);
builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();*/
case DIALOG_MULTIPLE_ACCOUNT: case DIALOG_MULTIPLE_ACCOUNT:
CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length]; CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
for (int i = 0; i < ac.length; ++i) { for (int i = 0; i < ac.length; ++i) {
@ -284,9 +262,6 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
uploadFiles(); uploadFiles();
break; break;
/*case android.R.id.button1: // dynamic action for create aditional dir
showDialog(DIALOG_GET_DIRNAME);
break;*/
default: default:
throw new IllegalArgumentException("Wrong element clicked"); throw new IllegalArgumentException("Wrong element clicked");
} }
@ -358,46 +333,92 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro
public void uploadFiles() { public void uploadFiles() {
try { try {
WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext()); WebdavClient webdav = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
ArrayList<String> local = new ArrayList<String>();
ArrayList<String> remote = new ArrayList<String>();
// create last directory in path if necessary // create last directory in path if necessary
if (mCreateDir) { if (mCreateDir) {
wdc.createDirectory(mUploadPath); webdav.createDirectory(mUploadPath);
} }
// this checks the mimeType
for (Parcelable mStream : mStreamsToUpload) {
String[] local = new String[mStreamsToUpload.size()], remote = new String[mStreamsToUpload.size()]; Uri uri = (Uri) mStream;
if (uri !=null) {
for (int i = 0; i < mStreamsToUpload.size(); ++i) {
Uri uri = (Uri) mStreamsToUpload.get(i);
if (uri.getScheme().equals("content")) { if (uri.getScheme().equals("content")) {
Cursor c = getContentResolver().query((Uri) mStreamsToUpload.get(i),
CONTENT_PROJECTION,
null,
null,
null);
if (!c.moveToFirst()) String mimeType = getContentResolver().getType(uri);
continue;
if (mimeType.contains("image")) {
String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE};
Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
c.moveToFirst();
int index = c.getColumnIndex(Images.Media.DATA);
String data = c.getString(index);
local.add(data);
remote.add(mUploadPath + c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME)));
}
else if (mimeType.contains("video")) {
String[] CONTENT_PROJECTION = { Video.Media.DATA, Video.Media.DISPLAY_NAME, Video.Media.MIME_TYPE, Video.Media.SIZE, Video.Media.DATE_MODIFIED };
Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
c.moveToFirst();
int index = c.getColumnIndex(Video.Media.DATA);
String data = c.getString(index);
local.add(data);
remote.add(mUploadPath + c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME)));
}
else if (mimeType.contains("audio")) {
String[] CONTENT_PROJECTION = { Audio.Media.DATA, Audio.Media.DISPLAY_NAME, Audio.Media.MIME_TYPE, Audio.Media.SIZE };
Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
c.moveToFirst();
int index = c.getColumnIndex(Audio.Media.DATA);
String data = c.getString(index);
local.add(data);
remote.add(mUploadPath + c.getString(c.getColumnIndex(Audio.Media.DISPLAY_NAME)));
}
else {
String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
// cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
if (filePath.contains("mnt")) {
String splitedFilePath[] = filePath.split("/mnt");
filePath = splitedFilePath[1];
}
final File file = new File(filePath);
local.add(file.getAbsolutePath());
remote.add(mUploadPath + file.getName());
}
final String display_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
data = c.getString(c.getColumnIndex(Media.DATA));
local[i] = data;
remote[i] = mUploadPath + display_name;
} else if (uri.getScheme().equals("file")) { } else if (uri.getScheme().equals("file")) {
final File file = new File(Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "")); String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
local[i] = file.getAbsolutePath(); if (filePath.contains("mnt")) {
remote[i] = mUploadPath + file.getName(); String splitedFilePath[] = filePath.split("/mnt");
filePath = splitedFilePath[1];
}
final File file = new File(filePath);
local.add(file.getAbsolutePath());
remote.add(mUploadPath + file.getName());
}
else {
throw new SecurityException();
}
}
else {
throw new SecurityException();
} }
}
Intent intent = new Intent(getApplicationContext(), FileUploader.class); Intent intent = new Intent(getApplicationContext(), FileUploader.class);
intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES); intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
intent.putExtra(FileUploader.KEY_LOCAL_FILE, local); intent.putExtra(FileUploader.KEY_LOCAL_FILE, local.toArray(new String[local.size()]));
intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote); intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote.toArray(new String[remote.size()]));
intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount); intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
startService(intent); startService(intent);
finish(); finish();
}
} catch (SecurityException e) { } catch (SecurityException e) {
String message = String.format(getString(R.string.uploader_error_forbidden_content), getString(R.string.app_name)); String message = String.format(getString(R.string.uploader_error_forbidden_content), getString(R.string.app_name));
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); Toast.makeText(this, message, Toast.LENGTH_LONG).show();