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

@ -1,407 +1,428 @@
/* ownCloud Android client application /* ownCloud Android client application
* Copyright (C) 2012 Bartek Przybylski * Copyright (C) 2012 Bartek Przybylski
* Copyright (C) 2012-2013 ownCloud Inc. * Copyright (C) 2012-2013 ownCloud Inc.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or * the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
package com.owncloud.android; package com.owncloud.android;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; 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 android.accounts.Account;
import com.owncloud.android.datamodel.DataStorageManager; import android.accounts.AccountManager;
import com.owncloud.android.datamodel.FileDataStorageManager; import android.app.AlertDialog;
import com.owncloud.android.datamodel.OCFile; import android.app.AlertDialog.Builder;
import com.owncloud.android.files.services.FileUploader; import android.app.Dialog;
import com.owncloud.android.network.OwnCloudClientUtils; import android.app.ListActivity;
import android.app.ProgressDialog;
import android.accounts.Account; import android.content.Context;
import android.accounts.AccountManager; import android.content.DialogInterface;
import android.app.AlertDialog; import android.content.DialogInterface.OnCancelListener;
import android.app.AlertDialog.Builder; import android.content.DialogInterface.OnClickListener;
import android.app.Dialog; import android.content.Intent;
import android.app.ListActivity; import android.database.Cursor;
import android.app.ProgressDialog; import android.net.Uri;
import android.content.Context; import android.os.Bundle;
import android.content.DialogInterface; import android.os.Parcelable;
import android.content.DialogInterface.OnCancelListener; import android.provider.MediaStore.Audio;
import android.content.DialogInterface.OnClickListener; import android.provider.MediaStore.Images;
import android.content.Intent; import android.provider.MediaStore.Video;
import android.database.Cursor; import android.util.Log;
import android.net.Uri; import android.view.View;
import android.os.Bundle; import android.view.Window;
import android.os.Parcelable; import android.widget.AdapterView;
import android.provider.MediaStore.Images.Media; import android.widget.AdapterView.OnItemClickListener;
import android.util.Log; import android.widget.Button;
import android.view.View; import android.widget.EditText;
import android.view.Window; import android.widget.SimpleAdapter;
import android.widget.AdapterView; import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button; import com.owncloud.android.authenticator.AccountAuthenticator;
import android.widget.EditText; import com.owncloud.android.datamodel.DataStorageManager;
import android.widget.SimpleAdapter; import com.owncloud.android.datamodel.FileDataStorageManager;
import android.widget.Toast; import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.files.services.FileUploader;
import com.owncloud.android.R; import com.owncloud.android.network.OwnCloudClientUtils;
import eu.alefzero.webdav.WebdavClient;
import eu.alefzero.webdav.WebdavClient;
/**
* This can be used to upload things to an ownCloud instance. /**
* * This can be used to upload things to an ownCloud instance.
* @author Bartek Przybylski *
* * @author Bartek Przybylski
*/ *
public class Uploader extends ListActivity implements OnItemClickListener, android.view.View.OnClickListener { */
private static final String TAG = "ownCloudUploader"; public class Uploader extends ListActivity implements OnItemClickListener, android.view.View.OnClickListener {
private static final String TAG = "ownCloudUploader";
private Account mAccount;
private AccountManager mAccountManager; private Account mAccount;
private Stack<String> mParents; private AccountManager mAccountManager;
private ArrayList<Parcelable> mStreamsToUpload; private Stack<String> mParents;
private boolean mCreateDir; private ArrayList<Parcelable> mStreamsToUpload;
private String mUploadPath; private boolean mCreateDir;
private static final String[] CONTENT_PROJECTION = { Media.DATA, Media.DISPLAY_NAME, Media.MIME_TYPE, Media.SIZE }; private String mUploadPath;
private DataStorageManager mStorageManager; private DataStorageManager mStorageManager;
private OCFile mFile; private OCFile mFile;
private final static int DIALOG_NO_ACCOUNT = 0; private final static int DIALOG_NO_ACCOUNT = 0;
private final static int DIALOG_WAITING = 1; private final static int DIALOG_WAITING = 1;
private final static int DIALOG_NO_STREAM = 2; private final static int DIALOG_NO_STREAM = 2;
private final static int DIALOG_MULTIPLE_ACCOUNT = 3; private final static int DIALOG_MULTIPLE_ACCOUNT = 3;
//private final static int DIALOG_GET_DIRNAME = 4; //private final static int DIALOG_GET_DIRNAME = 4;
private final static int REQUEST_CODE_SETUP_ACCOUNT = 0; private final static int REQUEST_CODE_SETUP_ACCOUNT = 0;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE); getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mParents = new Stack<String>(); mParents = new Stack<String>();
mParents.add(""); mParents.add("");
/*if (getIntent().hasExtra(Intent.EXTRA_STREAM)) { /*if (getIntent().hasExtra(Intent.EXTRA_STREAM)) {
prepareStreamsToUpload();*/ prepareStreamsToUpload();*/
if (prepareStreamsToUpload()) { if (prepareStreamsToUpload()) {
mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE); mAccountManager = (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE); Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE);
if (accounts.length == 0) { if (accounts.length == 0) {
Log.i(TAG, "No ownCloud account is available"); Log.i(TAG, "No ownCloud account is available");
showDialog(DIALOG_NO_ACCOUNT); showDialog(DIALOG_NO_ACCOUNT);
} else if (accounts.length > 1) { } else if (accounts.length > 1) {
Log.i(TAG, "More then one ownCloud is available"); Log.i(TAG, "More then one ownCloud is available");
showDialog(DIALOG_MULTIPLE_ACCOUNT); showDialog(DIALOG_MULTIPLE_ACCOUNT);
} else { } else {
mAccount = accounts[0]; mAccount = accounts[0];
mStorageManager = new FileDataStorageManager(mAccount, getContentResolver()); mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
populateDirectoryList(); populateDirectoryList();
} }
} else { } else {
showDialog(DIALOG_NO_STREAM); showDialog(DIALOG_NO_STREAM);
} }
} }
@Override @Override
protected Dialog onCreateDialog(final int id) { protected Dialog onCreateDialog(final int id) {
final AlertDialog.Builder builder = new Builder(this); final AlertDialog.Builder builder = new Builder(this);
switch (id) { switch (id) {
case DIALOG_WAITING: case DIALOG_WAITING:
ProgressDialog pDialog = new ProgressDialog(this); ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(false); pDialog.setIndeterminate(false);
pDialog.setCancelable(false); pDialog.setCancelable(false);
pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading)); pDialog.setMessage(getResources().getString(R.string.uploader_info_uploading));
return pDialog; return pDialog;
case DIALOG_NO_ACCOUNT: case DIALOG_NO_ACCOUNT:
builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setTitle(R.string.uploader_wrn_no_account_title); builder.setTitle(R.string.uploader_wrn_no_account_title);
builder.setMessage(String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name))); builder.setMessage(String.format(getString(R.string.uploader_wrn_no_account_text), getString(R.string.app_name)));
builder.setCancelable(false); builder.setCancelable(false);
builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() { builder.setPositiveButton(R.string.uploader_wrn_no_account_setup_btn_text, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) { if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
// using string value since in API7 this // using string value since in API7 this
// constatn is not defined // constatn is not defined
// in API7 < this constatant is defined in // in API7 < this constatant is defined in
// Settings.ADD_ACCOUNT_SETTINGS // Settings.ADD_ACCOUNT_SETTINGS
// and Settings.EXTRA_AUTHORITIES // and Settings.EXTRA_AUTHORITIES
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS"); Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
intent.putExtra("authorities", new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE }); intent.putExtra("authorities", new String[] { AccountAuthenticator.AUTH_TOKEN_TYPE });
startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT); startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
} else { } else {
// since in API7 there is no direct call for // since in API7 there is no direct call for
// account setup, so we need to // account setup, so we need to
// show our own AccountSetupAcricity, get // show our own AccountSetupAcricity, get
// desired results and setup // desired results and setup
// everything for ourself // everything for ourself
Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class); Intent intent = new Intent(getBaseContext(), AccountAuthenticator.class);
startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT); startActivityForResult(intent, REQUEST_CODE_SETUP_ACCOUNT);
} }
} }
}); });
builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() { builder.setNegativeButton(R.string.uploader_wrn_no_account_quit_btn_text, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
finish(); finish();
} }
}); });
return builder.create(); return builder.create();
/*case DIALOG_GET_DIRNAME: case DIALOG_MULTIPLE_ACCOUNT:
final EditText dirName = new EditText(getBaseContext()); CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length];
builder.setView(dirName); for (int i = 0; i < ac.length; ++i) {
builder.setTitle(R.string.uploader_info_dirname); ac[i] = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[i].name;
String pathToUpload; }
if (mParents.empty()) { builder.setTitle(R.string.common_choose_account);
pathToUpload = "/"; builder.setItems(ac, new OnClickListener() {
} else { @Override
mCursor = managedQuery(Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, mParents.peek()), null, public void onClick(DialogInterface dialog, int which) {
null, null, null); mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[which];
mCursor.moveToFirst(); mStorageManager = new FileDataStorageManager(mAccount, getContentResolver());
pathToUpload = mCursor.getString(mCursor.getColumnIndex(ProviderTableMeta.FILE_PATH)) populateDirectoryList();
+ 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.setCancelable(true);
builder.setPositiveButton(R.string.common_ok, a); builder.setOnCancelListener(new OnCancelListener() {
builder.setNegativeButton(R.string.common_cancel, new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) { public void onCancel(DialogInterface dialog) {
dialog.cancel(); dialog.cancel();
} finish();
}); }
return builder.create();*/ });
case DIALOG_MULTIPLE_ACCOUNT: return builder.create();
CharSequence ac[] = new CharSequence[mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length]; case DIALOG_NO_STREAM:
for (int i = 0; i < ac.length; ++i) { builder.setIcon(android.R.drawable.ic_dialog_alert);
ac[i] = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[i].name; builder.setTitle(R.string.uploader_wrn_no_content_title);
} builder.setMessage(R.string.uploader_wrn_no_content_text);
builder.setTitle(R.string.common_choose_account); builder.setCancelable(false);
builder.setItems(ac, new OnClickListener() { builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
mAccount = mAccountManager.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[which]; finish();
mStorageManager = new FileDataStorageManager(mAccount, getContentResolver()); }
populateDirectoryList(); });
} return builder.create();
}); default:
builder.setCancelable(true); throw new IllegalArgumentException("Unknown dialog id: " + id);
builder.setOnCancelListener(new OnCancelListener() { }
@Override }
public void onCancel(DialogInterface dialog) {
dialog.cancel(); class a implements OnClickListener {
finish(); String mPath;
} EditText mDirname;
});
return builder.create(); public a(String path, EditText dirname) {
case DIALOG_NO_STREAM: mPath = path;
builder.setIcon(android.R.drawable.ic_dialog_alert); mDirname = dirname;
builder.setTitle(R.string.uploader_wrn_no_content_title); }
builder.setMessage(R.string.uploader_wrn_no_content_text);
builder.setCancelable(false); @Override
builder.setNegativeButton(R.string.common_cancel, new OnClickListener() { public void onClick(DialogInterface dialog, int which) {
@Override Uploader.this.mUploadPath = mPath + mDirname.getText().toString();
public void onClick(DialogInterface dialog, int which) { Uploader.this.mCreateDir = true;
finish(); uploadFiles();
} }
}); }
return builder.create();
default: @Override
throw new IllegalArgumentException("Unknown dialog id: " + id); public void onBackPressed() {
}
} if (mParents.size() <= 1) {
super.onBackPressed();
class a implements OnClickListener { return;
String mPath; } else {
EditText mDirname; mParents.pop();
populateDirectoryList();
public a(String path, EditText dirname) { }
mPath = path; }
mDirname = dirname;
} @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@Override // click on folder in the list
public void onClick(DialogInterface dialog, int which) { Log.d(TAG, "on item click");
Uploader.this.mUploadPath = mPath + mDirname.getText().toString(); Vector<OCFile> tmpfiles = mStorageManager.getDirectoryContent(mFile);
Uploader.this.mCreateDir = true; if (tmpfiles.size() <= 0) return;
uploadFiles(); // filter on dirtype
} Vector<OCFile> files = new Vector<OCFile>();
} for (OCFile f : tmpfiles)
if (f.isDirectory())
@Override files.add(f);
public void onBackPressed() { if (files.size() < position) {
throw new IndexOutOfBoundsException("Incorrect item selected");
if (mParents.size() <= 1) { }
super.onBackPressed(); mParents.push(files.get(position).getFileName());
return; populateDirectoryList();
} else { }
mParents.pop();
populateDirectoryList(); @Override
} public void onClick(View v) {
} // click on button
switch (v.getId()) {
@Override case R.id.uploader_choose_folder:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mUploadPath = ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix
// click on folder in the list for (String p : mParents)
Log.d(TAG, "on item click"); mUploadPath += p + OCFile.PATH_SEPARATOR;
Vector<OCFile> tmpfiles = mStorageManager.getDirectoryContent(mFile); Log.d(TAG, "Uploading file to dir " + mUploadPath);
if (tmpfiles.size() <= 0) return;
// filter on dirtype uploadFiles();
Vector<OCFile> files = new Vector<OCFile>();
for (OCFile f : tmpfiles) break;
if (f.isDirectory()) default:
files.add(f); throw new IllegalArgumentException("Wrong element clicked");
if (files.size() < position) { }
throw new IndexOutOfBoundsException("Incorrect item selected"); }
}
mParents.push(files.get(position).getFileName()); @Override
populateDirectoryList(); protected void onActivityResult(int requestCode, int resultCode, Intent data) {
} super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "result received. req: " + requestCode + " res: " + resultCode);
@Override if (requestCode == REQUEST_CODE_SETUP_ACCOUNT) {
public void onClick(View v) { dismissDialog(DIALOG_NO_ACCOUNT);
// click on button if (resultCode == RESULT_CANCELED) {
switch (v.getId()) { finish();
case R.id.uploader_choose_folder: }
mUploadPath = ""; // first element in mParents is root dir, represented by ""; init mUploadPath with "/" results in a "//" prefix Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.AUTH_TOKEN_TYPE);
for (String p : mParents) if (accounts.length == 0) {
mUploadPath += p + OCFile.PATH_SEPARATOR; showDialog(DIALOG_NO_ACCOUNT);
Log.d(TAG, "Uploading file to dir " + mUploadPath); } else {
// there is no need for checking for is there more then one
uploadFiles(); // account at this point
// since account setup can set only one account at time
break; mAccount = accounts[0];
/*case android.R.id.button1: // dynamic action for create aditional dir populateDirectoryList();
showDialog(DIALOG_GET_DIRNAME); }
break;*/ }
default: }
throw new IllegalArgumentException("Wrong element clicked");
} private void populateDirectoryList() {
} setContentView(R.layout.uploader_layout);
@Override String full_path = "";
protected void onActivityResult(int requestCode, int resultCode, Intent data) { for (String a : mParents)
super.onActivityResult(requestCode, resultCode, data); full_path += a + "/";
Log.i(TAG, "result received. req: " + requestCode + " res: " + resultCode);
if (requestCode == REQUEST_CODE_SETUP_ACCOUNT) { Log.d(TAG, "Populating view with content of : " + full_path);
dismissDialog(DIALOG_NO_ACCOUNT);
if (resultCode == RESULT_CANCELED) { mFile = mStorageManager.getFileByPath(full_path);
finish(); if (mFile != null) {
} Vector<OCFile> files = mStorageManager.getDirectoryContent(mFile);
Account[] accounts = mAccountManager.getAccountsByType(AccountAuthenticator.AUTH_TOKEN_TYPE); List<HashMap<String, Object>> data = new LinkedList<HashMap<String,Object>>();
if (accounts.length == 0) { for (OCFile f : files) {
showDialog(DIALOG_NO_ACCOUNT); HashMap<String, Object> h = new HashMap<String, Object>();
} else { if (f.isDirectory()) {
// there is no need for checking for is there more then one h.put("dirname", f.getFileName());
// account at this point data.add(h);
// since account setup can set only one account at time }
mAccount = accounts[0]; }
populateDirectoryList(); SimpleAdapter sa = new SimpleAdapter(this,
} data,
} R.layout.uploader_list_item_layout,
} new String[] {"dirname"},
new int[] {R.id.textView1});
private void populateDirectoryList() { setListAdapter(sa);
setContentView(R.layout.uploader_layout); Button btn = (Button) findViewById(R.id.uploader_choose_folder);
btn.setOnClickListener(this);
String full_path = ""; getListView().setOnItemClickListener(this);
for (String a : mParents) }
full_path += a + "/"; }
Log.d(TAG, "Populating view with content of : " + full_path); private boolean prepareStreamsToUpload() {
if (getIntent().getAction().equals(Intent.ACTION_SEND)) {
mFile = mStorageManager.getFileByPath(full_path); mStreamsToUpload = new ArrayList<Parcelable>();
if (mFile != null) { mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM));
Vector<OCFile> files = mStorageManager.getDirectoryContent(mFile); } else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
List<HashMap<String, Object>> data = new LinkedList<HashMap<String,Object>>(); mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (OCFile f : files) { }
HashMap<String, Object> h = new HashMap<String, Object>(); return (mStreamsToUpload != null && mStreamsToUpload.get(0) != null);
if (f.isDirectory()) { }
h.put("dirname", f.getFileName());
data.add(h); public void uploadFiles() {
} try {
} WebdavClient webdav = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext());
SimpleAdapter sa = new SimpleAdapter(this,
data, ArrayList<String> local = new ArrayList<String>();
R.layout.uploader_list_item_layout, ArrayList<String> remote = new ArrayList<String>();
new String[] {"dirname"},
new int[] {R.id.textView1}); // create last directory in path if necessary
setListAdapter(sa); if (mCreateDir) {
Button btn = (Button) findViewById(R.id.uploader_choose_folder); webdav.createDirectory(mUploadPath);
btn.setOnClickListener(this); }
getListView().setOnItemClickListener(this); // this checks the mimeType
} for (Parcelable mStream : mStreamsToUpload) {
}
Uri uri = (Uri) mStream;
private boolean prepareStreamsToUpload() { if (uri !=null) {
if (getIntent().getAction().equals(Intent.ACTION_SEND)) { if (uri.getScheme().equals("content")) {
mStreamsToUpload = new ArrayList<Parcelable>();
mStreamsToUpload.add(getIntent().getParcelableExtra(Intent.EXTRA_STREAM)); String mimeType = getContentResolver().getType(uri);
} else if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
mStreamsToUpload = getIntent().getParcelableArrayListExtra(Intent.EXTRA_STREAM); if (mimeType.contains("image")) {
} String[] CONTENT_PROJECTION = { Images.Media.DATA, Images.Media.DISPLAY_NAME, Images.Media.MIME_TYPE, Images.Media.SIZE};
return (mStreamsToUpload != null && mStreamsToUpload.get(0) != null); Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
} c.moveToFirst();
int index = c.getColumnIndex(Images.Media.DATA);
public void uploadFiles() { String data = c.getString(index);
try { local.add(data);
WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getApplicationContext()); remote.add(mUploadPath + c.getString(c.getColumnIndex(Images.Media.DISPLAY_NAME)));
// create last directory in path if necessary }
if (mCreateDir) { else if (mimeType.contains("video")) {
wdc.createDirectory(mUploadPath); 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();
String[] local = new String[mStreamsToUpload.size()], remote = new String[mStreamsToUpload.size()]; int index = c.getColumnIndex(Video.Media.DATA);
String data = c.getString(index);
for (int i = 0; i < mStreamsToUpload.size(); ++i) { local.add(data);
Uri uri = (Uri) mStreamsToUpload.get(i); remote.add(mUploadPath + c.getString(c.getColumnIndex(Video.Media.DISPLAY_NAME)));
if (uri.getScheme().equals("content")) {
Cursor c = getContentResolver().query((Uri) mStreamsToUpload.get(i), }
CONTENT_PROJECTION, else if (mimeType.contains("audio")) {
null, String[] CONTENT_PROJECTION = { Audio.Media.DATA, Audio.Media.DISPLAY_NAME, Audio.Media.MIME_TYPE, Audio.Media.SIZE };
null, Cursor c = getContentResolver().query(uri, CONTENT_PROJECTION, null, null, null);
null); c.moveToFirst();
int index = c.getColumnIndex(Audio.Media.DATA);
if (!c.moveToFirst()) String data = c.getString(index);
continue; local.add(data);
remote.add(mUploadPath + c.getString(c.getColumnIndex(Audio.Media.DISPLAY_NAME)));
final String display_name = c.getString(c.getColumnIndex(Media.DISPLAY_NAME)),
data = c.getString(c.getColumnIndex(Media.DATA)); }
local[i] = data; else {
remote[i] = mUploadPath + display_name; String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
} else if (uri.getScheme().equals("file")) { // cut everything whats before mnt. It occured to me that sometimes apps send their name into the URI
final File file = new File(Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "")); if (filePath.contains("mnt")) {
local[i] = file.getAbsolutePath(); String splitedFilePath[] = filePath.split("/mnt");
remote[i] = mUploadPath + file.getName(); filePath = splitedFilePath[1];
} }
final File file = new File(filePath);
} local.add(file.getAbsolutePath());
Intent intent = new Intent(getApplicationContext(), FileUploader.class); remote.add(mUploadPath + file.getName());
intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES); }
intent.putExtra(FileUploader.KEY_LOCAL_FILE, local);
intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote); } else if (uri.getScheme().equals("file")) {
intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount); String filePath = Uri.decode(uri.toString()).replace(uri.getScheme() + "://", "");
startService(intent); if (filePath.contains("mnt")) {
finish(); String splitedFilePath[] = filePath.split("/mnt");
filePath = splitedFilePath[1];
} catch (SecurityException e) { }
String message = String.format(getString(R.string.uploader_error_forbidden_content), getString(R.string.app_name)); final File file = new File(filePath);
Toast.makeText(this, message, Toast.LENGTH_LONG).show(); 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.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
intent.putExtra(FileUploader.KEY_LOCAL_FILE, local.toArray(new String[local.size()]));
intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote.toArray(new String[remote.size()]));
intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
startService(intent);
finish();
}
} catch (SecurityException e) {
String message = String.format(getString(R.string.uploader_error_forbidden_content), getString(R.string.app_name));
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
}