X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/5116bd99e26954fb83621863ff26e351d0a08cfd..080a8ab7af2bc48e3c5fa1334ae91d21073e26c2:/src/com/owncloud/android/ui/preview/FileDownloadFragment.java diff --git a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java index 7af29c64..e63b3245 100644 --- a/src/com/owncloud/android/ui/preview/FileDownloadFragment.java +++ b/src/com/owncloud/android/ui/preview/FileDownloadFragment.java @@ -1,6 +1,8 @@ -/* ownCloud Android client application - * - * Copyright (C) 2012-2013 ownCloud Inc. +/** + * ownCloud Android client application + * + * @author David A. Velasco + * Copyright (C) 2015 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -21,52 +23,80 @@ import java.lang.ref.WeakReference; import com.owncloud.android.R; import com.owncloud.android.datamodel.OCFile; -import com.owncloud.android.files.services.FileDownloader.FileDownloaderBinder; import com.owncloud.android.ui.fragment.FileFragment; import android.accounts.Account; import android.os.Bundle; +import android.support.v4.app.Fragment; import android.support.v4.app.FragmentStatePagerAdapter; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; -import android.widget.ImageButton; -import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; import com.owncloud.android.lib.common.network.OnDatatransferProgressListener; import com.owncloud.android.lib.common.utils.Log_OC; +import com.owncloud.android.utils.DisplayUtils; /** * This Fragment is used to monitor the progress of a file downloading. - * - * @author David A. Velasco */ public class FileDownloadFragment extends FileFragment implements OnClickListener { public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; private static final String EXTRA_ERROR = "ERROR"; + + private static final String ARG_FILE = "FILE"; + private static final String ARG_IGNORE_FIRST = "IGNORE_FIRST"; + private static final String ARG_ACCOUNT = "ACCOUNT" ; private View mView; private Account mAccount; - + public ProgressListener mProgressListener; private boolean mListening; - + private static final String TAG = FileDownloadFragment.class.getSimpleName(); - + private boolean mIgnoreFirstSavedState; private boolean mError; - + + + /** + * Public factory method to create a new fragment that shows the progress of a file download. + * + * Android strongly recommends keep the empty constructor of fragments as the only public constructor, and + * use {@link #setArguments(Bundle)} to set the needed arguments. + * + * This method hides to client objects the need of doing the construction in two steps. + * + * When 'file' is null creates a dummy layout (useful when a file wasn't tapped before). + * + * @param file An {@link OCFile} to show in the fragment + * @param account An OC account; needed to start downloads + * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter} + * TODO better solution + */ + public static Fragment newInstance(OCFile file, Account account, boolean ignoreFirstSavedState) { + FileDownloadFragment frag = new FileDownloadFragment(); + Bundle args = new Bundle(); + args.putParcelable(ARG_FILE, file); + args.putParcelable(ARG_ACCOUNT, account); + args.putBoolean(ARG_IGNORE_FIRST, ignoreFirstSavedState); + frag.setArguments(args); + return frag; + } + /** * 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. + * It's necessary to keep a public constructor without parameters; the system uses it when tries to + * reinstantiate a fragment automatically. */ public FileDownloadFragment() { super(); @@ -76,58 +106,45 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene mIgnoreFirstSavedState = false; mError = false; } - - - /** - * 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 - * @param ignoreFirstSavedState Flag to work around an unexpected behaviour of {@link FragmentStatePagerAdapter}; TODO better solution - */ - public FileDownloadFragment(OCFile fileToDetail, Account ocAccount, boolean ignoreFirstSavedState) { - super(fileToDetail); - mAccount = ocAccount; - mProgressListener = null; - mListening = false; - mIgnoreFirstSavedState = ignoreFirstSavedState; - mError = false; - } - - + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + Bundle args = getArguments(); + setFile((OCFile)args.getParcelable(ARG_FILE)); + // TODO better in super, but needs to check ALL the class extending FileFragment; not right now + + mIgnoreFirstSavedState = args.getBoolean(ARG_IGNORE_FIRST); + mAccount = args.getParcelable(ARG_ACCOUNT); } - + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { + Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); - + if (savedInstanceState != null) { if (!mIgnoreFirstSavedState) { - setFile((OCFile)savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE)); + setFile((OCFile) savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_FILE)); mAccount = savedInstanceState.getParcelable(FileDownloadFragment.EXTRA_ACCOUNT); mError = savedInstanceState.getBoolean(FileDownloadFragment.EXTRA_ERROR); - } else { + } + else { mIgnoreFirstSavedState = false; } } - - View view = null; - view = inflater.inflate(R.layout.file_download_fragment, container, false); - mView = view; + + mView = inflater.inflate(R.layout.file_download_fragment, container, false); ProgressBar progressBar = (ProgressBar)mView.findViewById(R.id.progressBar); + DisplayUtils.colorPreLollipopHorizontalProgressBar(progressBar); mProgressListener = new ProgressListener(progressBar); + + (mView.findViewById(R.id.cancelBtn)).setOnClickListener(this); - ((ImageButton)mView.findViewById(R.id.cancelBtn)).setOnClickListener(this); - - ((LinearLayout)mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() { + (mView.findViewById(R.id.fileDownloadLL)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((PreviewImageActivity) getActivity()).toggleFullScreen(); @@ -136,13 +153,14 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene if (mError) { setButtonsForRemote(); - } else { + } + else { setButtonsForTransferring(); } - - return view; + + return mView; } - + @Override public void onSaveInstanceState(Bundle outState) { @@ -157,7 +175,7 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene super.onStart(); listenForTransferProgress(); } - + @Override public void onResume() { super.onResume(); @@ -169,19 +187,19 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene super.onPause(); } - + @Override public void onStop() { leaveTransferProgress(); super.onStop(); } - + @Override public void onDestroy() { super.onDestroy(); } - - + + @Override public View getView() { if (!mListening) { @@ -190,7 +208,7 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene return super.getView() == null ? mView : super.getView(); } - + @Override public void onClick(View v) { switch (v.getId()) { @@ -204,78 +222,52 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene } } - - /** - * Updates the view depending upon the state of the downloading file. - * - * @param transferring When true, the view must be updated assuming that the holded file is - * downloading, no matter what the downloaderBinder says. - */ - /* - public void updateView(boolean transferring) { - // configure UI for depending upon local state of the file - // TODO remove - if (transferring || getFile().isDownloading()) { - setButtonsForTransferring(); - - } else if (getFile().isDown()) { - - setButtonsForDown(); - - } else { - setButtonsForRemote(); - } - getView().invalidate(); - - } - */ /** * Enables or disables buttons for a file being downloaded */ private void setButtonsForTransferring() { getView().findViewById(R.id.cancelBtn).setVisibility(View.VISIBLE); - + // show the progress bar for the transfer getView().findViewById(R.id.progressBar).setVisibility(View.VISIBLE); - TextView progressText = (TextView)getView().findViewById(R.id.progressText); + TextView progressText = (TextView) getView().findViewById(R.id.progressText); progressText.setText(R.string.downloader_download_in_progress_ticker); progressText.setVisibility(View.VISIBLE); - + // hides the error icon getView().findViewById(R.id.errorText).setVisibility(View.GONE); getView().findViewById(R.id.error_image).setVisibility(View.GONE); } - /** - * Enables or disables buttons for a file locally available + * Enables or disables buttons for a file locally available */ private void setButtonsForDown() { getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE); - + // hides the progress bar getView().findViewById(R.id.progressBar).setVisibility(View.GONE); - + // updates the text message - TextView progressText = (TextView)getView().findViewById(R.id.progressText); + TextView progressText = (TextView) getView().findViewById(R.id.progressText); progressText.setText(R.string.common_loading); progressText.setVisibility(View.VISIBLE); - + // hides the error icon getView().findViewById(R.id.errorText).setVisibility(View.GONE); getView().findViewById(R.id.error_image).setVisibility(View.GONE); } - + /** - * Enables or disables buttons for a file not locally available - * + * Enables or disables buttons for a file not locally available + *

* Currently, this is only used when a download was failed */ private void setButtonsForRemote() { getView().findViewById(R.id.cancelBtn).setVisibility(View.GONE); - + // hides the progress bar and message getView().findViewById(R.id.progressBar).setVisibility(View.GONE); getView().findViewById(R.id.progressText).setVisibility(View.GONE); @@ -284,44 +276,48 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene getView().findViewById(R.id.errorText).setVisibility(View.VISIBLE); getView().findViewById(R.id.error_image).setVisibility(View.VISIBLE); } - + public void listenForTransferProgress() { if (mProgressListener != null && !mListening) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener(mProgressListener, mAccount, getFile()); + mContainerActivity.getFileDownloaderBinder().addDatatransferProgressListener( + mProgressListener, mAccount, getFile() + ); mListening = true; setButtonsForTransferring(); } } } - - + + public void leaveTransferProgress() { if (mProgressListener != null) { if (mContainerActivity.getFileDownloaderBinder() != null) { - mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener(mProgressListener, mAccount, getFile()); + mContainerActivity.getFileDownloaderBinder().removeDatatransferProgressListener( + mProgressListener, mAccount, getFile() + ); mListening = false; } } } - + /** - * Helper class responsible for updating the progress bar shown for file uploading or downloading - * - * @author David A. Velasco + * Helper class responsible for updating the progress bar shown for file uploading or downloading */ private class ProgressListener implements OnDatatransferProgressListener { int mLastPercent = 0; WeakReference mProgressBar = null; - + ProgressListener(ProgressBar progressBar) { mProgressBar = new WeakReference(progressBar); } - + @Override - public void onTransferProgress(long progressRate, long totalTransferredSoFar, long totalToTransfer, String filename) { + 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(); @@ -338,8 +334,9 @@ public class FileDownloadFragment extends FileFragment implements OnClickListene public void setError(boolean error) { mError = error; - }; - + } + + ; }