handle dir creation from filelist

This commit is contained in:
Bartek Przybylski 2012-04-08 00:25:51 +02:00
parent affc7d5bc8
commit d8d535a0a2
3 changed files with 108 additions and 2 deletions

View file

@ -2,5 +2,5 @@
<menu <menu
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Settings" android:icon="@android:drawable/ic_menu_preferences" android:id="@+id/settingsItem"></item> <item android:title="Settings" android:icon="@android:drawable/ic_menu_preferences" android:id="@+id/settingsItem"></item>
<item android:id="@+id/item1" android:title="Create Directory" android:icon="@android:drawable/ic_menu_add"></item> <item android:id="@+id/createDirectoryItem" android:title="Create Directory" android:icon="@android:drawable/ic_menu_add"></item>
</menu> </menu>

View file

@ -74,6 +74,28 @@ public class OCFile {
return new_file; return new_file;
} }
public static OCFile createNewFile(ContentResolver contentResolver, Account a,
String path, int length, int creation_timestamp, int modified_timestamp,
String mimetype, long parent_id) {
OCFile new_file = new OCFile(contentResolver, a);
Cursor c = new_file.cr_.query(ProviderTableMeta.CONTENT_URI_FILE, null,
ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND "
+ ProviderTableMeta.FILE_PATH + "=?", new String[]{new_file.account_.name,
path}, null);
if (c.moveToFirst())
new_file.setFileData(c);
c.close();
new_file.path_ = path;
new_file.length_ = length;
new_file.creation_timestamp_ = creation_timestamp;
new_file.modified_timestamp_ = modified_timestamp;
new_file.mimetype_ = mimetype;
new_file.parent_id_ = parent_id;
return new_file;
}
public OCFile(ContentResolver cr, Account account, long id) { public OCFile(ContentResolver cr, Account account, long id) {
cr_ = cr; cr_ = cr;
account_ = account; account_ = account;
@ -245,6 +267,12 @@ public class OCFile {
resetData(); resetData();
} }
private OCFile(ContentResolver cr, Account account) {
account_ = account;
cr_ = cr;
resetData();
}
private void resetData() { private void resetData() {
id_ = -1; id_ = -1;
path_ = null; path_ = null;

View file

@ -22,9 +22,12 @@ import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.ActionBar; import android.support.v4.app.ActionBar;
import android.support.v4.app.ActionBar.OnNavigationListener; import android.support.v4.app.ActionBar.OnNavigationListener;
@ -32,10 +35,12 @@ import android.support.v4.view.Menu;
import android.support.v4.view.MenuItem; import android.support.v4.view.MenuItem;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.EditText;
import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.R;
import eu.alefzero.owncloud.R.id;
import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.authenticator.AccountAuthenticator;
import eu.alefzero.owncloud.datamodel.OCFile;
import eu.alefzero.owncloud.ui.fragment.FileList; import eu.alefzero.owncloud.ui.fragment.FileList;
import eu.alefzero.webdav.WebdavClient;
/** /**
* Displays, what files the user has available in his ownCloud. * Displays, what files the user has available in his ownCloud.
@ -57,6 +62,46 @@ public class FileDisplayActivity extends android.support.v4.app.FragmentActivity
return !mDirectories.isEmpty(); return !mDirectories.isEmpty();
} }
@Override
protected Dialog onCreateDialog(int id, Bundle args) {
final AlertDialog.Builder builder = new Builder(this);
final EditText dirName = new EditText(getBaseContext());
builder.setView(dirName);
builder.setTitle(R.string.uploader_info_dirname);
builder.setPositiveButton(R.string.common_ok, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String s = dirName.getText().toString();
if (s.trim().isEmpty()) {
dialog.cancel();
return;
}
AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
// following account choosing is incorrect and needs to be replaced
// with some sort of session mechanism
Account a = am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)[0];
String path = "";
for (int i = mDirectories.getCount()-2; i >= 0; --i) {
path += "/" + mDirectories.getItem(i);
}
OCFile parent = new OCFile(getContentResolver(), a, path+"/");
path += "/" + s + "/";
Thread thread = new Thread(new DirectoryCreator(path, a, am));
thread.start();
OCFile.createNewFile(getContentResolver(), a, path, 0, 0, 0, "DIR", parent.getFileId()).save();
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.common_cancel, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
return builder.create();
}
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -73,9 +118,16 @@ public class FileDisplayActivity extends android.support.v4.app.FragmentActivity
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.settingsItem : case R.id.settingsItem :
{
Intent i = new Intent(this, Preferences.class); Intent i = new Intent(this, Preferences.class);
startActivity(i); startActivity(i);
break; break;
}
case R.id.createDirectoryItem:
{
showDialog(0);
break;
}
} }
return true; return true;
} }
@ -142,4 +194,30 @@ public class FileDisplayActivity extends android.support.v4.app.FragmentActivity
} }
((FileList)getSupportFragmentManager().findFragmentById(R.id.fileList)).onBackPressed(); ((FileList)getSupportFragmentManager().findFragmentById(R.id.fileList)).onBackPressed();
} }
private class DirectoryCreator implements Runnable {
private String mTargetPath;
private Account mAccount;
private AccountManager mAm;
public DirectoryCreator(String targetPath, Account account, AccountManager am) {
mTargetPath = targetPath;
mAccount = account;
mAm = am;
}
@Override
public void run() {
WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData(mAccount,
AccountAuthenticator.KEY_OC_URL)));
String username = mAccount.name.substring(0, mAccount.name.lastIndexOf('@'));
String password = mAm.getPassword(mAccount);
wdc.setCredentials(username, password);
wdc.allowUnsignedCertificates();
wdc.createDirectory(mTargetPath);
}
}
} }