make uploader acrtivity use uploader service

This commit is contained in:
Bartek Przybylski 2012-05-17 22:27:29 +02:00
parent 54909e5372
commit 9931f93b9a

View file

@ -57,6 +57,8 @@ import android.widget.AdapterView.OnItemClickListener;
import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
import eu.alefzero.owncloud.db.ProviderMeta; import eu.alefzero.owncloud.db.ProviderMeta;
import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta;
import eu.alefzero.owncloud.files.services.FileUploader;
import eu.alefzero.owncloud.utils.OwnCloudVersion;
import eu.alefzero.webdav.WebdavClient; import eu.alefzero.webdav.WebdavClient;
import eu.alefzero.webdav.WebdavUtils; import eu.alefzero.webdav.WebdavUtils;
@ -78,6 +80,12 @@ public class Uploader extends ListActivity implements OnItemClickListener,
private Thread mUploadThread; private Thread mUploadThread;
private Handler mHandler; private Handler mHandler;
private ArrayList<Parcelable> mStreamsToUpload; private ArrayList<Parcelable> mStreamsToUpload;
private boolean mCreateDir;
private String mUploadPath;
private static final String[] CONTENT_PROJECTION = {Media.DATA,
Media.DISPLAY_NAME,
Media.MIME_TYPE,
Media.SIZE};
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;
@ -234,11 +242,9 @@ public class Uploader extends ListActivity implements OnItemClickListener,
} }
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
showDialog(DIALOG_WAITING); Uploader.this.mUploadPath = mPath + mDirname.getText().toString();
mUploadThread = new Thread(new BackgroundUploader(mPath Uploader.this.mCreateDir = true;
+ mDirname.getText().toString(), mStreamsToUpload, uploadFiles();
mHandler, true));
mUploadThread.start();
} }
} }
@ -307,10 +313,9 @@ public class Uploader extends ListActivity implements OnItemClickListener,
} }
Log.d(TAG, "Uploading file to dir " + pathToUpload); Log.d(TAG, "Uploading file to dir " + pathToUpload);
showDialog(DIALOG_WAITING); mUploadPath = pathToUpload;
mUploadThread = new Thread(new BackgroundUploader(pathToUpload, mCreateDir = false;
mStreamsToUpload, mHandler)); uploadFiles();
mUploadThread.start();
break; break;
case android.R.id.button1: // dynamic action for create aditional dir case android.R.id.button1: // dynamic action for create aditional dir
@ -394,6 +399,7 @@ public class Uploader extends ListActivity implements OnItemClickListener,
setListAdapter(sca); setListAdapter(sca);
Button btn = (Button) findViewById(R.id.uploader_choose_folder); Button btn = (Button) findViewById(R.id.uploader_choose_folder);
btn.setOnClickListener(this); btn.setOnClickListener(this);
/* disable this until new server interaction service wont be created
// insert create new directory for multiple items uploading // insert create new directory for multiple items uploading
if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) { if (getIntent().getAction().equals(Intent.ACTION_SEND_MULTIPLE)) {
Button createDirBtn = new Button(this); Button createDirBtn = new Button(this);
@ -403,7 +409,7 @@ public class Uploader extends ListActivity implements OnItemClickListener,
((LinearLayout) findViewById(R.id.linearLayout1)).addView( ((LinearLayout) findViewById(R.id.linearLayout1)).addView(
createDirBtn, LayoutParams.FILL_PARENT, createDirBtn, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT); LayoutParams.WRAP_CONTENT);
} }*/
} }
} }
@ -447,32 +453,14 @@ public class Uploader extends ListActivity implements OnItemClickListener,
getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv); getContentResolver().insert(ProviderTableMeta.CONTENT_URI_FILE, cv);
} }
class BackgroundUploader implements Runnable { public void uploadFiles() {
private ArrayList<Parcelable> mUploadStreams; OwnCloudVersion ocv = new OwnCloudVersion(mAccountManager
private Handler mHandler; .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
private String mUploadPath; String base_url = mAccountManager.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
private boolean mCreateDir; String webdav_path = AccountUtils.getWebdavPath(ocv);
Uri oc_uri = Uri.parse(base_url+webdav_path);
public BackgroundUploader(String pathToUpload,
ArrayList<Parcelable> streamsToUpload, Handler handler) { WebdavClient wdc = new WebdavClient(oc_uri);
mUploadStreams = streamsToUpload;
mHandler = handler;
mUploadPath = pathToUpload.replace(" ", "%20");
mCreateDir = false;
}
public BackgroundUploader(String pathToUpload,
ArrayList<Parcelable> streamsToUpload, Handler handler,
boolean createDir) {
mUploadStreams = streamsToUpload;
mHandler = handler;
mUploadPath = pathToUpload.replace(" ", "%20");
mCreateDir = createDir;
}
public void run() {
WebdavClient wdc = new WebdavClient(Uri.parse(mAccountManager
.getUserData(mAccount, AccountAuthenticator.KEY_OC_URL)));
wdc.setCredentials(mUsername, mPassword); wdc.setCredentials(mUsername, mPassword);
wdc.allowUnsignedCertificates(); wdc.allowUnsignedCertificates();
@ -480,50 +468,26 @@ public class Uploader extends ListActivity implements OnItemClickListener,
if (mCreateDir) { if (mCreateDir) {
wdc.createDirectory(mUploadPath); wdc.createDirectory(mUploadPath);
} }
String[] local = new String[mStreamsToUpload.size()],
remote = new String[mStreamsToUpload.size()];
for (int i = 0; i < mUploadStreams.size(); ++i) { for (int i = 0; i < mStreamsToUpload.size(); ++i) {
Uri uri = (Uri) mUploadStreams.get(i); Uri uri = (Uri) mStreamsToUpload.get(i);
if (uri.getScheme().equals("content")) { if (uri.getScheme().equals("content")) {
final Cursor c = getContentResolver() Cursor c = getContentResolver().query((Uri) mStreamsToUpload.get(i),
.query((Uri) mUploadStreams.get(i), null, null, CONTENT_PROJECTION,
null, null); null,
null,
null);
if (!c.moveToFirst()) continue;
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;
if (!wdc.putFile(
c.getString(c.getColumnIndex(Media.DATA)),
mUploadPath
+ "/"
+ c.getString(c
.getColumnIndex(Media.DISPLAY_NAME)),
c.getString(c.getColumnIndex(Media.MIME_TYPE)))) {
mHandler.post(new Runnable() {
public void run() {
Uploader.this
.onUploadComplete(
false,
"Error while uploading file: "
+ c.getString(c
.getColumnIndex(Media.DISPLAY_NAME)));
}
});
} else {
mHandler.post(new Runnable() {
public void run() {
Uploader.this.PartialupdateUpload(
c.getString(c
.getColumnIndex(Media.DATA)),
c.getString(c
.getColumnIndex(Media.DISPLAY_NAME)),
mUploadPath
+ "/"
+ c.getString(c
.getColumnIndex(Media.DISPLAY_NAME)),
c.getString(c
.getColumnIndex(Media.MIME_TYPE)),
c.getString(c
.getColumnIndex(Media.SIZE)));
}
});
}
} else if (uri.getScheme().equals("file")) { } else if (uri.getScheme().equals("file")) {
final File file = new File(Uri.decode(uri.toString()) final File file = new File(Uri.decode(uri.toString())
.replace(uri.getScheme() + "://", "")); .replace(uri.getScheme() + "://", ""));
@ -547,13 +511,15 @@ public class Uploader extends ListActivity implements OnItemClickListener,
} }
} }
mHandler.post(new Runnable() { Intent intent = new Intent(getApplicationContext(), FileUploader.class);
public void run() { intent.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_MULTIPLE_FILES);
Uploader.this.onUploadComplete(true, null); intent.putExtra(FileUploader.KEY_LOCAL_FILE, local);
} intent.putExtra(FileUploader.KEY_REMOTE_FILE, remote);
}); intent.putExtra(FileUploader.KEY_ACCOUNT, mAccount);
startService(intent);
finish();
} }
}
} }