From eb1c63d3dc5294e2ff7a7d2c32d762fd25286223 Mon Sep 17 00:00:00 2001 From: Lennart Rosam Date: Wed, 4 Jul 2012 01:06:27 +0200 Subject: [PATCH] Singleton didn't work (worth a try though ^^) - Make a new constructor instead --- src/eu/alefzero/owncloud/Uploader.java | 25 ++--------- .../authenticator/AuthenticationRunnable.java | 4 +- .../ConnectionCheckerRunnable.java | 2 +- .../files/services/FileDownloader.java | 15 +------ .../owncloud/files/services/FileUploader.java | 30 +++---------- .../files/services/InstantUploadService.java | 2 +- .../AbstractOwnCloudSyncAdapter.java | 9 +--- .../ui/activity/FileDisplayActivity.java | 3 +- src/eu/alefzero/webdav/WebdavClient.java | 42 +++++++++---------- 9 files changed, 36 insertions(+), 96 deletions(-) diff --git a/src/eu/alefzero/owncloud/Uploader.java b/src/eu/alefzero/owncloud/Uploader.java index 757f8030a7..fcb22a2ae6 100644 --- a/src/eu/alefzero/owncloud/Uploader.java +++ b/src/eu/alefzero/owncloud/Uploader.java @@ -28,15 +28,15 @@ import java.util.Vector; import android.accounts.Account; import android.accounts.AccountManager; import android.app.AlertDialog; +import android.app.AlertDialog.Builder; import android.app.Dialog; import android.app.ListActivity; import android.app.ProgressDialog; -import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; -import android.content.Intent; import android.content.DialogInterface.OnCancelListener; import android.content.DialogInterface.OnClickListener; +import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; @@ -46,21 +46,15 @@ import android.util.Log; import android.view.View; import android.view.Window; import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.EditText; -import android.widget.ListView; import android.widget.SimpleAdapter; -import android.widget.SimpleCursorAdapter; -import android.widget.AdapterView.OnItemClickListener; -import android.widget.Toast; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.datamodel.DataStorageManager; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; -import eu.alefzero.owncloud.db.ProviderMeta; -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; /** @@ -74,7 +68,6 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro private Account mAccount; private AccountManager mAccountManager; - private String mUsername, mPassword; private Stack mParents; private ArrayList mStreamsToUpload; private boolean mCreateDir; @@ -298,8 +291,6 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro } private void populateDirectoryList() { - mUsername = mAccount.name.substring(0, mAccount.name.indexOf('@')); - mPassword = mAccountManager.getPassword(mAccount); setContentView(R.layout.uploader_layout); String full_path = ""; @@ -378,15 +369,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro } public void uploadFiles() { - OwnCloudVersion ocv = new OwnCloudVersion(mAccountManager.getUserData(mAccount, - AccountAuthenticator.KEY_OC_VERSION)); - String base_url = mAccountManager.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); - String webdav_path = AccountUtils.getWebdavPath(ocv); - Uri oc_uri = Uri.parse(base_url + webdav_path); - - WebdavClient wdc = new WebdavClient(oc_uri); - wdc.setCredentials(mUsername, mPassword); - wdc.allowSelfsignedCertificates(); + WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext()); // create last directory in path if nessesary if (mCreateDir) { diff --git a/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java b/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java index f161c6a21d..691c93e66e 100644 --- a/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java +++ b/src/eu/alefzero/owncloud/authenticator/AuthenticationRunnable.java @@ -52,9 +52,7 @@ public class AuthenticationRunnable implements Runnable { public void run() { Uri uri; uri = Uri.parse(mUrl.toString()); - WebdavClient client = new WebdavClient(uri); - client.setCredentials(mUsername, mPassword); - int login_result = client.tryToLogin(); + int login_result = WebdavClient.tryToLogin(uri, mUsername, mPassword); switch (login_result) { case HttpStatus.SC_OK: postResult(true, uri.toString()); diff --git a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java index bc0ff78997..62131eff08 100644 --- a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java +++ b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java @@ -94,7 +94,7 @@ public class ConnectionCheckerRunnable implements Runnable { } private boolean tryConnection(Uri uri) { - WebdavClient wc = new WebdavClient(uri); + WebdavClient wc = new WebdavClient(); wc.allowSelfsignedCertificates(); GetMethod get = new GetMethod(uri.toString()); boolean retval = false; diff --git a/src/eu/alefzero/owncloud/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java index 734359edd0..d6d7780a72 100644 --- a/src/eu/alefzero/owncloud/files/services/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -11,7 +11,6 @@ import android.app.PendingIntent; import android.app.Service; import android.content.ContentValues; import android.content.Intent; -import android.net.Uri; import android.os.Environment; import android.os.Handler; import android.os.HandlerThread; @@ -21,14 +20,10 @@ import android.os.Message; import android.os.Process; import android.util.Log; import android.widget.RemoteViews; -import eu.alefzero.owncloud.AccountUtils; import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.R.drawable; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; -import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; -import eu.alefzero.owncloud.utils.OwnCloudVersion; import eu.alefzero.webdav.WebdavClient; public class FileDownloader extends Service implements OnDatatransferProgressListener { @@ -100,13 +95,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis void downloadFile() { AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE); - String oc_base_url = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL); - OwnCloudVersion ocv = new OwnCloudVersion(am - .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION)); - String webdav_path = AccountUtils.getWebdavPath(ocv); - Uri oc_url = Uri.parse(oc_base_url+webdav_path); - WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path)); + + WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext()); String username = mAccount.name.split("@")[0]; String password = ""; @@ -143,8 +134,6 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis e.printStackTrace(); } - Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString()); - Log.e(TAG, mFilePath+""); String message; if (wdc.downloadFile(mRemotePath, file)) { ContentValues cv = new ContentValues(); diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index 1e688b5e80..68ddafb7e8 100644 --- a/src/eu/alefzero/owncloud/files/services/FileUploader.java +++ b/src/eu/alefzero/owncloud/files/services/FileUploader.java @@ -1,16 +1,7 @@ package eu.alefzero.owncloud.files.services; import java.io.File; -import java.util.List; -import eu.alefzero.owncloud.AccountUtils; -import eu.alefzero.owncloud.R; -import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.datamodel.FileDataStorageManager; -import eu.alefzero.owncloud.datamodel.OCFile; -import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; -import eu.alefzero.owncloud.utils.OwnCloudVersion; -import eu.alefzero.webdav.WebdavClient; import android.accounts.Account; import android.accounts.AccountManager; import android.app.Notification; @@ -18,7 +9,6 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; -import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -29,6 +19,11 @@ import android.util.Log; import android.webkit.MimeTypeMap; import android.widget.RemoteViews; import android.widget.Toast; +import eu.alefzero.owncloud.R; +import eu.alefzero.owncloud.datamodel.FileDataStorageManager; +import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; +import eu.alefzero.webdav.WebdavClient; public class FileUploader extends Service implements OnDatatransferProgressListener { @@ -47,7 +42,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe private NotificationManager mNotificationManager; private Looper mServiceLooper; private ServiceHandler mServiceHandler; - private AccountManager mAccountManager; private Account mAccount; private String[] mLocalPaths, mRemotePaths; private int mUploadType; @@ -82,7 +76,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe thread.start(); mServiceLooper = thread.getLooper(); mServiceHandler = new ServiceHandler(mServiceLooper); - mAccountManager = AccountManager.get(this); } @Override @@ -134,15 +127,6 @@ public class FileUploader extends Service implements OnDatatransferProgressListe } public void uploadFile() { - String baseUrl = mAccountManager.getUserData(mAccount, - AccountAuthenticator.KEY_OC_BASE_URL), ocVerStr = mAccountManager - .getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION); - OwnCloudVersion ocVer = new OwnCloudVersion(ocVerStr); - String webdav_path = AccountUtils.getWebdavPath(ocVer); - Uri ocUri = Uri.parse(baseUrl + webdav_path); - String username = mAccount.name.substring(0, - mAccount.name.lastIndexOf('@')); - String password = mAccountManager.getPassword(mAccount); FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, getContentResolver()); mTotalDataToSend = mSendData = mPreviousPercent = 0; @@ -160,10 +144,8 @@ public class FileUploader extends Service implements OnDatatransferProgressListe mNotificationManager.notify(42, mNotification); - WebdavClient wc = new WebdavClient(ocUri); - wc.allowSelfsignedCertificates(); + WebdavClient wc = new WebdavClient(mAccount, getApplicationContext()); wc.setDataTransferProgressListener(this); - wc.setCredentials(username, password); for (int i = 0; i < mLocalPaths.length; ++i) { File f = new File(mLocalPaths[i]); diff --git a/src/eu/alefzero/owncloud/files/services/InstantUploadService.java b/src/eu/alefzero/owncloud/files/services/InstantUploadService.java index 9ac0411b31..1cf22c207c 100644 --- a/src/eu/alefzero/owncloud/files/services/InstantUploadService.java +++ b/src/eu/alefzero/owncloud/files/services/InstantUploadService.java @@ -139,7 +139,7 @@ public class InstantUploadService extends Service { String oc_version = am.getUserData(account, AccountAuthenticator.KEY_OC_VERSION); OwnCloudVersion ocv = new OwnCloudVersion(oc_version); String webdav_path = AccountUtils.getWebdavPath(ocv); - WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path)); + WebdavClient wdc = new WebdavClient(account, getApplicationContext()); wdc.allowSelfsignedCertificates(); wdc.setCredentials(username, password); diff --git a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java index 2b11978f4f..5ea4e3e7e2 100644 --- a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java @@ -149,18 +149,11 @@ public abstract class AbstractOwnCloudSyncAdapter extends protected WebdavClient getClient() throws OperationCanceledException, AuthenticatorException, IOException { if (mClient == null) { - String username = getAccount().name.split("@")[0]; - String password = this.getAccountManager().blockingGetAuthToken( - getAccount(), AccountAuthenticator.AUTH_TOKEN_TYPE, true); if (this.getAccountManager().getUserData(getAccount(), AccountAuthenticator.KEY_OC_URL) == null) { throw new UnknownHostException(); } - Uri uri = getUri(); - - mClient = new WebdavClient(uri); - mClient.setCredentials(username, password); - mClient.allowSelfsignedCertificates(); + mClient = new WebdavClient(account, getContext()); // mHost = mClient.getTargetHost(); } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index beb9f66621..07ca6f53d3 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -560,8 +560,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements @Override public void run() { - WebdavClient wdc = new WebdavClient(Uri.parse(mAm.getUserData( - mAccount, AccountAuthenticator.KEY_OC_URL))); + WebdavClient wdc = new WebdavClient(mAccount, getApplicationContext()); String username = mAccount.name.substring(0, mAccount.name.lastIndexOf('@')); diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index 797bad4dd7..3ed86c1725 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -59,31 +59,26 @@ public class WebdavClient extends HttpClient { private static HashMap clients = new HashMap(); /** - * Gets a WebdavClient setup for the current account + * Creates a WebdavClient setup for the current account * @param account The client accout * @param context The application context * @return */ - public static synchronized WebdavClient getInstance(Account account, Context context){ - WebdavClient instance = clients.get(account.name); - if(instance == null ){ - OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, - AccountAuthenticator.KEY_OC_VERSION)); - String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); - String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion); - WebdavClient client = new WebdavClient(); - - String username = account.name.substring(0, account.name.indexOf('@')); - String password = AccountManager.get(context).getPassword(account); - - client.mUri = Uri.parse(baseUrl + webDavPath); - client.getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); - client.setCredentials(username, password); - client.allowSelfsignedCertificates(); - clients.put(account.name, client); - } - return instance; + public WebdavClient (Account account, Context context){ + OwnCloudVersion ownCloudVersion = new OwnCloudVersion(AccountManager.get(context).getUserData(account, + AccountAuthenticator.KEY_OC_VERSION)); + String baseUrl = AccountManager.get(context).getUserData(account, AccountAuthenticator.KEY_OC_BASE_URL); + String webDavPath = AccountUtils.getWebdavPath(ownCloudVersion); + String username = account.name.substring(0, account.name.indexOf('@')); + String password = AccountManager.get(context).getPassword(account); + + mUri = Uri.parse(baseUrl + webDavPath); + getParams().setParameter(HttpMethodParams.USER_AGENT, USER_AGENT); + setCredentials(username, password); + allowSelfsignedCertificates(); } + + public WebdavClient(){} public void setCredentials(String username, String password) { getParams().setAuthenticationPreemptive(true); @@ -200,12 +195,13 @@ public class WebdavClient extends HttpClient { * @param password Password to verify * @return A {@link HttpStatus}-Code of the result. SC_OK is good. */ - public int tryToLogin(Uri uri, String username, String password) { + public static int tryToLogin(Uri uri, String username, String password) { int returnCode = 0; - setCredentials(username, password); + WebdavClient client = new WebdavClient(); + client.setCredentials(username, password); HeadMethod head = new HeadMethod(uri.toString()); try { - returnCode = executeMethod(head); + returnCode = client.executeMethod(head); } catch (Exception e) { Log.e(TAG, "Error: " + e.getMessage()); }