From 24a1111aa117f9d825646c23909e39a0d12f43b8 Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Sat, 26 May 2012 13:47:26 +0200 Subject: [PATCH] making file download using progressbar view --- AndroidManifest.xml | 5 +- res/menu/menu.xml | 10 ++-- src/eu/alefzero/owncloud/Uploader.java | 2 +- .../authenticator/ConnectionCheckerRunnable.java | 2 +- .../interfaces/OnDatatransferProgressListener.java | 6 +++ .../{ => files/services}/FileDownloader.java | 55 ++++++++++++++++------ .../owncloud/files/services/FileUploader.java | 2 +- .../syncadapter/AbstractOwnCloudSyncAdapter.java | 2 +- .../owncloud/ui/activity/FileDisplayActivity.java | 9 ++-- .../owncloud/ui/fragment/FileDetailFragment.java | 3 +- .../owncloud/ui/fragment/FileListFragment.java | 2 +- src/eu/alefzero/webdav/WebdavClient.java | 15 ++++-- 12 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 src/eu/alefzero/owncloud/files/interfaces/OnDatatransferProgressListener.java rename src/eu/alefzero/owncloud/{ => files/services}/FileDownloader.java (66%) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 68def42e..47f65c12 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -45,8 +45,7 @@ + android:theme="@style/Theme.ownCloud"> @@ -123,7 +122,7 @@ android:theme="@style/Theme.ownCloud.noActionBar" > - + diff --git a/res/menu/menu.xml b/res/menu/menu.xml index 590eabd6..5ec8ec0a 100644 --- a/res/menu/menu.xml +++ b/res/menu/menu.xml @@ -1,10 +1,10 @@ - - + + - - - + + + diff --git a/src/eu/alefzero/owncloud/Uploader.java b/src/eu/alefzero/owncloud/Uploader.java index a63010a6..a5d2316d 100644 --- a/src/eu/alefzero/owncloud/Uploader.java +++ b/src/eu/alefzero/owncloud/Uploader.java @@ -358,7 +358,7 @@ public class Uploader extends ListActivity implements OnItemClickListener, andro WebdavClient wdc = new WebdavClient(oc_uri); wdc.setCredentials(mUsername, mPassword); - wdc.allowUnsignedCertificates(); + wdc.allowSelfsignedCertificates(); // create last directory in path if nessesary if (mCreateDir) { diff --git a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java index 66c6c3d5..bc0ff789 100644 --- a/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java +++ b/src/eu/alefzero/owncloud/authenticator/ConnectionCheckerRunnable.java @@ -95,7 +95,7 @@ public class ConnectionCheckerRunnable implements Runnable { private boolean tryConnection(Uri uri) { WebdavClient wc = new WebdavClient(uri); - wc.allowUnsignedCertificates(); + wc.allowSelfsignedCertificates(); GetMethod get = new GetMethod(uri.toString()); boolean retval = false; try { diff --git a/src/eu/alefzero/owncloud/files/interfaces/OnDatatransferProgressListener.java b/src/eu/alefzero/owncloud/files/interfaces/OnDatatransferProgressListener.java new file mode 100644 index 00000000..4a897dd7 --- /dev/null +++ b/src/eu/alefzero/owncloud/files/interfaces/OnDatatransferProgressListener.java @@ -0,0 +1,6 @@ +package eu.alefzero.owncloud.files.interfaces; + +public interface OnDatatransferProgressListener { + void transferProgress(long progressRate); + +} diff --git a/src/eu/alefzero/owncloud/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java similarity index 66% rename from src/eu/alefzero/owncloud/FileDownloader.java rename to src/eu/alefzero/owncloud/files/services/FileDownloader.java index 1936e8ea..087b103e 100644 --- a/src/eu/alefzero/owncloud/FileDownloader.java +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -1,4 +1,4 @@ -package eu.alefzero.owncloud; +package eu.alefzero.owncloud.files.services; import java.io.File; @@ -19,23 +19,33 @@ import android.os.Looper; 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 { +public class FileDownloader extends Service implements OnDatatransferProgressListener { public static final String DOWNLOAD_FINISH_MESSAGE = "DOWNLOAD_FINISH"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; public static final String EXTRA_FILE_PATH = "FILE_PATH"; + public static final String EXTRA_FILE_SIZE = "FILE_SIZE"; private static final String TAG = "FileDownloader"; - private NotificationManager nm; + private NotificationManager mNotificationMngr; private Looper mServiceLooper; private ServiceHandler mServiceHandler; private Account mAccount; private String mFilePath; + private int mLastPercent; + private long mTotalDownloadSize; + private long mCurrentDownlodSize; + private Notification mNotification; private final class ServiceHandler extends Handler { public ServiceHandler(Looper looper) { @@ -52,7 +62,7 @@ public class FileDownloader extends Service { @Override public void onCreate() { super.onCreate(); - nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); HandlerThread thread = new HandlerThread("FileDownladerThread", Process.THREAD_PRIORITY_BACKGROUND); thread.start(); @@ -77,6 +87,8 @@ public class FileDownloader extends Service { Message msg = mServiceHandler.obtainMessage(); msg.arg1 = startId; mServiceHandler.sendMessage(msg); + mCurrentDownlodSize = mLastPercent = 0; + mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1); return START_NOT_STICKY; } @@ -90,7 +102,7 @@ public class FileDownloader extends Service { Uri oc_url = Uri.parse(oc_base_url+webdav_path); WebdavClient wdc = new WebdavClient(Uri.parse(oc_base_url + webdav_path)); - + String username = mAccount.name.split("@")[0]; String password = ""; try { @@ -102,15 +114,17 @@ public class FileDownloader extends Service { } wdc.setCredentials(username, password); - wdc.allowUnsignedCertificates(); + wdc.allowSelfsignedCertificates(); + wdc.setDataTransferProgressListener(this); + + mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis()); - Notification n = new Notification(R.drawable.icon, "Downloading file", - System.currentTimeMillis()); - PendingIntent pi = PendingIntent.getActivity(this, 1, new Intent(this, - FileDisplayActivity.class), 0); - n.setLatestEventInfo(this, "Downloading file", "Downloading file " - + mFilePath, pi); - nm.notify(1, n); + mNotification.flags |= Notification.FLAG_ONGOING_EVENT; + mNotification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.progressbar_layout); + mNotification.contentView.setProgressBar(R.id.status_progress, 100, 0, mTotalDownloadSize == -1); + mNotification.contentView.setImageViewResource(R.id.status_icon, R.drawable.icon); + + mNotificationMngr.notify(1, mNotification); File sdCard = Environment.getExternalStorageDirectory(); File dir = new File(sdCard.getAbsolutePath() + "/owncloud"); @@ -131,9 +145,22 @@ public class FileDownloader extends Service { mFilePath.substring(mFilePath.lastIndexOf('/') + 1), mAccount.name }); } - nm.cancel(1); + mNotificationMngr.cancel(1); Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); sendBroadcast(end); } + @Override + public void transferProgress(long progressRate) { + mCurrentDownlodSize += progressRate; + int percent = (int)(100.0*((double)mCurrentDownlodSize)/((double)mTotalDownloadSize)); + if (percent != mLastPercent) { + mNotification.contentView.setProgressBar(R.id.status_progress, 100, (int)(100*mCurrentDownlodSize/mTotalDownloadSize), mTotalDownloadSize == -1); + mNotification.contentView.setTextViewText(R.id.status_text, percent+"%"); + mNotificationMngr.notify(1, mNotification); + } + + mLastPercent = percent; + } + } diff --git a/src/eu/alefzero/owncloud/files/services/FileUploader.java b/src/eu/alefzero/owncloud/files/services/FileUploader.java index 30d91e1c..f6e1c515 100644 --- a/src/eu/alefzero/owncloud/files/services/FileUploader.java +++ b/src/eu/alefzero/owncloud/files/services/FileUploader.java @@ -147,7 +147,7 @@ public class FileUploader extends Service implements OnUploadProgressListener { mNotificationManager.notify(42, mNotification); WebdavClient wc = new WebdavClient(ocUri); - wc.allowUnsignedCertificates(); + wc.allowSelfsignedCertificates(); wc.setUploadListener(this); wc.setCredentials(username, password); diff --git a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java index efd4c2bd..2b11978f 100644 --- a/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java +++ b/src/eu/alefzero/owncloud/syncadapter/AbstractOwnCloudSyncAdapter.java @@ -160,7 +160,7 @@ public abstract class AbstractOwnCloudSyncAdapter extends mClient = new WebdavClient(uri); mClient.setCredentials(username, password); - mClient.allowUnsignedCertificates(); + mClient.allowSelfsignedCertificates(); // mHost = mClient.getTargetHost(); } diff --git a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java index 26953214..dc564d6f 100644 --- a/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java +++ b/src/eu/alefzero/owncloud/ui/activity/FileDisplayActivity.java @@ -104,12 +104,9 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements @Override public boolean onCreateOptionsMenu(Menu menu) { - if (accountsAreSetup()) { - MenuInflater inflater = getSherlock().getMenuInflater(); + MenuInflater inflater = getSherlock().getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; - } - return false; } @Override @@ -206,7 +203,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements @Override public void onBackPressed() { - if (mDirectories == null || mDirectories.getCount() == 1) { + if (mDirectories == null || mDirectories.getCount() <= 1) { finish(); return; } @@ -499,7 +496,7 @@ public class FileDisplayActivity extends SherlockFragmentActivity implements String password = mAm.getPassword(mAccount); wdc.setCredentials(username, password); - wdc.allowUnsignedCertificates(); + wdc.allowSelfsignedCertificates(); wdc.createDirectory(mTargetPath); } diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java index d2f5d106..2241f6f6 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileDetailFragment.java @@ -40,10 +40,10 @@ import android.widget.Toast; import com.actionbarsherlock.app.SherlockFragment; import eu.alefzero.owncloud.DisplayUtils; -import eu.alefzero.owncloud.FileDownloader; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.authenticator.AccountAuthenticator; import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.services.FileDownloader; import eu.alefzero.owncloud.utils.OwnCloudVersion; /** @@ -142,6 +142,7 @@ public class FileDetailFragment extends SherlockFragment implements i.putExtra(FileDownloader.EXTRA_ACCOUNT, mIntent.getParcelableExtra(FileDownloader.EXTRA_ACCOUNT)); i.putExtra(FileDownloader.EXTRA_FILE_PATH, mFile.getRemotePath()); + i.putExtra(FileDownloader.EXTRA_FILE_SIZE, mFile.getFileLength()); getActivity().startService(i); } diff --git a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java index d9ef3daa..d5dae1ca 100644 --- a/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java +++ b/src/eu/alefzero/owncloud/ui/fragment/FileListFragment.java @@ -30,11 +30,11 @@ import android.view.View; import android.widget.AdapterView; import android.widget.Toast; import eu.alefzero.owncloud.AccountUtils; -import eu.alefzero.owncloud.FileDownloader; import eu.alefzero.owncloud.R; import eu.alefzero.owncloud.datamodel.DataStorageManager; import eu.alefzero.owncloud.datamodel.FileDataStorageManager; import eu.alefzero.owncloud.datamodel.OCFile; +import eu.alefzero.owncloud.files.services.FileDownloader; import eu.alefzero.owncloud.ui.FragmentListView; import eu.alefzero.owncloud.ui.activity.FileDetailActivity; import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; diff --git a/src/eu/alefzero/webdav/WebdavClient.java b/src/eu/alefzero/webdav/WebdavClient.java index f7cc060e..c4e0a9be 100644 --- a/src/eu/alefzero/webdav/WebdavClient.java +++ b/src/eu/alefzero/webdav/WebdavClient.java @@ -38,6 +38,7 @@ import org.apache.http.HttpStatus; import org.apache.jackrabbit.webdav.client.methods.MkColMethod; import eu.alefzero.owncloud.authenticator.EasySSLSocketFactory; +import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener; import android.net.Uri; import android.util.Log; @@ -48,6 +49,7 @@ public class WebdavClient extends HttpClient { final private static String TAG = "WebdavClient"; private static final String USER_AGENT = "Android-ownCloud"; private OnUploadProgressListener mUploadProgressListener; + private OnDatatransferProgressListener mDataTransferListener; public WebdavClient(Uri uri) { mUri = uri; @@ -66,7 +68,7 @@ public class WebdavClient extends HttpClient { return mCredentials; } - public void allowUnsignedCertificates() { + public void allowSelfsignedCertificates() { // https Protocol.registerProtocol("https", new Protocol("https", new EasySSLSocketFactory(), 443)); @@ -99,10 +101,13 @@ public class WebdavClient extends HttpClient { get.getResponseBodyAsStream()); FileOutputStream fos = new FileOutputStream(targetPath); - byte[] bytes = new byte[512]; + byte[] bytes = new byte[4096]; int readResult; - while ((readResult = bis.read(bytes)) != -1) + while ((readResult = bis.read(bytes)) != -1) { + if (mDataTransferListener != null) + mDataTransferListener.transferProgress(readResult); fos.write(bytes, 0, readResult); + } } catch (IOException e) { e.printStackTrace(); @@ -115,6 +120,10 @@ public class WebdavClient extends HttpClient { mUploadProgressListener = listener; } + public void setDataTransferProgressListener(OnDatatransferProgressListener listener) { + mDataTransferListener = listener; + } + public boolean putFile(String localFile, String remoteTarget, String contentType) { boolean result = true; -- 2.11.0