From: Bartek Przybylski Date: Sat, 26 May 2012 11:47:26 +0000 (+0200) Subject: making file download using progressbar view X-Git-Tag: oc-android-1.4.3~380 X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/commitdiff_plain/24a1111aa117f9d825646c23909e39a0d12f43b8 making file download using progressbar view --- 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/FileDownloader.java b/src/eu/alefzero/owncloud/FileDownloader.java deleted file mode 100644 index 1936e8ea..00000000 --- a/src/eu/alefzero/owncloud/FileDownloader.java +++ /dev/null @@ -1,139 +0,0 @@ -package eu.alefzero.owncloud; - -import java.io.File; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.app.Notification; -import android.app.NotificationManager; -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; -import android.os.IBinder; -import android.os.Looper; -import android.os.Message; -import android.os.Process; -import android.util.Log; -import eu.alefzero.owncloud.authenticator.AccountAuthenticator; -import eu.alefzero.owncloud.db.ProviderMeta.ProviderTableMeta; -import eu.alefzero.owncloud.ui.activity.FileDisplayActivity; -import eu.alefzero.owncloud.utils.OwnCloudVersion; -import eu.alefzero.webdav.WebdavClient; - -public class FileDownloader extends Service { - 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"; - private static final String TAG = "FileDownloader"; - - private NotificationManager nm; - private Looper mServiceLooper; - private ServiceHandler mServiceHandler; - private Account mAccount; - private String mFilePath; - - private final class ServiceHandler extends Handler { - public ServiceHandler(Looper looper) { - super(looper); - } - - @Override - public void handleMessage(Message msg) { - downloadFile(); - stopSelf(msg.arg1); - } - } - - @Override - public void onCreate() { - super.onCreate(); - nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - HandlerThread thread = new HandlerThread("FileDownladerThread", - Process.THREAD_PRIORITY_BACKGROUND); - thread.start(); - mServiceLooper = thread.getLooper(); - mServiceHandler = new ServiceHandler(mServiceLooper); - } - - @Override - public IBinder onBind(Intent arg0) { - return null; - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - if (!intent.hasExtra(EXTRA_ACCOUNT) - && !intent.hasExtra(EXTRA_FILE_PATH)) { - Log.e(TAG, "Not enough information provided in intent"); - return START_STICKY; - } - mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); - mFilePath = intent.getStringExtra(EXTRA_FILE_PATH); - Message msg = mServiceHandler.obtainMessage(); - msg.arg1 = startId; - mServiceHandler.sendMessage(msg); - - return START_NOT_STICKY; - } - - 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)); - - String username = mAccount.name.split("@")[0]; - String password = ""; - try { - password = am.blockingGetAuthToken(mAccount, - AccountAuthenticator.AUTH_TOKEN_TYPE, true); - } catch (Exception e) { - e.printStackTrace(); - return; - } - - wdc.setCredentials(username, password); - wdc.allowUnsignedCertificates(); - - 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); - - File sdCard = Environment.getExternalStorageDirectory(); - File dir = new File(sdCard.getAbsolutePath() + "/owncloud"); - dir.mkdirs(); - File file = new File(dir, mFilePath.replace('/', '.')); - - Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString()); - Log.e(TAG, mFilePath+""); - if (wdc.downloadFile(mFilePath, file)) { - ContentValues cv = new ContentValues(); - cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); - getContentResolver().update( - ProviderTableMeta.CONTENT_URI, - cv, - ProviderTableMeta.FILE_NAME + "=? AND " - + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", - new String[] { - mFilePath.substring(mFilePath.lastIndexOf('/') + 1), - mAccount.name }); - } - nm.cancel(1); - Intent end = new Intent(DOWNLOAD_FINISH_MESSAGE); - sendBroadcast(end); - } - -} 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/files/services/FileDownloader.java b/src/eu/alefzero/owncloud/files/services/FileDownloader.java new file mode 100644 index 00000000..087b103e --- /dev/null +++ b/src/eu/alefzero/owncloud/files/services/FileDownloader.java @@ -0,0 +1,166 @@ +package eu.alefzero.owncloud.files.services; + +import java.io.File; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.Notification; +import android.app.NotificationManager; +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; +import android.os.IBinder; +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 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 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) { + super(looper); + } + + @Override + public void handleMessage(Message msg) { + downloadFile(); + stopSelf(msg.arg1); + } + } + + @Override + public void onCreate() { + super.onCreate(); + mNotificationMngr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + HandlerThread thread = new HandlerThread("FileDownladerThread", + Process.THREAD_PRIORITY_BACKGROUND); + thread.start(); + mServiceLooper = thread.getLooper(); + mServiceHandler = new ServiceHandler(mServiceLooper); + } + + @Override + public IBinder onBind(Intent arg0) { + return null; + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + if (!intent.hasExtra(EXTRA_ACCOUNT) + && !intent.hasExtra(EXTRA_FILE_PATH)) { + Log.e(TAG, "Not enough information provided in intent"); + return START_STICKY; + } + mAccount = intent.getParcelableExtra(EXTRA_ACCOUNT); + mFilePath = intent.getStringExtra(EXTRA_FILE_PATH); + Message msg = mServiceHandler.obtainMessage(); + msg.arg1 = startId; + mServiceHandler.sendMessage(msg); + mCurrentDownlodSize = mLastPercent = 0; + mTotalDownloadSize = intent.getLongExtra(EXTRA_FILE_SIZE, -1); + + return START_NOT_STICKY; + } + + 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)); + + String username = mAccount.name.split("@")[0]; + String password = ""; + try { + password = am.blockingGetAuthToken(mAccount, + AccountAuthenticator.AUTH_TOKEN_TYPE, true); + } catch (Exception e) { + e.printStackTrace(); + return; + } + + wdc.setCredentials(username, password); + wdc.allowSelfsignedCertificates(); + wdc.setDataTransferProgressListener(this); + + mNotification = new Notification(R.drawable.icon, "Downloading file", System.currentTimeMillis()); + + 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"); + dir.mkdirs(); + File file = new File(dir, mFilePath.replace('/', '.')); + + Log.e(TAG, file.getAbsolutePath() + " " + oc_url.toString()); + Log.e(TAG, mFilePath+""); + if (wdc.downloadFile(mFilePath, file)) { + ContentValues cv = new ContentValues(); + cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getAbsolutePath()); + getContentResolver().update( + ProviderTableMeta.CONTENT_URI, + cv, + ProviderTableMeta.FILE_NAME + "=? AND " + + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?", + new String[] { + mFilePath.substring(mFilePath.lastIndexOf('/') + 1), + mAccount.name }); + } + 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;