2013-02-25 17:33:16 +04:00
|
|
|
/* ownCloud Android client application
|
2013-02-25 18:19:18 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2013 ownCloud Inc.
|
2013-02-25 17:33:16 +04:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2013-02-26 16:17:44 +04:00
|
|
|
package com.owncloud.android.ui.preview;
|
2013-02-25 17:33:16 +04:00
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.actionbarsherlock.app.SherlockFragment;
|
|
|
|
import com.owncloud.android.datamodel.FileDataStorageManager;
|
|
|
|
import com.owncloud.android.datamodel.OCFile;
|
|
|
|
import com.owncloud.android.files.services.FileDownloader;
|
|
|
|
import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder;
|
2013-02-26 16:17:44 +04:00
|
|
|
import com.owncloud.android.ui.fragment.FileFragment;
|
2013-02-25 17:33:16 +04:00
|
|
|
|
|
|
|
import com.owncloud.android.R;
|
|
|
|
|
|
|
|
import eu.alefzero.webdav.OnDatatransferProgressListener;
|
|
|
|
|
|
|
|
/**
|
2013-02-25 18:19:18 +04:00
|
|
|
* This Fragment is used to monitor the progress of a file downloading.
|
2013-02-25 17:33:16 +04:00
|
|
|
*
|
|
|
|
* @author David A. Velasco
|
|
|
|
*/
|
2013-02-26 22:26:45 +04:00
|
|
|
public class FileDownloadFragment extends SherlockFragment implements OnClickListener, FileFragment {
|
2013-02-25 17:33:16 +04:00
|
|
|
|
|
|
|
public static final String EXTRA_FILE = "FILE";
|
|
|
|
public static final String EXTRA_ACCOUNT = "ACCOUNT";
|
|
|
|
|
|
|
|
private FileFragment.ContainerActivity mContainerActivity;
|
|
|
|
|
|
|
|
private View mView;
|
|
|
|
private OCFile mFile;
|
|
|
|
private Account mAccount;
|
|
|
|
private FileDataStorageManager mStorageManager;
|
|
|
|
|
|
|
|
private DownloadFinishReceiver mDownloadFinishReceiver;
|
|
|
|
public ProgressListener mProgressListener;
|
2013-02-26 15:33:28 +04:00
|
|
|
private boolean mListening;
|
2013-02-25 17:33:16 +04:00
|
|
|
|
|
|
|
private static final String TAG = FileDownloadFragment.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an empty details fragment.
|
|
|
|
*
|
|
|
|
* It's necessary to keep a public constructor without parameters; the system uses it when tries to reinstantiate a fragment automatically.
|
|
|
|
*/
|
|
|
|
public FileDownloadFragment() {
|
|
|
|
mFile = null;
|
|
|
|
mAccount = null;
|
|
|
|
mStorageManager = null;
|
|
|
|
mProgressListener = null;
|
2013-02-26 15:33:28 +04:00
|
|
|
mListening = false;
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a details fragment.
|
|
|
|
*
|
|
|
|
* When 'fileToDetail' or 'ocAccount' are null, creates a dummy layout (to use when a file wasn't tapped before).
|
|
|
|
*
|
|
|
|
* @param fileToDetail An {@link OCFile} to show in the fragment
|
|
|
|
* @param ocAccount An ownCloud account; needed to start downloads
|
|
|
|
*/
|
|
|
|
public FileDownloadFragment(OCFile fileToDetail, Account ocAccount) {
|
|
|
|
mFile = fileToDetail;
|
|
|
|
mAccount = ocAccount;
|
|
|
|
mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment
|
|
|
|
mProgressListener = null;
|
2013-02-26 15:33:28 +04:00
|
|
|
mListening = false;
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONCREATE " + ((mFile == null)? "(NULL)" : mFile.getFileName()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
mFile = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE);
|
|
|
|
mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
View view = null;
|
2013-02-25 18:19:18 +04:00
|
|
|
view = inflater.inflate(R.layout.file_download_fragment, container, false);
|
2013-02-25 17:33:16 +04:00
|
|
|
mView = view;
|
|
|
|
|
2013-02-26 15:33:28 +04:00
|
|
|
ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar);
|
2013-02-25 18:19:18 +04:00
|
|
|
mProgressListener = new ProgressListener(progressBar);
|
2013-02-25 17:33:16 +04:00
|
|
|
|
2013-02-26 22:26:45 +04:00
|
|
|
((Button)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this);
|
|
|
|
|
2013-02-25 17:33:16 +04:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
2013-02-26 15:33:28 +04:00
|
|
|
Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONATTACH " + ((mFile == null)?" (NULL)":mFile.getFileName()));
|
2013-02-25 17:33:16 +04:00
|
|
|
try {
|
|
|
|
mContainerActivity = (ContainerActivity) activity;
|
|
|
|
|
|
|
|
} catch (ClassCastException e) {
|
2013-02-25 18:19:18 +04:00
|
|
|
throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName());
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onActivityCreated(Bundle savedInstanceState) {
|
|
|
|
super.onActivityCreated(savedInstanceState);
|
2013-02-26 15:33:28 +04:00
|
|
|
Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONACTIVITYCREATED " + ((mFile == null)?" (NULL)":mFile.getFileName()));
|
2013-02-25 17:33:16 +04:00
|
|
|
if (mAccount != null) {
|
|
|
|
mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver());;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putParcelable(FileDownloadFragment.EXTRA_FILE, mFile);
|
|
|
|
outState.putParcelable(FileDownloadFragment.EXTRA_ACCOUNT, mAccount);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStart() {
|
|
|
|
super.onStart();
|
|
|
|
Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTART " + mFile.getFileName());
|
|
|
|
listenForTransferProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONRESUME " + mFile.getFileName());
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
mDownloadFinishReceiver = new DownloadFinishReceiver();
|
|
|
|
IntentFilter filter = new IntentFilter(
|
|
|
|
FileDownloader.DOWNLOAD_FINISH_MESSAGE);
|
|
|
|
getActivity().registerReceiver(mDownloadFinishReceiver, filter);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPause() {
|
|
|
|
super.onPause();
|
|
|
|
Log.e(TAG, "PREVIEW_DOWNLOAD_FRAGMENT ONPAUSE " + mFile.getFileName());
|
|
|
|
|
|
|
|
getActivity().unregisterReceiver(mDownloadFinishReceiver);
|
|
|
|
mDownloadFinishReceiver = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onStop() {
|
|
|
|
super.onStop();
|
|
|
|
Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONSTOP " + mFile.getFileName());
|
|
|
|
leaveTransferProgress();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
|
|
|
Log.e(TAG, "FILE_DOWNLOAD_FRAGMENT ONDESTROY " + mFile.getFileName());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView() {
|
2013-02-26 15:33:28 +04:00
|
|
|
if (!mListening) {
|
|
|
|
listenForTransferProgress();
|
|
|
|
}
|
2013-02-25 17:33:16 +04:00
|
|
|
return super.getView() == null ? mView : super.getView();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
switch (v.getId()) {
|
2013-02-25 18:19:18 +04:00
|
|
|
case R.id.cancelBtn: {
|
2013-02-25 17:33:16 +04:00
|
|
|
FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
|
|
|
|
if (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile)) {
|
|
|
|
downloaderBinder.cancel(mAccount, mFile);
|
2013-02-26 22:26:45 +04:00
|
|
|
leaveTransferProgress();
|
2013-02-25 17:33:16 +04:00
|
|
|
if (mFile.isDown()) {
|
|
|
|
setButtonsForDown();
|
|
|
|
} else {
|
|
|
|
setButtonsForRemote();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
Log.e(TAG, "Incorrect view clicked!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
public OCFile getFile(){
|
|
|
|
return mFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-02-25 18:19:18 +04:00
|
|
|
* Updates the view depending upon the state of the downloading file.
|
2013-02-26 15:33:28 +04:00
|
|
|
*
|
|
|
|
* @param transferring When true, the view must be updated assuming that the holded file is
|
|
|
|
* downloading, no matter what the downloaderBinder says.
|
2013-02-25 17:33:16 +04:00
|
|
|
*/
|
2013-02-26 15:33:28 +04:00
|
|
|
public void updateView(boolean transferring) {
|
2013-02-25 18:19:18 +04:00
|
|
|
// configure UI for depending upon local state of the file
|
|
|
|
FileDownloaderBinder downloaderBinder = mContainerActivity.getFileDownloaderBinder();
|
2013-02-26 15:33:28 +04:00
|
|
|
if (transferring || (downloaderBinder != null && downloaderBinder.isDownloading(mAccount, mFile))) {
|
2013-02-25 18:19:18 +04:00
|
|
|
setButtonsForTransferring();
|
2013-02-25 17:33:16 +04:00
|
|
|
|
2013-02-25 18:19:18 +04:00
|
|
|
} else if (mFile.isDown()) {
|
2013-02-25 17:33:16 +04:00
|
|
|
|
2013-02-25 18:19:18 +04:00
|
|
|
setButtonsForDown();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
setButtonsForRemote();
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
getView().invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables buttons for a file being downloaded
|
|
|
|
*/
|
|
|
|
private void setButtonsForTransferring() {
|
2013-02-25 18:19:18 +04:00
|
|
|
Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
|
|
|
|
downloadButton.setText(R.string.common_cancel);
|
|
|
|
|
|
|
|
// show the progress bar for the transfer
|
2013-02-26 15:33:28 +04:00
|
|
|
ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressBar.setVisibility(View.VISIBLE);
|
2013-02-26 15:33:28 +04:00
|
|
|
TextView progressText = (TextView)getView().findViewById(R.id.progressText);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressText.setText(R.string.downloader_download_in_progress_ticker);
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enables or disables buttons for a file locally available
|
|
|
|
*/
|
|
|
|
private void setButtonsForDown() {
|
2013-02-25 18:19:18 +04:00
|
|
|
Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
|
|
|
|
downloadButton.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
// hides the progress bar
|
2013-02-26 15:33:28 +04:00
|
|
|
ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressBar.setVisibility(View.GONE);
|
2013-02-25 17:33:16 +04:00
|
|
|
|
2013-02-25 18:19:18 +04:00
|
|
|
// updates the text message
|
2013-02-26 15:33:28 +04:00
|
|
|
TextView progressText = (TextView)getView().findViewById(R.id.progressText);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressText.setText(R.string.common_loading);
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
|
2013-02-25 18:19:18 +04:00
|
|
|
|
2013-02-25 17:33:16 +04:00
|
|
|
/**
|
|
|
|
* Enables or disables buttons for a file not locally available
|
|
|
|
*/
|
|
|
|
private void setButtonsForRemote() {
|
2013-02-25 18:19:18 +04:00
|
|
|
Button downloadButton = (Button) getView().findViewById(R.id.cancelBtn);
|
|
|
|
downloadButton.setVisibility(View.GONE);
|
|
|
|
//downloadButton.setText(R.string.filedetails_download);
|
|
|
|
|
|
|
|
// hides the progress bar
|
2013-02-26 15:33:28 +04:00
|
|
|
ProgressBar progressBar = (ProgressBar)getView().findViewById(R.id.progressBar);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
// updates the text message
|
2013-02-26 15:33:28 +04:00
|
|
|
TextView progressText = (TextView)getView().findViewById(R.id.progressText);
|
2013-02-25 18:19:18 +04:00
|
|
|
progressText.setText(R.string.downloader_not_downloaded_yet);
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Once the file download has finished -> update view
|
|
|
|
*/
|
|
|
|
private class DownloadFinishReceiver extends BroadcastReceiver {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
String accountName = intent.getStringExtra(FileDownloader.ACCOUNT_NAME);
|
2013-02-25 18:19:18 +04:00
|
|
|
if (accountName.equals(mAccount.name)) {
|
2013-02-25 17:33:16 +04:00
|
|
|
boolean downloadWasFine = intent.getBooleanExtra(FileDownloader.EXTRA_DOWNLOAD_RESULT, false);
|
|
|
|
String downloadedRemotePath = intent.getStringExtra(FileDownloader.EXTRA_REMOTE_PATH);
|
|
|
|
if (mFile.getRemotePath().equals(downloadedRemotePath)) {
|
|
|
|
if (downloadWasFine) {
|
|
|
|
mFile = mStorageManager.getFileByPath(downloadedRemotePath);
|
|
|
|
}
|
2013-02-26 15:33:28 +04:00
|
|
|
updateView(false);
|
2013-02-25 17:33:16 +04:00
|
|
|
getActivity().removeStickyBroadcast(intent);
|
2013-02-26 15:33:28 +04:00
|
|
|
mContainerActivity.notifySuccessfulDownload(mFile, intent, downloadWasFine);
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void listenForTransferProgress() {
|
2013-02-26 15:33:28 +04:00
|
|
|
if (mProgressListener != null && !mListening) {
|
2013-02-25 17:33:16 +04:00
|
|
|
if (mContainerActivity.getFileDownloaderBinder() != null) {
|
|
|
|
mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, mFile);
|
2013-02-26 15:33:28 +04:00
|
|
|
mListening = true;
|
|
|
|
setButtonsForTransferring();
|
2013-02-25 17:33:16 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void leaveTransferProgress() {
|
|
|
|
if (mProgressListener != null) {
|
|
|
|
if (mContainerActivity.getFileDownloaderBinder() != null) {
|
|
|
|
mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, mFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper class responsible for updating the progress bar shown for file uploading or downloading
|
|
|
|
*
|
|
|
|
* @author David A. Velasco
|
|
|
|
*/
|
|
|
|
private class ProgressListener implements OnDatatransferProgressListener {
|
|
|
|
int mLastPercent = 0;
|
|
|
|
WeakReference<ProgressBar> mProgressBar = null;
|
|
|
|
|
|
|
|
ProgressListener(ProgressBar progressBar) {
|
|
|
|
mProgressBar = new WeakReference<ProgressBar>(progressBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTransferProgress(long progressRate) {
|
|
|
|
// old method, nothing here
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) {
|
|
|
|
int percent = (int)(100.0*((double)totalTransferredSoFar)/((double)totalToTransfer));
|
|
|
|
if (percent != mLastPercent) {
|
|
|
|
ProgressBar pb = mProgressBar.get();
|
|
|
|
if (pb != null) {
|
|
|
|
pb.setProgress(percent);
|
|
|
|
pb.postInvalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mLastPercent = percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|